LibreOffice Module test (master)  1
xnamedranges.cxx
Go to the documentation of this file.
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 
11 #include <rtl/ustring.hxx>
12 
13 #include <com/sun/star/container/XIndexAccess.hpp>
14 #include <com/sun/star/sheet/Border.hpp>
15 #include <com/sun/star/sheet/NamedRangeFlag.hpp>
16 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
17 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
18 #include <com/sun/star/sheet/XNamedRange.hpp>
19 #include <com/sun/star/sheet/XNamedRanges.hpp>
20 #include <com/sun/star/table/CellAddress.hpp>
21 #include <com/sun/star/table/CellRangeAddress.hpp>
22 #include <com/sun/star/table/XCell.hpp>
23 #include <com/sun/star/text/XTextRange.hpp>
24 
25 #include <com/sun/star/uno/Reference.hxx>
26 
27 #include <cppunit/TestAssert.h>
28 
29 #include <iostream>
30 
31 using namespace css;
32 using namespace css::uno;
33 
34 namespace apitest
35 {
36 void XNamedRanges::testAddNewByName()
37 {
38  uno::Reference<sheet::XNamedRanges> xNamedRanges(init(), UNO_QUERY_THROW);
39  uno::Reference<container::XNameAccess> xNamedRangesNameAccess(getXNamedRanges(1),
40  UNO_QUERY_THROW);
41 
42  table::CellAddress aBaseAddress(0, 0, 0);
43 
44  sal_Int32 nType = 0;
45  OUString aName1("type_0");
46  xNamedRanges->addNewByName(aName1, "D1", aBaseAddress, nType);
47  CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type 0 - Normal case",
48  xNamedRanges->hasByName(aName1));
49 
50  nType = ::sheet::NamedRangeFlag::COLUMN_HEADER;
51  OUString aName2("type_COLUMN_HEADER");
52  xNamedRanges->addNewByName(aName2, "D2", aBaseAddress, nType);
53  CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type COLUMN_HEADER",
54  xNamedRanges->hasByName(aName2));
55 
56  nType = ::sheet::NamedRangeFlag::FILTER_CRITERIA;
57  OUString aName3("type_FILTER_CRITERIA");
58  xNamedRanges->addNewByName(aName3, "D3", aBaseAddress, nType);
59  CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type FILTER_CRITERIA",
60  xNamedRanges->hasByName(aName3));
61 
62  nType = ::sheet::NamedRangeFlag::PRINT_AREA;
63  OUString aName4("type_PRINT_AREA");
64  xNamedRanges->addNewByName(aName4, "D4", aBaseAddress, nType);
65  CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type PRINT_AREA",
66  xNamedRanges->hasByName(aName4));
67 
68  nType = ::sheet::NamedRangeFlag::ROW_HEADER;
69  OUString aName5("type_ROW_HEADER");
70  xNamedRanges->addNewByName(aName5, "D5", aBaseAddress, nType);
71  CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type ROW_HEADER",
72  xNamedRanges->hasByName(aName5));
73 }
74 
75 void XNamedRanges::testAddNewFromTitles()
76 {
77  uno::Reference<sheet::XNamedRanges> xNamedRanges(getXNamedRanges(1), UNO_QUERY_THROW);
78  uno::Reference<container::XIndexAccess> xNamedRangesIndex(xNamedRanges, UNO_QUERY_THROW);
79  uno::Reference<container::XNameAccess> xNamedRangesNameAccess(xNamedRanges, UNO_QUERY_THROW);
80 
81  table::CellRangeAddress aCellRangeAddress(1, 0, 0, 3, 3);
82 
83  OUString aString;
84  uno::Reference<table::XCell> xCell;
85 
86  xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_TOP);
87 
88  for (sal_Int32 i = 1; i < 4; i++)
89  {
90  // verify namedrange exists
91  xCell = xSheet->getCellByPosition(i, 0);
92  uno::Reference<text::XTextRange> xTextRange(xCell, UNO_QUERY_THROW);
93  aString = xTextRange->getString();
94  std::cout << "addNewFromTitles: verify " << aString << std::endl;
95  CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
96 
97  // verify it points on the right cell
98  uno::Any aNr = xNamedRangesNameAccess->getByName(aString);
99  uno::Reference<sheet::XNamedRange> xNamedRange(aNr, UNO_QUERY_THROW);
100 
101  uno::Reference<sheet::XCellRangeReferrer> xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
102  uno::Reference<sheet::XCellRangeAddressable> xCellRangeAdr(
103  xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
104  table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
105 
106  CPPUNIT_ASSERT_EQUAL(i, cellRangeAddress.EndColumn);
107  CPPUNIT_ASSERT_EQUAL(i, cellRangeAddress.StartColumn);
108  CPPUNIT_ASSERT_EQUAL(sal_Int32(1), cellRangeAddress.StartRow);
109  CPPUNIT_ASSERT_EQUAL(sal_Int32(3), cellRangeAddress.EndRow);
110  CPPUNIT_ASSERT_EQUAL(sal_Int16(1), cellRangeAddress.Sheet);
111  }
112 
113  xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_LEFT);
114 
115  for (sal_Int32 i = 1; i < 4; i++)
116  {
117  // verify namedrange exists
118  xCell = xSheet->getCellByPosition(0, i);
119  uno::Reference<text::XTextRange> xTextRange(xCell, UNO_QUERY_THROW);
120  aString = xTextRange->getString();
121  std::cout << "verify " << aString << std::endl;
122  CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
123 
124  // verify it points on the right cell
125  uno::Any aNr = xNamedRangesNameAccess->getByName(aString);
126  uno::Reference<sheet::XNamedRange> xNamedRange(aNr, UNO_QUERY_THROW);
127 
128  uno::Reference<sheet::XCellRangeReferrer> xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
129  uno::Reference<sheet::XCellRangeAddressable> xCellRangeAdr(
130  xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
131  table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
132 
133  CPPUNIT_ASSERT_EQUAL(i, cellRangeAddress.EndRow);
134  CPPUNIT_ASSERT_EQUAL(i, cellRangeAddress.StartRow);
135  CPPUNIT_ASSERT_EQUAL(sal_Int32(1), cellRangeAddress.StartColumn);
136  CPPUNIT_ASSERT_EQUAL(sal_Int32(3), cellRangeAddress.EndColumn);
137  CPPUNIT_ASSERT_EQUAL(sal_Int16(1), cellRangeAddress.Sheet);
138  }
139 }
140 
141 void XNamedRanges::testRemoveByName()
142 {
143  uno::Reference<sheet::XNamedRanges> xNamedRanges(init(), UNO_QUERY_THROW);
144  uno::Reference<container::XIndexAccess> xIndex(xNamedRanges, UNO_QUERY_THROW);
145 
146  bool bHasIt = xNamedRanges->hasByName(maNameToRemove);
147  CPPUNIT_ASSERT_MESSAGE("NamedRange initial1 does not exist, can't remove it", bHasIt);
148 
149  if (!bHasIt)
150  return;
151 
152  // remove existing
153  sal_Int32 nInitialCount = xIndex->getCount();
154  xNamedRanges->removeByName(maNameToRemove);
155  sal_Int32 nNewCount = xIndex->getCount();
156  CPPUNIT_ASSERT_EQUAL_MESSAGE("NamedRange initial1 not removed", nNewCount, nInitialCount - 1);
157  CPPUNIT_ASSERT_MESSAGE("Wrong NamedRange removed, initial1 still present",
158  !xNamedRanges->hasByName(maNameToRemove));
159  // try to remove non existing
160  xNamedRanges->removeByName("dummyNonExistingNamedRange"); // an exception should be raised here
161 }
162 
163 void XNamedRanges::testOutputList()
164 {
165  table::CellAddress aCellAddress(0, 2, 0);
166  uno::Reference<sheet::XNamedRanges> xNamedRanges(init(), UNO_QUERY_THROW);
167  uno::Reference<container::XIndexAccess> xNamedRangesIndex(init(), UNO_QUERY_THROW);
168  sal_Int32 nElementsCount = xNamedRangesIndex->getCount();
169 
170  xNamedRanges->outputList(aCellAddress);
171 
172  OUString aString;
173  uno::Reference<table::XCell> xCell;
174 
175  for (sal_Int32 i = 0; i < nElementsCount; i++)
176  {
177  xCell = xSheet->getCellByPosition(2, i);
178  uno::Reference<text::XTextRange> xTextRange(xCell, UNO_QUERY_THROW);
179  aString = xTextRange->getString();
180  std::cout << "verify " << aString << std::endl;
181  CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
182  }
183 }
184 
185 } // namespace apitest
186 
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
int i
QPRO_FUNC_TYPE nType
FILE * init(int, char **)