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/xenumeration.hxx>
12 
13 #include <com/sun/star/beans/XPropertySet.hpp>
14 #include <com/sun/star/container/XIndexAccess.hpp>
15 #include <com/sun/star/lang/XComponent.hpp>
16 #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
17 #include <com/sun/star/sheet/GeneralFunction.hpp>
18 #include <com/sun/star/sheet/XDataPilotDescriptor.hpp>
19 #include <com/sun/star/sheet/XDataPilotField.hpp>
20 #include <com/sun/star/sheet/XDataPilotTables.hpp>
21 #include <com/sun/star/sheet/XDataPilotTablesSupplier.hpp>
22 #include <com/sun/star/sheet/XSpreadsheet.hpp>
23 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
24 #include <com/sun/star/sheet/XSpreadsheets.hpp>
25 #include <com/sun/star/table/CellAddress.hpp>
26 #include <com/sun/star/table/CellRangeAddress.hpp>
27 #include <com/sun/star/uno/XInterface.hpp>
28 
29 #include <com/sun/star/uno/Reference.hxx>
30 
31 using namespace css;
32 using namespace css::uno;
33 using namespace com::sun::star;
34 
35 namespace sc_apitest
36 {
37 class ScIndexEnumeration_DataPilotItemsEnumeration : public CalcUnoApiTest,
38                                                      public apitest::XEnumeration
39 {
40 public:
41     ScIndexEnumeration_DataPilotItemsEnumeration();
42 
43     virtual uno::Reference<uno::XInterface> init() override;
44     virtual void setUp() override;
45     virtual void tearDown() override;
46 
47     CPPUNIT_TEST_SUITE(ScIndexEnumeration_DataPilotItemsEnumeration);
48 
49     // XEnumeration
50     CPPUNIT_TEST(testHasMoreElements);
51     CPPUNIT_TEST(testNextElement);
52 
53     CPPUNIT_TEST_SUITE_END();
54 
55 private:
56     static const int m_nMaxFieldIndex = 6;
57     uno::Reference<lang::XComponent> m_xComponent;
58 };
59 
ScIndexEnumeration_DataPilotItemsEnumeration()60 ScIndexEnumeration_DataPilotItemsEnumeration::ScIndexEnumeration_DataPilotItemsEnumeration()
61     : CalcUnoApiTest("/sc/qa/extras/testdocuments")
62 {
63 }
64 
init()65 uno::Reference<uno::XInterface> ScIndexEnumeration_DataPilotItemsEnumeration::init()
66 {
67     table::CellRangeAddress aCellRangeAddress(0, 1, 0, m_nMaxFieldIndex - 1, m_nMaxFieldIndex - 1);
68     table::CellAddress aCellAddress(0, 7, 8);
69 
70     uno::Reference<sheet::XSpreadsheetDocument> xDoc(m_xComponent, uno::UNO_QUERY_THROW);
71     CPPUNIT_ASSERT_MESSAGE("no document", xDoc.is());
72     uno::Reference<sheet::XSpreadsheets> xSheets(xDoc->getSheets(), uno::UNO_SET_THROW);
73 
74     uno::Reference<container::XIndexAccess> xIA(xSheets, uno::UNO_QUERY_THROW);
75     xSheets->insertNewByName("Some Sheet", 0);
76 
77     uno::Reference<sheet::XSpreadsheet> xSheet0(xIA->getByIndex(0), uno::UNO_QUERY_THROW);
78     uno::Reference<sheet::XSpreadsheet> xSheet1(xIA->getByIndex(1), uno::UNO_QUERY_THROW);
79 
80     for (auto i = 1; i < m_nMaxFieldIndex; ++i)
81     {
82         xSheet0->getCellByPosition(i, 0)->setFormula("Col" + OUString::number(i));
83         xSheet0->getCellByPosition(0, i)->setFormula("Row" + OUString::number(i));
84         xSheet1->getCellByPosition(i, 0)->setFormula("Col" + OUString::number(i));
85         xSheet1->getCellByPosition(0, i)->setFormula("Row" + OUString::number(i));
86     }
87 
88     for (auto i = 1; i < m_nMaxFieldIndex; ++i)
89     {
90         for (auto j = 1; j < m_nMaxFieldIndex; ++j)
91         {
92             xSheet0->getCellByPosition(i, j)->setValue(i * (j + 1));
93             xSheet1->getCellByPosition(i, j)->setValue(i * (j + 2));
94         }
95     }
96 
97     xSheet0->getCellByPosition(1, 5);
98     xSheet0->getCellByPosition(aCellAddress.Column, aCellAddress.Row + 3);
99 
100     uno::Reference<sheet::XDataPilotTablesSupplier> xDPTS(xSheet0, uno::UNO_QUERY_THROW);
101     uno::Reference<sheet::XDataPilotTables> xDPT(xDPTS->getDataPilotTables(), uno::UNO_SET_THROW);
102     uno::Reference<sheet::XDataPilotDescriptor> xDPD(xDPT->createDataPilotDescriptor(),
103                                                      uno::UNO_SET_THROW);
104 
105     xDPD->setSourceRange(aCellRangeAddress);
106 
107     uno::Any aValue;
108     uno::Reference<beans::XPropertySet> xPropertySet(xDPD->getDataPilotFields()->getByIndex(0),
109                                                      uno::UNO_QUERY_THROW);
110     aValue <<= sheet::DataPilotFieldOrientation_DATA;
111     xPropertySet->setPropertyValue("Orientation", aValue);
112     aValue <<= sheet::GeneralFunction_SUM;
113     xPropertySet->setPropertyValue("Function", aValue);
114 
115     xDPT->insertNewByName("DataPilotTable", aCellAddress, xDPD);
116 
117     uno::Reference<sheet::XDataPilotField> xDPF(xDPD->getDataPilotFields()->getByIndex(0),
118                                                 uno::UNO_QUERY_THROW);
119     uno::Reference<container::XEnumerationAccess> xEA(xDPF->getItems(), uno::UNO_QUERY_THROW);
120     return xEA->createEnumeration();
121 }
122 
setUp()123 void ScIndexEnumeration_DataPilotItemsEnumeration::setUp()
124 {
125     CalcUnoApiTest::setUp();
126     // create a calc document
127     m_xComponent = loadFromDesktop("private:factory/scalc");
128     CPPUNIT_ASSERT_MESSAGE("no component", m_xComponent.is());
129 }
130 
tearDown()131 void ScIndexEnumeration_DataPilotItemsEnumeration::tearDown()
132 {
133     closeDocument(m_xComponent);
134     CalcUnoApiTest::tearDown();
135 }
136 
137 CPPUNIT_TEST_SUITE_REGISTRATION(ScIndexEnumeration_DataPilotItemsEnumeration);
138 } // namespace sc_apitest
139 
140 CPPUNIT_PLUGIN_IMPLEMENT();
141 
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
143