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/calc_unoapi_test.hxx>
11 #include <test/container/xnamed.hxx>
12 #include <test/lang/xserviceinfo.hxx>
13 
14 #include <com/sun/star/beans/XPropertySet.hpp>
15 #include <com/sun/star/container/XIndexAccess.hpp>
16 #include <com/sun/star/container/XNameAccess.hpp>
17 #include <com/sun/star/lang/XComponent.hpp>
18 #include <com/sun/star/sheet/DataPilotFieldGroupInfo.hpp>
19 #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
20 #include <com/sun/star/sheet/GeneralFunction.hpp>
21 #include <com/sun/star/sheet/XDataPilotDescriptor.hpp>
22 #include <com/sun/star/sheet/XDataPilotFieldGrouping.hpp>
23 #include <com/sun/star/sheet/XDataPilotTables.hpp>
24 #include <com/sun/star/sheet/XDataPilotTablesSupplier.hpp>
25 #include <com/sun/star/sheet/XSpreadsheet.hpp>
26 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
27 #include <com/sun/star/sheet/XSpreadsheets.hpp>
28 #include <com/sun/star/table/CellAddress.hpp>
29 #include <com/sun/star/table/CellRangeAddress.hpp>
30 #include <com/sun/star/uno/XInterface.hpp>
31 
32 #include <com/sun/star/uno/Reference.hxx>
33 #include <com/sun/star/uno/Sequence.hxx>
34 
35 #include <comphelper/types.hxx>
36 
37 using namespace css;
38 
39 namespace sc_apitest
40 {
41 class ScDataPilotFieldGroupItemObj : public CalcUnoApiTest,
42                                      public apitest::XNamed,
43                                      public apitest::XServiceInfo
44 {
45 public:
46     ScDataPilotFieldGroupItemObj();
47 
48     virtual uno::Reference<uno::XInterface> init() override;
49     virtual void setUp() override;
50     virtual void tearDown() override;
51 
52     CPPUNIT_TEST_SUITE(ScDataPilotFieldGroupItemObj);
53 
54     // XNamed
55     CPPUNIT_TEST(testGetName);
56     CPPUNIT_TEST(testSetName);
57 
58     // XServiceInfo
59     CPPUNIT_TEST(testGetImplementationName);
60     CPPUNIT_TEST(testGetSupportedServiceNames);
61     CPPUNIT_TEST(testSupportsService);
62 
63     CPPUNIT_TEST_SUITE_END();
64 
65 private:
66     static const int m_nMaxFieldIndex = 6;
67     uno::Reference<lang::XComponent> m_xComponent;
68 };
69 
ScDataPilotFieldGroupItemObj()70 ScDataPilotFieldGroupItemObj::ScDataPilotFieldGroupItemObj()
71     : CalcUnoApiTest("/sc/qa/extras/testdocuments")
72     , XNamed("aName")
73     , XServiceInfo("ScDataPilotFieldGroupItemObj", "com.sun.star.sheet.DataPilotFieldGroupItem")
74 {
75 }
76 
init()77 uno::Reference<uno::XInterface> ScDataPilotFieldGroupItemObj::init()
78 {
79     table::CellRangeAddress aCellRangeAddress(0, 1, 0, m_nMaxFieldIndex - 1, m_nMaxFieldIndex - 1);
80     table::CellAddress aCellAddress(0, 7, 8);
81 
82     uno::Reference<sheet::XSpreadsheetDocument> xDoc(m_xComponent, uno::UNO_QUERY_THROW);
83     uno::Reference<sheet::XSpreadsheets> xSheets(xDoc->getSheets(), uno::UNO_SET_THROW);
84     uno::Reference<container::XIndexAccess> xIA(xSheets, uno::UNO_QUERY_THROW);
85     xSheets->insertNewByName("Some Sheet", 0);
86 
87     uno::Reference<sheet::XSpreadsheet> xSheet0(xIA->getByIndex(0), uno::UNO_QUERY_THROW);
88     uno::Reference<sheet::XSpreadsheet> xSheet1(xIA->getByIndex(1), uno::UNO_QUERY_THROW);
89 
90     for (auto i = 1; i < m_nMaxFieldIndex; ++i)
91     {
92         xSheet0->getCellByPosition(i, 0)->setFormula("Col" + OUString::number(i));
93         xSheet0->getCellByPosition(0, i)->setFormula("Row" + OUString::number(i));
94         xSheet1->getCellByPosition(i, 0)->setFormula("Col" + OUString::number(i));
95         xSheet1->getCellByPosition(0, i)->setFormula("Row" + OUString::number(i));
96     }
97 
98     for (auto i = 1; i < m_nMaxFieldIndex; ++i)
99     {
100         for (auto j = 1; j < m_nMaxFieldIndex; ++j)
101         {
102             xSheet0->getCellByPosition(i, j)->setValue(i * (j + 1));
103             xSheet1->getCellByPosition(i, j)->setValue(i * (j + 2));
104         }
105     }
106 
107     xSheet0->getCellByPosition(1, 1)->setFormula("aName");
108     xSheet0->getCellByPosition(1, 2)->setFormula("otherName");
109     xSheet0->getCellByPosition(1, 3)->setFormula("una");
110     xSheet0->getCellByPosition(1, 4)->setFormula("otherName");
111     xSheet0->getCellByPosition(1, 5)->setFormula("somethingelse");
112 
113     xSheet0->getCellByPosition(1, 5);
114     xSheet0->getCellByPosition(aCellAddress.Column, aCellAddress.Row + 3);
115 
116     uno::Reference<sheet::XDataPilotTablesSupplier> xDPTS(xSheet0, uno::UNO_QUERY_THROW);
117     uno::Reference<sheet::XDataPilotTables> xDPT(xDPTS->getDataPilotTables(), uno::UNO_SET_THROW);
118     uno::Reference<sheet::XDataPilotDescriptor> xDPD(xDPT->createDataPilotDescriptor(),
119                                                      uno::UNO_SET_THROW);
120 
121     xDPD->setSourceRange(aCellRangeAddress);
122 
123     uno::Reference<beans::XPropertySet> xPropertySet0(xDPD->getDataPilotFields()->getByIndex(0),
124                                                       uno::UNO_QUERY_THROW);
125     xPropertySet0->setPropertyValue("Orientation",
126                                     uno::makeAny(sheet::DataPilotFieldOrientation_ROW));
127 
128     uno::Reference<beans::XPropertySet> xPropertySet1(xDPD->getDataPilotFields()->getByIndex(1),
129                                                       uno::UNO_QUERY_THROW);
130     xPropertySet1->setPropertyValue("Function", uno::makeAny(sheet::GeneralFunction_SUM));
131     xPropertySet1->setPropertyValue("Orientation",
132                                     uno::makeAny(sheet::DataPilotFieldOrientation_DATA));
133 
134     uno::Reference<beans::XPropertySet> xPropertySet2(xDPD->getDataPilotFields()->getByIndex(2),
135                                                       uno::UNO_QUERY_THROW);
136     xPropertySet2->setPropertyValue("Orientation",
137                                     uno::makeAny(sheet::DataPilotFieldOrientation_COLUMN));
138 
139     xDPT->insertNewByName("DataPilotTable", aCellAddress, xDPD);
140 
141     uno::Reference<container::XIndexAccess> xIA_DPT0(xDPTS->getDataPilotTables(),
142                                                      uno::UNO_QUERY_THROW);
143     uno::Reference<sheet::XDataPilotDescriptor> xDPD0(xIA_DPT0->getByIndex(0),
144                                                       uno::UNO_QUERY_THROW);
145     uno::Reference<container::XIndexAccess> xIA_RF0(xDPD0->getRowFields(), uno::UNO_SET_THROW);
146 
147     uno::Reference<sheet::XDataPilotFieldGrouping> xDPFG(xIA_RF0->getByIndex(0),
148                                                          uno::UNO_QUERY_THROW);
149     uno::Sequence<OUString> aElements(2);
150     aElements[0] = "aName";
151     aElements[1] = "otherName";
152     xDPFG->createNameGroup(aElements);
153 
154     uno::Reference<container::XNameAccess> xNA_GroupNames;
155 
156     uno::Reference<container::XIndexAccess> xIA_DPT1(xDPTS->getDataPilotTables(),
157                                                      uno::UNO_QUERY_THROW);
158     uno::Reference<sheet::XDataPilotDescriptor> xDPD1(xIA_DPT1->getByIndex(0),
159                                                       uno::UNO_QUERY_THROW);
160     uno::Reference<container::XIndexAccess> xIA_RF1(xDPD1->getRowFields(), uno::UNO_SET_THROW);
161 
162     sheet::DataPilotFieldGroupInfo aDPFGI;
163     for (auto i = 0; i < xIA_RF1->getCount(); ++i)
164     {
165         uno::Reference<beans::XPropertySet> xPropertySet(xIA_RF1->getByIndex(i),
166                                                          uno::UNO_QUERY_THROW);
167         if (::comphelper::getBOOL(xPropertySet->getPropertyValue("IsGroupField")))
168         {
169             CPPUNIT_ASSERT(xPropertySet->getPropertyValue("GroupInfo") >>= aDPFGI);
170         }
171     }
172 
173     uno::Reference<container::XIndexAccess> xIA_GI(aDPFGI.Groups, uno::UNO_QUERY_THROW);
174     uno::Reference<container::XNameAccess> xNA_GN(xIA_GI->getByIndex(0), uno::UNO_QUERY_THROW);
175 
176     uno::Reference<uno::XInterface> xReturn(xNA_GN->getByName("aName"), uno::UNO_QUERY_THROW);
177     return xReturn;
178 }
179 
setUp()180 void ScDataPilotFieldGroupItemObj::setUp()
181 {
182     CalcUnoApiTest::setUp();
183     // create a calc document
184     m_xComponent = loadFromDesktop("private:factory/scalc");
185 }
186 
tearDown()187 void ScDataPilotFieldGroupItemObj::tearDown()
188 {
189     closeDocument(m_xComponent);
190     CalcUnoApiTest::tearDown();
191 }
192 
193 CPPUNIT_TEST_SUITE_REGISTRATION(ScDataPilotFieldGroupItemObj);
194 
195 } // namespace sc_apitest
196 
197 CPPUNIT_PLUGIN_IMPLEMENT();
198 
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
200