Line data Source code
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 :
10 : #include <test/sheet/xsheetannotations.hxx>
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/extensions/HelperMacros.h"
18 : #include <rtl/ustring.hxx>
19 :
20 : using namespace css;
21 : using namespace css::uno;
22 :
23 : namespace apitest {
24 :
25 1 : void XSheetAnnotations::testInsertNew()
26 : {
27 1 : uno::Reference< sheet::XSheetAnnotations > aSheetAnnotations (init(), UNO_QUERY_THROW);
28 :
29 : // count before inserting
30 2 : uno::Reference< container::XIndexAccess > xAnnotationsIndex (aSheetAnnotations, UNO_QUERY_THROW);
31 1 : sal_Int32 nBefore = xAnnotationsIndex->getCount();
32 :
33 : // insert the annotation
34 1 : table::CellAddress xTargetCellAddress (0,3,4);
35 1 : aSheetAnnotations->insertNew(xTargetCellAddress, "an inserted annotation");
36 :
37 : // count after inserting
38 : //uno::Reference< container::XIndexAccess > xAnnotationsIndexAfter (aSheetAnnotations, UNO_QUERY_THROW);
39 1 : sal_Int32 nAfter = xAnnotationsIndex->getCount();
40 :
41 1 : CPPUNIT_ASSERT_MESSAGE("Annotations index not updated", nAfter == nBefore + 1);
42 :
43 : // is the position ok ?
44 2 : uno::Reference< sheet::XSheetAnnotation > aLastSheetAnnotation (xAnnotationsIndex->getByIndex(nAfter-1), UNO_QUERY_THROW);
45 1 : table::CellAddress xResultCellAddress = aLastSheetAnnotation->getPosition();
46 :
47 1 : CPPUNIT_ASSERT_MESSAGE("Insert Annotation - Wrong SHEET reference position", xResultCellAddress.Sheet == xTargetCellAddress.Sheet);
48 1 : CPPUNIT_ASSERT_MESSAGE("Insert Annotation - Wrong COLUMN reference position", xResultCellAddress.Column == xTargetCellAddress.Column);
49 1 : CPPUNIT_ASSERT_MESSAGE("Insert Annotation - Wrong ROW reference position", xResultCellAddress.Row == xTargetCellAddress.Row);
50 :
51 : // is the string ok ?
52 2 : uno::Reference< text::XTextRange > aTextSheetAnnotation(aLastSheetAnnotation, UNO_QUERY_THROW);
53 2 : OUString aString = aTextSheetAnnotation->getString();
54 :
55 2 : CPPUNIT_ASSERT_MESSAGE("Insert Annotation - Wrong string", aString == "an inserted annotation");
56 :
57 1 : }
58 :
59 1 : void XSheetAnnotations::testRemoveByIndex()
60 : {
61 1 : uno::Reference< sheet::XSheetAnnotations > aSheetAnnotations (init(), UNO_QUERY_THROW);
62 :
63 : // insert some annotations
64 1 : table::CellAddress xTargetCellAddress (0,4,5);
65 1 : aSheetAnnotations->insertNew(xTargetCellAddress, "an inserted annotation 1");
66 1 : table::CellAddress xToBeRemovedCellAddress (0,5,6);
67 1 : aSheetAnnotations->insertNew(xToBeRemovedCellAddress, "an inserted annotation 2");
68 1 : table::CellAddress xOtherCellAddress (0,7,8);
69 1 : aSheetAnnotations->insertNew(xOtherCellAddress, "an inserted annotation 3");
70 :
71 : // count before removing
72 2 : uno::Reference< container::XIndexAccess > xAnnotationsIndex (aSheetAnnotations, UNO_QUERY_THROW);
73 1 : sal_Int32 nBefore = xAnnotationsIndex->getCount();
74 :
75 : // remove the xToBeRemovedCellAddress
76 1 : aSheetAnnotations->removeByIndex(nBefore-2);
77 :
78 : // count after removing
79 : //uno::Reference< container::XIndexAccess > xAnnotationsIndex (aSheetAnnotations, UNO_QUERY_THROW);
80 1 : sal_Int32 nAfter = xAnnotationsIndex->getCount();
81 :
82 : // the last position should be xOtherCellAddress
83 2 : uno::Reference< sheet::XSheetAnnotation > aLastSheetAnnotation (xAnnotationsIndex->getByIndex(nAfter-1), UNO_QUERY_THROW);
84 1 : table::CellAddress xResultCellAddress = aLastSheetAnnotation->getPosition();
85 :
86 1 : CPPUNIT_ASSERT_MESSAGE("Remove Annotation - Wrong SHEET reference position", xResultCellAddress.Sheet == xOtherCellAddress.Sheet);
87 1 : CPPUNIT_ASSERT_MESSAGE("Remove Annotation - Wrong COLUMN reference position", xResultCellAddress.Column == xOtherCellAddress.Column);
88 1 : CPPUNIT_ASSERT_MESSAGE("Remove Annotation - Wrong ROW reference position", xResultCellAddress.Row == xOtherCellAddress.Row);
89 :
90 : // is the string ok ?
91 2 : uno::Reference< text::XTextRange > aLastTextSheetAnnotation(aLastSheetAnnotation, UNO_QUERY_THROW);
92 2 : OUString aLastString = aLastTextSheetAnnotation->getString();
93 :
94 1 : CPPUNIT_ASSERT_MESSAGE("Remove Annotation - Wrong string", aLastString == "an inserted annotation 3");
95 :
96 : // the previous should be xTargetCellAddress
97 2 : uno::Reference< sheet::XSheetAnnotation > aPreviousSheetAnnotation (xAnnotationsIndex->getByIndex(nAfter-2), UNO_QUERY_THROW);
98 1 : table::CellAddress xPreviousCellAddress = aPreviousSheetAnnotation->getPosition();
99 :
100 1 : CPPUNIT_ASSERT_MESSAGE("Remove Annotation - Wrong SHEET reference position", xPreviousCellAddress.Sheet == xTargetCellAddress.Sheet);
101 1 : CPPUNIT_ASSERT_MESSAGE("Remove Annotation - Wrong COLUMN reference position", xPreviousCellAddress.Column == xTargetCellAddress.Column);
102 1 : CPPUNIT_ASSERT_MESSAGE("Remove Annotation - Wrong ROW reference position", xPreviousCellAddress.Row == xTargetCellAddress.Row);
103 :
104 : // is the string ok ?
105 2 : uno::Reference< text::XTextRange > aPreviousTextSheetAnnotation(aPreviousSheetAnnotation, UNO_QUERY_THROW);
106 2 : OUString aPreviousString = aPreviousTextSheetAnnotation->getString();
107 :
108 2 : CPPUNIT_ASSERT_MESSAGE("Remove Annotation - Wrong string", aPreviousString == "an inserted annotation 1");
109 :
110 1 : }
111 :
112 45 : }
113 :
114 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|