Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / qa / extras / scddelinkobj.cxx
blob49a8fda8c244df97550e6c6f5b32af04b8c42117
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <test/calc_unoapi_test.hxx>
11 #include <test/container/xnamed.hxx>
12 #include <test/sheet/xddelink.hxx>
13 #include <test/util/xrefreshable.hxx>
15 #include <unotools/tempfile.hxx>
16 #include <vcl/svapp.hxx>
18 #include <com/sun/star/beans/XPropertySet.hpp>
19 #include <com/sun/star/container/XIndexAccess.hpp>
20 #include <com/sun/star/container/XNameAccess.hpp>
21 #include <com/sun/star/lang/XComponent.hpp>
22 #include <com/sun/star/sheet/XDDELink.hpp>
23 #include <com/sun/star/sheet/XSpreadsheet.hpp>
24 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
25 #include <com/sun/star/sheet/XSpreadsheets.hpp>
26 #include <com/sun/star/uno/XInterface.hpp>
28 #include <com/sun/star/uno/Reference.hxx>
30 using namespace css;
31 using namespace css::uno;
32 using namespace com::sun::star;
34 namespace sc_apitest
36 static utl::TempFile createTempCopy(OUString const& url)
38 utl::TempFile tmp;
39 tmp.EnableKillingFile();
40 auto const e = osl::File::copy(url, tmp.GetURL());
41 CPPUNIT_ASSERT_EQUAL_MESSAGE(
42 (OUStringToOString("<" + url + "> -> <" + tmp.GetURL() + ">", RTL_TEXTENCODING_UTF8)
43 .getStr()),
44 osl::FileBase::E_None, e);
45 return tmp;
48 struct TempFileBase
50 utl::TempFile m_TempFile;
51 explicit TempFileBase(OUString const& url)
52 : m_TempFile(createTempCopy(url))
57 class ScDDELinkObj : public CalcUnoApiTest,
58 public TempFileBase,
59 public apitest::XDDELink,
60 public apitest::XNamed,
61 public apitest::XRefreshable
63 public:
64 ScDDELinkObj();
66 virtual uno::Reference<uno::XInterface> init() override;
67 virtual void setUp() override;
68 virtual void tearDown() override;
70 CPPUNIT_TEST_SUITE(ScDDELinkObj);
72 // XDDELink
73 CPPUNIT_TEST(testGetApplication);
74 CPPUNIT_TEST(testGetItem);
75 CPPUNIT_TEST(testGetTopic);
77 // XNamed
78 CPPUNIT_TEST(testGetName);
79 CPPUNIT_TEST(testSetNameThrowsException);
81 // XRefreshable
82 CPPUNIT_TEST(testRefreshListener);
84 CPPUNIT_TEST_SUITE_END();
86 private:
87 uno::Reference<lang::XComponent> mxComponent;
90 ScDDELinkObj::ScDDELinkObj()
91 : CalcUnoApiTest("/sc/qa/extras/testdocuments")
92 , TempFileBase(m_directories.getURLFromSrc("/sc/qa/unoapi/testdocuments/ScDDELinksObj.ods"))
93 , XDDELink(m_TempFile.GetURL())
94 , XNamed("soffice|" + m_TempFile.GetURL() + "!Sheet1.A1")
98 uno::Reference<uno::XInterface> ScDDELinkObj::init()
100 uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
102 uno::Reference<sheet::XSpreadsheets> xSheets(xDoc->getSheets(), UNO_SET_THROW);
103 uno::Reference<container::XIndexAccess> xIA(xSheets, UNO_QUERY_THROW);
104 uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
106 const OUString testdoc = m_TempFile.GetURL();
108 xSheet->getCellByPosition(5, 5)->setFormula("=DDE(\"soffice\";\"" + testdoc
109 + "\";\"Sheet1.A1\")");
110 xSheet->getCellByPosition(1, 4)->setFormula("=DDE(\"soffice\";\"" + testdoc
111 + "\";\"Sheet1.A1\")");
113 uno::Reference<beans::XPropertySet> xPropSet(xDoc, UNO_QUERY_THROW);
114 uno::Any aDDELinks = xPropSet->getPropertyValue("DDELinks");
115 uno::Reference<container::XNameAccess> xNA(aDDELinks, UNO_QUERY_THROW);
116 uno::Sequence<OUString> sLinkNames = xNA->getElementNames();
117 uno::Reference<sheet::XDDELink> xDDELink(xNA->getByName(sLinkNames[0]), UNO_QUERY_THROW);
118 return xDDELink;
121 void ScDDELinkObj::setUp()
123 Application::SetAppName("soffice"); // Enable DDE
124 CalcUnoApiTest::setUp();
125 // create a calc document
126 mxComponent = loadFromDesktop("private:factory/scalc");
129 void ScDDELinkObj::tearDown()
131 closeDocument(mxComponent);
132 CalcUnoApiTest::tearDown();
135 CPPUNIT_TEST_SUITE_REGISTRATION(ScDDELinkObj);
137 } // namespace sc_apitest
139 CPPUNIT_PLUGIN_IMPLEMENT();
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */