Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / sfx2 / qa / cppunit / doc.cxx
blobb16c078ef412a5c75ebc315be4ed115ea40eaa03
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/unoapi_test.hxx>
12 #include <com/sun/star/view/XSelectionSupplier.hpp>
13 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
14 #include <com/sun/star/beans/PropertyAttribute.hpp>
15 #include <com/sun/star/beans/XPropertyAccess.hpp>
17 #include <comphelper/propertyvalue.hxx>
18 #include <sfx2/objsh.hxx>
19 #include <sfx2/sfxbasemodel.hxx>
20 #include <osl/file.hxx>
21 #include <comphelper/sequenceashashmap.hxx>
22 #include <comphelper/propertysequence.hxx>
23 #include <comphelper/sequence.hxx>
25 using namespace com::sun::star;
27 namespace
29 /// Covers sfx2/source/doc/ fixes.
30 class Test : public UnoApiTest
32 public:
33 Test()
34 : UnoApiTest(u"/sfx2/qa/cppunit/data/"_ustr)
39 CPPUNIT_TEST_FIXTURE(Test, testNoGrabBagShape)
41 // Load a document and select the first shape.
42 css::uno::Sequence<css::beans::PropertyValue> aArgs{ comphelper::makePropertyValue(
43 u"ReadOnly"_ustr, true) };
44 loadWithParams(u"private:factory/simpress"_ustr, aArgs);
45 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
46 uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier(xModel, uno::UNO_QUERY);
47 uno::Reference<container::XIndexAccess> xDrawPage(
48 xDrawPagesSupplier->getDrawPages()->getByIndex(0), uno::UNO_QUERY);
49 uno::Any aShape = xDrawPage->getByIndex(0);
50 uno::Reference<view::XSelectionSupplier> xController(xModel->getCurrentController(),
51 uno::UNO_QUERY);
52 xController->select(aShape);
54 // See if it has a signing certificate associated.
55 auto pBaseModel = dynamic_cast<SfxBaseModel*>(xModel.get());
56 CPPUNIT_ASSERT(pBaseModel);
57 SfxObjectShell* pObjectShell = pBaseModel->GetObjectShell();
59 // Without the accompanying fix in place, this test would have failed with:
60 // An uncaught exception of type com.sun.star.beans.UnknownPropertyException
61 // which was not caught later, resulting in a crash.
62 pObjectShell->GetSignPDFCertificate();
65 CPPUNIT_TEST_FIXTURE(Test, testTempFilePath)
67 // Create a test file in a directory that contains the URL-encoded "testÿ" string.
68 loadFromURL(u"private:factory/swriter"_ustr);
69 auto pBaseModel = dynamic_cast<SfxBaseModel*>(mxComponent.get());
70 CPPUNIT_ASSERT(pBaseModel);
71 OUString aTargetDir
72 = m_directories.getURLFromWorkdir(u"CppunitTest/sfx2_doc.test.user/test%25C3%25Bf");
73 osl::Directory::create(aTargetDir);
74 OUString aTargetFile = aTargetDir + "/test.odt";
75 css::uno::Sequence<css::beans::PropertyValue> aArgs{ comphelper::makePropertyValue(
76 u"FilterName"_ustr, u"writer8"_ustr) };
77 pBaseModel->storeAsURL(aTargetFile, aArgs);
79 // Load it and export to PDF.
80 loadFromURL(aTargetFile);
81 pBaseModel = dynamic_cast<SfxBaseModel*>(mxComponent.get());
82 OUString aPdfTarget = aTargetDir + "/test.pdf";
83 css::uno::Sequence<css::beans::PropertyValue> aPdfArgs{ comphelper::makePropertyValue(
84 u"FilterName"_ustr, u"writer_pdf_Export"_ustr) };
85 // Without the accompanying fix in place, this test would have failed on Windows with:
86 // An uncaught exception of type com.sun.star.io.IOException
87 // because we first tried to create a temp file next to test.odt in a directory named
88 // "test%25C3%25Bf" instead of a directory named "test%C3%Bf".
89 pBaseModel->storeToURL(aPdfTarget, aPdfArgs);
92 CPPUNIT_TEST_FIXTURE(Test, testSetDocumentPropertiesUpdate)
94 // Given a document with 3 custom props, 2 Zotero ones and one other:
95 loadFromURL(u"private:factory/swriter"_ustr);
96 auto pBaseModel = dynamic_cast<SfxBaseModel*>(mxComponent.get());
97 CPPUNIT_ASSERT(pBaseModel);
98 uno::Reference<document::XDocumentProperties> xDP = pBaseModel->getDocumentProperties();
99 uno::Reference<beans::XPropertyContainer> xUDP = xDP->getUserDefinedProperties();
100 xUDP->addProperty(u"ZOTERO_PREF_1"_ustr, beans::PropertyAttribute::REMOVABLE,
101 uno::Any(u"foo"_ustr));
102 xUDP->addProperty(u"ZOTERO_PREF_2"_ustr, beans::PropertyAttribute::REMOVABLE,
103 uno::Any(u"bar"_ustr));
104 xUDP->addProperty(u"OTHER"_ustr, beans::PropertyAttribute::REMOVABLE, uno::Any(u"baz"_ustr));
106 // When updating the Zotero ones (1 update, 1 removal):
107 std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
109 "UpdatedProperties": {
110 "type": "[]com.sun.star.beans.PropertyValue",
111 "value": {
112 "NamePrefix": {
113 "type": "string",
114 "value": "ZOTERO_PREF_"
116 "UserDefinedProperties": {
117 "type": "[]com.sun.star.beans.PropertyValue",
118 "value": {
119 "ZOTERO_PREF_1": {
120 "type": "string",
121 "value": "test"
128 )json");
129 uno::Sequence<beans::PropertyValue> aArgs = comphelper::containerToSequence(aArgsVec);
130 dispatchCommand(mxComponent, u".uno:SetDocumentProperties"_ustr, aArgs);
132 // Then make sure that OTHER is still there and that ZOTERO_PREF_1 + ZOTERO_PREF_2 gets updated
133 // to the new value of a single ZOTERO_PREF_1:
134 uno::Reference<beans::XPropertyAccess> xUDPAccess(xUDP, uno::UNO_QUERY);
135 comphelper::SequenceAsHashMap aMap(xUDPAccess->getPropertyValues());
136 auto it = aMap.find(u"ZOTERO_PREF_1"_ustr);
137 CPPUNIT_ASSERT(it != aMap.end());
138 // Without the accompanying fix in place, this test would have failed with:
139 // - Expected: test
140 // - Actual : foo
141 // i.e. ZOTERO_PREF_1 was not updated.
142 CPPUNIT_ASSERT_EQUAL(u"test"_ustr, it->second.get<OUString>());
143 CPPUNIT_ASSERT(bool(aMap.find(u"ZOTERO_PREF_2"_ustr) == aMap.end()));
144 CPPUNIT_ASSERT(aMap.find(u"OTHER"_ustr) != aMap.end());
148 CPPUNIT_PLUGIN_IMPLEMENT();
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */