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 
10 #include <test/calc_unoapi_test.hxx>
11 #include <test/container/xnamed.hxx>
12 #include <test/sheet/datapilotitem.hxx>
13 
14 #include <com/sun/star/beans/XPropertySet.hpp>
15 #include <com/sun/star/container/XIndexAccess.hpp>
16 #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
17 #include <com/sun/star/sheet/GeneralFunction.hpp>
18 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
19 #include <com/sun/star/sheet/XSpreadsheet.hpp>
20 #include <com/sun/star/sheet/XDataPilotDescriptor.hpp>
21 #include <com/sun/star/sheet/XDataPilotField.hpp>
22 #include <com/sun/star/sheet/XDataPilotTablesSupplier.hpp>
23 #include <com/sun/star/sheet/XDataPilotTables.hpp>
24 #include <com/sun/star/table/CellAddress.hpp>
25 #include <com/sun/star/table/CellRangeAddress.hpp>
26 #include <com/sun/star/uno/XInterface.hpp>
27 
28 #include <com/sun/star/uno/Reference.hxx>
29 
30 using namespace css;
31 
32 namespace sc_apitest
33 {
34 class ScDataPilotItemObj : public CalcUnoApiTest,
35                            public apitest::DataPilotItem,
36                            public apitest::XNamed
37 {
38 public:
39     virtual void setUp() override;
40     virtual void tearDown() override;
41     virtual uno::Reference<uno::XInterface> init() override;
42 
43     ScDataPilotItemObj();
44 
45     CPPUNIT_TEST_SUITE(ScDataPilotItemObj);
46 
47     // DataPilotItem
48     CPPUNIT_TEST(testProperties);
49 
50     // XNamed
51     CPPUNIT_TEST(testGetName);
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 
ScDataPilotItemObj()60 ScDataPilotItemObj::ScDataPilotItemObj()
61     : CalcUnoApiTest("/sc/qa/extras/testdocuments")
62     , XNamed("2")
63 {
64 }
65 
init()66 uno::Reference<uno::XInterface> ScDataPilotItemObj::init()
67 {
68     table::CellRangeAddress aCellRangeAddress(0, 1, 0, m_nMaxFieldIndex - 1, m_nMaxFieldIndex - 1);
69     table::CellAddress aCellAddress(0, 7, 8);
70 
71     uno::Reference<sheet::XSpreadsheetDocument> xDoc(m_xComponent, uno::UNO_QUERY_THROW);
72     uno::Reference<sheet::XSpreadsheets> xSheets(xDoc->getSheets(), uno::UNO_SET_THROW);
73 
74     xSheets->insertNewByName("Some Sheet", 0);
75 
76     uno::Reference<container::XIndexAccess> xIA(xSheets, uno::UNO_QUERY_THROW);
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         for (auto j = 1; j < m_nMaxFieldIndex; j++)
90         {
91             xSheet0->getCellByPosition(i, j)->setValue(i * (j + 1));
92             xSheet1->getCellByPosition(i, j)->setValue(i * (j + 2));
93         }
94 
95     xSheet0->getCellByPosition(1, 5);
96     xSheet0->getCellByPosition(aCellAddress.Column, aCellAddress.Row + 3);
97 
98     uno::Reference<sheet::XDataPilotTablesSupplier> xDPTS(xSheet0, uno::UNO_QUERY_THROW);
99     uno::Reference<sheet::XDataPilotTables> xDPT(xDPTS->getDataPilotTables(), uno::UNO_SET_THROW);
100     uno::Reference<sheet::XDataPilotDescriptor> xDPD(xDPT->createDataPilotDescriptor(),
101                                                      uno::UNO_SET_THROW);
102     xDPD->setSourceRange(aCellRangeAddress);
103 
104     uno::Reference<beans::XPropertySet> xDataPilotFieldProp(
105         xDPD->getDataPilotFields()->getByIndex(0), uno::UNO_QUERY_THROW);
106     xDataPilotFieldProp->setPropertyValue("Function", uno::makeAny(sheet::GeneralFunction_SUM));
107     xDataPilotFieldProp->setPropertyValue("Orientation",
108                                           uno::makeAny(sheet::DataPilotFieldOrientation_DATA));
109 
110     if (xDPT->hasByName("DataPilotTable"))
111         xDPT->removeByName("DataPilotTable");
112 
113     uno::Reference<container::XIndexAccess> xIA_DPF(xDPD->getDataPilotFields(), uno::UNO_SET_THROW);
114 
115     xDPT->insertNewByName("DataPilotTable", aCellAddress, xDPD);
116     uno::Reference<sheet::XDataPilotField> xDPF(xIA_DPF->getByIndex(0), uno::UNO_QUERY_THROW);
117     uno::Reference<uno::XInterface> xReturn(xDPF->getItems()->getByIndex(0), uno::UNO_QUERY_THROW);
118     return xReturn;
119 }
120 
setUp()121 void ScDataPilotItemObj::setUp()
122 {
123     CalcUnoApiTest::setUp();
124     // create calc document
125     m_xComponent = loadFromDesktop("private:factory/scalc");
126 }
127 
tearDown()128 void ScDataPilotItemObj::tearDown()
129 {
130     closeDocument(m_xComponent);
131     CalcUnoApiTest::tearDown();
132 }
133 
134 CPPUNIT_TEST_SUITE_REGISTRATION(ScDataPilotItemObj);
135 
136 } // namespace sc_apitest
137 
138 CPPUNIT_PLUGIN_IMPLEMENT();
139 
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
141