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