1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  */
9 
10 #include <test/style/xstyleloader.hxx>
11 
12 #include <com/sun/star/beans/XPropertySet.hpp>
13 #include <com/sun/star/container/XNameContainer.hpp>
14 #include <com/sun/star/io/XInputStream.hpp>
15 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
16 #include <com/sun/star/style/XStyle.hpp>
17 #include <com/sun/star/style/XStyleLoader2.hpp>
18 
19 #include <com/sun/star/uno/Any.hxx>
20 #include <com/sun/star/uno/Reference.hxx>
21 #include <com/sun/star/uno/Sequence.hxx>
22 
23 #include <rtl/ustring.hxx>
24 #include <cppunit/TestAssert.h>
25 #include <comphelper/processfactory.hxx>
26 #include <comphelper/storagehelper.hxx>
27 
28 using namespace comphelper;
29 using namespace css;
30 using namespace css::uno;
31 
32 namespace apitest
33 {
testLoadStylesFromURL()34 void XStyleLoader::testLoadStylesFromURL()
35 {
36     uno::Reference<style::XStyleLoader2> xStyleLoader(init(), uno::UNO_QUERY_THROW);
37 
38     uno::Reference<sheet::XSpreadsheetDocument> xDoc(getTargetDoc(), uno::UNO_SET_THROW);
39     const OUString aFileURL = getTestURL();
40 
41     uno::Sequence<beans::PropertyValue> aOptions = xStyleLoader->getStyleLoaderOptions();
42     xStyleLoader->loadStylesFromURL(aFileURL, aOptions);
43 
44     uno::Reference<style::XStyleFamiliesSupplier> xFamilySupplier(xDoc, UNO_QUERY_THROW);
45     checkStyleProperties(xFamilySupplier);
46 }
47 
testLoadStylesFromDocument()48 void XStyleLoader::testLoadStylesFromDocument()
49 {
50     uno::Reference<style::XStyleLoader2> xStyleLoader(init(), uno::UNO_QUERY_THROW);
51 
52     uno::Reference<sheet::XSpreadsheetDocument> xDoc(getTargetDoc(), uno::UNO_SET_THROW);
53     uno::Reference<lang::XComponent> xSrcComponent(getSourceComponent(), UNO_SET_THROW);
54 
55     uno::Sequence<beans::PropertyValue> aOptions = xStyleLoader->getStyleLoaderOptions();
56     xStyleLoader->loadStylesFromDocument(xSrcComponent, aOptions);
57 
58     uno::Reference<style::XStyleFamiliesSupplier> xFamilySupplier(xDoc, UNO_QUERY_THROW);
59     checkStyleProperties(xFamilySupplier);
60 }
61 
testLoadStylesFromStream()62 void XStyleLoader::testLoadStylesFromStream()
63 {
64     uno::Reference<style::XStyleLoader2> xStyleLoader(init(), uno::UNO_QUERY_THROW);
65 
66     uno::Reference<sheet::XSpreadsheetDocument> xDoc(getTargetDoc(), uno::UNO_SET_THROW);
67     const OUString aFileURL = getTestURL();
68     const uno::Reference<io::XInputStream> xInputStream
69         = OStorageHelper::GetInputStreamFromURL(aFileURL, getProcessComponentContext());
70 
71     uno::Sequence<beans::PropertyValue> aOptions = xStyleLoader->getStyleLoaderOptions();
72     auto nLength = aOptions.getLength();
73     aOptions.realloc(nLength + 1);
74     beans::PropertyValue aInputStream;
75     aInputStream.Name = "InputStream";
76     uno::Any aTmp;
77     aTmp <<= xInputStream;
78     aInputStream.Value = aTmp;
79     aOptions[nLength] = aInputStream;
80 
81     xStyleLoader->loadStylesFromURL("private:stream", aOptions);
82 
83     uno::Reference<style::XStyleFamiliesSupplier> xFamilySupplier(xDoc, UNO_QUERY_THROW);
84     checkStyleProperties(xFamilySupplier);
85 }
86 
checkStyleProperties(uno::Reference<style::XStyleFamiliesSupplier> const & xFamilySupplier)87 void XStyleLoader::checkStyleProperties(
88     uno::Reference<style::XStyleFamiliesSupplier> const& xFamilySupplier)
89 {
90     // check if targetDocument has myStyle
91     uno::Reference<container::XNameAccess> xFamilies(xFamilySupplier->getStyleFamilies(),
92                                                      UNO_SET_THROW);
93     uno::Reference<container::XNameContainer> xCellStyles(xFamilies->getByName("CellStyles"),
94                                                           UNO_QUERY_THROW);
95 
96     CPPUNIT_ASSERT_MESSAGE("Style not imported", xCellStyles->hasByName("myStyle"));
97 
98     // test the backgroundcolor is correctly imported
99     uno::Reference<style::XStyle> xMyStyle(xCellStyles->getByName("myStyle"), UNO_QUERY_THROW);
100     uno::Reference<beans::XPropertySet> xPropSet(xMyStyle, UNO_QUERY_THROW);
101 
102     uno::Any aBackColor = xPropSet->getPropertyValue("CellBackColor");
103     uno::Any expectedBackColor(sal_Int32(16724787));
104 
105     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong CellBackColor", expectedBackColor, aBackColor);
106 
107     // test default pageStyle
108 
109     uno::Reference<container::XNameContainer> xPageStyles(xFamilies->getByName("PageStyles"),
110                                                           UNO_QUERY_THROW);
111     uno::Reference<beans::XPropertySet> xPagePropSet(xPageStyles->getByName("Default"),
112                                                      UNO_QUERY_THROW);
113 
114     uno::Any aPageBackColor = xPagePropSet->getPropertyValue("BackColor");
115     uno::Any expectedPageBackColor(sal_Int32(13434879));
116 
117     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong page style BackColor", expectedPageBackColor,
118                                  aPageBackColor);
119 }
120 
121 } // namespace apitest
122 
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
124