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/calc_unoapi_test.hxx>
11 : #include <test/beans/xpropertyset.hxx>
12 : #include <test/text/xtextfield.hxx>
13 : #include <test/text/xtextcontent.hxx>
14 :
15 : #include <com/sun/star/beans/XPropertySet.hpp>
16 : #include <com/sun/star/container/XEnumerationAccess.hpp>
17 : #include <com/sun/star/text/XText.hpp>
18 : #include <com/sun/star/text/XTextField.hpp>
19 : #include <com/sun/star/text/XTextFieldsSupplier.hpp>
20 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
21 : #include <com/sun/star/sheet/XSpreadsheet.hpp>
22 :
23 : #define NUMBER_OF_TESTS 7
24 :
25 : using namespace css;
26 : using namespace css::uno;
27 :
28 : namespace sc_apitest {
29 :
30 28 : class ScEditFieldObj_Cell : public CalcUnoApiTest, apitest::XTextField, apitest::XTextContent, apitest::XPropertySet
31 : {
32 : public:
33 : ScEditFieldObj_Cell();
34 :
35 : virtual void setUp() SAL_OVERRIDE;
36 : virtual void tearDown() SAL_OVERRIDE;
37 : virtual uno::Reference<uno::XInterface> init() SAL_OVERRIDE;
38 : virtual uno::Reference<text::XTextContent> getTextContent() SAL_OVERRIDE;
39 : virtual uno::Reference<text::XTextRange> getTextRange() SAL_OVERRIDE;
40 0 : virtual bool isAttachSupported() SAL_OVERRIDE { return true; }
41 :
42 : void testEditFieldProperties();
43 :
44 4 : CPPUNIT_TEST_SUITE(ScEditFieldObj_Cell);
45 :
46 : // XPropertySet
47 2 : CPPUNIT_TEST(testGetPropertySetInfo);
48 2 : CPPUNIT_TEST(testGetPropertyValue);
49 2 : CPPUNIT_TEST(testSetPropertyValue);
50 :
51 : // XTextField
52 2 : CPPUNIT_TEST(testGetPresentation);
53 :
54 : // XTextContent
55 2 : CPPUNIT_TEST(testGetAnchor);
56 2 : CPPUNIT_TEST(testAttach);
57 :
58 : // Tests specific to this service implementation.
59 2 : CPPUNIT_TEST(testEditFieldProperties);
60 :
61 4 : CPPUNIT_TEST_SUITE_END();
62 :
63 : private:
64 : static sal_Int32 nTest;
65 : static uno::Reference<lang::XComponent> mxComponent;
66 : static uno::Reference<text::XTextField> mxField;
67 : };
68 :
69 : sal_Int32 ScEditFieldObj_Cell::nTest = 0;
70 2 : uno::Reference<lang::XComponent> ScEditFieldObj_Cell::mxComponent;
71 2 : uno::Reference<text::XTextField> ScEditFieldObj_Cell::mxField;
72 :
73 14 : ScEditFieldObj_Cell::ScEditFieldObj_Cell()
74 14 : : CalcUnoApiTest("/sc/qa/extras/testdocuments")
75 : {
76 14 : }
77 :
78 14 : void ScEditFieldObj_Cell::setUp()
79 : {
80 14 : ++nTest;
81 14 : CalcUnoApiTest::setUp();
82 14 : }
83 :
84 14 : void ScEditFieldObj_Cell::tearDown()
85 : {
86 14 : if (nTest == NUMBER_OF_TESTS)
87 : {
88 2 : mxField.clear();
89 2 : closeDocument(mxComponent);
90 2 : mxComponent.clear();
91 : }
92 :
93 14 : CalcUnoApiTest::tearDown();
94 14 : }
95 :
96 : namespace {
97 :
98 4 : uno::Reference<text::XTextField> getNewField(const uno::Reference<lang::XMultiServiceFactory>& xSM)
99 : {
100 : uno::Reference<text::XTextField> xField(
101 4 : xSM->createInstance("com.sun.star.text.TextField.URL"), UNO_QUERY_THROW);
102 8 : uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
103 4 : xPropSet->setPropertyValue("Representation", uno::makeAny(OUString("LibreOffice")));
104 4 : xPropSet->setPropertyValue("URL", uno::makeAny(OUString("http://www.libreoffice.org/")));
105 8 : return xField;
106 : }
107 :
108 : }
109 :
110 38 : uno::Reference<uno::XInterface> ScEditFieldObj_Cell::init()
111 : {
112 : // Return a field that's already in the cell.
113 38 : if (!mxField.is())
114 : {
115 2 : if (!mxComponent.is())
116 : // Load an empty document.
117 2 : mxComponent = loadFromDesktop("private:factory/scalc");
118 :
119 2 : uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
120 :
121 : // Create a new URL field object, and populate it with name and URL.
122 2 : mxField = getNewField(xSM);
123 :
124 : // Insert this field into a cell.
125 4 : uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
126 4 : uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), UNO_QUERY_THROW);
127 4 : uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
128 : // Use cell A1 for this.
129 4 : uno::Reference<table::XCell> xCell = xSheet->getCellByPosition(0, 0);
130 4 : uno::Reference<text::XText> xText(xCell, UNO_QUERY_THROW);
131 :
132 4 : uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
133 4 : uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
134 4 : uno::Reference<text::XTextContent> xContent(mxField, UNO_QUERY_THROW);
135 4 : xText->insertTextContent(xRange, xContent, sal_False);
136 : }
137 38 : return mxField;
138 : }
139 :
140 2 : uno::Reference<text::XTextContent> ScEditFieldObj_Cell::getTextContent()
141 : {
142 : // Return a field object that's not yet inserted.
143 2 : uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
144 2 : return uno::Reference<text::XTextContent>(getNewField(xSM), UNO_QUERY_THROW);
145 : }
146 :
147 2 : uno::Reference<text::XTextRange> ScEditFieldObj_Cell::getTextRange()
148 : {
149 : // Use cell A2 for this.
150 2 : uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
151 4 : uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), UNO_QUERY_THROW);
152 4 : uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
153 4 : uno::Reference<table::XCell> xCell = xSheet->getCellByPosition(0, 1);
154 4 : uno::Reference<text::XText> xText(xCell, UNO_QUERY_THROW);
155 :
156 4 : uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
157 2 : uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
158 4 : return xRange;
159 : }
160 :
161 2 : void ScEditFieldObj_Cell::testEditFieldProperties()
162 : {
163 2 : CPPUNIT_ASSERT_MESSAGE("component doesn't exist.", mxComponent.is());
164 2 : uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
165 :
166 : {
167 : // Test properties of date time field.
168 : uno::Reference<text::XTextField> xField(
169 2 : xSM->createInstance("com.sun.star.text.textfield.DateTime"), UNO_QUERY_THROW);
170 4 : uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
171 :
172 4 : uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
173 2 : CPPUNIT_ASSERT_MESSAGE("failed to retrieve property set info.", xInfo.is());
174 :
175 4 : CPPUNIT_ASSERT_MESSAGE("Calc's date time field should have 'IsFixed' property.",
176 4 : xInfo->hasPropertyByName("IsFixed"));
177 : }
178 :
179 : {
180 : // Test properties of document title field.
181 : uno::Reference<text::XTextField> xField(
182 2 : xSM->createInstance("com.sun.star.text.textfield.docinfo.Title"), UNO_QUERY_THROW);
183 4 : uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
184 :
185 4 : uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
186 2 : CPPUNIT_ASSERT_MESSAGE("failed to retrieve property set info.", xInfo.is());
187 :
188 4 : CPPUNIT_ASSERT_MESSAGE("Calc's title field shouldn't have 'IsFixed' property.",
189 4 : !xInfo->hasPropertyByName("IsFixed"));
190 2 : }
191 2 : }
192 :
193 2 : CPPUNIT_TEST_SUITE_REGISTRATION(ScEditFieldObj_Cell);
194 :
195 : }
196 :
197 8 : CPPUNIT_PLUGIN_IMPLEMENT();
198 :
199 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|