Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2011 Laurent Godard lgodard.libre@laposte.net (initial developer)
17 : *
18 : * All Rights Reserved.
19 : *
20 : * For minor contributions see the git repository.
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : #include <test/sheet/xnamedranges.hxx>
30 :
31 : #include <com/sun/star/beans/XPropertySet.hpp>
32 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
33 : #include <com/sun/star/sheet/XSpreadsheet.hpp>
34 : #include <com/sun/star/table/XCellRange.hpp>
35 : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
36 : #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
37 : #include <com/sun/star/sheet/XNamedRanges.hpp>
38 : #include <com/sun/star/sheet/XNamedRange.hpp>
39 : #include <com/sun/star/table/XCell.hpp>
40 : #include <com/sun/star/text/XTextRange.hpp>
41 : #include <com/sun/star/container/XIndexAccess.hpp>
42 :
43 : #include <com/sun/star/table/CellAddress.hpp>
44 : #include <com/sun/star/table/CellRangeAddress.hpp>
45 : #include <com/sun/star/sheet/Border.hpp>
46 : #include <com/sun/star/sheet/NamedRangeFlag.hpp>
47 :
48 : #include <rtl/ustring.hxx>
49 : #include "cppunit/extensions/HelperMacros.h"
50 : #include <iostream>
51 :
52 : using namespace com::sun::star::uno;
53 :
54 : namespace apitest {
55 :
56 6 : XNamedRanges::XNamedRanges():
57 6 : maNameToRemove(RTL_CONSTASCII_USTRINGPARAM("initial1"))
58 : {
59 :
60 6 : }
61 :
62 0 : XNamedRanges::XNamedRanges(const rtl::OUString& rNameToRemove):
63 0 : maNameToRemove(rNameToRemove)
64 : {
65 :
66 0 : }
67 :
68 6 : XNamedRanges::~XNamedRanges()
69 : {
70 6 : }
71 :
72 2 : void XNamedRanges::testAddNewByName()
73 : {
74 2 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(), UNO_QUERY_THROW);
75 2 : uno::Reference< container::XNameAccess > xNamedRangesNameAccess(init(1), UNO_QUERY_THROW);
76 :
77 2 : table::CellAddress aBaseAddress = table::CellAddress(0,0,0);
78 :
79 2 : sal_Int32 nType = 0;
80 2 : rtl::OUString aContent1(RTL_CONSTASCII_USTRINGPARAM("D1"));
81 2 : rtl::OUString aName1(RTL_CONSTASCII_USTRINGPARAM("type_0"));
82 2 : xNamedRanges->addNewByName(aName1, aContent1, aBaseAddress, nType);
83 2 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type 0 - Normal case", xNamedRanges->hasByName(aName1));
84 :
85 2 : nType = ::sheet::NamedRangeFlag::COLUMN_HEADER;
86 2 : rtl::OUString aContent2(RTL_CONSTASCII_USTRINGPARAM("D2"));
87 2 : rtl::OUString aName2(RTL_CONSTASCII_USTRINGPARAM("type_COLUMN_HEADER"));
88 2 : xNamedRanges->addNewByName(aName2, aContent2, aBaseAddress, nType);
89 2 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type COLUMN_HEADER", xNamedRanges->hasByName(aName2));
90 :
91 2 : nType = ::sheet::NamedRangeFlag::FILTER_CRITERIA;
92 2 : rtl::OUString aContent3(RTL_CONSTASCII_USTRINGPARAM("D3"));
93 2 : rtl::OUString aName3(RTL_CONSTASCII_USTRINGPARAM("type_FILTER_CRITERIA"));
94 2 : xNamedRanges->addNewByName(aName3, aContent3, aBaseAddress, nType);
95 2 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type FILTER_CRITERIA", xNamedRanges->hasByName(aName3));
96 :
97 2 : nType = ::sheet::NamedRangeFlag::PRINT_AREA;
98 2 : rtl::OUString aContent4(RTL_CONSTASCII_USTRINGPARAM("D4"));
99 2 : rtl::OUString aName4(RTL_CONSTASCII_USTRINGPARAM("type_PRINT_AREA"));
100 2 : xNamedRanges->addNewByName(aName4, aContent4, aBaseAddress, nType);
101 2 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type PRINT_AREA", xNamedRanges->hasByName(aName4));
102 :
103 2 : nType = ::sheet::NamedRangeFlag::ROW_HEADER;
104 2 : rtl::OUString aContent5(RTL_CONSTASCII_USTRINGPARAM("D5"));
105 2 : rtl::OUString aName5(RTL_CONSTASCII_USTRINGPARAM("type_ROW_HEADER"));
106 2 : xNamedRanges->addNewByName(aName5, aContent5, aBaseAddress, nType);
107 2 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type ROW_HEADER", xNamedRanges->hasByName(aName5));
108 :
109 2 : }
110 :
111 2 : void XNamedRanges::testAddNewFromTitles()
112 : {
113 2 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(1), UNO_QUERY_THROW);
114 2 : uno::Reference< container::XIndexAccess > xNamedRangesIndex(xNamedRanges, UNO_QUERY_THROW);
115 2 : uno::Reference< container::XNameAccess > xNamedRangesNameAccess(xNamedRanges, UNO_QUERY_THROW);
116 :
117 2 : table::CellRangeAddress aCellRangeAddress = table::CellRangeAddress(1,0,0,3,3);
118 :
119 2 : rtl::OUString aString;
120 2 : uno::Reference< table::XCell > xCell;
121 :
122 2 : xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_TOP);
123 :
124 8 : for (sal_Int32 i = 1; i < 4; i++)
125 : {
126 : // verify namedrange exists
127 6 : xCell = xSheet->getCellByPosition(i,0);
128 6 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
129 6 : aString = xTextRange->getString();
130 6 : std::cout << "addNewFromTitles: verify " << aString << std::endl;
131 6 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
132 :
133 : // verify it points on the right cell
134 6 : uno::Any aNr = xNamedRangesNameAccess->getByName(aString);
135 6 : uno::Reference< sheet::XNamedRange > xNamedRange(aNr, UNO_QUERY_THROW);
136 :
137 6 : uno::Reference< sheet::XCellRangeReferrer > xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
138 6 : uno::Reference< sheet::XCellRangeAddressable > xCellRangeAdr(xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
139 6 : table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
140 :
141 6 : CPPUNIT_ASSERT(cellRangeAddress.EndColumn == i && cellRangeAddress.StartColumn == i);
142 6 : CPPUNIT_ASSERT(cellRangeAddress.StartRow == 1);
143 6 : CPPUNIT_ASSERT(cellRangeAddress.EndRow == 3);
144 6 : CPPUNIT_ASSERT(cellRangeAddress.Sheet == 1);
145 6 : }
146 :
147 2 : xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_LEFT);
148 :
149 8 : for (sal_Int32 i = 1; i < 4; i++)
150 : {
151 : // verify namedrange exists
152 6 : xCell = xSheet->getCellByPosition(0,i);
153 6 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
154 6 : aString = xTextRange->getString();
155 6 : std::cout << "verify " << aString << std::endl;
156 6 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
157 :
158 : // verify it points on the right cell
159 6 : uno::Any aNr= xNamedRangesNameAccess->getByName(aString);
160 6 : uno::Reference< sheet::XNamedRange > xNamedRange(aNr, UNO_QUERY_THROW);
161 :
162 6 : uno::Reference< sheet::XCellRangeReferrer > xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
163 6 : uno::Reference< sheet::XCellRangeAddressable > xCellRangeAdr(xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
164 6 : table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
165 :
166 6 : CPPUNIT_ASSERT(cellRangeAddress.EndRow == i && cellRangeAddress.StartRow == i);
167 6 : CPPUNIT_ASSERT(cellRangeAddress.StartColumn == 1);
168 6 : CPPUNIT_ASSERT(cellRangeAddress.EndColumn == 3);
169 6 : CPPUNIT_ASSERT(cellRangeAddress.Sheet == 1);
170 8 : }
171 2 : }
172 :
173 0 : void XNamedRanges::testRemoveByName()
174 : {
175 0 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(), UNO_QUERY_THROW);
176 0 : uno::Reference< container::XIndexAccess > xIndex(xNamedRanges, UNO_QUERY_THROW);
177 :
178 0 : bool bHasIt = xNamedRanges->hasByName(maNameToRemove);
179 0 : CPPUNIT_ASSERT_MESSAGE("NamedRange initial1 des not exits, can't remove it", bHasIt);
180 :
181 0 : if (bHasIt)
182 : {
183 : // remove existing
184 0 : sal_Int32 nInitialCount = xIndex->getCount();
185 0 : xNamedRanges->removeByName(maNameToRemove);
186 0 : sal_Int32 nNewCount = xIndex->getCount();
187 0 : CPPUNIT_ASSERT_EQUAL_MESSAGE("NamedRange initial1 not removed", nNewCount, nInitialCount - 1);
188 0 : CPPUNIT_ASSERT_MESSAGE("Wrong NamedRange removed, initial1 still present", !xNamedRanges->hasByName(maNameToRemove));
189 : // try to remove non existing
190 0 : rtl::OUString aNr2(RTL_CONSTASCII_USTRINGPARAM("dummyNonExistingNamedRange"));
191 0 : xNamedRanges->removeByName(aNr2);// an exception should be raised here
192 0 : }
193 0 : }
194 :
195 2 : void XNamedRanges::testOutputList()
196 : {
197 2 : table::CellAddress xCellAddress = table::CellAddress (0,2,0);
198 2 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(), UNO_QUERY_THROW);
199 2 : uno::Reference< container::XIndexAccess > xNamedRangesIndex(init(), UNO_QUERY_THROW);
200 2 : sal_Int32 nElementsCount = xNamedRangesIndex->getCount();
201 :
202 2 : xNamedRanges->outputList(xCellAddress);
203 :
204 2 : rtl::OUString aString;
205 2 : uno::Reference< table::XCell > xCell;
206 :
207 32 : for (sal_Int32 i = 0; i < nElementsCount; i++)
208 : {
209 30 : xCell = xSheet->getCellByPosition(2,i);
210 30 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
211 30 : aString = xTextRange->getString();
212 30 : std::cout << "verify " << aString << std::endl;
213 30 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
214 32 : }
215 2 : }
216 :
217 66 : }
218 :
219 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|