2014-12-02 18:27:48 +09:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2014-09-23 10:00:13 +02:00
/*
* This file is part of the LibreOffice project .
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/.
*/
2014-10-01 11:31:35 +02:00
# include <sal/config.h>
# include <set>
2016-01-20 10:48:37 +01:00
# include <vector>
2014-10-01 11:31:35 +02:00
2014-09-23 10:00:13 +02:00
# include <swmodeltestbase.hxx>
# include <com/sun/star/awt/FontWeight.hpp>
# include <com/sun/star/style/PageStyleLayout.hpp>
# include <com/sun/star/table/XCell.hpp>
# include <com/sun/star/table/BorderLine.hpp>
# include <com/sun/star/text/XTextTable.hpp>
# include <com/sun/star/text/MailMergeType.hpp>
# include <com/sun/star/sdb/XDocumentDataSource.hpp>
# include <com/sun/star/text/TextContentAnchorType.hpp>
2016-01-20 10:48:37 +01:00
# include <com/sun/star/sdbc/XRowSet.hpp>
# include <com/sun/star/sdbcx/XRowLocate.hpp>
2014-09-23 10:00:13 +02:00
2016-10-04 10:13:24 +00:00
# include <tools/urlobj.hxx>
2014-09-23 10:00:13 +02:00
# include <wrtsh.hxx>
# include <ndtxt.hxx>
# include <swdtflvr.hxx>
# include <view.hxx>
# include <edtwin.hxx>
# include <olmenu.hxx>
# include <cmdid.h>
2016-08-11 18:39:20 +02:00
# include <pagefrm.hxx>
2014-09-23 10:00:13 +02:00
2014-11-08 13:52:38 +01:00
/**
* Maps database URIs to the registered database names for quick lookups
*/
typedef std : : map < OUString , OUString > DBuriMap ;
2017-01-09 15:40:22 +01:00
static DBuriMap aDBuriMap ;
2014-11-08 13:52:38 +01:00
2014-09-23 10:00:13 +02:00
class MMTest : public SwModelTestBase
{
2014-11-06 16:51:33 +01:00
public :
2014-11-21 22:11:02 +01:00
MMTest ( ) ;
2015-10-12 16:04:04 +02:00
virtual void tearDown ( ) override
2014-11-21 22:11:02 +01:00
{
if ( mxMMComponent . is ( ) )
{
if ( mnCurOutputType = = text : : MailMergeType : : SHELL )
{
2015-05-20 13:05:49 +02:00
SwXTextDocument * pTextDoc = dynamic_cast < SwXTextDocument * > ( mxMMComponent . get ( ) ) ;
CPPUNIT_ASSERT ( pTextDoc ) ;
pTextDoc - > GetDocShell ( ) - > DoClose ( ) ;
2014-11-21 22:11:02 +01:00
}
else
mxMMComponent - > dispose ( ) ;
}
2016-12-22 15:33:33 +01:00
if ( mxCurResultSet . is ( ) )
{
css : : uno : : Reference < css : : lang : : XComponent > (
mxCurResultSet , css : : uno : : UNO_QUERY_THROW ) - > dispose ( ) ;
}
2014-11-21 22:11:02 +01:00
SwModelTestBase : : tearDown ( ) ;
}
2014-11-05 09:39:58 +01:00
/**
* Helper func used by each unit test to test the ' mail merge ' code .
*
* Registers the data source , loads the original file as reference ,
* initializes the mail merge job and its default argument sequence .
*
* The ' verify ' method actually has to execute the mail merge by
* calling executeMailMerge ( ) after modifying the job arguments .
*/
2016-08-30 13:42:20 +02:00
void executeMailMergeTest ( const char * filename , const char * datasource , const char * tablename ,
bool file , int selection , const char * column )
2014-11-05 09:39:58 +01:00
{
2016-01-20 10:48:37 +01:00
maMMtestFilename = filename ;
2014-11-05 09:39:58 +01:00
header ( ) ;
preTest ( filename ) ;
utl : : TempFile aTempDir ( nullptr , true ) ;
const OUString aWorkDir = aTempDir . GetURL ( ) ;
2016-03-09 14:14:18 +01:00
const OUString aURI ( m_directories . getURLFromSrc ( mpTestDocumentPath ) + OUString : : createFromAscii ( datasource ) ) ;
2016-08-30 13:42:20 +02:00
const OUString aPrefix = column ? OUString : : createFromAscii ( column ) : " LOMM_ " ;
const OUString aDBName = registerDBsource ( aURI , aWorkDir ) ;
initMailMergeJobAndArgs ( filename , tablename , aDBName , aPrefix , aWorkDir , file , selection , column ! = nullptr ) ;
2014-11-05 09:39:58 +01:00
verify ( ) ;
finish ( ) ;
: : utl : : removeTree ( aWorkDir ) ;
2014-11-21 22:11:02 +01:00
mnCurOutputType = 0 ;
2014-11-05 09:39:58 +01:00
}
2014-11-08 13:52:38 +01:00
OUString registerDBsource ( const OUString & aURI , const OUString & aWorkDir )
{
OUString aDBName ;
DBuriMap : : const_iterator pos = aDBuriMap . find ( aURI ) ;
if ( pos = = aDBuriMap . end ( ) )
{
2015-11-10 10:25:48 +01:00
aDBName = SwDBManager : : LoadAndRegisterDataSource ( aURI , nullptr , & aWorkDir ) ;
2014-11-08 13:52:38 +01:00
aDBuriMap . insert ( std : : pair < OUString , OUString > ( aURI , aDBName ) ) ;
std : : cout < < " New datasource name: ' " < < aDBName < < " ' " < < std : : endl ;
}
else
{
aDBName = pos - > second ;
std : : cout < < " Old datasource name: ' " < < aDBName < < " ' " < < std : : endl ;
}
CPPUNIT_ASSERT ( ! aDBName . isEmpty ( ) ) ;
return aDBName ;
}
2016-01-20 10:48:37 +01:00
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 ;
}
2014-11-08 13:52:38 +01:00
void initMailMergeJobAndArgs ( const char * filename , const char * tablename , const OUString & aDBName ,
2016-08-30 13:42:20 +02:00
const OUString & aPrefix , const OUString & aWorkDir , bool file , int nDataSets ,
const bool bPrefixIsColumn )
2014-11-08 13:52:38 +01:00
{
uno : : Reference < task : : XJob > xJob ( getMultiServiceFactory ( ) - > createInstance ( " com.sun.star.text.MailMerge " ) , uno : : UNO_QUERY_THROW ) ;
mxJob . set ( xJob ) ;
2016-10-03 21:51:56 +02:00
mMMargs . reserve ( 15 ) ;
2016-01-20 10:48:37 +01:00
2016-10-03 21:51:56 +02:00
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_OUTPUT_TYPE ) , uno : : Any ( file ? text : : MailMergeType : : FILE : text : : MailMergeType : : SHELL ) ) ) ;
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_DOCUMENT_URL ) , uno : : Any (
2016-01-20 10:48:37 +01:00
( OUString ( m_directories . getURLFromSrc ( mpTestDocumentPath ) + OUString : : createFromAscii ( filename ) ) ) ) ) ) ;
2016-10-03 21:51:56 +02:00
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_DATA_SOURCE_NAME ) , uno : : Any ( aDBName ) ) ) ;
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_OUTPUT_URL ) , uno : : Any ( aWorkDir ) ) ) ;
2016-10-04 10:13:24 +00:00
if ( file )
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_FILE_NAME_PREFIX ) , uno : : Any ( aPrefix ) ) ) ;
2016-01-20 10:48:37 +01:00
2016-08-30 13:42:20 +02:00
if ( bPrefixIsColumn )
2016-10-03 21:51:56 +02:00
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_FILE_NAME_FROM_COLUMN ) , uno : : Any ( true ) ) ) ;
2016-08-30 13:42:20 +02:00
2014-11-08 13:52:38 +01:00
if ( tablename )
{
2016-10-03 21:51:56 +02:00
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_DAD_COMMAND_TYPE ) , uno : : Any ( sdb : : CommandType : : TABLE ) ) ) ;
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_DAD_COMMAND ) , uno : : Any ( OUString : : createFromAscii ( tablename ) ) ) ) ;
2016-01-20 10:48:37 +01:00
}
if ( nDataSets > 0 )
{
2016-12-22 15:33:33 +01:00
mxCurResultSet = getXResultFromDataset ( tablename , aDBName ) ;
uno : : Reference < sdbcx : : XRowLocate > xCurRowLocate ( mxCurResultSet , uno : : UNO_QUERY ) ;
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_RESULT_SET ) , uno : : Any ( mxCurResultSet ) ) ) ;
2016-01-20 10:48:37 +01:00
std : : vector < uno : : Any > vResult ;
vResult . reserve ( nDataSets ) ;
sal_Int32 i ;
2016-12-22 15:33:33 +01:00
for ( i = 0 , mxCurResultSet - > first ( ) ; i < nDataSets ; i + + , mxCurResultSet - > next ( ) )
2016-01-20 10:48:37 +01:00
{
vResult . push_back ( uno : : Any ( xCurRowLocate - > getBookmark ( ) ) ) ;
}
2016-10-03 21:51:56 +02:00
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_SELECTION ) , uno : : Any ( comphelper : : containerToSequence ( vResult ) ) ) ) ;
2014-11-08 13:52:38 +01:00
}
2016-01-20 10:48:37 +01:00
2014-11-08 13:52:38 +01:00
}
2016-10-04 10:13:24 +00:00
void executeMailMerge ( bool bDontLoadResult = false )
2014-11-08 13:52:38 +01:00
{
2016-10-03 21:51:56 +02:00
uno : : Sequence < beans : : NamedValue > aSeqMailMergeArgs = comphelper : : containerToSequence ( mMMargs ) ;
uno : : Any res = mxJob - > execute ( aSeqMailMergeArgs ) ;
2014-11-08 13:52:38 +01:00
2016-10-03 21:51:56 +02:00
const beans : : NamedValue * pArguments = aSeqMailMergeArgs . getConstArray ( ) ;
2014-11-08 13:52:38 +01:00
bool bOk = true ;
2016-08-30 13:42:20 +02:00
bool bMMFilenameFromColumn = false ;
2016-10-03 21:51:56 +02:00
sal_Int32 nArgs = aSeqMailMergeArgs . getLength ( ) ;
2014-11-08 13:52:38 +01:00
for ( sal_Int32 i = 0 ; i < nArgs ; + + i ) {
const OUString & rName = pArguments [ i ] . Name ;
const uno : : Any & rValue = pArguments [ i ] . Value ;
// all error checking was already done by the MM job execution
if ( rName = = UNO_NAME_OUTPUT_URL )
2016-10-03 21:51:56 +02:00
bOk & = rValue > > = msMailMergeOutputURL ;
2014-11-08 13:52:38 +01:00
else if ( rName = = UNO_NAME_FILE_NAME_PREFIX )
2016-10-03 21:51:56 +02:00
bOk & = rValue > > = msMailMergeOutputPrefix ;
2014-11-08 13:52:38 +01:00
else if ( rName = = UNO_NAME_OUTPUT_TYPE )
2014-11-21 22:11:02 +01:00
bOk & = rValue > > = mnCurOutputType ;
2016-08-30 13:42:20 +02:00
else if ( rName = = UNO_NAME_FILE_NAME_FROM_COLUMN )
bOk & = rValue > > = bMMFilenameFromColumn ;
2016-10-04 10:13:24 +00:00
else if ( rName = = UNO_NAME_DOCUMENT_URL )
bOk & = rValue > > = msMailMergeDocumentURL ;
2014-11-08 13:52:38 +01:00
}
CPPUNIT_ASSERT ( bOk ) ;
2016-01-20 10:48:37 +01:00
// 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 ) ;
2014-11-21 22:11:02 +01:00
if ( mnCurOutputType = = text : : MailMergeType : : SHELL )
2014-11-08 13:52:38 +01:00
{
CPPUNIT_ASSERT ( res > > = mxMMComponent ) ;
CPPUNIT_ASSERT ( mxMMComponent . is ( ) ) ;
}
else
{
2017-01-20 11:39:37 +01:00
CPPUNIT_ASSERT_EQUAL ( uno : : makeAny ( true ) , res ) ;
2016-10-04 10:13:24 +00:00
if ( ! bMMFilenameFromColumn & & ! bDontLoadResult )
2016-08-30 13:42:20 +02:00
loadMailMergeDocument ( 0 ) ;
2014-11-08 13:52:38 +01:00
}
}
2014-11-21 22:11:02 +01:00
/**
* Like parseExport ( ) , but for given mail merge document .
*/
2016-03-14 13:41:38 +02:00
xmlDocPtr parseMailMergeExport ( const OUString & rStreamName )
2014-11-21 22:11:02 +01:00
{
if ( mnCurOutputType ! = text : : MailMergeType : : FILE )
2015-11-10 10:25:48 +01:00
return nullptr ;
2014-11-21 22:11:02 +01:00
2016-10-03 21:51:56 +02:00
OUString name = msMailMergeOutputPrefix + OUString : : number ( 0 ) + " .odt " ;
return parseExportInternal ( msMailMergeOutputURL + " / " + name , rStreamName ) ;
2014-11-21 22:11:02 +01:00
}
2016-08-30 13:42:20 +02:00
void loadMailMergeDocument ( const OUString & filename )
2014-11-21 22:11:02 +01:00
{
assert ( mnCurOutputType = = text : : MailMergeType : : FILE ) ;
if ( mxComponent . is ( ) )
mxComponent - > dispose ( ) ;
// Output name early, so in the case of a hang, the name of the hanging input file is visible.
2016-08-30 13:42:20 +02:00
std : : cout < < filename < < " , " ;
2014-11-21 22:11:02 +01:00
mnStartTime = osl_getGlobalTimer ( ) ;
2016-10-03 21:51:56 +02:00
mxComponent = loadFromDesktop ( msMailMergeOutputURL + " / " + filename , " com.sun.star.text.TextDocument " ) ;
2014-11-21 22:11:02 +01:00
CPPUNIT_ASSERT ( mxComponent . is ( ) ) ;
2016-08-30 13:42:20 +02:00
OString name2 = OUStringToOString ( filename , RTL_TEXTENCODING_UTF8 ) ;
2014-11-21 22:11:02 +01:00
discardDumpedLayout ( ) ;
if ( mustCalcLayoutOf ( name2 . getStr ( ) ) )
calcLayout ( ) ;
}
2016-08-30 13:42:20 +02:00
/**
Loads number - th document from mail merge . Requires file output from mail merge .
*/
void loadMailMergeDocument ( int number )
{
2016-10-04 10:13:24 +00:00
OUString name ;
if ( ! msMailMergeOutputPrefix . isEmpty ( ) )
name = msMailMergeOutputPrefix ;
else
{
INetURLObject aURLObj ;
aURLObj . SetSmartProtocol ( INetProtocol : : File ) ;
aURLObj . SetSmartURL ( msMailMergeDocumentURL ) ;
name = aURLObj . GetBase ( ) ;
}
name + = OUString : : number ( number ) + " .odt " ;
2016-08-30 13:42:20 +02:00
loadMailMergeDocument ( name ) ;
}
2014-11-06 16:51:33 +01:00
protected :
// Returns page number of the first page of a MM document inside the large MM document (used in the SHELL case).
int documentStartPageNumber ( int document ) const ;
2014-11-21 22:11:02 +01:00
2015-11-05 15:56:28 +02:00
uno : : Reference < css : : task : : XJob > mxJob ;
2016-10-03 21:51:56 +02:00
std : : vector < beans : : NamedValue > mMMargs ;
2016-10-04 10:13:24 +00:00
OUString msMailMergeDocumentURL ;
2016-10-03 21:51:56 +02:00
OUString msMailMergeOutputURL ;
OUString msMailMergeOutputPrefix ;
2014-11-21 22:11:02 +01:00
sal_Int16 mnCurOutputType ;
uno : : Reference < lang : : XComponent > mxMMComponent ;
2016-12-22 15:33:33 +01:00
uno : : Reference < sdbc : : XRowSet > mxCurResultSet ;
2016-01-20 10:48:37 +01:00
const char * maMMtestFilename ;
2014-09-23 10:00:13 +02:00
} ;
2016-08-30 13:42:20 +02:00
# define DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, file, BaseClass, selection, column) \
2014-11-04 16:54:19 +01:00
class TestName : public BaseClass { \
protected : \
2015-10-12 16:04:04 +02:00
virtual OUString getTestName ( ) override { return OUString ( # TestName ) ; } \
2014-11-04 16:54:19 +01:00
public : \
CPPUNIT_TEST_SUITE ( TestName ) ; \
CPPUNIT_TEST ( MailMerge ) ; \
CPPUNIT_TEST_SUITE_END ( ) ; \
\
void MailMerge ( ) { \
2016-08-30 13:42:20 +02:00
executeMailMergeTest ( filename , datasource , tablename , file , selection , column ) ; \
2014-11-04 16:54:19 +01:00
} \
2015-10-12 16:04:04 +02:00
void verify ( ) override ; \
2014-11-04 16:54:19 +01:00
} ; \
CPPUNIT_TEST_SUITE_REGISTRATION ( TestName ) ; \
void TestName : : verify ( )
2014-11-05 14:19:23 +01:00
// Will generate the resulting document in mxMMDocument.
# define DECLARE_SHELL_MAILMERGE_TEST(TestName, filename, datasource, tablename) \
2016-08-30 13:42:20 +02:00
DECLARE_MAILMERGE_TEST ( TestName , filename , datasource , tablename , false , MMTest , 0 , nullptr )
2014-09-23 10:00:13 +02:00
2014-11-05 14:19:23 +01:00
// Will generate documents as files, use loadMailMergeDocument().
# define DECLARE_FILE_MAILMERGE_TEST(TestName, filename, datasource, tablename) \
2016-08-30 13:42:20 +02:00
DECLARE_MAILMERGE_TEST ( TestName , filename , datasource , tablename , true , MMTest , 0 , nullptr )
2016-01-20 10:48:37 +01:00
# define DECLARE_SHELL_MAILMERGE_TEST_SELECTION(TestName, filename, datasource, tablename, selection) \
2016-08-30 13:42:20 +02:00
DECLARE_MAILMERGE_TEST ( TestName , filename , datasource , tablename , false , MMTest , selection , nullptr )
# define DECLARE_FILE_MAILMERGE_TEST_COLUMN(TestName, filename, datasource, tablename, column) \
DECLARE_MAILMERGE_TEST ( TestName , filename , datasource , tablename , true , MMTest , 0 , column )
2014-11-05 14:19:23 +01:00
2014-11-06 16:51:33 +01:00
int MMTest : : documentStartPageNumber ( int document ) const
2015-12-23 17:43:14 +01:00
{ // See documentStartPageNumber() .
2015-05-20 13:05:49 +02:00
SwXTextDocument * pTextDoc = dynamic_cast < SwXTextDocument * > ( mxMMComponent . get ( ) ) ;
CPPUNIT_ASSERT ( pTextDoc ) ;
SwWrtShell * shell = pTextDoc - > GetDocShell ( ) - > GetWrtShell ( ) ;
2014-11-06 16:51:33 +01:00
IDocumentMarkAccess * marks = shell - > GetDoc ( ) - > getIDocumentMarkAccess ( ) ;
// Unfortunately, the pages are marked using UNO bookmarks, which have internals names, so they cannot be referred to by their names.
// Assume that there are no other UNO bookmarks than the ones used by mail merge, and that they are in the sorted order.
IDocumentMarkAccess : : const_iterator_t mark ;
int pos = 0 ;
for ( mark = marks - > getAllMarksBegin ( ) ; mark ! = marks - > getAllMarksEnd ( ) & & pos < document ; + + mark )
{
2015-03-11 14:15:28 +02:00
if ( IDocumentMarkAccess : : GetType ( * * mark ) = = IDocumentMarkAccess : : MarkType : : UNO_BOOKMARK )
2014-11-06 16:51:33 +01:00
+ + pos ;
}
2017-01-20 11:39:37 +01:00
CPPUNIT_ASSERT_EQUAL ( document , pos ) ;
2014-11-06 16:51:33 +01:00
sal_uInt16 page , dummy ;
shell - > Push ( ) ;
shell - > GotoMark ( mark - > get ( ) ) ;
shell - > GetPageNum ( page , dummy ) ;
shell - > Pop ( false ) ;
return page ;
}
2016-01-20 10:48:37 +01:00
2014-11-21 22:11:02 +01:00
MMTest : : MMTest ( )
: SwModelTestBase ( " /sw/qa/extras/mailmerge/data/ " , " writer8 " )
, mnCurOutputType ( 0 )
2016-01-20 10:48:37 +01:00
, maMMtestFilename ( nullptr )
2014-11-21 22:11:02 +01:00
{
}
2014-11-06 16:51:33 +01:00
2014-11-05 14:19:23 +01:00
DECLARE_SHELL_MAILMERGE_TEST ( testMultiPageAnchoredDraws , " multiple-page-anchored-draws.odt " , " 4_v01.ods " , " Tabelle1 " )
2014-09-23 10:00:13 +02:00
{
executeMailMerge ( ) ;
2015-05-20 13:05:49 +02:00
SwXTextDocument * pTextDoc = dynamic_cast < SwXTextDocument * > ( mxMMComponent . get ( ) ) ;
CPPUNIT_ASSERT ( pTextDoc ) ;
sal_uInt16 nPhysPages = pTextDoc - > GetDocShell ( ) - > GetWrtShell ( ) - > GetPhyPageNum ( ) ;
2014-09-23 10:00:13 +02:00
CPPUNIT_ASSERT_EQUAL ( sal_uInt16 ( 8 ) , nPhysPages ) ;
uno : : Reference < drawing : : XDrawPageSupplier > xDrawPageSupplier ( mxMMComponent , uno : : UNO_QUERY ) ;
uno : : Reference < container : : XIndexAccess > xDraws ( xDrawPageSupplier - > getDrawPage ( ) , uno : : UNO_QUERY ) ;
CPPUNIT_ASSERT_EQUAL ( sal_Int32 ( 8 ) , xDraws - > getCount ( ) ) ;
2014-10-01 11:31:35 +02:00
std : : set < sal_uInt16 > pages ;
2014-09-23 10:00:13 +02:00
uno : : Reference < beans : : XPropertySet > xPropertySet ;
for ( sal_Int32 i = 0 ; i < xDraws - > getCount ( ) ; i + + )
{
xPropertySet . set ( xDraws - > getByIndex ( i ) , uno : : UNO_QUERY ) ;
2016-01-14 15:06:18 +01:00
text : : TextContentAnchorType nAnchorType ;
CPPUNIT_ASSERT ( xPropertySet - > getPropertyValue ( UNO_NAME_ANCHOR_TYPE ) > > = nAnchorType ) ;
2014-09-23 10:00:13 +02:00
CPPUNIT_ASSERT_EQUAL ( text : : TextContentAnchorType_AT_PAGE , nAnchorType ) ;
2016-01-14 15:06:18 +01:00
sal_uInt16 nAnchorPageNo = { } ;
CPPUNIT_ASSERT ( xPropertySet - > getPropertyValue ( UNO_NAME_ANCHOR_PAGE_NO ) > > = nAnchorPageNo ) ;
2014-10-01 11:31:35 +02:00
// are all shapes are on different page numbers?
CPPUNIT_ASSERT ( pages . insert ( nAnchorPageNo ) . second ) ;
2014-09-23 10:00:13 +02:00
}
}
2014-11-05 14:19:23 +01:00
DECLARE_FILE_MAILMERGE_TEST ( testMissingDefaultLineColor , " missing-default-line-color.ott " , " one-empty-address.ods " , " one-empty-address " )
2014-10-03 18:21:08 +02:00
{
executeMailMerge ( ) ;
// The document was created by LO version which didn't write out the default value for line color
// (see XMLGraphicsDefaultStyle::SetDefaults()).
2014-11-05 14:19:23 +01:00
uno : : Reference < drawing : : XDrawPageSupplier > xDrawPageSupplier ( mxComponent , uno : : UNO_QUERY ) ;
2014-10-03 18:21:08 +02:00
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 " ) ) ;
2015-05-20 13:05:49 +02:00
SwXTextDocument * pTextDoc = dynamic_cast < SwXTextDocument * > ( mxComponent . get ( ) ) ;
CPPUNIT_ASSERT ( pTextDoc ) ;
2014-11-05 14:19:23 +01:00
uno : : Reference < lang : : XMultiServiceFactory > xFact ( mxComponent , uno : : UNO_QUERY ) ;
2014-10-03 18:21:08 +02:00
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 ( ) ) ;
CPPUNIT_ASSERT ( xInfo - > hasPropertyByName ( " LineColor " ) ) ;
sal_uInt32 lineColor ;
xDefaults - > getPropertyValue ( " LineColor " ) > > = lineColor ;
// And the default value is black (wasn't copied properly by mailmerge).
CPPUNIT_ASSERT_EQUAL ( COL_BLACK , lineColor ) ;
2014-11-05 14:19:23 +01:00
// And check that the resulting file has the proper default.
2016-03-14 13:41:38 +02:00
xmlDocPtr pXmlDoc = parseMailMergeExport ( " styles.xml " ) ;
2014-11-05 14:19:23 +01:00
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 " ) ) ;
2014-10-03 18:21:08 +02:00
}
2014-11-05 19:27:19 +01:00
DECLARE_FILE_MAILMERGE_TEST ( testSimpleMailMerge , " simple-mail-merge.odt " , " 10-testing-addresses.ods " , " testing-addresses " )
{
executeMailMerge ( ) ;
for ( int doc = 0 ;
doc < 10 ;
+ + doc )
{
loadMailMergeDocument ( doc ) ;
CPPUNIT_ASSERT_EQUAL ( 1 , getPages ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " Fixed text. " ) , getRun ( getParagraph ( 1 ) , 1 ) - > getString ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " lastname " + OUString : : number ( doc + 1 ) ) , getRun ( getParagraph ( 2 ) , 1 ) - > getString ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " Another fixed text. " ) , getRun ( getParagraph ( 3 ) , 1 ) - > getString ( ) ) ;
}
}
2014-11-05 20:46:49 +01:00
DECLARE_FILE_MAILMERGE_TEST ( test2Pages , " simple-mail-merge-2pages.odt " , " 10-testing-addresses.ods " , " testing-addresses " )
{
executeMailMerge ( ) ;
for ( int doc = 0 ;
doc < 10 ;
+ + doc )
{
loadMailMergeDocument ( doc ) ;
OUString lastname = " lastname " + OUString : : number ( doc + 1 ) ;
OUString firstname = " firstname " + OUString : : number ( doc + 1 ) ;
CPPUNIT_ASSERT_EQUAL ( 2 , getPages ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " Fixed text. " ) , getRun ( getParagraph ( 1 ) , 1 ) - > getString ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( lastname , getRun ( getParagraph ( 2 ) , 1 ) - > getString ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " Another fixed text. " ) , getRun ( getParagraph ( 3 ) , 1 ) - > getString ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " " ) , getRun ( getParagraph ( 4 ) , 1 ) - > getString ( ) ) ; // empty para at the end of page 1
CPPUNIT_ASSERT_EQUAL ( OUString ( " Second page. " ) , getRun ( getParagraph ( 5 ) , 1 ) - > getString ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( firstname , getRun ( getParagraph ( 6 ) , 1 ) - > getString ( ) ) ;
// Also verify the layout.
CPPUNIT_ASSERT_EQUAL ( lastname , parseDump ( " /root/page[1]/body/txt[2]/Special " , " rText " ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " Fixed text. " ) , parseDump ( " /root/page[1]/body/txt[1] " , " " ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " " ) , parseDump ( " /root/page[1]/body/txt[4] " , " " ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " Second page. " ) , parseDump ( " /root/page[2]/body/txt[1] " , " " ) ) ;
CPPUNIT_ASSERT_EQUAL ( firstname , parseDump ( " /root/page[2]/body/txt[2]/Special " , " rText " ) ) ;
}
}
2014-11-06 16:51:33 +01:00
DECLARE_SHELL_MAILMERGE_TEST ( testPageBoundariesSimpleMailMerge , " simple-mail-merge.odt " , " 10-testing-addresses.ods " , " testing-addresses " )
{
// This is like the test above, but this one uses the create-single-document-containing-everything-generated approach,
// and verifies that boundaries of the generated sub-documents are correct inside that document.
2015-12-23 17:43:14 +01:00
// These boundaries are done using "documentStartPageNumber<number>" UNO bookmarks (see also
// documentStartPageNumber() ).
2014-11-06 16:51:33 +01:00
executeMailMerge ( ) ;
// Here getPages() works on the source document, so get pages of the resulting one.
2015-05-20 13:05:49 +02:00
SwXTextDocument * pTextDoc = dynamic_cast < SwXTextDocument * > ( mxMMComponent . get ( ) ) ;
CPPUNIT_ASSERT ( pTextDoc ) ;
CPPUNIT_ASSERT_EQUAL ( sal_uInt16 ( 19 ) , pTextDoc - > GetDocShell ( ) - > GetWrtShell ( ) - > GetPhyPageNum ( ) ) ; // 10 pages, but each sub-document starts on odd page number
2014-11-06 16:51:33 +01:00
for ( int doc = 0 ;
doc < 10 ;
+ + doc )
{
CPPUNIT_ASSERT_EQUAL ( doc * 2 + 1 , documentStartPageNumber ( doc ) ) ;
}
}
DECLARE_SHELL_MAILMERGE_TEST ( testPageBoundaries2Pages , " simple-mail-merge-2pages.odt " , " 10-testing-addresses.ods " , " testing-addresses " )
{
executeMailMerge ( ) ;
2015-05-20 13:05:49 +02:00
SwXTextDocument * pTextDoc = dynamic_cast < SwXTextDocument * > ( mxMMComponent . get ( ) ) ;
CPPUNIT_ASSERT ( pTextDoc ) ;
CPPUNIT_ASSERT_EQUAL ( sal_uInt16 ( 20 ) , pTextDoc - > GetDocShell ( ) - > GetWrtShell ( ) - > GetPhyPageNum ( ) ) ; // 20 pages, each sub-document starts on odd page number
2014-11-06 16:51:33 +01:00
for ( int doc = 0 ;
doc < 10 ;
+ + doc )
{
CPPUNIT_ASSERT_EQUAL ( doc * 2 + 1 , documentStartPageNumber ( doc ) ) ;
}
}
2015-04-09 14:20:44 +02:00
DECLARE_SHELL_MAILMERGE_TEST ( testTdf89214 , " tdf89214.odt " , " 10-testing-addresses.ods " , " testing-addresses " )
{
executeMailMerge ( ) ;
uno : : Reference < text : : XTextDocument > xTextDocument ( mxMMComponent , uno : : UNO_QUERY ) ;
uno : : Reference < text : : XTextRange > xParagraph ( getParagraphOrTable ( 3 , xTextDocument - > getText ( ) ) , uno : : UNO_QUERY ) ;
// Make sure that we assert the right paragraph.
CPPUNIT_ASSERT_EQUAL ( OUString ( " a " ) , xParagraph - > getString ( ) ) ;
// This paragraph had a bullet numbering, make sure that the list id is not empty.
CPPUNIT_ASSERT ( ! getProperty < OUString > ( xParagraph , " ListId " ) . isEmpty ( ) ) ;
}
tdf#90230 SwDoc::AppendDoc: take care of marks when inserting page break
SwDoc::AppendDoc() inserts nodes from an other document, and before
doing that, it inserts a page break at the end of the document. In case
there are marks at the end of the last paragraph, the insertion of the
page break moves them to the next page. This is a rare situation, but
happens e.g. when the source document is an empty one: then MM puts a
mark at the first paragraph of each inserted MM part, and then the first
paragraph == the last paragraph, so the mark of the only paragraph in
the document gets moved to the next page.
This is a problem on its own, but is detected by the SwIndexReg dtor
when that empty paragraph gets deleted later in SwNodes::Delete() called
by SwDoc::AppendDoc(), resulting in an assertion failure.
Triggered by commit a305a2c91420652db450b7f8edd140e1d69f42cf (use
bookmarks to mark mailmerge parts in a mailmerge document (fdo#80823),
2014-10-20), these not adjusted bookmarks were not detected before.
Change-Id: I89775b477a2fd3182b2bc87144aed2bfe7912aff
2015-04-10 11:54:03 +02:00
DECLARE_SHELL_MAILMERGE_TEST ( testTdf90230 , " empty.odt " , " 10-testing-addresses.ods " , " testing-addresses " )
{
// MM of an empty document caused an assertion in the SwIndexReg dtor.
executeMailMerge ( ) ;
}
2016-01-02 02:07:24 +01:00
DECLARE_SHELL_MAILMERGE_TEST ( testTdf92623 , " tdf92623.odt " , " 10-testing-addresses.ods " , " testing-addresses " )
{
// Copying bookmarks for MM was broken because of the StartOfContent node copy
2016-04-30 17:30:50 +02:00
// copied marks were off by one
2016-01-02 02:07:24 +01:00
executeMailMerge ( ) ;
SwXTextDocument * pTextDoc = dynamic_cast < SwXTextDocument * > ( mxComponent . get ( ) ) ;
CPPUNIT_ASSERT ( pTextDoc ) ;
IDocumentMarkAccess const * pIDMA = pTextDoc - > GetDocShell ( ) - > GetDoc ( ) - > getIDocumentMarkAccess ( ) ;
// There is just one mark...
CPPUNIT_ASSERT_EQUAL ( sal_Int32 ( 1 ) , pIDMA - > getAllMarksCount ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( sal_Int32 ( 0 ) , pIDMA - > getBookmarksCount ( ) ) ;
IDocumentMarkAccess : : const_iterator_t mark = pIDMA - > getAllMarksBegin ( ) ;
// and it's a TEXT_FIELDMARK
CPPUNIT_ASSERT_EQUAL ( sal_Int32 ( IDocumentMarkAccess : : GetType ( * * mark ) ) ,
sal_Int32 ( IDocumentMarkAccess : : MarkType : : TEXT_FIELDMARK ) ) ;
sal_uLong src_pos = ( * mark ) - > GetMarkPos ( ) . nNode . GetIndex ( ) ;
// Get the size of the document in nodes
SwDoc * doc = pTextDoc - > GetDocShell ( ) - > GetDoc ( ) ;
sal_uLong size = doc - > GetNodes ( ) . GetEndOfContent ( ) . GetIndex ( ) - doc - > GetNodes ( ) . GetEndOfExtras ( ) . GetIndex ( ) ;
CPPUNIT_ASSERT_EQUAL ( sal_uLong ( 13 ) , size ) ;
size - = 2 ; // For common start and end nodes
// Iterate over all field marks in the target document and check that they
// are positioned at a multitude of the document size
SwXTextDocument * pMMTextDoc = dynamic_cast < SwXTextDocument * > ( mxMMComponent . get ( ) ) ;
CPPUNIT_ASSERT ( pMMTextDoc ) ;
pIDMA = pMMTextDoc - > GetDocShell ( ) - > GetDoc ( ) - > getIDocumentMarkAccess ( ) ;
// The target document has the duplicated amount of bookmarks
// as the helping uno bookmark from the mail merge is left in the doc
// TODO should be fixed!
CPPUNIT_ASSERT_EQUAL ( sal_Int32 ( 20 ) , pIDMA - > getAllMarksCount ( ) ) ;
std : : set < sal_uLong > pages ;
sal_Int32 countFieldMarks = 0 ;
for ( mark = pIDMA - > getAllMarksBegin ( ) ; mark ! = pIDMA - > getAllMarksEnd ( ) ; + + mark )
{
IDocumentMarkAccess : : MarkType markType = IDocumentMarkAccess : : GetType ( * * mark ) ;
if ( markType = = IDocumentMarkAccess : : MarkType : : TEXT_FIELDMARK )
{
sal_uLong pos = ( * mark ) - > GetMarkPos ( ) . nNode . GetIndex ( ) - src_pos ;
CPPUNIT_ASSERT_EQUAL ( sal_uLong ( 0 ) , pos % size ) ;
CPPUNIT_ASSERT ( pages . insert ( pos ) . second ) ;
countFieldMarks + + ;
}
else // see previous TODO
CPPUNIT_ASSERT_EQUAL ( sal_Int32 ( markType ) , sal_Int32 ( IDocumentMarkAccess : : MarkType : : UNO_BOOKMARK ) ) ;
}
CPPUNIT_ASSERT_EQUAL ( sal_Int32 ( 10 ) , countFieldMarks ) ;
}
2016-01-20 10:48:37 +01:00
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 ( ) ) ;
}
2016-08-11 18:39:20 +02:00
DECLARE_SHELL_MAILMERGE_TEST ( test_sections_first_last , " sections_first_last.odt " , " 10-testing-addresses.ods " , " testing-addresses " )
{
// A document with a leading, middle and trailing section
// Originally we were losing the trailing section during merge
executeMailMerge ( ) ;
SwXTextDocument * pTextDoc = dynamic_cast < SwXTextDocument * > ( mxComponent . get ( ) ) ;
CPPUNIT_ASSERT ( pTextDoc ) ;
// Get the size of the document in nodes
SwDoc * pDoc = pTextDoc - > GetDocShell ( ) - > GetDoc ( ) ;
sal_uLong nSize = pDoc - > GetNodes ( ) . GetEndOfContent ( ) . GetIndex ( ) - pDoc - > GetNodes ( ) . GetEndOfExtras ( ) . GetIndex ( ) ;
nSize - = 2 ; // The common start and end node
CPPUNIT_ASSERT_EQUAL ( sal_uLong ( 13 ) , nSize ) ;
SwXTextDocument * pTextDocMM = dynamic_cast < SwXTextDocument * > ( mxMMComponent . get ( ) ) ;
CPPUNIT_ASSERT ( pTextDocMM ) ;
SwDoc * pDocMM = pTextDocMM - > GetDocShell ( ) - > GetDoc ( ) ;
sal_uLong nSizeMM = pDocMM - > GetNodes ( ) . GetEndOfContent ( ) . GetIndex ( ) - pDocMM - > GetNodes ( ) . GetEndOfExtras ( ) . GetIndex ( ) ;
nSizeMM - = 2 ;
CPPUNIT_ASSERT_EQUAL ( sal_uLong ( 10 * nSize ) , nSizeMM ) ;
CPPUNIT_ASSERT_EQUAL ( sal_uInt16 ( 19 ) , pDocMM - > GetDocShell ( ) - > GetWrtShell ( ) - > GetPhyPageNum ( ) ) ;
// All even pages should be empty, all sub-documents have two pages
const SwRootFrame * pLayout = pDocMM - > getIDocumentLayoutAccess ( ) . GetCurrentLayout ( ) ;
const SwPageFrame * pPageFrm = static_cast < const SwPageFrame * > ( pLayout - > Lower ( ) ) ;
while ( pPageFrm )
{
bool bOdd = ( 1 = = ( pPageFrm - > GetPhyPageNum ( ) % 2 ) ) ;
CPPUNIT_ASSERT_EQUAL ( ! bOdd , pPageFrm - > IsEmptyPage ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( sal_uInt16 ( bOdd ? 1 : 2 ) , pPageFrm - > GetVirtPageNum ( ) ) ;
pPageFrm = static_cast < const SwPageFrame * > ( pPageFrm - > GetNext ( ) ) ;
}
}
2016-08-30 13:42:20 +02:00
DECLARE_FILE_MAILMERGE_TEST_COLUMN ( testDirMailMerge , " simple-mail-merge.odt " , " 10-testing-addresses.ods " , " testing-addresses " , " Filename " )
{
executeMailMerge ( ) ;
for ( int doc = 1 ;
doc < = 10 ;
+ + doc )
{
OUString filename = " sub/lastname " + OUString : : number ( doc )
+ " firstname " + OUString : : number ( doc ) + " .odt " ;
loadMailMergeDocument ( filename ) ;
CPPUNIT_ASSERT_EQUAL ( 1 , getPages ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " Fixed text. " ) , getRun ( getParagraph ( 1 ) , 1 ) - > getString ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " lastname " + OUString : : number ( doc ) ) , getRun ( getParagraph ( 2 ) , 1 ) - > getString ( ) ) ;
CPPUNIT_ASSERT_EQUAL ( OUString ( " Another fixed text. " ) , getRun ( getParagraph ( 3 ) , 1 ) - > getString ( ) ) ;
}
}
2016-10-04 10:13:24 +00:00
DECLARE_FILE_MAILMERGE_TEST ( testTdf102010 , " empty.odt " , " 10-testing-addresses.ods " , " testing-addresses " )
{
// Create "correct" automatic filename for non-user-supplied-prefix
for ( auto aNamedValueIter = mMMargs . begin ( ) ; aNamedValueIter ! = mMMargs . end ( ) ; )
{
if ( aNamedValueIter - > Name = = UNO_NAME_FILE_NAME_PREFIX )
aNamedValueIter = mMMargs . erase ( aNamedValueIter ) ;
else
{
std : : cout < < aNamedValueIter - > Name < < " : " < < aNamedValueIter - > Value < < std : : endl ;
+ + aNamedValueIter ;
}
}
mMMargs . push_back ( beans : : NamedValue ( OUString ( UNO_NAME_SAVE_AS_SINGLE_FILE ) , uno : : Any ( true ) ) ) ;
// Generate correct mail merge result filename
executeMailMerge ( ) ;
// Don't overwrite previous result
executeMailMerge ( true ) ;
loadMailMergeDocument ( 1 ) ;
}
2014-09-23 10:00:13 +02:00
CPPUNIT_PLUGIN_IMPLEMENT ( ) ;
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */