summaryrefslogtreecommitdiff
path: root/desktop
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2020-08-02 12:41:19 -0400
committerAshod Nakashian <ash@collabora.com>2020-08-10 04:50:20 +0200
commit347e0d5336ccd881356c89ecc5d8186dd9b773ad (patch)
treebed9d2fd7e27f8ac1d30b38b8bd112069a3e8272 /desktop
parentafaed0b1141d67c36ff0714be5d7eb2825a79bdd (diff)
DesktopLOKTest: cleanup
Refactor the handling of document loading and unloading and cleanup to allow more flexibility when loading multiple documents and for manual destruction. Also, correctly set the document type when loading, which was defaulted to TEXT even when loading spreadsheet and presentation documents. Minor misc cleanup such as dangling semicolons and unregistering the callback twice. Change-Id: Ia244aafd526d60f73c46e99fb8c7e63f63b0a8f2 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99974 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com> Reviewed-by: Ashod Nakashian <ash@collabora.com>
Diffstat (limited to 'desktop')
-rw-r--r--desktop/qa/desktop_lib/test_desktop_lib.cxx112
1 files changed, 86 insertions, 26 deletions
diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 0123037ec75d..864041b71d49 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -59,9 +59,34 @@
#include <lib/init.hxx>
#include <svx/svxids.hrc>
+#include <cppunit/TestAssert.h>
+
using namespace com::sun::star;
using namespace desktop;
+static LibreOfficeKitDocumentType getDocumentTypeFromName(const char* pName)
+{
+ CPPUNIT_ASSERT_MESSAGE("Document name must be valid.", pName != nullptr);
+
+ const std::string name(pName);
+ CPPUNIT_ASSERT_MESSAGE("Document name must include extension.", name.size() > 4);
+
+ const auto it = name.rfind('.');
+ if (it != name.npos)
+ {
+ const std::string ext = name.substr(it);
+
+ if (ext == ".ods")
+ return LOK_DOCTYPE_SPREADSHEET;
+
+ if (ext == ".odp")
+ return LOK_DOCTYPE_PRESENTATION;
+ }
+
+ CPPUNIT_ASSERT_MESSAGE("Document name must include extension.", it != name.npos);
+ return LOK_DOCTYPE_TEXT;
+}
+
class DesktopLOKTest : public UnoApiTest
{
class Resetter
@@ -106,22 +131,39 @@ public:
UnoApiTest::setUp();
mxDesktop.set(frame::Desktop::create(comphelper::getComponentContext(getMultiServiceFactory())));
SfxApplication::GetOrCreate();
- };
+ }
virtual void tearDown() override
{
- if (m_pDocument)
- m_pDocument->pClass->registerCallback(m_pDocument.get(), nullptr, nullptr);
closeDoc();
UnoApiTest::tearDown();
comphelper::LibreOfficeKit::setActive(false);
- };
+ }
+
+ std::pair<std::unique_ptr<LibLODocument_Impl>, uno::Reference<lang::XComponent>>
+ loadDocImpl(const char* pName, LibreOfficeKitDocumentType eType);
+
+ std::pair<std::unique_ptr<LibLODocument_Impl>, uno::Reference<lang::XComponent>>
+ loadDocImpl(const char* pName)
+ {
+ return loadDocImpl(pName, getDocumentTypeFromName(pName));
+ }
+
+ std::pair<std::unique_ptr<LibLODocument_Impl>, uno::Reference<lang::XComponent>>
+ loadDocUrlImpl(const OUString& rFileURL, LibreOfficeKitDocumentType eType);
LibLODocument_Impl* loadDocUrl(const OUString& rFileURL, LibreOfficeKitDocumentType eType);
- LibLODocument_Impl* loadDoc(const char* pName, LibreOfficeKitDocumentType eType = LOK_DOCTYPE_TEXT);
- void closeDoc();
+ LibLODocument_Impl* loadDoc(const char* pName, LibreOfficeKitDocumentType eType);
+ LibLODocument_Impl* loadDoc(const char* pName)
+ {
+ return loadDoc(pName, getDocumentTypeFromName(pName));
+ }
+
+ void closeDoc(std::unique_ptr<LibLODocument_Impl>& loDocument,
+ uno::Reference<lang::XComponent>& xComponent);
+ void closeDoc() { closeDoc(m_pDocument, mxComponent); }
static void callback(int nType, const char* pPayload, void* pData);
void callbackImpl(int nType, const char* pPayload);
@@ -287,7 +329,8 @@ static Control* GetFocusControl(vcl::Window const * pParent)
return nullptr;
}
-LibLODocument_Impl* DesktopLOKTest::loadDocUrl(const OUString& rFileURL, LibreOfficeKitDocumentType eType)
+std::pair<std::unique_ptr<LibLODocument_Impl>, uno::Reference<lang::XComponent>>
+DesktopLOKTest::loadDocUrlImpl(const OUString& rFileURL, LibreOfficeKitDocumentType eType)
{
OUString aService;
switch (eType)
@@ -305,28 +348,48 @@ LibLODocument_Impl* DesktopLOKTest::loadDocUrl(const OUString& rFileURL, LibreOf
CPPUNIT_ASSERT(false);
break;
}
- mxComponent = loadFromDesktop(rFileURL, aService);
- if (!mxComponent.is())
- {
- CPPUNIT_ASSERT(false);
- }
- m_pDocument.reset(new LibLODocument_Impl(mxComponent));
- return m_pDocument.get();
+
+ uno::Reference<lang::XComponent> xComponent = loadFromDesktop(rFileURL, aService);
+ CPPUNIT_ASSERT(xComponent.is());
+
+ std::unique_ptr<LibLODocument_Impl> pDocument(new LibLODocument_Impl(xComponent));
+
+ return std::make_pair(std::move(pDocument), xComponent);
}
-LibLODocument_Impl* DesktopLOKTest::loadDoc(const char* pName, LibreOfficeKitDocumentType eType)
+std::pair<std::unique_ptr<LibLODocument_Impl>, uno::Reference<lang::XComponent>>
+DesktopLOKTest::loadDocImpl(const char* pName, LibreOfficeKitDocumentType eType)
{
OUString aFileURL;
createFileURL(OUString::createFromAscii(pName), aFileURL);
- return loadDocUrl(aFileURL, eType);
+ return loadDocUrlImpl(aFileURL, eType);
+}
+
+LibLODocument_Impl* DesktopLOKTest::loadDocUrl(const OUString& rFileURL, LibreOfficeKitDocumentType eType)
+{
+ std::tie(m_pDocument, mxComponent) = loadDocUrlImpl(rFileURL, eType);
+ return m_pDocument.get();
}
-void DesktopLOKTest::closeDoc()
+LibLODocument_Impl* DesktopLOKTest::loadDoc(const char* pName, LibreOfficeKitDocumentType eType)
{
- if (mxComponent.is())
+ std::tie(m_pDocument, mxComponent) = loadDocImpl(pName, eType);
+ return m_pDocument.get();
+}
+
+void DesktopLOKTest::closeDoc(std::unique_ptr<LibLODocument_Impl>& pDocument,
+ uno::Reference<lang::XComponent>& xComponent)
+{
+ if (pDocument)
+ {
+ pDocument->pClass->registerCallback(pDocument.get(), nullptr, nullptr);
+ pDocument.reset();
+ }
+
+ if (xComponent.is())
{
- closeDocument(mxComponent);
- mxComponent.clear();
+ closeDocument(xComponent);
+ xComponent.clear();
}
}
@@ -2379,8 +2442,7 @@ void DesktopLOKTest::testInsertCertificate_DER_ODT()
CPPUNIT_ASSERT(pDocument->pClass->saveAs(pDocument, aTempFile.GetURL().toUtf8().getStr(), "odt", nullptr));
closeDoc();
- mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
- pDocument = new LibLODocument_Impl(mxComponent);
+ pDocument = loadDocUrl(aTempFile.GetURL(), LOK_DOCTYPE_TEXT);
Scheduler::ProcessEventsToIdle();
CPPUNIT_ASSERT(mxComponent.is());
@@ -2430,8 +2492,7 @@ void DesktopLOKTest::testInsertCertificate_PEM_ODT()
CPPUNIT_ASSERT(pDocument->pClass->saveAs(pDocument, aTempFile.GetURL().toUtf8().getStr(), "odt", nullptr));
closeDoc();
- mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
- pDocument = new LibLODocument_Impl(mxComponent);
+ pDocument = loadDocUrl(aTempFile.GetURL(), LOK_DOCTYPE_TEXT);
Scheduler::ProcessEventsToIdle();
CPPUNIT_ASSERT(mxComponent.is());
@@ -2488,8 +2549,7 @@ void DesktopLOKTest::testInsertCertificate_PEM_DOCX()
CPPUNIT_ASSERT(pDocument->pClass->saveAs(pDocument, aTempFile.GetURL().toUtf8().getStr(), "docx", nullptr));
closeDoc();
- mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
- pDocument = new LibLODocument_Impl(mxComponent);
+ pDocument = loadDocUrl(aTempFile.GetURL(), LOK_DOCTYPE_TEXT);
Scheduler::ProcessEventsToIdle();
CPPUNIT_ASSERT(mxComponent.is());