tdf#158198 sw: prevent xBookmark.getAnchor().setString("") from deleting
[LibreOffice.git] / sc / qa / extras / scdatapilotfieldgroupsobj.cxx
blobd221ab9fd3eef3d0f2b3cc8a0e64b86733d4dbcd
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.
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>
11 #include <test/container/xelementaccess.hxx>
12 #include <test/container/xenumerationaccess.hxx>
13 #include <test/container/xindexaccess.hxx>
14 #include <test/container/xnameaccess.hxx>
15 #include <test/container/xnamecontainer.hxx>
16 #include <test/lang/xserviceinfo.hxx>
18 #include <comphelper/types.hxx>
20 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <com/sun/star/container/XIndexAccess.hpp>
22 #include <com/sun/star/container/XNameAccess.hpp>
23 #include <com/sun/star/sheet/DataPilotFieldGroupInfo.hpp>
24 #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
25 #include <com/sun/star/sheet/GeneralFunction.hpp>
26 #include <com/sun/star/sheet/XDataPilotDescriptor.hpp>
27 #include <com/sun/star/sheet/XDataPilotFieldGrouping.hpp>
28 #include <com/sun/star/sheet/XDataPilotTables.hpp>
29 #include <com/sun/star/sheet/XDataPilotTablesSupplier.hpp>
30 #include <com/sun/star/sheet/XSpreadsheet.hpp>
31 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
32 #include <com/sun/star/sheet/XSpreadsheets.hpp>
33 #include <com/sun/star/table/CellAddress.hpp>
34 #include <com/sun/star/table/CellRangeAddress.hpp>
35 #include <com/sun/star/uno/XInterface.hpp>
37 #include <com/sun/star/uno/Reference.hxx>
38 #include <com/sun/star/uno/Sequence.hxx>
40 using namespace css;
42 namespace sc_apitest
44 class ScDataPilotFieldGroupsObj : public UnoApiTest,
45 public apitest::XElementAccess,
46 public apitest::XEnumerationAccess,
47 public apitest::XIndexAccess,
48 public apitest::XNameAccess,
49 public apitest::XNameContainer,
50 public apitest::XServiceInfo
52 public:
53 ScDataPilotFieldGroupsObj();
55 virtual uno::Reference<uno::XInterface> init() override;
56 virtual void setUp() override;
58 CPPUNIT_TEST_SUITE(ScDataPilotFieldGroupsObj);
60 // XElementAccess
61 CPPUNIT_TEST(testGetElementType);
62 CPPUNIT_TEST(testHasElements);
64 // XEnumerationAccess
65 CPPUNIT_TEST(testCreateEnumeration);
67 // XIndexAccess
68 CPPUNIT_TEST(testGetByIndex);
69 CPPUNIT_TEST(testGetCount);
71 // XNameAccess
72 CPPUNIT_TEST(testGetByName);
73 CPPUNIT_TEST(testGetElementNames);
74 CPPUNIT_TEST(testHasByName);
76 // XNameContainer
77 CPPUNIT_TEST(testInsertByName);
78 CPPUNIT_TEST(testInsertByNameEmptyName);
79 CPPUNIT_TEST(testRemoveByName);
80 CPPUNIT_TEST(testRemoveByNameEmptyName);
81 CPPUNIT_TEST(testRemoveByNameNoneExistingElement);
83 // XServiceInfo
84 CPPUNIT_TEST(testGetImplementationName);
85 CPPUNIT_TEST(testGetSupportedServiceNames);
86 CPPUNIT_TEST(testSupportsService);
88 CPPUNIT_TEST_SUITE_END();
90 private:
91 static const int m_nMaxFieldIndex = 6;
94 ScDataPilotFieldGroupsObj::ScDataPilotFieldGroupsObj()
95 : UnoApiTest(u"/sc/qa/extras/testdocuments"_ustr)
96 , XElementAccess(cppu::UnoType<container::XNameAccess>::get())
97 , XIndexAccess(1)
98 , XNameAccess(u"Group1"_ustr)
99 , XNameContainer(u"Group1"_ustr)
100 , XServiceInfo(u"ScDataPilotFieldGroupsObj"_ustr,
101 u"com.sun.star.sheet.DataPilotFieldGroups"_ustr)
105 uno::Reference<uno::XInterface> ScDataPilotFieldGroupsObj::init()
107 table::CellRangeAddress aCellRangeAddress(0, 1, 0, m_nMaxFieldIndex - 1, m_nMaxFieldIndex - 1);
108 table::CellAddress aCellAddress(0, 7, 8);
110 uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, uno::UNO_QUERY_THROW);
111 uno::Reference<sheet::XSpreadsheets> xSheets(xDoc->getSheets(), uno::UNO_SET_THROW);
112 uno::Reference<container::XIndexAccess> xIA(xSheets, uno::UNO_QUERY_THROW);
113 xSheets->insertNewByName(u"Some Sheet"_ustr, 0);
115 uno::Reference<sheet::XSpreadsheet> xSheet0(xIA->getByIndex(0), uno::UNO_QUERY_THROW);
116 uno::Reference<sheet::XSpreadsheet> xSheet1(xIA->getByIndex(1), uno::UNO_QUERY_THROW);
118 for (auto i = 1; i < m_nMaxFieldIndex; ++i)
120 xSheet0->getCellByPosition(i, 0)->setFormula("Col" + OUString::number(i));
121 xSheet0->getCellByPosition(0, i)->setFormula("Row" + OUString::number(i));
122 xSheet1->getCellByPosition(i, 0)->setFormula("Col" + OUString::number(i));
123 xSheet1->getCellByPosition(0, i)->setFormula("Row" + OUString::number(i));
126 for (auto i = 1; i < m_nMaxFieldIndex; ++i)
128 for (auto j = 1; j < m_nMaxFieldIndex; ++j)
130 xSheet0->getCellByPosition(i, j)->setValue(i * (j + 1));
131 xSheet1->getCellByPosition(i, j)->setValue(i * (j + 2));
135 xSheet0->getCellByPosition(1, 1)->setFormula(u"aName"_ustr);
136 xSheet0->getCellByPosition(1, 2)->setFormula(u"otherName"_ustr);
137 xSheet0->getCellByPosition(1, 3)->setFormula(u"una"_ustr);
138 xSheet0->getCellByPosition(1, 4)->setFormula(u"otherName"_ustr);
139 xSheet0->getCellByPosition(1, 5)->setFormula(u"somethingelse"_ustr);
141 xSheet0->getCellByPosition(1, 5);
142 xSheet0->getCellByPosition(aCellAddress.Column, aCellAddress.Row + 3);
144 uno::Reference<sheet::XDataPilotTablesSupplier> xDPTS(xSheet0, uno::UNO_QUERY_THROW);
145 uno::Reference<sheet::XDataPilotTables> xDPT(xDPTS->getDataPilotTables(), uno::UNO_SET_THROW);
146 uno::Reference<sheet::XDataPilotDescriptor> xDPD(xDPT->createDataPilotDescriptor(),
147 uno::UNO_SET_THROW);
149 xDPD->setSourceRange(aCellRangeAddress);
151 uno::Reference<beans::XPropertySet> xPropertySet0(xDPD->getDataPilotFields()->getByIndex(0),
152 uno::UNO_QUERY_THROW);
153 xPropertySet0->setPropertyValue(u"Orientation"_ustr,
154 uno::Any(sheet::DataPilotFieldOrientation_ROW));
156 uno::Reference<beans::XPropertySet> xPropertySet1(xDPD->getDataPilotFields()->getByIndex(1),
157 uno::UNO_QUERY_THROW);
158 xPropertySet1->setPropertyValue(u"Function"_ustr, uno::Any(sheet::GeneralFunction_SUM));
159 xPropertySet1->setPropertyValue(u"Orientation"_ustr,
160 uno::Any(sheet::DataPilotFieldOrientation_DATA));
162 uno::Reference<beans::XPropertySet> xPropertySet2(xDPD->getDataPilotFields()->getByIndex(2),
163 uno::UNO_QUERY_THROW);
164 xPropertySet2->setPropertyValue(u"Orientation"_ustr,
165 uno::Any(sheet::DataPilotFieldOrientation_COLUMN));
167 xDPT->insertNewByName(u"DataPilotTable"_ustr, aCellAddress, xDPD);
169 uno::Reference<container::XIndexAccess> xIA_DPT0(xDPTS->getDataPilotTables(),
170 uno::UNO_QUERY_THROW);
171 uno::Reference<sheet::XDataPilotDescriptor> xDPD0(xIA_DPT0->getByIndex(0),
172 uno::UNO_QUERY_THROW);
173 uno::Reference<container::XIndexAccess> xIA_RF0(xDPD0->getRowFields(), uno::UNO_SET_THROW);
175 uno::Reference<sheet::XDataPilotFieldGrouping> xDPFG(xIA_RF0->getByIndex(0),
176 uno::UNO_QUERY_THROW);
177 xDPFG->createNameGroup({ u"aName"_ustr, u"otherName"_ustr });
179 uno::Reference<container::XIndexAccess> xIA_DPT1(xDPTS->getDataPilotTables(),
180 uno::UNO_QUERY_THROW);
181 uno::Reference<sheet::XDataPilotDescriptor> xDPD1(xIA_DPT1->getByIndex(0),
182 uno::UNO_QUERY_THROW);
183 uno::Reference<container::XIndexAccess> xIA_RF1(xDPD1->getRowFields(), uno::UNO_SET_THROW);
185 sheet::DataPilotFieldGroupInfo aDPFGI;
186 for (auto i = 0; i < xIA_RF1->getCount(); ++i)
188 uno::Reference<beans::XPropertySet> xPropertySet(xIA_RF1->getByIndex(i),
189 uno::UNO_QUERY_THROW);
190 if (::comphelper::getBOOL(xPropertySet->getPropertyValue(u"IsGroupField"_ustr)))
192 CPPUNIT_ASSERT(xPropertySet->getPropertyValue(u"GroupInfo"_ustr) >>= aDPFGI);
196 // set element for testing XNameContainer::insertByName()
197 uno::Any aElement;
198 setElement(aElement);
200 return aDPFGI.Groups;
203 void ScDataPilotFieldGroupsObj::setUp()
205 UnoApiTest::setUp();
206 // create a calc document
207 loadFromURL(u"private:factory/scalc"_ustr);
210 CPPUNIT_TEST_SUITE_REGISTRATION(ScDataPilotFieldGroupsObj);
212 } // namespace sc_apitest
214 CPPUNIT_PLUGIN_IMPLEMENT();
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */