Update git submodules
[LibreOffice.git] / sc / qa / extras / scdatapilotitemobj.cxx
blobc8ed93f3ff02f290ce859bd561d13f918811c6e5
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/xnamed.hxx>
12 #include <test/sheet/datapilotitem.hxx>
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>
28 #include <com/sun/star/uno/Reference.hxx>
30 using namespace css;
32 namespace sc_apitest
34 class ScDataPilotItemObj : public UnoApiTest, public apitest::DataPilotItem, public apitest::XNamed
36 public:
37 virtual void setUp() override;
38 virtual uno::Reference<uno::XInterface> init() override;
40 ScDataPilotItemObj();
42 CPPUNIT_TEST_SUITE(ScDataPilotItemObj);
44 // DataPilotItem
45 CPPUNIT_TEST(testProperties);
47 // XNamed
48 CPPUNIT_TEST(testGetName);
50 CPPUNIT_TEST_SUITE_END();
52 private:
53 static const int m_nMaxFieldIndex = 6;
56 ScDataPilotItemObj::ScDataPilotItemObj()
57 : UnoApiTest(u"/sc/qa/extras/testdocuments"_ustr)
58 , XNamed(u"2"_ustr)
62 uno::Reference<uno::XInterface> ScDataPilotItemObj::init()
64 table::CellRangeAddress aCellRangeAddress(0, 1, 0, m_nMaxFieldIndex - 1, m_nMaxFieldIndex - 1);
65 table::CellAddress aCellAddress(0, 7, 8);
67 uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, uno::UNO_QUERY_THROW);
68 uno::Reference<sheet::XSpreadsheets> xSheets(xDoc->getSheets(), uno::UNO_SET_THROW);
70 xSheets->insertNewByName(u"Some Sheet"_ustr, 0);
72 uno::Reference<container::XIndexAccess> xIA(xSheets, uno::UNO_QUERY_THROW);
73 uno::Reference<sheet::XSpreadsheet> xSheet0(xIA->getByIndex(0), uno::UNO_QUERY_THROW);
74 uno::Reference<sheet::XSpreadsheet> xSheet1(xIA->getByIndex(1), uno::UNO_QUERY_THROW);
76 for (auto i = 1; i < m_nMaxFieldIndex; i++)
78 xSheet0->getCellByPosition(i, 0)->setFormula("Col" + OUString::number(i));
79 xSheet0->getCellByPosition(0, i)->setFormula("Row" + OUString::number(i));
80 xSheet1->getCellByPosition(i, 0)->setFormula("Col" + OUString::number(i));
81 xSheet1->getCellByPosition(0, i)->setFormula("Row" + OUString::number(i));
84 for (auto i = 1; i < m_nMaxFieldIndex; i++)
85 for (auto j = 1; j < m_nMaxFieldIndex; j++)
87 xSheet0->getCellByPosition(i, j)->setValue(i * (j + 1));
88 xSheet1->getCellByPosition(i, j)->setValue(i * (j + 2));
91 xSheet0->getCellByPosition(1, 5);
92 xSheet0->getCellByPosition(aCellAddress.Column, aCellAddress.Row + 3);
94 uno::Reference<sheet::XDataPilotTablesSupplier> xDPTS(xSheet0, uno::UNO_QUERY_THROW);
95 uno::Reference<sheet::XDataPilotTables> xDPT(xDPTS->getDataPilotTables(), uno::UNO_SET_THROW);
96 uno::Reference<sheet::XDataPilotDescriptor> xDPD(xDPT->createDataPilotDescriptor(),
97 uno::UNO_SET_THROW);
98 xDPD->setSourceRange(aCellRangeAddress);
100 uno::Reference<beans::XPropertySet> xDataPilotFieldProp(
101 xDPD->getDataPilotFields()->getByIndex(0), uno::UNO_QUERY_THROW);
102 xDataPilotFieldProp->setPropertyValue(u"Function"_ustr, uno::Any(sheet::GeneralFunction_SUM));
103 xDataPilotFieldProp->setPropertyValue(u"Orientation"_ustr,
104 uno::Any(sheet::DataPilotFieldOrientation_DATA));
106 if (xDPT->hasByName(u"DataPilotTable"_ustr))
107 xDPT->removeByName(u"DataPilotTable"_ustr);
109 uno::Reference<container::XIndexAccess> xIA_DPF(xDPD->getDataPilotFields(), uno::UNO_SET_THROW);
111 xDPT->insertNewByName(u"DataPilotTable"_ustr, aCellAddress, xDPD);
112 uno::Reference<sheet::XDataPilotField> xDPF(xIA_DPF->getByIndex(0), uno::UNO_QUERY_THROW);
113 uno::Reference<uno::XInterface> xReturn(xDPF->getItems()->getByIndex(0), uno::UNO_QUERY_THROW);
114 return xReturn;
117 void ScDataPilotItemObj::setUp()
119 UnoApiTest::setUp();
120 // create calc document
121 loadFromURL(u"private:factory/scalc"_ustr);
124 CPPUNIT_TEST_SUITE_REGISTRATION(ScDataPilotItemObj);
126 } // namespace sc_apitest
128 CPPUNIT_PLUGIN_IMPLEMENT();
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */