2011-11-14 22:38:34 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
2013-04-19 21:10:42 +01:00
|
|
|
* This file is part of the LibreOffice project.
|
2011-11-14 22:38:34 +01:00
|
|
|
*
|
2013-04-19 21:10:42 +01:00
|
|
|
* 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/.
|
2011-11-14 22:38:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sal/config.h>
|
2011-11-27 13:47:35 +01:00
|
|
|
#include <unotest/macros_test.hxx>
|
2011-11-14 22:38:34 +01:00
|
|
|
#include <test/bootstrapfixture.hxx>
|
|
|
|
#include <rtl/strbuf.hxx>
|
|
|
|
#include <osl/file.hxx>
|
|
|
|
|
|
|
|
#include <com/sun/star/frame/XDesktop.hpp>
|
|
|
|
|
|
|
|
#include <com/sun/star/lang/XComponent.hpp>
|
2012-12-07 12:23:21 +01:00
|
|
|
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
|
|
|
|
#include <com/sun/star/beans/XPropertySet.hpp>
|
2012-12-14 12:58:00 +02:00
|
|
|
#include <com/sun/star/frame/Desktop.hpp>
|
2011-11-14 22:38:34 +01:00
|
|
|
#include <com/sun/star/frame/XComponentLoader.hpp>
|
2014-05-21 22:26:05 +02:00
|
|
|
#include <com/sun/star/frame/XStorable.hpp>
|
2011-11-14 22:38:34 +01:00
|
|
|
#include <com/sun/star/document/MacroExecMode.hpp>
|
2014-05-21 22:26:05 +02:00
|
|
|
#include <com/sun/star/document/XEmbeddedScripts.hpp>
|
|
|
|
#include <com/sun/star/script/XLibraryContainer.hpp>
|
|
|
|
#include <com/sun/star/script/XLibraryContainerPassword.hpp>
|
2012-12-07 12:23:21 +01:00
|
|
|
#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
|
|
|
|
#include <com/sun/star/drawing/XShapes.hpp>
|
|
|
|
#include <com/sun/star/drawing/XShape.hpp>
|
|
|
|
#include <com/sun/star/text/XTextDocument.hpp>
|
|
|
|
#include <com/sun/star/text/TextContentAnchorType.hpp>
|
2011-11-14 22:38:34 +01:00
|
|
|
|
|
|
|
#include <sfx2/app.hxx>
|
|
|
|
#include <sfx2/docfilt.hxx>
|
|
|
|
#include <sfx2/docfile.hxx>
|
|
|
|
#include <sfx2/sfxmodelfactory.hxx>
|
|
|
|
#include <svl/intitem.hxx>
|
2012-12-14 12:58:00 +02:00
|
|
|
#include <comphelper/processfactory.hxx>
|
2011-11-14 22:38:34 +01:00
|
|
|
|
|
|
|
#include <basic/sbxdef.hxx>
|
2014-05-21 22:26:05 +02:00
|
|
|
#include <unotools/tempfile.hxx>
|
2011-11-14 22:38:34 +01:00
|
|
|
|
2012-12-07 12:23:21 +01:00
|
|
|
#include <doc.hxx>
|
2014-08-06 16:21:14 +02:00
|
|
|
#include <IDocumentLayoutAccess.hxx>
|
2015-01-29 13:38:40 +01:00
|
|
|
#include <IDocumentUndoRedo.hxx>
|
|
|
|
#include <IDocumentContentOperations.hxx>
|
2011-11-14 22:38:34 +01:00
|
|
|
#include "docsh.hxx"
|
|
|
|
|
2014-04-07 12:31:09 +02:00
|
|
|
typedef tools::SvRef<SwDocShell> SwDocShellRef;
|
2011-11-14 22:38:34 +01:00
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
using namespace ::com::sun::star::uno;
|
|
|
|
|
|
|
|
/* Implementation of Macros test */
|
|
|
|
|
2011-11-27 13:47:35 +01:00
|
|
|
class SwMacrosTest : public test::BootstrapFixture, public unotest::MacrosTest
|
2011-11-14 22:38:34 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
SwMacrosTest();
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
void createFileURL(const OUString& aFileBase, const OUString& aFileExtension, OUString& rFilePath);
|
2011-11-14 22:38:34 +01:00
|
|
|
|
2014-03-27 18:12:18 +01:00
|
|
|
virtual void setUp() SAL_OVERRIDE;
|
|
|
|
virtual void tearDown() SAL_OVERRIDE;
|
2011-11-14 22:38:34 +01:00
|
|
|
|
|
|
|
//void testStarBasic();
|
2014-04-15 23:29:14 +02:00
|
|
|
#if !defined MACOSX && !defined WNT
|
2011-11-14 22:38:34 +01:00
|
|
|
void testVba();
|
2014-04-15 23:29:14 +02:00
|
|
|
#endif
|
2015-01-29 13:38:40 +01:00
|
|
|
void testBookmarkDeleteAndJoin();
|
2012-12-07 12:23:21 +01:00
|
|
|
void testFdo55289();
|
2014-05-21 22:26:05 +02:00
|
|
|
void testFdo68983();
|
2011-11-14 22:38:34 +01:00
|
|
|
CPPUNIT_TEST_SUITE(SwMacrosTest);
|
|
|
|
#if !defined(MACOSX) && !defined(WNT)
|
|
|
|
//enable this test if you want to play with star basic macros in unit tests
|
|
|
|
//works but does nothing useful yet
|
|
|
|
//CPPUNIT_TEST(testStarBasic);
|
|
|
|
CPPUNIT_TEST(testVba);
|
|
|
|
#endif
|
2015-01-29 13:38:40 +01:00
|
|
|
CPPUNIT_TEST(testBookmarkDeleteAndJoin);
|
2012-12-07 12:23:21 +01:00
|
|
|
CPPUNIT_TEST(testFdo55289);
|
2014-05-21 22:26:05 +02:00
|
|
|
CPPUNIT_TEST(testFdo68983);
|
2011-11-14 22:38:34 +01:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
private:
|
2011-11-27 13:47:35 +01:00
|
|
|
uno::Reference<uno::XInterface> m_xWriterComponent;
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString m_aBaseString;
|
2011-11-14 22:38:34 +01:00
|
|
|
};
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
void SwMacrosTest::createFileURL(const OUString& aFileBase, const OUString& aFileExtension, OUString& rFilePath)
|
2011-11-14 22:38:34 +01:00
|
|
|
{
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString aSep("/");
|
|
|
|
OUStringBuffer aBuffer( getSrcRootURL() );
|
2011-11-14 22:38:34 +01:00
|
|
|
aBuffer.append(m_aBaseString).append(aSep).append(aFileExtension);
|
|
|
|
aBuffer.append(aSep).append(aFileBase).append(aFileExtension);
|
|
|
|
rFilePath = aBuffer.makeStringAndClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
void SwMacrosTest::testStarBasic()
|
|
|
|
{
|
2013-04-07 12:06:47 +02:00
|
|
|
const OUString aFileNameBase("StarBasic.");
|
|
|
|
OUString aFileExtension(aFileFormats[0].pName, strlen(aFileFormats[0].pName), RTL_TEXTENCODING_UTF8 );
|
|
|
|
OUString aFileName;
|
2011-11-14 22:38:34 +01:00
|
|
|
createFileURL(aFileNameBase, aFileExtension, aFileName);
|
2013-02-05 14:38:43 -05:00
|
|
|
uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.text.TextDocument");
|
2011-11-14 22:38:34 +01:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString aURL("vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document");
|
2011-11-14 22:38:34 +01:00
|
|
|
String sUrl = aURL;
|
|
|
|
Any aRet;
|
|
|
|
Sequence< sal_Int16 > aOutParamIndex;
|
|
|
|
Sequence< Any > aOutParam;
|
|
|
|
Sequence< uno::Any > aParams;
|
|
|
|
|
|
|
|
SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
|
|
|
|
|
|
|
|
pFoundShell->CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
|
|
|
|
pFoundShell->DoClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-04-15 23:29:14 +02:00
|
|
|
#if !defined MACOSX && !defined WNT
|
2011-11-14 22:38:34 +01:00
|
|
|
void SwMacrosTest::testVba()
|
|
|
|
{
|
|
|
|
TestMacroInfo testInfo[] = {
|
|
|
|
{
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString("testVba."),
|
|
|
|
OUString("vnd.sun.Star.script:Project.NewMacros.Macro1?language=Basic&location=document")
|
2011-11-14 22:38:34 +01:00
|
|
|
}
|
|
|
|
};
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString aFileExtension( "doc" );
|
2011-11-14 22:38:34 +01:00
|
|
|
for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( testInfo ); ++i )
|
|
|
|
{
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString aFileName;
|
2011-11-14 22:38:34 +01:00
|
|
|
createFileURL(testInfo[i].sFileBaseName, aFileExtension, aFileName);
|
2013-02-05 14:38:43 -05:00
|
|
|
uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.text.TextDocument");
|
2013-12-13 11:24:35 +02:00
|
|
|
OUStringBuffer sMsg( "Failed to load " );
|
2011-11-14 22:38:34 +01:00
|
|
|
sMsg.append ( aFileName );
|
2013-04-07 12:06:47 +02:00
|
|
|
CPPUNIT_ASSERT_MESSAGE( OUStringToOString( sMsg.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ).getStr(), xComponent.is() );
|
2011-11-14 22:38:34 +01:00
|
|
|
|
2013-10-16 15:43:54 +02:00
|
|
|
OUString sUrl = testInfo[i].sMacroUrl;
|
2011-11-14 22:38:34 +01:00
|
|
|
Any aRet;
|
|
|
|
Sequence< sal_Int16 > aOutParamIndex;
|
|
|
|
Sequence< Any > aOutParam;
|
|
|
|
Sequence< uno::Any > aParams;
|
|
|
|
|
|
|
|
SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
|
2014-06-13 17:52:30 +02:00
|
|
|
SfxObjectShell::CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString aStringRes;
|
2011-11-14 22:38:34 +01:00
|
|
|
aRet >>= aStringRes;
|
2013-04-07 12:06:47 +02:00
|
|
|
std::cout << "value of Ret " << OUStringToOString( aStringRes, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
|
2012-04-06 14:09:04 +02:00
|
|
|
//CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes == "OK" );
|
2011-11-14 22:38:34 +01:00
|
|
|
pFoundShell->DoClose();
|
|
|
|
}
|
|
|
|
}
|
2014-04-15 23:29:14 +02:00
|
|
|
#endif
|
2011-11-14 22:38:34 +01:00
|
|
|
|
2015-01-29 13:38:40 +01:00
|
|
|
void SwMacrosTest::testBookmarkDeleteAndJoin()
|
|
|
|
{
|
|
|
|
SwDoc *const pDoc = new SwDoc;
|
|
|
|
pDoc->GetIDocumentUndoRedo().DoUndo(true); // bug is in SwUndoDelete
|
|
|
|
SwNodeIndex aIdx(pDoc->GetNodes().GetEndOfContent(), -1);
|
|
|
|
SwPaM aPaM(aIdx);
|
|
|
|
|
|
|
|
IDocumentContentOperations & rIDCO(pDoc->getIDocumentContentOperations());
|
|
|
|
rIDCO.AppendTxtNode(*aPaM.GetPoint());
|
|
|
|
rIDCO.InsertString(aPaM, OUString("A"));
|
|
|
|
rIDCO.AppendTxtNode(*aPaM.GetPoint());
|
|
|
|
rIDCO.InsertString(aPaM, OUString("A"));
|
|
|
|
rIDCO.AppendTxtNode(*aPaM.GetPoint());
|
|
|
|
aPaM.Move(fnMoveBackward, fnGoNode);
|
|
|
|
aPaM.Move(fnMoveBackward, fnGoNode);
|
|
|
|
aPaM.Move(fnMoveBackward, fnGoCntnt);
|
|
|
|
aPaM.SetMark();
|
|
|
|
aPaM.Move(fnMoveForward, fnGoDoc);
|
|
|
|
IDocumentMarkAccess & rIDMA = *pDoc->getIDocumentMarkAccess();
|
|
|
|
sw::mark::IMark *pMark =
|
|
|
|
rIDMA.makeMark(aPaM, "test", IDocumentMarkAccess::BOOKMARK);
|
|
|
|
CPPUNIT_ASSERT(pMark);
|
|
|
|
// select so pMark start position is on a node that is fully deleted
|
|
|
|
aPaM.Move(fnMoveBackward, fnGoNode);
|
|
|
|
// must leave un-selected content in last node to get the bJoinPrev flag!
|
|
|
|
aPaM.Move(fnMoveBackward, fnGoCntnt);
|
|
|
|
aPaM.Exchange();
|
|
|
|
aPaM.Move(fnMoveBackward, fnGoDoc);
|
|
|
|
// delete
|
|
|
|
rIDCO.DeleteAndJoin(aPaM, false);
|
|
|
|
|
|
|
|
for (IDocumentMarkAccess::const_iterator_t i = rIDMA.getAllMarksBegin(); i != rIDMA.getAllMarksEnd(); ++i)
|
|
|
|
{
|
|
|
|
// problem was that the nContent was pointing at deleted node
|
|
|
|
CPPUNIT_ASSERT((*i)->GetMarkStart().nNode.GetNode().GetCntntNode() ==
|
|
|
|
static_cast<const SwCntntNode*>((*i)->GetMarkStart().nContent.GetIdxReg()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 12:23:21 +01:00
|
|
|
void SwMacrosTest::testFdo55289()
|
|
|
|
{
|
|
|
|
SwDoc *const pDoc = new SwDoc;
|
|
|
|
SwDocShellRef pDocShell = new SwDocShell(pDoc, SFX_CREATE_MODE_EMBEDDED);
|
|
|
|
// this needs to run with no layout to tickle the bugs in the special
|
|
|
|
// cases in SwXShape re-anchoring
|
2014-08-06 16:21:14 +02:00
|
|
|
assert(!pDoc->getIDocumentLayoutAccess().GetCurrentLayout());
|
2012-12-07 12:23:21 +01:00
|
|
|
|
|
|
|
uno::Reference<frame::XModel> const xModel(pDocShell->GetModel());
|
|
|
|
uno::Reference<drawing::XDrawPageSupplier> const xDPS(xModel, UNO_QUERY);
|
|
|
|
uno::Reference<drawing::XShapes> const xShapes(xDPS->getDrawPage(),
|
|
|
|
UNO_QUERY);
|
|
|
|
uno::Reference<beans::XPropertySet> const xShape(
|
|
|
|
uno::Reference<lang::XMultiServiceFactory>(xModel, UNO_QUERY)->
|
|
|
|
createInstance("com.sun.star.drawing.GraphicObjectShape"),
|
|
|
|
UNO_QUERY);
|
|
|
|
xShape->setPropertyValue("AnchorType",
|
|
|
|
makeAny(text::TextContentAnchorType_AT_PAGE));
|
|
|
|
xShapes->add(uno::Reference<drawing::XShape>(xShape, UNO_QUERY));
|
|
|
|
xShape->setPropertyValue("AnchorType",
|
|
|
|
makeAny(text::TextContentAnchorType_AT_CHARACTER));
|
|
|
|
xShape->setPropertyValue("AnchorType",
|
|
|
|
makeAny(text::TextContentAnchorType_AS_CHARACTER));
|
|
|
|
xShape->setPropertyValue("AnchorType",
|
|
|
|
makeAny(text::TextContentAnchorType_AT_CHARACTER));
|
|
|
|
xShape->setPropertyValue("AnchorType",
|
|
|
|
makeAny(text::TextContentAnchorType_AS_CHARACTER));
|
|
|
|
uno::Reference<text::XTextRange> const xEnd =
|
|
|
|
uno::Reference<text::XTextDocument>(xModel, UNO_QUERY)->getText()->getEnd();
|
|
|
|
uno::Reference<text::XTextContent> const xShapeContent(xShape, UNO_QUERY);
|
|
|
|
xShapeContent->attach(xEnd);
|
|
|
|
}
|
|
|
|
|
2014-05-21 22:26:05 +02:00
|
|
|
void SwMacrosTest::testFdo68983()
|
|
|
|
{
|
|
|
|
OUString aFileName;
|
|
|
|
createFileURL("fdo68983.", "odt", aFileName);
|
|
|
|
Reference< com::sun::star::lang::XComponent > xComponent =
|
|
|
|
loadFromDesktop(aFileName, "com.sun.star.text.TextDocument");
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
|
|
|
|
|
|
|
|
Reference< frame::XStorable > xDocStorable(xComponent, UNO_QUERY_THROW);
|
|
|
|
CPPUNIT_ASSERT(xDocStorable.is());
|
|
|
|
|
|
|
|
utl::TempFile aTempFile;
|
|
|
|
aTempFile.EnableKillingFile();
|
|
|
|
Sequence<beans::PropertyValue> desc(1);
|
|
|
|
desc[0].Name = "FilterName";
|
|
|
|
desc[0].Value <<= OUString("writer8");
|
|
|
|
xDocStorable->storeAsURL(aTempFile.GetURL(), desc);
|
|
|
|
|
|
|
|
Reference<util::XCloseable>(xComponent, UNO_QUERY_THROW)->close(false);
|
|
|
|
|
|
|
|
// re-load
|
|
|
|
xComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
|
|
|
|
|
|
|
|
// check that password-protected library survived store and re-load
|
|
|
|
Reference<document::XEmbeddedScripts> xDocScr(xComponent, UNO_QUERY_THROW);
|
|
|
|
Reference<script::XStorageBasedLibraryContainer> xStorBasLib(xDocScr->getBasicLibraries());
|
|
|
|
Reference<script::XLibraryContainer> xBasLib(xStorBasLib, UNO_QUERY_THROW);
|
|
|
|
Reference<script::XLibraryContainerPassword> xBasLibPwd(xStorBasLib, UNO_QUERY_THROW);
|
|
|
|
CPPUNIT_ASSERT(xBasLibPwd->isLibraryPasswordProtected("Library1"));
|
|
|
|
CPPUNIT_ASSERT(xBasLibPwd->verifyLibraryPassword("Library1", "foo"));
|
|
|
|
xBasLib->loadLibrary("Library1");
|
|
|
|
CPPUNIT_ASSERT(xBasLib->isLibraryLoaded("Library1"));
|
|
|
|
|
|
|
|
// close
|
|
|
|
Reference<util::XCloseable> xDocCloseable(xComponent, UNO_QUERY_THROW);
|
|
|
|
xDocCloseable->close(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-14 22:38:34 +01:00
|
|
|
SwMacrosTest::SwMacrosTest()
|
2013-03-18 23:17:03 +01:00
|
|
|
: m_aBaseString("/sw/qa/core/data")
|
2011-11-14 22:38:34 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwMacrosTest::setUp()
|
|
|
|
{
|
|
|
|
test::BootstrapFixture::setUp();
|
|
|
|
|
|
|
|
// This is a bit of a fudge, we do this to ensure that SwGlobals::ensure,
|
|
|
|
// which is a private symbol to us, gets called
|
2011-11-27 13:47:35 +01:00
|
|
|
m_xWriterComponent =
|
2013-04-07 12:06:47 +02:00
|
|
|
getMultiServiceFactory()->createInstance(OUString(
|
2013-03-18 23:17:03 +01:00
|
|
|
"com.sun.star.comp.Writer.TextDocument"));
|
2011-11-27 13:47:35 +01:00
|
|
|
CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xWriterComponent.is());
|
2012-12-14 12:58:00 +02:00
|
|
|
mxDesktop = com::sun::star::frame::Desktop::create( comphelper::getComponentContext(getMultiServiceFactory()) );
|
2011-11-14 22:38:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SwMacrosTest::tearDown()
|
|
|
|
{
|
2011-11-27 13:47:35 +01:00
|
|
|
uno::Reference< lang::XComponent >( m_xWriterComponent, UNO_QUERY_THROW )->dispose();
|
2011-11-14 22:38:34 +01:00
|
|
|
test::BootstrapFixture::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(SwMacrosTest);
|
|
|
|
|
|
|
|
CPPUNIT_PLUGIN_IMPLEMENT();
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|