Kaydet (Commit) b4bc6521 authored tarafından Luboš Luňák's avatar Luboš Luňák

make it possible to check mailmerge output files in unittests

Change-Id: Ifa54445c8f46bb2ed5539870c5fce1a2e62beef7
üst f373aa0b
...@@ -182,6 +182,8 @@ protected: ...@@ -182,6 +182,8 @@ protected:
utl::TempFile maTempFile; utl::TempFile maTempFile;
bool mbExported; ///< Does maTempFile already contain something useful? bool mbExported; ///< Does maTempFile already contain something useful?
sal_Int16 nCurOutputType; sal_Int16 nCurOutputType;
OUString mailMergeOutputURL;
OUString mailMergeOutputPrefix;
protected: protected:
virtual OUString getTestName() { return OUString(); } virtual OUString getTestName() { return OUString(); }
...@@ -612,6 +614,25 @@ protected: ...@@ -612,6 +614,25 @@ protected:
calcLayout(); 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) void reload(const char* pFilter, const char* filename)
{ {
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY); uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
...@@ -692,8 +713,25 @@ protected: ...@@ -692,8 +713,25 @@ protected:
if (!mbExported) if (!mbExported)
return 0; 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. // 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); uno::Reference<io::XInputStream> xInputStream(xNameAccess->getByName(rStreamName), uno::UNO_QUERY);
boost::shared_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true)); boost::shared_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true));
...@@ -783,7 +821,7 @@ protected: ...@@ -783,7 +821,7 @@ protected:
} }
virtual void initMailMergeJobAndArgs( const char* filename, const char* tablename, const OUString &aDBName, 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 ); uno::Reference< task::XJob > xJob( getMultiServiceFactory()->createInstance( "com.sun.star.text.MailMerge" ), uno::UNO_QUERY_THROW );
mxJob.set( xJob ); mxJob.set( xJob );
...@@ -793,7 +831,7 @@ protected: ...@@ -793,7 +831,7 @@ protected:
mSeqMailMergeArgs.realloc( seq_id ); mSeqMailMergeArgs.realloc( seq_id );
seq_id = 0; 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( mSeqMailMergeArgs[ seq_id++ ] = beans::NamedValue( OUString( UNO_NAME_DOCUMENT_URL ), uno::Any(
( OUString(getURLFromSrc(mpTestDocumentPath) + OUString::createFromAscii(filename)) ) ) ); ( OUString(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_DATA_SOURCE_NAME ), uno::Any( aDBName ) );
...@@ -810,8 +848,6 @@ protected: ...@@ -810,8 +848,6 @@ protected:
{ {
uno::Any res = mxJob->execute( mSeqMailMergeArgs ); uno::Any res = mxJob->execute( mSeqMailMergeArgs );
OUString aCurOutputURL;
OUString aCurFileNamePrefix;
const beans::NamedValue *pArguments = mSeqMailMergeArgs.getConstArray(); const beans::NamedValue *pArguments = mSeqMailMergeArgs.getConstArray();
bool bOk = true; bool bOk = true;
sal_Int32 nArgs = mSeqMailMergeArgs.getLength(); sal_Int32 nArgs = mSeqMailMergeArgs.getLength();
...@@ -822,9 +858,9 @@ protected: ...@@ -822,9 +858,9 @@ protected:
// all error checking was already done by the MM job execution // all error checking was already done by the MM job execution
if (rName == UNO_NAME_OUTPUT_URL) if (rName == UNO_NAME_OUTPUT_URL)
bOk &= rValue >>= aCurOutputURL; bOk &= rValue >>= mailMergeOutputURL;
else if (rName == UNO_NAME_FILE_NAME_PREFIX) else if (rName == UNO_NAME_FILE_NAME_PREFIX)
bOk &= rValue >>= aCurFileNamePrefix; bOk &= rValue >>= mailMergeOutputPrefix;
else if (rName == UNO_NAME_OUTPUT_TYPE) else if (rName == UNO_NAME_OUTPUT_TYPE)
bOk &= rValue >>= nCurOutputType; bOk &= rValue >>= nCurOutputType;
} }
...@@ -839,10 +875,7 @@ protected: ...@@ -839,10 +875,7 @@ protected:
else else
{ {
CPPUNIT_ASSERT(res == true); CPPUNIT_ASSERT(res == true);
mxMMComponent = loadFromDesktop( aCurOutputURL + "/" + aCurFileNamePrefix + "0.odt", loadMailMergeDocument( 0 );
"com.sun.star.text.TextDocument");
CPPUNIT_ASSERT(mxMMComponent.is());
calcLayout();
} }
} }
}; };
......
...@@ -44,7 +44,7 @@ class MMTest : public SwModelTestBase ...@@ -44,7 +44,7 @@ class MMTest : public SwModelTestBase
* The 'verify' method actually has to execute the mail merge by * The 'verify' method actually has to execute the mail merge by
* calling executeMailMerge() after modifying the job arguments. * 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(); header();
preTest(filename); preTest(filename);
...@@ -55,18 +55,19 @@ class MMTest : public SwModelTestBase ...@@ -55,18 +55,19 @@ class MMTest : public SwModelTestBase
const OUString aWorkDir = aTempDir.GetURL(); const OUString aWorkDir = aTempDir.GetURL();
const OUString aURI( getURLFromSrc(mpTestDocumentPath) + OUString::createFromAscii(datasource) ); const OUString aURI( getURLFromSrc(mpTestDocumentPath) + OUString::createFromAscii(datasource) );
OUString aDBName = registerDBsource( aURI, aPrefix, aWorkDir ); OUString aDBName = registerDBsource( aURI, aPrefix, aWorkDir );
initMailMergeJobAndArgs( filename, tablename, aDBName, aPrefix, aWorkDir ); initMailMergeJobAndArgs( filename, tablename, aDBName, aPrefix, aWorkDir, file );
postTest(filename); postTest(filename);
verify(); verify();
finish(); finish();
::utl::removeTree(aWorkDir); ::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 { \ class TestName : public BaseClass { \
protected: \ protected: \
virtual OUString getTestName() SAL_OVERRIDE { return OUString::createFromAscii(#TestName); } \ virtual OUString getTestName() SAL_OVERRIDE { return OUString::createFromAscii(#TestName); } \
...@@ -76,17 +77,22 @@ class MMTest : public SwModelTestBase ...@@ -76,17 +77,22 @@ class MMTest : public SwModelTestBase
CPPUNIT_TEST_SUITE_END(); \ CPPUNIT_TEST_SUITE_END(); \
\ \
void MailMerge() { \ void MailMerge() { \
executeMailMergeTest(filename, datasource, tablename); \ executeMailMergeTest(filename, datasource, tablename, file); \
} \ } \
void verify() SAL_OVERRIDE; \ void verify() SAL_OVERRIDE; \
}; \ }; \
CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \ CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \
void TestName::verify() void TestName::verify()
#define DECLARE_DFLT_MAILMERGE_TEST(TestName, filename, datasource, tablename) \ // Will generate the resulting document in mxMMDocument.
DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, MMTest) #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(); executeMailMerge();
...@@ -117,19 +123,19 @@ DECLARE_DFLT_MAILMERGE_TEST(testMultiPageAnchoredDraws, "multiple-page-anchored- ...@@ -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(); executeMailMerge();
// The document was created by LO version which didn't write out the default value for line color // The document was created by LO version which didn't write out the default value for line color
// (see XMLGraphicsDefaultStyle::SetDefaults()). // (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<container::XIndexAccess> xDraws(xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY);
uno::Reference<beans::XPropertySet> xPropertySet(xDraws->getByIndex(0), uno::UNO_QUERY); uno::Reference<beans::XPropertySet> xPropertySet(xDraws->getByIndex(0), uno::UNO_QUERY);
// Lines do not have a line color. // Lines do not have a line color.
CPPUNIT_ASSERT( !xPropertySet->getPropertySetInfo()->hasPropertyByName( "LineColor" )); CPPUNIT_ASSERT( !xPropertySet->getPropertySetInfo()->hasPropertyByName( "LineColor" ));
SwXTextDocument* pTxtDoc = dynamic_cast<SwXTextDocument *>(mxMMComponent.get()); SwXTextDocument* pTxtDoc = dynamic_cast<SwXTextDocument *>(mxComponent.get());
CPPUNIT_ASSERT(pTxtDoc); 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 ); uno::Reference< beans::XPropertySet > xDefaults( xFact->createInstance( "com.sun.star.drawing.Defaults" ), uno::UNO_QUERY );
CPPUNIT_ASSERT( xDefaults.is()); CPPUNIT_ASSERT( xDefaults.is());
uno::Reference< beans::XPropertySetInfo > xInfo( xDefaults->getPropertySetInfo()); uno::Reference< beans::XPropertySetInfo > xInfo( xDefaults->getPropertySetInfo());
...@@ -138,6 +144,10 @@ DECLARE_DFLT_MAILMERGE_TEST(testMissingDefaultLineColor, "missing-default-line-c ...@@ -138,6 +144,10 @@ DECLARE_DFLT_MAILMERGE_TEST(testMissingDefaultLineColor, "missing-default-line-c
xDefaults->getPropertyValue( "LineColor" ) >>= lineColor; xDefaults->getPropertyValue( "LineColor" ) >>= lineColor;
// And the default value is black (wasn't copied properly by mailmerge). // And the default value is black (wasn't copied properly by mailmerge).
CPPUNIT_ASSERT_EQUAL( COL_BLACK, lineColor ); 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 #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment