LibreOffice Module test (master) 1
xsheetannotations.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
12#include <com/sun/star/table/CellAddress.hpp>
13#include <com/sun/star/container/XIndexAccess.hpp>
14#include <com/sun/star/sheet/XSheetAnnotation.hpp>
15#include <com/sun/star/text/XTextRange.hpp>
16
17#include <cppunit/TestAssert.h>
18#include <rtl/ustring.hxx>
19
20using namespace css;
21using namespace css::uno;
22
23namespace apitest {
24
26{
27 uno::Reference< sheet::XSheetAnnotations > aSheetAnnotations (init(), UNO_QUERY_THROW);
28
29 // count on sheet 1 before inserting
30 uno::Reference< container::XIndexAccess > xAnnotationsIndex (aSheetAnnotations, UNO_QUERY_THROW);
31 sal_Int32 nBefore = xAnnotationsIndex->getCount();
32
33 // get Sheet 2 annotations
34 uno::Reference< sheet::XSheetAnnotations > xSheet2Annotations( getAnnotations(1), UNO_SET_THROW);
35
36 // insert a note on sheet 2
37 table::CellAddress aTargetCellAddress (1,0,0);
38 xSheet2Annotations->insertNew(aTargetCellAddress, "an inserted annotation on sheet 2");
39
40 // count again on sheet 1
41 sal_Int32 nAfter = xAnnotationsIndex->getCount();
42
43 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Annotations count should not change on sheet 1", nBefore, nAfter);
44}
45
47{
48 uno::Reference< sheet::XSheetAnnotations > aSheetAnnotations (init(), UNO_QUERY_THROW);
49
50 // count before inserting
51 uno::Reference< container::XIndexAccess > xAnnotationsIndex (aSheetAnnotations, UNO_QUERY_THROW);
52 sal_Int32 nBefore = xAnnotationsIndex->getCount();
53
54 CPPUNIT_ASSERT_EQUAL_MESSAGE(
55 "There should already be one note", sal_Int32(1), nBefore );
56
57 // insert the annotation
58 table::CellAddress aTargetCellAddress (0,3,4);
59 aSheetAnnotations->insertNew(aTargetCellAddress, "an inserted annotation");
60
61 // count after inserting
62 //uno::Reference< container::XIndexAccess > xAnnotationsIndexAfter (aSheetAnnotations, UNO_QUERY_THROW);
63 sal_Int32 nAfter = xAnnotationsIndex->getCount();
64
65 CPPUNIT_ASSERT_EQUAL_MESSAGE(
66 "Annotations index not updated", nBefore + 1, nAfter);
67
68 // is the position ok ?
69 uno::Reference< sheet::XSheetAnnotation > aLastSheetAnnotation (xAnnotationsIndex->getByIndex(nAfter-1), UNO_QUERY_THROW);
70 table::CellAddress aResultCellAddress = aLastSheetAnnotation->getPosition();
71
72 CPPUNIT_ASSERT_EQUAL_MESSAGE(
73 "Insert Annotation - Wrong SHEET reference position",
74 aTargetCellAddress.Sheet, aResultCellAddress.Sheet);
75 CPPUNIT_ASSERT_EQUAL_MESSAGE(
76 "Insert Annotation - Wrong COLUMN reference position",
77 aTargetCellAddress.Column, aResultCellAddress.Column);
78 CPPUNIT_ASSERT_EQUAL_MESSAGE(
79 "Insert Annotation - Wrong ROW reference position",
80 aTargetCellAddress.Row, aResultCellAddress.Row);
81
82 // is the string ok ?
83 uno::Reference< text::XTextRange > aTextSheetAnnotation(aLastSheetAnnotation, UNO_QUERY_THROW);
84 OUString aString = aTextSheetAnnotation->getString();
85
86 CPPUNIT_ASSERT_EQUAL_MESSAGE(
87 "Insert Annotation - Wrong string", OUString("an inserted annotation"),
88 aString);
89
90}
91
93{
94 uno::Reference< sheet::XSheetAnnotations > aSheetAnnotations (init(), UNO_QUERY_THROW);
95
96 // insert some annotations
97 table::CellAddress aTargetCellAddress (0,4,5);
98 aSheetAnnotations->insertNew(aTargetCellAddress, "an inserted annotation 1");
99 table::CellAddress aToBeRemovedCellAddress (0,5,6);
100 aSheetAnnotations->insertNew(aToBeRemovedCellAddress, "an inserted annotation 2");
101 table::CellAddress aOtherCellAddress (0,7,8);
102 aSheetAnnotations->insertNew(aOtherCellAddress, "an inserted annotation 3");
103
104 // count before removing
105 uno::Reference< container::XIndexAccess > xAnnotationsIndex (aSheetAnnotations, UNO_QUERY_THROW);
106 sal_Int32 nBefore = xAnnotationsIndex->getCount();
107
108 // remove the xToBeRemovedCellAddress
109 aSheetAnnotations->removeByIndex(nBefore-2);
110
111 // count after removing
112 //uno::Reference< container::XIndexAccess > xAnnotationsIndex (aSheetAnnotations, UNO_QUERY_THROW);
113 sal_Int32 nAfter = xAnnotationsIndex->getCount();
114
115 // the last position should be xOtherCellAddress
116 uno::Reference< sheet::XSheetAnnotation > aLastSheetAnnotation (xAnnotationsIndex->getByIndex(nAfter-1), UNO_QUERY_THROW);
117 table::CellAddress aResultCellAddress = aLastSheetAnnotation->getPosition();
118
119 CPPUNIT_ASSERT_EQUAL_MESSAGE(
120 "Remove Annotation - Wrong SHEET reference position",
121 aOtherCellAddress.Sheet, aResultCellAddress.Sheet);
122 CPPUNIT_ASSERT_EQUAL_MESSAGE(
123 "Remove Annotation - Wrong COLUMN reference position",
124 aOtherCellAddress.Column, aResultCellAddress.Column);
125 CPPUNIT_ASSERT_EQUAL_MESSAGE(
126 "Remove Annotation - Wrong ROW reference position",
127 aOtherCellAddress.Row, aResultCellAddress.Row);
128
129 // is the string ok ?
130 uno::Reference< text::XTextRange > aLastTextSheetAnnotation(aLastSheetAnnotation, UNO_QUERY_THROW);
131 OUString aLastString = aLastTextSheetAnnotation->getString();
132
133 CPPUNIT_ASSERT_EQUAL_MESSAGE(
134 "Remove Annotation - Wrong string",
135 OUString("an inserted annotation 3"), aLastString);
136
137 // the previous should be xTargetCellAddress
138 uno::Reference< sheet::XSheetAnnotation > aPreviousSheetAnnotation (xAnnotationsIndex->getByIndex(nAfter-2), UNO_QUERY_THROW);
139 table::CellAddress aPreviousCellAddress = aPreviousSheetAnnotation->getPosition();
140
141 CPPUNIT_ASSERT_EQUAL_MESSAGE(
142 "Remove Annotation - Wrong SHEET reference position",
143 aTargetCellAddress.Sheet, aPreviousCellAddress.Sheet);
144 CPPUNIT_ASSERT_EQUAL_MESSAGE(
145 "Remove Annotation - Wrong COLUMN reference position",
146 aTargetCellAddress.Column, aPreviousCellAddress.Column);
147 CPPUNIT_ASSERT_EQUAL_MESSAGE(
148 "Remove Annotation - Wrong ROW reference position",
149 aTargetCellAddress.Row, aPreviousCellAddress.Row);
150
151 // is the string ok ?
152 uno::Reference< text::XTextRange > aPreviousTextSheetAnnotation(aPreviousSheetAnnotation, UNO_QUERY_THROW);
153 OUString aPreviousString = aPreviousTextSheetAnnotation->getString();
154
155 CPPUNIT_ASSERT_EQUAL_MESSAGE(
156 "Remove Annotation - Wrong string",
157 OUString("an inserted annotation 1"), aPreviousString);
158}
159
161{
162
163 // testing #fdo80551 - getByIndex not on the first sheet
164
165 // insert annotations in first sheet
166 uno::Reference< sheet::XSheetAnnotations > aSheet0Annotations (init(), UNO_QUERY_THROW);
167 table::CellAddress aTargetCellAddress0 (0,0,1);
168 aSheet0Annotations->insertNew(aTargetCellAddress0, "an inserted annotation 1 on sheet 1");
169 table::CellAddress aSecondTargetCellAddress0 (0,0,2);
170 aSheet0Annotations->insertNew(aSecondTargetCellAddress0, "an inserted annotation 2 on sheet 1");
171 table::CellAddress aThirdCellAddress0 (0,0,3);
172 aSheet0Annotations->insertNew(aThirdCellAddress0, "an inserted annotation 3 on sheet 1");
173
174 // insert annotations in third sheet
175 uno::Reference< sheet::XSheetAnnotations > aSheet2Annotations (getAnnotations(2), UNO_SET_THROW);
176 table::CellAddress aTargetCellAddress2 (2,4,5);
177 aSheet2Annotations->insertNew(aTargetCellAddress2, "an inserted annotation 1 on sheet 3");
178 table::CellAddress aSecondTargetCellAddress2 (2,5,6);
179 aSheet2Annotations->insertNew(aSecondTargetCellAddress2, "an inserted annotation 2 on sheet 3");
180 table::CellAddress aThirdCellAddress2 (2,7,8);
181 aSheet2Annotations->insertNew(aThirdCellAddress2, "an inserted annotation 3 on sheet 3");
182
183 // get second annotation for second sheet
184 uno::Reference< sheet::XSheetAnnotations > aSheetAnnotations (getAnnotations(2), UNO_SET_THROW);
185 uno::Reference< container::XIndexAccess > xAnnotationsIndex (aSheetAnnotations, UNO_QUERY_THROW);
186 uno::Reference< sheet::XSheetAnnotation > aAnnotation (xAnnotationsIndex->getByIndex(1), UNO_QUERY_THROW);
187
188 table::CellAddress aToBeAnalyzedCellAddress = aAnnotation->getPosition();
189
190 // is the CellAddress ok ?
191 CPPUNIT_ASSERT_EQUAL_MESSAGE(
192 "GetByIndex Annotation - Wrong SHEET reference position",
193 aSecondTargetCellAddress2.Sheet, aToBeAnalyzedCellAddress.Sheet);
194 CPPUNIT_ASSERT_EQUAL_MESSAGE(
195 "GetByIndex Annotation - Wrong COLUMN reference position",
196 aSecondTargetCellAddress2.Column, aToBeAnalyzedCellAddress.Column);
197 CPPUNIT_ASSERT_EQUAL_MESSAGE(
198 "GetByIndex Annotation - Wrong ROW reference position",
199 aSecondTargetCellAddress2.Row, aToBeAnalyzedCellAddress.Row);
200
201 // is the string ok ?
202 uno::Reference< text::XTextRange > aTextSheetAnnotation(aAnnotation, UNO_QUERY_THROW);
203 OUString aString = aTextSheetAnnotation->getString();
204
205 CPPUNIT_ASSERT_EQUAL_MESSAGE(
206 "GetByIndex Annotation - Wrong string",
207 OUString("an inserted annotation 2 on sheet 3"), aString);
208}
209
210}
211
212/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::sheet::XSheetAnnotations > getAnnotations(tools::Long nIndex)=0
virtual css::uno::Reference< css::uno::XInterface > init()=0