Line data Source code
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/sheet/xnamedranges.hxx>
11 :
12 : #include <com/sun/star/beans/XPropertySet.hpp>
13 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
14 : #include <com/sun/star/sheet/XSpreadsheet.hpp>
15 : #include <com/sun/star/table/XCellRange.hpp>
16 : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
17 : #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
18 : #include <com/sun/star/sheet/XNamedRanges.hpp>
19 : #include <com/sun/star/sheet/XNamedRange.hpp>
20 : #include <com/sun/star/table/XCell.hpp>
21 : #include <com/sun/star/text/XTextRange.hpp>
22 : #include <com/sun/star/container/XIndexAccess.hpp>
23 :
24 : #include <com/sun/star/table/CellAddress.hpp>
25 : #include <com/sun/star/table/CellRangeAddress.hpp>
26 : #include <com/sun/star/sheet/Border.hpp>
27 : #include <com/sun/star/sheet/NamedRangeFlag.hpp>
28 :
29 : #include <rtl/ustring.hxx>
30 : #include "cppunit/extensions/HelperMacros.h"
31 : #include <iostream>
32 :
33 : using namespace css;
34 : using namespace css::uno;
35 :
36 : namespace apitest {
37 :
38 6 : XNamedRanges::XNamedRanges():
39 6 : maNameToRemove("initial1")
40 : {
41 :
42 6 : }
43 :
44 0 : XNamedRanges::XNamedRanges(const OUString& rNameToRemove):
45 0 : maNameToRemove(rNameToRemove)
46 : {
47 :
48 0 : }
49 :
50 6 : XNamedRanges::~XNamedRanges()
51 : {
52 6 : }
53 :
54 2 : void XNamedRanges::testAddNewByName()
55 : {
56 2 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(), UNO_QUERY_THROW);
57 4 : uno::Reference< container::XNameAccess > xNamedRangesNameAccess(init(1), UNO_QUERY_THROW);
58 :
59 2 : table::CellAddress aBaseAddress = table::CellAddress(0,0,0);
60 :
61 2 : sal_Int32 nType = 0;
62 4 : OUString aContent1("D1");
63 4 : OUString aName1("type_0");
64 2 : xNamedRanges->addNewByName(aName1, aContent1, aBaseAddress, nType);
65 2 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type 0 - Normal case", xNamedRanges->hasByName(aName1));
66 :
67 2 : nType = ::sheet::NamedRangeFlag::COLUMN_HEADER;
68 4 : OUString aContent2("D2");
69 4 : OUString aName2("type_COLUMN_HEADER");
70 2 : xNamedRanges->addNewByName(aName2, aContent2, aBaseAddress, nType);
71 2 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type COLUMN_HEADER", xNamedRanges->hasByName(aName2));
72 :
73 2 : nType = ::sheet::NamedRangeFlag::FILTER_CRITERIA;
74 4 : OUString aContent3("D3");
75 4 : OUString aName3("type_FILTER_CRITERIA");
76 2 : xNamedRanges->addNewByName(aName3, aContent3, aBaseAddress, nType);
77 2 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type FILTER_CRITERIA", xNamedRanges->hasByName(aName3));
78 :
79 2 : nType = ::sheet::NamedRangeFlag::PRINT_AREA;
80 4 : OUString aContent4("D4");
81 4 : OUString aName4("type_PRINT_AREA");
82 2 : xNamedRanges->addNewByName(aName4, aContent4, aBaseAddress, nType);
83 2 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type PRINT_AREA", xNamedRanges->hasByName(aName4));
84 :
85 2 : nType = ::sheet::NamedRangeFlag::ROW_HEADER;
86 4 : OUString aContent5("D5");
87 4 : OUString aName5("type_ROW_HEADER");
88 2 : xNamedRanges->addNewByName(aName5, aContent5, aBaseAddress, nType);
89 4 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type ROW_HEADER", xNamedRanges->hasByName(aName5));
90 :
91 2 : }
92 :
93 2 : void XNamedRanges::testAddNewFromTitles()
94 : {
95 2 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(1), UNO_QUERY_THROW);
96 4 : uno::Reference< container::XIndexAccess > xNamedRangesIndex(xNamedRanges, UNO_QUERY_THROW);
97 4 : uno::Reference< container::XNameAccess > xNamedRangesNameAccess(xNamedRanges, UNO_QUERY_THROW);
98 :
99 2 : table::CellRangeAddress aCellRangeAddress = table::CellRangeAddress(1,0,0,3,3);
100 :
101 4 : OUString aString;
102 4 : uno::Reference< table::XCell > xCell;
103 :
104 2 : xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_TOP);
105 :
106 8 : for (sal_Int32 i = 1; i < 4; i++)
107 : {
108 : // verify namedrange exists
109 6 : xCell = xSheet->getCellByPosition(i,0);
110 6 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
111 6 : aString = xTextRange->getString();
112 6 : std::cout << "addNewFromTitles: verify " << aString << std::endl;
113 6 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
114 :
115 : // verify it points on the right cell
116 12 : uno::Any aNr = xNamedRangesNameAccess->getByName(aString);
117 12 : uno::Reference< sheet::XNamedRange > xNamedRange(aNr, UNO_QUERY_THROW);
118 :
119 12 : uno::Reference< sheet::XCellRangeReferrer > xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
120 12 : uno::Reference< sheet::XCellRangeAddressable > xCellRangeAdr(xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
121 6 : table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
122 :
123 6 : CPPUNIT_ASSERT(cellRangeAddress.EndColumn == i && cellRangeAddress.StartColumn == i);
124 6 : CPPUNIT_ASSERT(cellRangeAddress.StartRow == 1);
125 6 : CPPUNIT_ASSERT(cellRangeAddress.EndRow == 3);
126 6 : CPPUNIT_ASSERT(cellRangeAddress.Sheet == 1);
127 6 : }
128 :
129 2 : xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_LEFT);
130 :
131 8 : for (sal_Int32 i = 1; i < 4; i++)
132 : {
133 : // verify namedrange exists
134 6 : xCell = xSheet->getCellByPosition(0,i);
135 6 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
136 6 : aString = xTextRange->getString();
137 6 : std::cout << "verify " << aString << std::endl;
138 6 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
139 :
140 : // verify it points on the right cell
141 12 : uno::Any aNr= xNamedRangesNameAccess->getByName(aString);
142 12 : uno::Reference< sheet::XNamedRange > xNamedRange(aNr, UNO_QUERY_THROW);
143 :
144 12 : uno::Reference< sheet::XCellRangeReferrer > xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
145 12 : uno::Reference< sheet::XCellRangeAddressable > xCellRangeAdr(xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
146 6 : table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
147 :
148 6 : CPPUNIT_ASSERT(cellRangeAddress.EndRow == i && cellRangeAddress.StartRow == i);
149 6 : CPPUNIT_ASSERT(cellRangeAddress.StartColumn == 1);
150 6 : CPPUNIT_ASSERT(cellRangeAddress.EndColumn == 3);
151 6 : CPPUNIT_ASSERT(cellRangeAddress.Sheet == 1);
152 8 : }
153 2 : }
154 :
155 0 : void XNamedRanges::testRemoveByName()
156 : {
157 0 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(), UNO_QUERY_THROW);
158 0 : uno::Reference< container::XIndexAccess > xIndex(xNamedRanges, UNO_QUERY_THROW);
159 :
160 0 : bool bHasIt = xNamedRanges->hasByName(maNameToRemove);
161 0 : CPPUNIT_ASSERT_MESSAGE("NamedRange initial1 des not exits, can't remove it", bHasIt);
162 :
163 0 : if (bHasIt)
164 : {
165 : // remove existing
166 0 : sal_Int32 nInitialCount = xIndex->getCount();
167 0 : xNamedRanges->removeByName(maNameToRemove);
168 0 : sal_Int32 nNewCount = xIndex->getCount();
169 0 : CPPUNIT_ASSERT_EQUAL_MESSAGE("NamedRange initial1 not removed", nNewCount, nInitialCount - 1);
170 0 : CPPUNIT_ASSERT_MESSAGE("Wrong NamedRange removed, initial1 still present", !xNamedRanges->hasByName(maNameToRemove));
171 : // try to remove non existing
172 0 : OUString aNr2("dummyNonExistingNamedRange");
173 0 : xNamedRanges->removeByName(aNr2);// an exception should be raised here
174 0 : }
175 0 : }
176 :
177 2 : void XNamedRanges::testOutputList()
178 : {
179 2 : table::CellAddress xCellAddress = table::CellAddress (0,2,0);
180 2 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(), UNO_QUERY_THROW);
181 4 : uno::Reference< container::XIndexAccess > xNamedRangesIndex(init(), UNO_QUERY_THROW);
182 2 : sal_Int32 nElementsCount = xNamedRangesIndex->getCount();
183 :
184 2 : xNamedRanges->outputList(xCellAddress);
185 :
186 4 : OUString aString;
187 4 : uno::Reference< table::XCell > xCell;
188 :
189 32 : for (sal_Int32 i = 0; i < nElementsCount; i++)
190 : {
191 30 : xCell = xSheet->getCellByPosition(2,i);
192 30 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
193 30 : aString = xTextRange->getString();
194 30 : std::cout << "verify " << aString << std::endl;
195 30 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
196 32 : }
197 2 : }
198 :
199 144 : }
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|