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 0 : XNamedRanges::XNamedRanges():
57 0 : maNameToRemove(RTL_CONSTASCII_USTRINGPARAM("initial1"))
58 : {
59 :
60 0 : }
61 :
62 0 : XNamedRanges::XNamedRanges(const rtl::OUString& rNameToRemove):
63 0 : maNameToRemove(rNameToRemove)
64 : {
65 :
66 0 : }
67 :
68 0 : XNamedRanges::~XNamedRanges()
69 : {
70 0 : }
71 :
72 0 : void XNamedRanges::testAddNewByName()
73 : {
74 0 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(), UNO_QUERY_THROW);
75 0 : uno::Reference< container::XNameAccess > xNamedRangesNameAccess(init(1), UNO_QUERY_THROW);
76 :
77 0 : table::CellAddress aBaseAddress = table::CellAddress(0,0,0);
78 :
79 0 : sal_Int32 nType = 0;
80 0 : rtl::OUString aContent1(RTL_CONSTASCII_USTRINGPARAM("D1"));
81 0 : rtl::OUString aName1(RTL_CONSTASCII_USTRINGPARAM("type_0"));
82 0 : xNamedRanges->addNewByName(aName1, aContent1, aBaseAddress, nType);
83 0 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type 0 - Normal case", xNamedRanges->hasByName(aName1));
84 :
85 0 : nType = ::sheet::NamedRangeFlag::COLUMN_HEADER;
86 0 : rtl::OUString aContent2(RTL_CONSTASCII_USTRINGPARAM("D2"));
87 0 : rtl::OUString aName2(RTL_CONSTASCII_USTRINGPARAM("type_COLUMN_HEADER"));
88 0 : xNamedRanges->addNewByName(aName2, aContent2, aBaseAddress, nType);
89 0 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type COLUMN_HEADER", xNamedRanges->hasByName(aName2));
90 :
91 0 : nType = ::sheet::NamedRangeFlag::FILTER_CRITERIA;
92 0 : rtl::OUString aContent3(RTL_CONSTASCII_USTRINGPARAM("D3"));
93 0 : rtl::OUString aName3(RTL_CONSTASCII_USTRINGPARAM("type_FILTER_CRITERIA"));
94 0 : xNamedRanges->addNewByName(aName3, aContent3, aBaseAddress, nType);
95 0 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type FILTER_CRITERIA", xNamedRanges->hasByName(aName3));
96 :
97 0 : nType = ::sheet::NamedRangeFlag::PRINT_AREA;
98 0 : rtl::OUString aContent4(RTL_CONSTASCII_USTRINGPARAM("D4"));
99 0 : rtl::OUString aName4(RTL_CONSTASCII_USTRINGPARAM("type_PRINT_AREA"));
100 0 : xNamedRanges->addNewByName(aName4, aContent4, aBaseAddress, nType);
101 0 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type PRINT_AREA", xNamedRanges->hasByName(aName4));
102 :
103 0 : nType = ::sheet::NamedRangeFlag::ROW_HEADER;
104 0 : rtl::OUString aContent5(RTL_CONSTASCII_USTRINGPARAM("D5"));
105 0 : rtl::OUString aName5(RTL_CONSTASCII_USTRINGPARAM("type_ROW_HEADER"));
106 0 : xNamedRanges->addNewByName(aName5, aContent5, aBaseAddress, nType);
107 0 : CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type ROW_HEADER", xNamedRanges->hasByName(aName5));
108 :
109 0 : }
110 :
111 0 : void XNamedRanges::testAddNewFromTitles()
112 : {
113 0 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(1), UNO_QUERY_THROW);
114 0 : uno::Reference< container::XIndexAccess > xNamedRangesIndex(xNamedRanges, UNO_QUERY_THROW);
115 0 : uno::Reference< container::XNameAccess > xNamedRangesNameAccess(xNamedRanges, UNO_QUERY_THROW);
116 :
117 0 : table::CellRangeAddress aCellRangeAddress = table::CellRangeAddress(1,0,0,3,3);
118 :
119 0 : rtl::OUString aString;
120 0 : uno::Reference< table::XCell > xCell;
121 :
122 0 : xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_TOP);
123 :
124 0 : for (sal_Int32 i = 1; i < 4; i++)
125 : {
126 : // verify namedrange exists
127 0 : xCell = xSheet->getCellByPosition(i,0);
128 0 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
129 0 : aString = xTextRange->getString();
130 0 : std::cout << "addNewFromTitles: verify " << aString << std::endl;
131 0 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
132 :
133 : // verify it points on the right cell
134 0 : uno::Any aNr = xNamedRangesNameAccess->getByName(aString);
135 0 : uno::Reference< sheet::XNamedRange > xNamedRange(aNr, UNO_QUERY_THROW);
136 :
137 0 : uno::Reference< sheet::XCellRangeReferrer > xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
138 0 : uno::Reference< sheet::XCellRangeAddressable > xCellRangeAdr(xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
139 0 : table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
140 :
141 0 : CPPUNIT_ASSERT(cellRangeAddress.EndColumn == i && cellRangeAddress.StartColumn == i);
142 0 : CPPUNIT_ASSERT(cellRangeAddress.StartRow == 1);
143 0 : CPPUNIT_ASSERT(cellRangeAddress.EndRow == 3);
144 0 : CPPUNIT_ASSERT(cellRangeAddress.Sheet == 1);
145 0 : }
146 :
147 0 : xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_LEFT);
148 :
149 0 : for (sal_Int32 i = 1; i < 4; i++)
150 : {
151 : // verify namedrange exists
152 0 : xCell = xSheet->getCellByPosition(0,i);
153 0 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
154 0 : aString = xTextRange->getString();
155 0 : std::cout << "verify " << aString << std::endl;
156 0 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
157 :
158 : // verify it points on the right cell
159 0 : uno::Any aNr= xNamedRangesNameAccess->getByName(aString);
160 0 : uno::Reference< sheet::XNamedRange > xNamedRange(aNr, UNO_QUERY_THROW);
161 :
162 0 : uno::Reference< sheet::XCellRangeReferrer > xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
163 0 : uno::Reference< sheet::XCellRangeAddressable > xCellRangeAdr(xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
164 0 : table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
165 :
166 0 : CPPUNIT_ASSERT(cellRangeAddress.EndRow == i && cellRangeAddress.StartRow == i);
167 0 : CPPUNIT_ASSERT(cellRangeAddress.StartColumn == 1);
168 0 : CPPUNIT_ASSERT(cellRangeAddress.EndColumn == 3);
169 0 : CPPUNIT_ASSERT(cellRangeAddress.Sheet == 1);
170 0 : }
171 0 : }
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 0 : void XNamedRanges::testOutputList()
196 : {
197 0 : table::CellAddress xCellAddress = table::CellAddress (0,2,0);
198 0 : uno::Reference< sheet::XNamedRanges > xNamedRanges(init(), UNO_QUERY_THROW);
199 0 : uno::Reference< container::XIndexAccess > xNamedRangesIndex(init(), UNO_QUERY_THROW);
200 0 : sal_Int32 nElementsCount = xNamedRangesIndex->getCount();
201 :
202 0 : xNamedRanges->outputList(xCellAddress);
203 :
204 0 : rtl::OUString aString;
205 0 : uno::Reference< table::XCell > xCell;
206 :
207 0 : for (sal_Int32 i = 0; i < nElementsCount; i++)
208 : {
209 0 : xCell = xSheet->getCellByPosition(2,i);
210 0 : uno::Reference< text::XTextRange > xTextRange(xCell, UNO_QUERY_THROW);
211 0 : aString = xTextRange->getString();
212 0 : std::cout << "verify " << aString << std::endl;
213 0 : CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
214 0 : }
215 0 : }
216 :
217 0 : }
218 :
219 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|