From b4bc6521759103a5a79819a082e7413737212a76 Mon Sep 17 00:00:00 2001
From: Luboš Luňák <l.lunak@collabora.com>
Date: Wed, 5 Nov 2014 14:19:23 +0100
Subject: make it possible to check mailmerge output files in unittests

Change-Id: Ifa54445c8f46bb2ed5539870c5fce1a2e62beef7
---
 sw/qa/extras/inc/swmodeltestbase.hxx | 55 ++++++++++++++++++++++++++++--------
 sw/qa/extras/mailmerge/mailmerge.cxx | 32 +++++++++++++--------
 2 files changed, 65 insertions(+), 22 deletions(-)

(limited to 'sw')

diff --git a/sw/qa/extras/inc/swmodeltestbase.hxx b/sw/qa/extras/inc/swmodeltestbase.hxx
index b642abc621fc..da7921f05f9b 100644
--- a/sw/qa/extras/inc/swmodeltestbase.hxx
+++ b/sw/qa/extras/inc/swmodeltestbase.hxx
@@ -182,6 +182,8 @@ protected:
     utl::TempFile maTempFile;
     bool mbExported; ///< Does maTempFile already contain something useful?
     sal_Int16 nCurOutputType;
+    OUString mailMergeOutputURL;
+    OUString mailMergeOutputPrefix;
 
 protected:
     virtual OUString getTestName() { return OUString(); }
@@ -612,6 +614,25 @@ protected:
             calcLayout();
     }
 
+    /**
+     Loads number-th document from mail merge. Requires file output from mail merge.
+    */
+    void loadMailMergeDocument( int number )
+    {
+        assert( nCurOutputType == text::MailMergeType::FILE );
+        if (mxComponent.is())
+            mxComponent->dispose();
+        OUString name = mailMergeOutputPrefix + OUString::number( number ) + ".odt";
+        // Output name early, so in the case of a hang, the name of the hanging input file is visible.
+        std::cout << name << ",";
+        mnStartTime = osl_getGlobalTimer();
+        mxComponent = loadFromDesktop(mailMergeOutputURL + "/" + name, "com.sun.star.text.TextDocument");
+        CPPUNIT_ASSERT( mxComponent.is());
+        OString name2 = OUStringToOString( name, RTL_TEXTENCODING_UTF8 );
+        if (mustCalcLayoutOf(name2.getStr()))
+            calcLayout();
+    }
+
     void reload(const char* pFilter, const char* filename)
     {
         uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
@@ -692,8 +713,25 @@ protected:
         if (!mbExported)
             return 0;
 
+        return parseExportInternal( maTempFile.GetURL(), rStreamName );
+    }
+
+    /**
+     * Like parseExport(), but for given mail merge document.
+     */
+    xmlDocPtr parseMailMergeExport(int number, const OUString& rStreamName = OUString("word/document.xml"))
+    {
+        if (nCurOutputType != text::MailMergeType::FILE)
+            return 0;
+
+        OUString name = mailMergeOutputPrefix + OUString::number( number ) + ".odt";
+        return parseExportInternal( mailMergeOutputURL + "/" + name, rStreamName );
+    }
+
+    xmlDocPtr parseExportInternal( const OUString& url, const OUString& rStreamName )
+    {
         // Read the XML stream we're interested in.
-        uno::Reference<packages::zip::XZipFileAccess2> xNameAccess = packages::zip::ZipFileAccess::createWithURL(comphelper::getComponentContext(m_xSFactory), maTempFile.GetURL());
+        uno::Reference<packages::zip::XZipFileAccess2> xNameAccess = packages::zip::ZipFileAccess::createWithURL(comphelper::getComponentContext(m_xSFactory), url);
         uno::Reference<io::XInputStream> xInputStream(xNameAccess->getByName(rStreamName), uno::UNO_QUERY);
         boost::shared_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true));
 
@@ -783,7 +821,7 @@ protected:
     }
 
     virtual void initMailMergeJobAndArgs( const char* filename, const char* tablename, const OUString &aDBName,
-                                          const OUString &aPrefix, const OUString &aWorkDir )
+                                          const OUString &aPrefix, const OUString &aWorkDir, bool file )
     {
         uno::Reference< task::XJob > xJob( getMultiServiceFactory()->createInstance( "com.sun.star.text.MailMerge" ), uno::UNO_QUERY_THROW );
         mxJob.set( xJob );
@@ -793,7 +831,7 @@ protected:
         mSeqMailMergeArgs.realloc( seq_id );
 
         seq_id = 0;
-        mSeqMailMergeArgs[ seq_id++ ] = beans::NamedValue( OUString( UNO_NAME_OUTPUT_TYPE ), uno::Any( text::MailMergeType::SHELL ) );
+        mSeqMailMergeArgs[ seq_id++ ] = beans::NamedValue( OUString( UNO_NAME_OUTPUT_TYPE ), uno::Any( file ? text::MailMergeType::FILE : text::MailMergeType::SHELL ) );
         mSeqMailMergeArgs[ seq_id++ ] = beans::NamedValue( OUString( UNO_NAME_DOCUMENT_URL ), uno::Any(
                                         ( OUString(getURLFromSrc(mpTestDocumentPath) + OUString::createFromAscii(filename)) ) ) );
         mSeqMailMergeArgs[ seq_id++ ] = beans::NamedValue( OUString( UNO_NAME_DATA_SOURCE_NAME ), uno::Any( aDBName ) );
@@ -810,8 +848,6 @@ protected:
     {
         uno::Any res = mxJob->execute( mSeqMailMergeArgs );
 
-        OUString aCurOutputURL;
-        OUString aCurFileNamePrefix;
         const beans::NamedValue *pArguments = mSeqMailMergeArgs.getConstArray();
         bool bOk = true;
         sal_Int32 nArgs = mSeqMailMergeArgs.getLength();
@@ -822,9 +858,9 @@ protected:
 
             // all error checking was already done by the MM job execution
             if (rName == UNO_NAME_OUTPUT_URL)
-                bOk &= rValue >>= aCurOutputURL;
+                bOk &= rValue >>= mailMergeOutputURL;
             else if (rName == UNO_NAME_FILE_NAME_PREFIX)
-                bOk &= rValue >>= aCurFileNamePrefix;
+                bOk &= rValue >>= mailMergeOutputPrefix;
             else if (rName == UNO_NAME_OUTPUT_TYPE)
                 bOk &= rValue >>= nCurOutputType;
         }
@@ -839,10 +875,7 @@ protected:
         else
         {
             CPPUNIT_ASSERT(res == true);
-            mxMMComponent = loadFromDesktop( aCurOutputURL + "/" + aCurFileNamePrefix + "0.odt",
-                                             "com.sun.star.text.TextDocument");
-            CPPUNIT_ASSERT(mxMMComponent.is());
-            calcLayout();
+            loadMailMergeDocument( 0 );
         }
     }
 };
diff --git a/sw/qa/extras/mailmerge/mailmerge.cxx b/sw/qa/extras/mailmerge/mailmerge.cxx
index 1260d6965edc..d278db1ccb25 100644
--- a/sw/qa/extras/mailmerge/mailmerge.cxx
+++ b/sw/qa/extras/mailmerge/mailmerge.cxx
@@ -44,7 +44,7 @@ class MMTest : public SwModelTestBase
      * The 'verify' method actually has to execute the mail merge by
      * calling executeMailMerge() after modifying the job arguments.
      */
-    void executeMailMergeTest(const char* filename, const char* datasource, const char* tablename = 0)
+    void executeMailMergeTest(const char* filename, const char* datasource, const char* tablename, bool file)
     {
         header();
         preTest(filename);
@@ -55,18 +55,19 @@ class MMTest : public SwModelTestBase
         const OUString aWorkDir = aTempDir.GetURL();
         const OUString aURI( getURLFromSrc(mpTestDocumentPath) + OUString::createFromAscii(datasource) );
         OUString aDBName = registerDBsource( aURI, aPrefix, aWorkDir );
-        initMailMergeJobAndArgs( filename, tablename, aDBName, aPrefix, aWorkDir );
+        initMailMergeJobAndArgs( filename, tablename, aDBName, aPrefix, aWorkDir, file );
 
         postTest(filename);
         verify();
         finish();
 
         ::utl::removeTree(aWorkDir);
+        nCurOutputType = 0;
     }
 
 };
 
-#define DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, BaseClass) \
+#define DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, file, BaseClass) \
     class TestName : public BaseClass { \
     protected: \
         virtual OUString getTestName() SAL_OVERRIDE { return OUString::createFromAscii(#TestName); } \
@@ -76,17 +77,22 @@ class MMTest : public SwModelTestBase
         CPPUNIT_TEST_SUITE_END(); \
     \
         void MailMerge() { \
-            executeMailMergeTest(filename, datasource, tablename); \
+            executeMailMergeTest(filename, datasource, tablename, file); \
         } \
         void verify() SAL_OVERRIDE; \
     }; \
     CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \
     void TestName::verify()
 
-#define DECLARE_DFLT_MAILMERGE_TEST(TestName, filename, datasource, tablename) \
-    DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, MMTest)
+// Will generate the resulting document in mxMMDocument.
+#define DECLARE_SHELL_MAILMERGE_TEST(TestName, filename, datasource, tablename) \
+    DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, false, MMTest)
 
-DECLARE_DFLT_MAILMERGE_TEST(testMultiPageAnchoredDraws, "multiple-page-anchored-draws.odt", "4_v01.ods", "Tabelle1")
+// Will generate documents as files, use loadMailMergeDocument().
+#define DECLARE_FILE_MAILMERGE_TEST(TestName, filename, datasource, tablename) \
+    DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, true, MMTest)
+
+DECLARE_SHELL_MAILMERGE_TEST(testMultiPageAnchoredDraws, "multiple-page-anchored-draws.odt", "4_v01.ods", "Tabelle1")
 {
     executeMailMerge();
 
@@ -117,19 +123,19 @@ DECLARE_DFLT_MAILMERGE_TEST(testMultiPageAnchoredDraws, "multiple-page-anchored-
     }
 }
 
-DECLARE_DFLT_MAILMERGE_TEST(testMissingDefaultLineColor, "missing-default-line-color.ott", "one-empty-address.ods", "one-empty-address")
+DECLARE_FILE_MAILMERGE_TEST(testMissingDefaultLineColor, "missing-default-line-color.ott", "one-empty-address.ods", "one-empty-address")
 {
     executeMailMerge();
     // The document was created by LO version which didn't write out the default value for line color
     // (see XMLGraphicsDefaultStyle::SetDefaults()).
-    uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxMMComponent, uno::UNO_QUERY);
+    uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxComponent, uno::UNO_QUERY);
     uno::Reference<container::XIndexAccess> xDraws(xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY);
     uno::Reference<beans::XPropertySet> xPropertySet(xDraws->getByIndex(0), uno::UNO_QUERY);
     // Lines do not have a line color.
     CPPUNIT_ASSERT( !xPropertySet->getPropertySetInfo()->hasPropertyByName( "LineColor" ));
-    SwXTextDocument* pTxtDoc = dynamic_cast<SwXTextDocument *>(mxMMComponent.get());
+    SwXTextDocument* pTxtDoc = dynamic_cast<SwXTextDocument *>(mxComponent.get());
     CPPUNIT_ASSERT(pTxtDoc);
-    uno::Reference< lang::XMultiServiceFactory > xFact( mxMMComponent, uno::UNO_QUERY );
+    uno::Reference< lang::XMultiServiceFactory > xFact( mxComponent, uno::UNO_QUERY );
     uno::Reference< beans::XPropertySet > xDefaults( xFact->createInstance( "com.sun.star.drawing.Defaults" ), uno::UNO_QUERY );
     CPPUNIT_ASSERT( xDefaults.is());
     uno::Reference< beans::XPropertySetInfo > xInfo( xDefaults->getPropertySetInfo());
@@ -138,6 +144,10 @@ DECLARE_DFLT_MAILMERGE_TEST(testMissingDefaultLineColor, "missing-default-line-c
     xDefaults->getPropertyValue( "LineColor" ) >>= lineColor;
     // And the default value is black (wasn't copied properly by mailmerge).
     CPPUNIT_ASSERT_EQUAL( COL_BLACK, lineColor );
+    // And check that the resulting file has the proper default.
+    xmlDocPtr pXmlDoc = parseMailMergeExport( 0, "styles.xml" );
+    CPPUNIT_ASSERT_EQUAL( OUString( "graphic" ), getXPath(pXmlDoc, "/office:document-styles/office:styles/style:default-style[1]", "family"));
+    CPPUNIT_ASSERT_EQUAL( OUString( "#000000" ), getXPath(pXmlDoc, "/office:document-styles/office:styles/style:default-style[1]/style:graphic-properties", "stroke-color"));
 }
 
 #endif
-- 
cgit