1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <test/sheet/xnamedranges.hxx>
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>
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>
29 #include <rtl/ustring.hxx>
30 #include "cppunit/extensions/HelperMacros.h"
34 using namespace css::uno
;
38 XNamedRanges::XNamedRanges():
39 maNameToRemove("initial1")
44 XNamedRanges::XNamedRanges(const OUString
& rNameToRemove
):
45 maNameToRemove(rNameToRemove
)
50 XNamedRanges::~XNamedRanges()
54 void XNamedRanges::testAddNewByName()
56 uno::Reference
< sheet::XNamedRanges
> xNamedRanges(init(), UNO_QUERY_THROW
);
57 uno::Reference
< container::XNameAccess
> xNamedRangesNameAccess(init(1), UNO_QUERY_THROW
);
59 table::CellAddress aBaseAddress
= table::CellAddress(0,0,0);
62 OUString
aContent1("D1");
63 OUString
aName1("type_0");
64 xNamedRanges
->addNewByName(aName1
, aContent1
, aBaseAddress
, nType
);
65 CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type 0 - Normal case", xNamedRanges
->hasByName(aName1
));
67 nType
= ::sheet::NamedRangeFlag::COLUMN_HEADER
;
68 OUString
aContent2("D2");
69 OUString
aName2("type_COLUMN_HEADER");
70 xNamedRanges
->addNewByName(aName2
, aContent2
, aBaseAddress
, nType
);
71 CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type COLUMN_HEADER", xNamedRanges
->hasByName(aName2
));
73 nType
= ::sheet::NamedRangeFlag::FILTER_CRITERIA
;
74 OUString
aContent3("D3");
75 OUString
aName3("type_FILTER_CRITERIA");
76 xNamedRanges
->addNewByName(aName3
, aContent3
, aBaseAddress
, nType
);
77 CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type FILTER_CRITERIA", xNamedRanges
->hasByName(aName3
));
79 nType
= ::sheet::NamedRangeFlag::PRINT_AREA
;
80 OUString
aContent4("D4");
81 OUString
aName4("type_PRINT_AREA");
82 xNamedRanges
->addNewByName(aName4
, aContent4
, aBaseAddress
, nType
);
83 CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type PRINT_AREA", xNamedRanges
->hasByName(aName4
));
85 nType
= ::sheet::NamedRangeFlag::ROW_HEADER
;
86 OUString
aContent5("D5");
87 OUString
aName5("type_ROW_HEADER");
88 xNamedRanges
->addNewByName(aName5
, aContent5
, aBaseAddress
, nType
);
89 CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type ROW_HEADER", xNamedRanges
->hasByName(aName5
));
93 void XNamedRanges::testAddNewFromTitles()
95 uno::Reference
< sheet::XNamedRanges
> xNamedRanges(init(1), UNO_QUERY_THROW
);
96 uno::Reference
< container::XIndexAccess
> xNamedRangesIndex(xNamedRanges
, UNO_QUERY_THROW
);
97 uno::Reference
< container::XNameAccess
> xNamedRangesNameAccess(xNamedRanges
, UNO_QUERY_THROW
);
99 table::CellRangeAddress aCellRangeAddress
= table::CellRangeAddress(1,0,0,3,3);
102 uno::Reference
< table::XCell
> xCell
;
104 xNamedRanges
->addNewFromTitles(aCellRangeAddress
, sheet::Border_TOP
);
106 for (sal_Int32 i
= 1; i
< 4; i
++)
108 // verify namedrange exists
109 xCell
= xSheet
->getCellByPosition(i
,0);
110 uno::Reference
< text::XTextRange
> xTextRange(xCell
, UNO_QUERY_THROW
);
111 aString
= xTextRange
->getString();
112 std::cout
<< "addNewFromTitles: verify " << aString
<< std::endl
;
113 CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges
->hasByName(aString
));
115 // verify it points on the right cell
116 uno::Any aNr
= xNamedRangesNameAccess
->getByName(aString
);
117 uno::Reference
< sheet::XNamedRange
> xNamedRange(aNr
, UNO_QUERY_THROW
);
119 uno::Reference
< sheet::XCellRangeReferrer
> xCellRangeRef(xNamedRange
, UNO_QUERY_THROW
);
120 uno::Reference
< sheet::XCellRangeAddressable
> xCellRangeAdr(xCellRangeRef
->getReferredCells(), UNO_QUERY_THROW
);
121 table::CellRangeAddress cellRangeAddress
= xCellRangeAdr
->getRangeAddress();
123 CPPUNIT_ASSERT(cellRangeAddress
.EndColumn
== i
&& cellRangeAddress
.StartColumn
== i
);
124 CPPUNIT_ASSERT(cellRangeAddress
.StartRow
== 1);
125 CPPUNIT_ASSERT(cellRangeAddress
.EndRow
== 3);
126 CPPUNIT_ASSERT(cellRangeAddress
.Sheet
== 1);
129 xNamedRanges
->addNewFromTitles(aCellRangeAddress
, sheet::Border_LEFT
);
131 for (sal_Int32 i
= 1; i
< 4; i
++)
133 // verify namedrange exists
134 xCell
= xSheet
->getCellByPosition(0,i
);
135 uno::Reference
< text::XTextRange
> xTextRange(xCell
, UNO_QUERY_THROW
);
136 aString
= xTextRange
->getString();
137 std::cout
<< "verify " << aString
<< std::endl
;
138 CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges
->hasByName(aString
));
140 // verify it points on the right cell
141 uno::Any aNr
= xNamedRangesNameAccess
->getByName(aString
);
142 uno::Reference
< sheet::XNamedRange
> xNamedRange(aNr
, UNO_QUERY_THROW
);
144 uno::Reference
< sheet::XCellRangeReferrer
> xCellRangeRef(xNamedRange
, UNO_QUERY_THROW
);
145 uno::Reference
< sheet::XCellRangeAddressable
> xCellRangeAdr(xCellRangeRef
->getReferredCells(), UNO_QUERY_THROW
);
146 table::CellRangeAddress cellRangeAddress
= xCellRangeAdr
->getRangeAddress();
148 CPPUNIT_ASSERT(cellRangeAddress
.EndRow
== i
&& cellRangeAddress
.StartRow
== i
);
149 CPPUNIT_ASSERT(cellRangeAddress
.StartColumn
== 1);
150 CPPUNIT_ASSERT(cellRangeAddress
.EndColumn
== 3);
151 CPPUNIT_ASSERT(cellRangeAddress
.Sheet
== 1);
155 void XNamedRanges::testRemoveByName()
157 uno::Reference
< sheet::XNamedRanges
> xNamedRanges(init(), UNO_QUERY_THROW
);
158 uno::Reference
< container::XIndexAccess
> xIndex(xNamedRanges
, UNO_QUERY_THROW
);
160 bool bHasIt
= xNamedRanges
->hasByName(maNameToRemove
);
161 CPPUNIT_ASSERT_MESSAGE("NamedRange initial1 des not exits, can't remove it", bHasIt
);
166 sal_Int32 nInitialCount
= xIndex
->getCount();
167 xNamedRanges
->removeByName(maNameToRemove
);
168 sal_Int32 nNewCount
= xIndex
->getCount();
169 CPPUNIT_ASSERT_EQUAL_MESSAGE("NamedRange initial1 not removed", nNewCount
, nInitialCount
- 1);
170 CPPUNIT_ASSERT_MESSAGE("Wrong NamedRange removed, initial1 still present", !xNamedRanges
->hasByName(maNameToRemove
));
171 // try to remove non existing
172 OUString
aNr2("dummyNonExistingNamedRange");
173 xNamedRanges
->removeByName(aNr2
);// an exception should be raised here
177 void XNamedRanges::testOutputList()
179 table::CellAddress xCellAddress
= table::CellAddress (0,2,0);
180 uno::Reference
< sheet::XNamedRanges
> xNamedRanges(init(), UNO_QUERY_THROW
);
181 uno::Reference
< container::XIndexAccess
> xNamedRangesIndex(init(), UNO_QUERY_THROW
);
182 sal_Int32 nElementsCount
= xNamedRangesIndex
->getCount();
184 xNamedRanges
->outputList(xCellAddress
);
187 uno::Reference
< table::XCell
> xCell
;
189 for (sal_Int32 i
= 0; i
< nElementsCount
; i
++)
191 xCell
= xSheet
->getCellByPosition(2,i
);
192 uno::Reference
< text::XTextRange
> xTextRange(xCell
, UNO_QUERY_THROW
);
193 aString
= xTextRange
->getString();
194 std::cout
<< "verify " << aString
<< std::endl
;
195 CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges
->hasByName(aString
));
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */