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 3 : XNamedRanges::XNamedRanges():
39 3 : maNameToRemove("initial1")
40 : {
41 :
42 3 : }
43 :
44 0 : XNamedRanges::XNamedRanges(const OUString& rNameToRemove):
45 0 : maNameToRemove(rNameToRemove)
46 : {
47 :
48 0 : }
49 :
50 3 : XNamedRanges::~XNamedRanges()
51 : {
52 3 : }
53 :
54 1 : void XNamedRanges::testAddNewByName()
55 : {
56 1 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(), UNO_QUERY_THROW);
57 2 : uno::Reference< container::XNameAccess > xNamedRangesNameAccess(init(1), UNO_QUERY_THROW);
58 :
59 1 : table::CellAddress aBaseAddress = table::CellAddress(0,0,0);
60 :
61 1 : sal_Int32 nType = 0;
62 2 : OUString aContent1("D1");
63 2 : OUString aName1("type_0");
64 1 : xNamedRanges->addNewByName(aName1, aContent1, aBaseAddress, nType);
65 1 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type 0 - Normal case", xNamedRanges->hasByName(aName1));
66 :
67 1 : nType = ::sheet::NamedRangeFlag::COLUMN_HEADER;
68 2 : OUString aContent2("D2");
69 2 : OUString aName2("type_COLUMN_HEADER");
70 1 : xNamedRanges->addNewByName(aName2, aContent2, aBaseAddress, nType);
71 1 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type COLUMN_HEADER", xNamedRanges->hasByName(aName2));
72 :
73 1 : nType = ::sheet::NamedRangeFlag::FILTER_CRITERIA;
74 2 : OUString aContent3("D3");
75 2 : OUString aName3("type_FILTER_CRITERIA");
76 1 : xNamedRanges->addNewByName(aName3, aContent3, aBaseAddress, nType);
77 1 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type FILTER_CRITERIA", xNamedRanges->hasByName(aName3));
78 :
79 1 : nType = ::sheet::NamedRangeFlag::PRINT_AREA;
80 2 : OUString aContent4("D4");
81 2 : OUString aName4("type_PRINT_AREA");
82 1 : xNamedRanges->addNewByName(aName4, aContent4, aBaseAddress, nType);
83 1 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type PRINT_AREA", xNamedRanges->hasByName(aName4));
84 :
85 1 : nType = ::sheet::NamedRangeFlag::ROW_HEADER;
86 2 : OUString aContent5("D5");
87 2 : OUString aName5("type_ROW_HEADER");
88 1 : xNamedRanges->addNewByName(aName5, aContent5, aBaseAddress, nType);
89 2 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type ROW_HEADER", xNamedRanges->hasByName(aName5));
90 :
91 1 : }
92 :
93 1 : void XNamedRanges::testAddNewFromTitles()
94 : {
95 1 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(1), UNO_QUERY_THROW);
96 2 : uno::Reference< container::XIndexAccess > xNamedRangesIndex(xNamedRanges, UNO_QUERY_THROW);
97 2 : uno::Reference< container::XNameAccess > xNamedRangesNameAccess(xNamedRanges, UNO_QUERY_THROW);
98 :
99 1 : table::CellRangeAddress aCellRangeAddress = table::CellRangeAddress(1,0,0,3,3);
100 :
101 2 : OUString aString;
102 2 : uno::Reference< table::XCell > xCell;
103 :
104 1 : xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_TOP);
105 :
106 4 : for (sal_Int32 i = 1; i < 4; i++)
107 : {
108 : // verify namedrange exists
109 3 : xCell = xSheet->getCellByPosition(i,0);
110 3 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
111 3 : aString = xTextRange->getString();
112 3 : std::cout << "addNewFromTitles: verify " << aString << std::endl;
113 3 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
114 :
115 : // verify it points on the right cell
116 6 : uno::Any aNr = xNamedRangesNameAccess->getByName(aString);
117 6 : uno::Reference< sheet::XNamedRange > xNamedRange(aNr, UNO_QUERY_THROW);
118 :
119 6 : uno::Reference< sheet::XCellRangeReferrer > xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
120 6 : uno::Reference< sheet::XCellRangeAddressable > xCellRangeAdr(xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
121 3 : table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
122 :
123 3 : CPPUNIT_ASSERT(cellRangeAddress.EndColumn == i && cellRangeAddress.StartColumn == i);
124 3 : CPPUNIT_ASSERT(cellRangeAddress.StartRow == 1);
125 3 : CPPUNIT_ASSERT(cellRangeAddress.EndRow == 3);
126 3 : CPPUNIT_ASSERT(cellRangeAddress.Sheet == 1);
127 3 : }
128 :
129 1 : xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_LEFT);
130 :
131 4 : for (sal_Int32 i = 1; i < 4; i++)
132 : {
133 : // verify namedrange exists
134 3 : xCell = xSheet->getCellByPosition(0,i);
135 3 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
136 3 : aString = xTextRange->getString();
137 3 : std::cout << "verify " << aString << std::endl;
138 3 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
139 :
140 : // verify it points on the right cell
141 6 : uno::Any aNr= xNamedRangesNameAccess->getByName(aString);
142 6 : uno::Reference< sheet::XNamedRange > xNamedRange(aNr, UNO_QUERY_THROW);
143 :
144 6 : uno::Reference< sheet::XCellRangeReferrer > xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
145 6 : uno::Reference< sheet::XCellRangeAddressable > xCellRangeAdr(xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
146 3 : table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
147 :
148 3 : CPPUNIT_ASSERT(cellRangeAddress.EndRow == i && cellRangeAddress.StartRow == i);
149 3 : CPPUNIT_ASSERT(cellRangeAddress.StartColumn == 1);
150 3 : CPPUNIT_ASSERT(cellRangeAddress.EndColumn == 3);
151 3 : CPPUNIT_ASSERT(cellRangeAddress.Sheet == 1);
152 4 : }
153 1 : }
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 1 : void XNamedRanges::testOutputList()
178 : {
179 1 : table::CellAddress xCellAddress = table::CellAddress (0,2,0);
180 1 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(), UNO_QUERY_THROW);
181 2 : uno::Reference< container::XIndexAccess > xNamedRangesIndex(init(), UNO_QUERY_THROW);
182 1 : sal_Int32 nElementsCount = xNamedRangesIndex->getCount();
183 :
184 1 : xNamedRanges->outputList(xCellAddress);
185 :
186 2 : OUString aString;
187 2 : uno::Reference< table::XCell > xCell;
188 :
189 16 : for (sal_Int32 i = 0; i < nElementsCount; i++)
190 : {
191 15 : xCell = xSheet->getCellByPosition(2,i);
192 15 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
193 15 : aString = xTextRange->getString();
194 15 : std::cout << "verify " << aString << std::endl;
195 15 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
196 16 : }
197 1 : }
198 :
199 90 : }
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|