summaryrefslogtreecommitdiff
path: root/test/source/sheet/sheetcell.cxx
blob: ef2f2b2aca9004398fabfb055f75f7ded8a88db3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <test/cppunitasserthelper.hxx>
#include <test/sheet/sheetcell.hxx>

#include <com/sun/star/awt/Point.hpp>
#include <com/sun/star/awt/Size.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/sheet/ValidationType.hpp>
#include <com/sun/star/sheet/XSheetConditionalEntry.hpp>
#include <com/sun/star/sheet/XSheetConditionalEntries.hpp>
#include <com/sun/star/table/CellContentType.hpp>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/uno/Sequence.hxx>

#include <comphelper/propertyvalue.hxx>
#include <cppunit/TestAssert.h>

using namespace com::sun::star;
using namespace com::sun::star::uno;

namespace apitest
{
void SheetCell::testSheetCellProperties()
{
    uno::Reference<beans::XPropertySet> xSheetCell(init(), UNO_QUERY_THROW);
    OUString propName;
    uno::Any aNewValue;

    propName = "Position";
    awt::Point aPositionGet;
    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue Position",
                           xSheetCell->getPropertyValue(propName) >>= aPositionGet);

    awt::Point aPositionSet(42, 42);
    aNewValue <<= aPositionSet;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aPositionSet);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue Position", aPositionGet, aPositionGet);

    propName = "Size";
    awt::Size aSizeGet;
    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue Size",
                           xSheetCell->getPropertyValue(propName) >>= aSizeGet);

    awt::Size aSizeSet(42, 42);
    aNewValue <<= aSizeGet;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aSizeSet);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue Size", aSizeGet, aSizeSet);

    propName = "FormulaLocal";
    OUString aFormulaLocal = u""_ustr;
    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue FormulaLocal",
                           xSheetCell->getPropertyValue(propName) >>= aFormulaLocal);

    aNewValue <<= u"FormulaLocal"_ustr;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aFormulaLocal);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue FormulaLocal", u"FormulaLocal"_ustr,
                                 aFormulaLocal);

    propName = "CellContentType";
    table::CellContentType aCellContentTypeGet;
    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue CellContentType",
                           xSheetCell->getPropertyValue(propName) >>= aCellContentTypeGet);

    table::CellContentType aCellContentTypeSet = table::CellContentType_TEXT;
    aNewValue <<= aCellContentTypeSet;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aCellContentTypeSet);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue CellContentType", aCellContentTypeGet,
                                 aCellContentTypeSet);

    // Special case, because the API returns the wrong type.
    propName = "FormulaResultType";
    table::CellContentType aFormulaResultTypeGet;
    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue FormulaResultType",
                           xSheetCell->getPropertyValue(propName) >>= aFormulaResultTypeGet);

    table::CellContentType aFormulaResultTypeSet = table::CellContentType_TEXT;
    aNewValue <<= aFormulaResultTypeSet;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aFormulaResultTypeSet);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue FormulaResultType",
                                 aFormulaResultTypeGet, aFormulaResultTypeSet);

    propName = "FormulaResultType2";
    sal_Int32 aFormulaResultType2Get = 42;
    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue FormulaResultType2",
                           xSheetCell->getPropertyValue(propName) >>= aFormulaResultType2Get);

    sal_Int32 aFormulaResultType2Set = 42;
    aNewValue <<= aFormulaResultType2Set;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aFormulaResultType2Set);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue FormulaResultType2",
                                 aFormulaResultType2Get, aFormulaResultType2Set);

    uno::Sequence<beans::PropertyValue> aPropValue{ comphelper::makePropertyValue(
        u"StyleName"_ustr, u"Result2"_ustr) };

    propName = "ConditionalFormat";
    uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatGet;
    uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatSet;

    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue ConditionalFormat",
                           xSheetCell->getPropertyValue(propName) >>= aConditionalFormatGet);

    uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatNew(aConditionalFormatGet,
                                                                          UNO_SET_THROW);
    aConditionalFormatNew->addNew(aPropValue);

    aNewValue <<= aConditionalFormatNew;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aConditionalFormatSet);
    for (auto i = 0; i < aConditionalFormatSet->getCount(); i++)
    {
        uno::Reference<sheet::XSheetConditionalEntry> xSCENew(aConditionalFormatNew->getByIndex(i),
                                                              UNO_QUERY_THROW);
        uno::Reference<sheet::XSheetConditionalEntry> xSCESet(aConditionalFormatSet->getByIndex(i),
                                                              UNO_QUERY_THROW);

        CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue ConditionalFormat["
                                         + std::to_string(i) + "]",
                                     xSCENew->getStyleName(), xSCESet->getStyleName());
    }

    propName = "ConditionalFormatLocal";
    uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatLocalGet;
    uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatLocalSet;

    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue ConditionalFormatLocal",
                           xSheetCell->getPropertyValue(propName) >>= aConditionalFormatLocalGet);

    uno::Reference<sheet::XSheetConditionalEntries> aConditionalFormatLocalNew(
        aConditionalFormatLocalGet, UNO_SET_THROW);
    aConditionalFormatLocalNew->addNew(aPropValue);

    aNewValue <<= aConditionalFormatLocalNew;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aConditionalFormatLocalSet);
    for (auto i = 0; i < aConditionalFormatLocalSet->getCount(); i++)
    {
        uno::Reference<sheet::XSheetConditionalEntry> xSCENew(
            aConditionalFormatLocalNew->getByIndex(i), UNO_QUERY_THROW);
        uno::Reference<sheet::XSheetConditionalEntry> xSCESet(
            aConditionalFormatLocalSet->getByIndex(i), UNO_QUERY_THROW);

        CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue ConditionalFormatLocal["
                                         + std::to_string(i) + "]",
                                     xSCENew->getStyleName(), xSCESet->getStyleName());
    }

    propName = "Validation";
    uno::Reference<beans::XPropertySet> aValidationGet;
    uno::Reference<beans::XPropertySet> aValidationSet;

    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue Validation",
                           xSheetCell->getPropertyValue(propName) >>= aValidationGet);

    uno::Reference<beans::XPropertySet> aValidationNew(aValidationGet, UNO_SET_THROW);
    uno::Any aValidationType;
    aValidationType <<= sheet::ValidationType_WHOLE;
    aValidationNew->setPropertyValue(u"Type"_ustr, aValidationType);

    aNewValue <<= aValidationNew;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aValidationSet);
    sheet::ValidationType aType;
    aValidationSet->getPropertyValue(u"Type"_ustr) >>= aType;
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue Validation",
                                 sheet::ValidationType_WHOLE, aType);

    propName = "ValidationLocal";
    uno::Reference<beans::XPropertySet> aValidationLocalGet;
    uno::Reference<beans::XPropertySet> aValidationLocalSet;

    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue ValidationLocal",
                           xSheetCell->getPropertyValue(propName) >>= aValidationLocalGet);

    uno::Reference<beans::XPropertySet> aValidationLocalNew(aValidationLocalGet, UNO_SET_THROW);
    aValidationType <<= sheet::ValidationType_WHOLE;
    aValidationLocalNew->setPropertyValue(u"Type"_ustr, aValidationType);

    aNewValue <<= aValidationLocalNew;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aValidationLocalSet);
    aValidationLocalSet->getPropertyValue(u"Type"_ustr) >>= aType;
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue ValidationLocal",
                                 sheet::ValidationType_WHOLE, aType);

    propName = "AbsoluteName";
    OUString aAbsoluteNameGet = u""_ustr;
    CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue AbsoluteName",
                           xSheetCell->getPropertyValue(propName) >>= aAbsoluteNameGet);

    OUString aAbsoluteNameSet = u"$Sheet1.$C$3"_ustr;
    aNewValue <<= aAbsoluteNameSet;
    xSheetCell->setPropertyValue(propName, aNewValue);
    CPPUNIT_ASSERT(xSheetCell->getPropertyValue(propName) >>= aAbsoluteNameSet);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Able to set PropertyValue AbsoluteName", aAbsoluteNameGet,
                                 aAbsoluteNameSet);
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */