1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <test/style/xstyleloader.hxx>
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>
19 #include <com/sun/star/uno/Any.hxx>
20 #include <com/sun/star/uno/Reference.hxx>
21 #include <com/sun/star/uno/Sequence.hxx>
23 #include <rtl/ustring.hxx>
24 #include <cppunit/TestAssert.h>
25 #include <comphelper/processfactory.hxx>
26 #include <comphelper/storagehelper.hxx>
28 using namespace comphelper
;
30 using namespace css::uno
;
34 void XStyleLoader::testLoadStylesFromURL()
36 uno::Reference
<style::XStyleLoader2
> xStyleLoader(init(), uno::UNO_QUERY_THROW
);
38 uno::Reference
<sheet::XSpreadsheetDocument
> xDoc(getTargetDoc(), uno::UNO_SET_THROW
);
39 const OUString aFileURL
= getTestURL();
41 uno::Sequence
<beans::PropertyValue
> aOptions
= xStyleLoader
->getStyleLoaderOptions();
42 xStyleLoader
->loadStylesFromURL(aFileURL
, aOptions
);
44 uno::Reference
<style::XStyleFamiliesSupplier
> xFamilySupplier(xDoc
, UNO_QUERY_THROW
);
45 checkStyleProperties(xFamilySupplier
);
48 void XStyleLoader::testLoadStylesFromDocument()
50 uno::Reference
<style::XStyleLoader2
> xStyleLoader(init(), uno::UNO_QUERY_THROW
);
52 uno::Reference
<sheet::XSpreadsheetDocument
> xDoc(getTargetDoc(), uno::UNO_SET_THROW
);
53 uno::Reference
<lang::XComponent
> xSrcComponent(getSourceComponent(), UNO_SET_THROW
);
55 uno::Sequence
<beans::PropertyValue
> aOptions
= xStyleLoader
->getStyleLoaderOptions();
56 xStyleLoader
->loadStylesFromDocument(xSrcComponent
, aOptions
);
58 uno::Reference
<style::XStyleFamiliesSupplier
> xFamilySupplier(xDoc
, UNO_QUERY_THROW
);
59 checkStyleProperties(xFamilySupplier
);
62 void XStyleLoader::testLoadStylesFromStream()
64 uno::Reference
<style::XStyleLoader2
> xStyleLoader(init(), uno::UNO_QUERY_THROW
);
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());
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 aInputStream
.Value
<<= xInputStream
;
77 aOptions
.getArray()[nLength
] = std::move(aInputStream
);
79 xStyleLoader
->loadStylesFromURL(u
"private:stream"_ustr
, aOptions
);
81 uno::Reference
<style::XStyleFamiliesSupplier
> xFamilySupplier(xDoc
, UNO_QUERY_THROW
);
82 checkStyleProperties(xFamilySupplier
);
85 void XStyleLoader::checkStyleProperties(
86 uno::Reference
<style::XStyleFamiliesSupplier
> const& xFamilySupplier
)
88 // check if targetDocument has myStyle
89 uno::Reference
<container::XNameAccess
> xFamilies(xFamilySupplier
->getStyleFamilies(),
91 uno::Reference
<container::XNameContainer
> xCellStyles(xFamilies
->getByName(u
"CellStyles"_ustr
),
94 CPPUNIT_ASSERT_MESSAGE("Style not imported", xCellStyles
->hasByName(u
"myStyle"_ustr
));
96 // test the backgroundcolor is correctly imported
97 uno::Reference
<style::XStyle
> xMyStyle(xCellStyles
->getByName(u
"myStyle"_ustr
),
99 uno::Reference
<beans::XPropertySet
> xPropSet(xMyStyle
, UNO_QUERY_THROW
);
101 uno::Any aBackColor
= xPropSet
->getPropertyValue(u
"CellBackColor"_ustr
);
102 uno::Any
expectedBackColor(sal_Int32(16724787));
104 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong CellBackColor", expectedBackColor
, aBackColor
);
106 // test default pageStyle
108 uno::Reference
<container::XNameContainer
> xPageStyles(xFamilies
->getByName(u
"PageStyles"_ustr
),
110 uno::Reference
<beans::XPropertySet
> xPagePropSet(xPageStyles
->getByName(u
"Default"_ustr
),
113 uno::Any aPageBackColor
= xPagePropSet
->getPropertyValue(u
"BackColor"_ustr
);
114 uno::Any
expectedPageBackColor(sal_Int32(13434879));
116 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong page style BackColor", expectedPageBackColor
,
120 } // namespace apitest
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */