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 <sal/config.h>
11 #include <test/bootstrapfixture.hxx>
12 
13 #include <docsh.hxx>
14 #include <chart2uno.hxx>
15 
16 #include <com/sun/star/chart/ChartDataRowSource.hpp>
17 #include <com/sun/star/chart2/data/XDataSource.hpp>
18 
19 #include "helper/qahelper.hxx"
20 
21 using namespace ::com::sun::star;
22 using namespace ::com::sun::star::uno;
23 
24 class ScChart2DataProviderTest : public ScBootstrapFixture
25 {
26 public:
27     ScChart2DataProviderTest();
28 
29     virtual void setUp() override;
30     virtual void tearDown() override;
31 
32     void testHeaderExpansion();
33 
34     CPPUNIT_TEST_SUITE(ScChart2DataProviderTest);
35     CPPUNIT_TEST(testHeaderExpansion);
36     CPPUNIT_TEST_SUITE_END();
37 
38 private:
39     uno::Reference<uno::XInterface> m_xCalcComponent;
40 };
41 
lcl_createAndCheckDataProvider(ScDocument & rDoc,const OUString & cellRange,bool hasCategories,bool firstCellAsLabel,sal_Int32 expectedRows,sal_Int32 expectedCols)42 static void lcl_createAndCheckDataProvider(ScDocument& rDoc, const OUString& cellRange,
43                                            bool hasCategories, bool firstCellAsLabel,
44                                            sal_Int32 expectedRows, sal_Int32 expectedCols)
45 {
46     uno::Reference<chart2::data::XDataProvider> xDataProvider = new ScChart2DataProvider(&rDoc);
47     CPPUNIT_ASSERT(xDataProvider.is());
48 
49     uno::Sequence<beans::PropertyValue> aArgs(4);
50 
51     aArgs[0].Name = "CellRangeRepresentation";
52     aArgs[0].Value <<= cellRange;
53 
54     aArgs[1].Name = "HasCategories";
55     aArgs[1].Value <<= hasCategories;
56 
57     aArgs[2].Name = "FirstCellAsLabel";
58     aArgs[2].Value <<= firstCellAsLabel;
59 
60     aArgs[3].Name = "DataRowSource";
61     aArgs[3].Value <<= chart::ChartDataRowSource_COLUMNS;
62 
63     uno::Reference<chart2::data::XDataSource> xDataSource = xDataProvider->createDataSource(aArgs);
64     CPPUNIT_ASSERT(xDataSource.is());
65 
66     css::uno::Sequence<uno::Reference<chart2::data::XLabeledDataSequence>> xSequences
67         = xDataSource->getDataSequences();
68 
69     CPPUNIT_ASSERT_EQUAL(expectedRows, xSequences.getLength());
70 
71     sal_Int32 nStartRow = hasCategories ? 1 : 0;
72     for (sal_Int32 nIdx = nStartRow; nIdx < xSequences.getLength(); ++nIdx)
73     {
74         Reference<chart2::data::XDataSequence> xValues(xSequences[nIdx]->getValues());
75         if (xValues.is())
76         {
77             sal_Int32 colsNum = xValues->getData().getLength();
78             CPPUNIT_ASSERT_EQUAL(expectedCols, colsNum);
79         }
80     }
81 }
82 
testHeaderExpansion()83 void ScChart2DataProviderTest::testHeaderExpansion()
84 {
85     ScDocShellRef xDocSh = loadDoc("chart2dataprovider.", FORMAT_ODS);
86     CPPUNIT_ASSERT_MESSAGE("Failed to load ch.ods.", xDocSh.is());
87 
88     ScDocument& rDoc = xDocSh->GetDocument();
89 
90     lcl_createAndCheckDataProvider(rDoc, "$Sheet1.$A$1:$D$4", false, false, 4, 4);
91     lcl_createAndCheckDataProvider(rDoc, "$Sheet1.$A$1:$D$4", true, true, 4, 3);
92 
93     lcl_createAndCheckDataProvider(rDoc, "$Sheet1.$A$17:$D$20", true, true, 3, 2);
94 
95     lcl_createAndCheckDataProvider(rDoc, "$Sheet1.$A$25:$D$28", true, true, 4, 2);
96 
97     xDocSh->DoClose();
98 }
99 
ScChart2DataProviderTest()100 ScChart2DataProviderTest::ScChart2DataProviderTest()
101     : ScBootstrapFixture("sc/qa/unit/data")
102 {
103 }
104 
setUp()105 void ScChart2DataProviderTest::setUp()
106 {
107     test::BootstrapFixture::setUp();
108 
109     // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure,
110     // which is a private symbol to us, gets called
111     m_xCalcComponent
112         = getMultiServiceFactory()->createInstance("com.sun.star.comp.Calc.SpreadsheetDocument");
113     CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xCalcComponent.is());
114 }
115 
tearDown()116 void ScChart2DataProviderTest::tearDown()
117 {
118     uno::Reference<lang::XComponent>(m_xCalcComponent, UNO_QUERY_THROW)->dispose();
119     test::BootstrapFixture::tearDown();
120 }
121 
122 CPPUNIT_TEST_SUITE_REGISTRATION(ScChart2DataProviderTest);
123 
124 CPPUNIT_PLUGIN_IMPLEMENT();
125 
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
127