LibreOffice Module test (master) 1
sheetcell.cxx
Go to the documentation of this file.
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.
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
12
13#include <com/sun/star/awt/Point.hpp>
14#include <com/sun/star/awt/Size.hpp>
15#include <com/sun/star/beans/XPropertySet.hpp>
16#include <com/sun/star/sheet/ValidationType.hpp>
17#include <com/sun/star/sheet/XSheetConditionalEntry.hpp>
18#include <com/sun/star/sheet/XSheetConditionalEntries.hpp>
19#include <com/sun/star/table/CellContentType.hpp>
20#include <com/sun/star/uno/Any.hxx>
21#include <com/sun/star/uno/Reference.hxx>
22#include <com/sun/star/uno/Sequence.hxx>
23
25#include <cppunit/TestAssert.h>
26
27using namespace com::sun::star;
28using namespace com::sun::star::uno;
29
30namespace apitest
31{
33{
34 uno::Reference<beans::XPropertySet> xSheetCell(init(), UNO_QUERY_THROW);
35 OUString propName;
36 uno::Any aNewValue;
37
38 propName = "Position";
39 awt::Point aPositionGet;
40 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue Position",
41 xSheetCell->getPropertyValue(propName) >>= aPositionGet);
42
43 awt::Point aPositionSet(42, 42);
44 aNewValue <<= aPositionSet;
45 xSheetCell->setPropertyValue(propName, aNewValue);
46 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aPositionSet);
47 CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue Position", aPositionGet, aPositionGet);
48
49 propName = "Size";
50 awt::Size aSizeGet;
51 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue Size",
52 xSheetCell->getPropertyValue(propName) >>= aSizeGet);
53
54 awt::Size aSizeSet(42, 42);
55 aNewValue <<= aSizeGet;
56 xSheetCell->setPropertyValue(propName, aNewValue);
57 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aSizeSet);
58 CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue Size", aSizeGet, aSizeSet);
59
60 propName = "FormulaLocal";
61 OUString aFormulaLocal = "";
62 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue FormulaLocal",
63 xSheetCell->getPropertyValue(propName) >>= aFormulaLocal);
64
65 aNewValue <<= OUString("FormulaLocal");
66 xSheetCell->setPropertyValue(propName, aNewValue);
67 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aFormulaLocal);
68 CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue FormulaLocal",
69 OUString("FormulaLocal"), aFormulaLocal);
70
71 propName = "CellContentType";
72 table::CellContentType aCellContentTypeGet;
73 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue CellContentType",
74 xSheetCell->getPropertyValue(propName) >>= aCellContentTypeGet);
75
76 table::CellContentType aCellContentTypeSet = table::CellContentType_TEXT;
77 aNewValue <<= aCellContentTypeSet;
78 xSheetCell->setPropertyValue(propName, aNewValue);
79 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aCellContentTypeSet);
80 CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue CellContentType", aCellContentTypeGet,
81 aCellContentTypeSet);
82
83 // Special case, because the API returns the wrong type.
84 propName = "FormulaResultType";
85 table::CellContentType aFormulaResultTypeGet;
86 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue FormulaResultType",
87 xSheetCell->getPropertyValue(propName) >>= aFormulaResultTypeGet);
88
89 table::CellContentType aFormulaResultTypeSet = table::CellContentType_TEXT;
90 aNewValue <<= aFormulaResultTypeSet;
91 xSheetCell->setPropertyValue(propName, aNewValue);
92 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aFormulaResultTypeSet);
93 CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue FormulaResultType",
94 aFormulaResultTypeGet, aFormulaResultTypeSet);
95
96 propName = "FormulaResultType2";
97 sal_Int32 aFormulaResultType2Get = 42;
98 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue FormulaResultType2",
99 xSheetCell->getPropertyValue(propName) >>= aFormulaResultType2Get);
100
101 sal_Int32 aFormulaResultType2Set = 42;
102 aNewValue <<= aFormulaResultType2Set;
103 xSheetCell->setPropertyValue(propName, aNewValue);
104 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aFormulaResultType2Set);
105 CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue FormulaResultType2",
106 aFormulaResultType2Get, aFormulaResultType2Set);
107
108 uno::Sequence<beans::PropertyValue> aPropValue{ comphelper::makePropertyValue(
109 "StyleName", OUString("Result2")) };
110
111 propName = "ConditionalFormat";
112 uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatGet;
113 uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatSet;
114
115 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue ConditionalFormat",
116 xSheetCell->getPropertyValue(propName) >>= aConditionalFormatGet);
117
118 uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatNew(aConditionalFormatGet,
119 UNO_SET_THROW);
120 aConditionalFormatNew->addNew(aPropValue);
121
122 aNewValue <<= aConditionalFormatNew;
123 xSheetCell->setPropertyValue(propName, aNewValue);
124 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aConditionalFormatSet);
125 for (auto i = 0; i < aConditionalFormatSet->getCount(); i++)
126 {
127 uno::Reference<sheet::XSheetConditionalEntry> xSCENew(aConditionalFormatNew->getByIndex(i),
128 UNO_QUERY_THROW);
129 uno::Reference<sheet::XSheetConditionalEntry> xSCESet(aConditionalFormatSet->getByIndex(i),
130 UNO_QUERY_THROW);
131
132 CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue ConditionalFormat["
133 + std::to_string(i) + "]",
134 xSCENew->getStyleName(), xSCESet->getStyleName());
135 }
136
137 propName = "ConditionalFormatLocal";
138 uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatLocalGet;
139 uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatLocalSet;
140
141 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue ConditionalFormatLocal",
142 xSheetCell->getPropertyValue(propName) >>= aConditionalFormatLocalGet);
143
144 uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatLocalNew(
145 aConditionalFormatLocalGet, UNO_SET_THROW);
146 aConditionalFormatLocalNew->addNew(aPropValue);
147
148 aNewValue <<= aConditionalFormatLocalNew;
149 xSheetCell->setPropertyValue(propName, aNewValue);
150 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aConditionalFormatLocalSet);
151 for (auto i = 0; i < aConditionalFormatLocalSet->getCount(); i++)
152 {
153 uno::Reference<sheet::XSheetConditionalEntry> xSCENew(
154 aConditionalFormatLocalNew->getByIndex(i), UNO_QUERY_THROW);
155 uno::Reference<sheet::XSheetConditionalEntry> xSCESet(
156 aConditionalFormatLocalSet->getByIndex(i), UNO_QUERY_THROW);
157
158 CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue ConditionalFormatLocal["
159 + std::to_string(i) + "]",
160 xSCENew->getStyleName(), xSCESet->getStyleName());
161 }
162
163 propName = "Validation";
164 uno::Reference<beans::XPropertySet> aValidationGet;
165 uno::Reference<beans::XPropertySet> aValidationSet;
166
167 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue Validation",
168 xSheetCell->getPropertyValue(propName) >>= aValidationGet);
169
170 uno::Reference<beans::XPropertySet> aValidationNew(aValidationGet, UNO_SET_THROW);
171 uno::Any aValidationType;
172 aValidationType <<= sheet::ValidationType_WHOLE;
173 aValidationNew->setPropertyValue("Type", aValidationType);
174
175 aNewValue <<= aValidationNew;
176 xSheetCell->setPropertyValue(propName, aNewValue);
177 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aValidationSet);
178 sheet::ValidationType aType;
179 aValidationSet->getPropertyValue("Type") >>= aType;
180 CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue Validation",
181 sheet::ValidationType_WHOLE, aType);
182
183 propName = "ValidationLocal";
184 uno::Reference<beans::XPropertySet> aValidationLocalGet;
185 uno::Reference<beans::XPropertySet> aValidationLocalSet;
186
187 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue ValidationLocal",
188 xSheetCell->getPropertyValue(propName) >>= aValidationLocalGet);
189
190 uno::Reference<beans::XPropertySet> aValidationLocalNew(aValidationLocalGet, UNO_SET_THROW);
191 aValidationType <<= sheet::ValidationType_WHOLE;
192 aValidationLocalNew->setPropertyValue("Type", aValidationType);
193
194 aNewValue <<= aValidationLocalNew;
195 xSheetCell->setPropertyValue(propName, aNewValue);
196 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aValidationLocalSet);
197 aValidationLocalSet->getPropertyValue("Type") >>= aType;
198 CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue ValidationLocal",
199 sheet::ValidationType_WHOLE, aType);
200
201 propName = "AbsoluteName";
202 OUString aAbsoluteNameGet = "";
203 CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue AbsoluteName",
204 xSheetCell->getPropertyValue(propName) >>= aAbsoluteNameGet);
205
206 OUString aAbsoluteNameSet = "$Sheet1.$C$3";
207 aNewValue <<= aAbsoluteNameSet;
208 xSheetCell->setPropertyValue(propName, aNewValue);
209 CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aAbsoluteNameSet);
210 CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue AbsoluteName", aAbsoluteNameGet,
211 aAbsoluteNameSet);
212}
213}
214
215/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
void testSheetCellProperties()
Definition: sheetcell.cxx:32
virtual css::uno::Reference< css::uno::XInterface > init()=0
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
int i