sw/CppunitTest_sw_mailmerge.mk                |    2 
 sw/qa/extras/mailmerge/data/linked-labels.odt |binary
 sw/qa/extras/mailmerge/mailmerge.cxx          |  112 ++++++++++++++++++++------
 3 files changed, 91 insertions(+), 23 deletions(-)

New commits:
commit 9dbaf6b3b6112d9e16fd64ab0ed5dff6e513e5f8
Author: Jan-Marek Glogowski <glo...@fbihome.de>
Date:   Wed Jan 20 10:48:37 2016 +0100

    tdf#95292 MM: add unit test
    
    bEndOfDB was handled inconsistently in ToNextRecord for record
    selections and normal record iteration.
    
    This unit test checks mail merge to generate the correct amount
    of three pages (actually five with the hidden, empty pages) for
    a five record data subset, mailing a linked label document with
    two labels. Previously the third / last page was missing.
    
    On Windows, this test originally failed, because the test document
    for synced labels opens the "synchronize" dialog, which is
    generated from an UI file, which are missing in the default test
    configuration.
    
    So this patch also switches the mail merge unit test to the
    "instdir configuration", which has locking enabled and prevents
    opening a documents twice. As a result we can't use a document for
    two test and have to load the source document for inspection after
    the UNO mail merge call, because it can just work with a file name
    as the source document. This way the document isn't opened twice,
    and the tests still pass on Windows.
    
    Change-Id: I98b33defc403c40b84af44d91ed7fdcf6cf347a3
    Reviewed-on: https://gerrit.libreoffice.org/23503
    Tested-by: Jenkins <c...@libreoffice.org>
    Reviewed-by: Jan-Marek Glogowski <glo...@fbihome.de>

diff --git a/sw/CppunitTest_sw_mailmerge.mk b/sw/CppunitTest_sw_mailmerge.mk
index 131fc81..701bb93 100644
--- a/sw/CppunitTest_sw_mailmerge.mk
+++ b/sw/CppunitTest_sw_mailmerge.mk
@@ -77,7 +77,7 @@ $(eval $(call gb_CppunitTest_use_components,sw_mailmerge, \
     xmloff/util/xo \
 ))
 
-$(eval $(call gb_CppunitTest_use_configuration,sw_mailmerge))
+$(eval $(call gb_CppunitTest_use_instdir_configuration,sw_mailmerge))
 $(eval $(call gb_CppunitTest_use_ure,sw_mailmerge))
 $(eval $(call gb_CppunitTest_use_vcl,sw_mailmerge))
 
diff --git a/sw/qa/extras/mailmerge/data/linked-labels.odt 
b/sw/qa/extras/mailmerge/data/linked-labels.odt
new file mode 100644
index 0000000..a89fd92
Binary files /dev/null and b/sw/qa/extras/mailmerge/data/linked-labels.odt 
differ
diff --git a/sw/qa/extras/mailmerge/mailmerge.cxx 
b/sw/qa/extras/mailmerge/mailmerge.cxx
index 8e13aef..4b2e8ef 100644
--- a/sw/qa/extras/mailmerge/mailmerge.cxx
+++ b/sw/qa/extras/mailmerge/mailmerge.cxx
@@ -10,6 +10,7 @@
 #include <sal/config.h>
 
 #include <set>
+#include <vector>
 
 #include <swmodeltestbase.hxx>
 
@@ -21,6 +22,8 @@
 #include <com/sun/star/text/MailMergeType.hpp>
 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
 #include <com/sun/star/text/TextContentAnchorType.hpp>
+#include <com/sun/star/sdbc/XRowSet.hpp>
+#include <com/sun/star/sdbcx/XRowLocate.hpp>
 
 #include <wrtsh.hxx>
 #include <ndtxt.hxx>
@@ -66,20 +69,20 @@ public:
      * 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, bool file)
+    void executeMailMergeTest(const char* filename, const char* datasource, 
const char* tablename, bool file, int selection)
     {
+        maMMtestFilename = filename;
         header();
         preTest(filename);
-        load(mpTestDocumentPath, filename);
 
         utl::TempFile aTempDir(nullptr, true);
         const OUString aWorkDir = aTempDir.GetURL();
         const OUString aURI( m_directories.getURLFromSrc(mpTestDocumentPath) + 
OUString::createFromAscii(datasource) );
         OUString aDBName = registerDBsource( aURI, aWorkDir );
-        initMailMergeJobAndArgs( filename, tablename, aDBName, "LOMM_", 
aWorkDir, file );
+        initMailMergeJobAndArgs( filename, tablename, aDBName, "LOMM_", 
aWorkDir, file, selection );
 
-        postTest(filename);
         verify();
+        postTest(filename);
         finish();
 
         ::utl::removeTree(aWorkDir);
@@ -105,28 +108,64 @@ public:
         return aDBName;
     }
 
+    uno::Reference< sdbc::XRowSet > getXResultFromDataset( const char* 
tablename, const OUString &aDBName )
+    {
+        uno::Reference< sdbc::XRowSet > xCurResultSet;
+        uno::Reference< uno::XInterface > xInstance = 
getMultiServiceFactory()->createInstance( "com.sun.star.sdb.RowSet" );
+        uno::Reference< beans::XPropertySet > xRowSetPropSet( xInstance, 
uno::UNO_QUERY );
+        assert( xRowSetPropSet.is() && "failed to get XPropertySet interface 
from RowSet" );
+        if (xRowSetPropSet.is())
+        {
+            xRowSetPropSet->setPropertyValue( "DataSourceName",    
uno::makeAny( aDBName ) );
+            xRowSetPropSet->setPropertyValue( "Command",           
uno::makeAny( OUString::createFromAscii(tablename) ) );
+            xRowSetPropSet->setPropertyValue( "CommandType",       
uno::makeAny( sdb::CommandType::TABLE ) );
+
+            uno::Reference< sdbc::XRowSet > xRowSet( xInstance, uno::UNO_QUERY 
);
+            if (xRowSet.is())
+                xRowSet->execute(); // build ResultSet from properties
+            xCurResultSet.set( xRowSet, uno::UNO_QUERY );
+            assert( xCurResultSet.is() && "failed to build ResultSet" );
+        }
+        return xCurResultSet;
+    }
+
     void initMailMergeJobAndArgs( const char* filename, const char* tablename, 
const OUString &aDBName,
-                                          const OUString &aPrefix, const 
OUString &aWorkDir, bool file )
+                                  const OUString &aPrefix, const OUString 
&aWorkDir, bool file, int nDataSets )
     {
         uno::Reference< task::XJob > xJob( 
getMultiServiceFactory()->createInstance( "com.sun.star.text.MailMerge" ), 
uno::UNO_QUERY_THROW );
         mxJob.set( xJob );
 
-        int seq_id = 5;
-        if (tablename) seq_id += 2;
-        mSeqMailMergeArgs.realloc( seq_id );
-
-        seq_id = 0;
-        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(m_directories.getURLFromSrc(mpTestDocumentPath) + 
OUString::createFromAscii(filename)) ) ) );
-        mSeqMailMergeArgs[ seq_id++ ] = beans::NamedValue( OUString( 
UNO_NAME_DATA_SOURCE_NAME ), uno::Any( aDBName ) );
-        mSeqMailMergeArgs[ seq_id++ ] = beans::NamedValue( OUString( 
UNO_NAME_OUTPUT_URL ), uno::Any( aWorkDir ) );
-        mSeqMailMergeArgs[ seq_id++ ] = beans::NamedValue( OUString( 
UNO_NAME_FILE_NAME_PREFIX ), uno::Any( aPrefix ));
+        std::vector< beans::NamedValue > args;
+
+        args.push_back( beans::NamedValue( OUString( UNO_NAME_OUTPUT_TYPE ), 
uno::Any( file ? text::MailMergeType::FILE : text::MailMergeType::SHELL ) ) );
+        args.push_back( beans::NamedValue( OUString( UNO_NAME_DOCUMENT_URL ), 
uno::Any(
+                                         ( OUString( 
m_directories.getURLFromSrc(mpTestDocumentPath) + 
OUString::createFromAscii(filename)) ) ) ) );
+        args.push_back( beans::NamedValue( OUString( UNO_NAME_DATA_SOURCE_NAME 
), uno::Any( aDBName ) ) );
+        args.push_back( beans::NamedValue( OUString( UNO_NAME_OUTPUT_URL ), 
uno::Any( aWorkDir ) ) );
+        args.push_back( beans::NamedValue( OUString( UNO_NAME_FILE_NAME_PREFIX 
), uno::Any( aPrefix )) );
+
         if (tablename)
         {
-            mSeqMailMergeArgs[ seq_id++ ] = beans::NamedValue( OUString( 
UNO_NAME_DAD_COMMAND_TYPE ), uno::Any( sdb::CommandType::TABLE ) );
-            mSeqMailMergeArgs[ seq_id++ ] = beans::NamedValue( OUString( 
UNO_NAME_DAD_COMMAND ), uno::Any( OUString::createFromAscii(tablename) ) );
+            args.push_back( beans::NamedValue( OUString( 
UNO_NAME_DAD_COMMAND_TYPE ), uno::Any( sdb::CommandType::TABLE ) ) );
+            args.push_back( beans::NamedValue( OUString( UNO_NAME_DAD_COMMAND 
), uno::Any( OUString::createFromAscii(tablename) ) ) );
+        }
+
+        if (nDataSets > 0)
+        {
+            uno::Reference< sdbc::XRowSet > xCurResultSet = 
getXResultFromDataset( tablename, aDBName );
+            uno::Reference< sdbcx::XRowLocate > xCurRowLocate( xCurResultSet, 
uno::UNO_QUERY );
+            args.push_back( beans::NamedValue( OUString( UNO_NAME_RESULT_SET 
), uno::Any( xCurResultSet ) ) );
+            std::vector< uno::Any > vResult;
+            vResult.reserve( nDataSets );
+            sal_Int32 i;
+            for (i = 0, xCurResultSet->first(); i < nDataSets; i++, 
xCurResultSet->next())
+            {
+                vResult.push_back( uno::Any( xCurRowLocate->getBookmark() ) );
+            }
+            args.push_back( beans::NamedValue( OUString( UNO_NAME_SELECTION ), 
uno::Any( comphelper::containerToSequence(vResult) ) ) );
         }
+
+        mSeqMailMergeArgs = comphelper::containerToSequence( args );
     }
 
     void executeMailMerge()
@@ -152,6 +191,12 @@ public:
 
         CPPUNIT_ASSERT(bOk);
 
+        // MM via UNO just works with file names. If we load the file on
+        // Windows before MM uses it, MM won't work, as it's already open.
+        // Don't move the load before the mail merge execution!
+        // (see gb_CppunitTest_use_instdir_configuration)
+        load(mpTestDocumentPath, maMMtestFilename);
+
         if (mnCurOutputType == text::MailMergeType::SHELL)
         {
             CPPUNIT_ASSERT(res >>= mxMMComponent);
@@ -206,9 +251,10 @@ protected:
     OUString mailMergeOutputPrefix;
     sal_Int16 mnCurOutputType;
     uno::Reference< lang::XComponent > mxMMComponent;
+    const char* maMMtestFilename;
 };
 
-#define DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, 
file, BaseClass) \
+#define DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, 
file, BaseClass, selection) \
     class TestName : public BaseClass { \
     protected: \
         virtual OUString getTestName() override { return OUString(#TestName); 
} \
@@ -218,7 +264,7 @@ protected:
         CPPUNIT_TEST_SUITE_END(); \
     \
         void MailMerge() { \
-            executeMailMergeTest(filename, datasource, tablename, file); \
+            executeMailMergeTest(filename, datasource, tablename, file, 
selection); \
         } \
         void verify() override; \
     }; \
@@ -227,11 +273,14 @@ protected:
 
 // 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_MAILMERGE_TEST(TestName, filename, datasource, tablename, false, 
MMTest, 0)
 
 // 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_MAILMERGE_TEST(TestName, filename, datasource, tablename, true, 
MMTest, 0)
+
+#define DECLARE_SHELL_MAILMERGE_TEST_SELECTION(TestName, filename, datasource, 
tablename, selection) \
+    DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, false, 
MMTest, selection)
 
 int MMTest::documentStartPageNumber( int document ) const
 {   // See documentStartPageNumber() .
@@ -256,9 +305,11 @@ int MMTest::documentStartPageNumber( int document ) const
     shell->Pop(false);
     return page;
 }
+
 MMTest::MMTest()
     : SwModelTestBase("/sw/qa/extras/mailmerge/data/", "writer8")
     , mnCurOutputType(0)
+    , maMMtestFilename(nullptr)
 {
 }
 
@@ -463,5 +514,22 @@ DECLARE_SHELL_MAILMERGE_TEST(testTdf92623, "tdf92623.odt", 
"10-testing-addresses
     CPPUNIT_ASSERT_EQUAL(sal_Int32(10), countFieldMarks);
 }
 
+DECLARE_SHELL_MAILMERGE_TEST_SELECTION(testTdf95292, "linked-labels.odt", 
"10-testing-addresses.ods", "testing-addresses", 5)
+{
+    // A document with two labes merged with 5 datasets should result in three 
pages
+    executeMailMerge();
+
+    SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument *>( 
mxComponent.get() );
+    CPPUNIT_ASSERT( pTextDoc );
+    SwWrtShell *pWrtShell = pTextDoc->GetDocShell()->GetWrtShell();
+    CPPUNIT_ASSERT( pWrtShell->IsLabelDoc() );
+
+    pTextDoc = dynamic_cast<SwXTextDocument *>( mxMMComponent.get() );
+    CPPUNIT_ASSERT( pTextDoc );
+    pWrtShell = pTextDoc->GetDocShell()->GetWrtShell();
+    CPPUNIT_ASSERT( !pWrtShell->IsLabelDoc() );
+    CPPUNIT_ASSERT_EQUAL( sal_uInt16( 5 ), pWrtShell->GetPhyPageNum() );
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to