Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2012 Kohei Yoshida <kohei.yoshida@suse.com>
17 : *
18 : * All Rights Reserved.
19 : *
20 : * For minor contributions see the git repository.
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : #include <test/unoapi_test.hxx>
30 : #include <test/beans/xpropertyset.hxx>
31 : #include <test/text/xtextfield.hxx>
32 : #include <test/text/xtextcontent.hxx>
33 :
34 : #include <com/sun/star/beans/XPropertySet.hpp>
35 : #include <com/sun/star/container/XEnumerationAccess.hpp>
36 : #include <com/sun/star/text/XText.hpp>
37 : #include <com/sun/star/text/XTextField.hpp>
38 : #include <com/sun/star/text/XTextFieldsSupplier.hpp>
39 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
40 : #include <com/sun/star/sheet/XSpreadsheet.hpp>
41 :
42 : #define NUMBER_OF_TESTS 7
43 :
44 : namespace sc_apitest {
45 :
46 28 : class ScEditFieldObj_Cell : public UnoApiTest, apitest::XTextField, apitest::XTextContent, apitest::XPropertySet
47 : {
48 : public:
49 : ScEditFieldObj_Cell();
50 :
51 : virtual void setUp();
52 : virtual void tearDown();
53 : virtual uno::Reference<uno::XInterface> init();
54 : virtual uno::Reference<text::XTextContent> getTextContent();
55 : virtual uno::Reference<text::XTextRange> getTextRange();
56 0 : virtual bool isAttachSupported() { return true; }
57 :
58 : void testEditFieldProperties();
59 :
60 4 : CPPUNIT_TEST_SUITE(ScEditFieldObj_Cell);
61 :
62 : // XPropertySet
63 2 : CPPUNIT_TEST(testGetPropertySetInfo);
64 2 : CPPUNIT_TEST(testGetPropertyValue);
65 2 : CPPUNIT_TEST(testSetPropertyValue);
66 :
67 : // XTextField
68 2 : CPPUNIT_TEST(testGetPresentation);
69 :
70 : // XTextContent
71 2 : CPPUNIT_TEST(testGetAnchor);
72 2 : CPPUNIT_TEST(testAttach);
73 :
74 : // Tests specific to this service implementation.
75 2 : CPPUNIT_TEST(testEditFieldProperties);
76 :
77 4 : CPPUNIT_TEST_SUITE_END();
78 :
79 : private:
80 : static sal_Int32 nTest;
81 : static uno::Reference<lang::XComponent> mxComponent;
82 : static uno::Reference<text::XTextField> mxField;
83 : };
84 :
85 : sal_Int32 ScEditFieldObj_Cell::nTest = 0;
86 2 : uno::Reference<lang::XComponent> ScEditFieldObj_Cell::mxComponent;
87 2 : uno::Reference<text::XTextField> ScEditFieldObj_Cell::mxField;
88 :
89 14 : ScEditFieldObj_Cell::ScEditFieldObj_Cell() {}
90 :
91 14 : void ScEditFieldObj_Cell::setUp()
92 : {
93 14 : ++nTest;
94 14 : UnoApiTest::setUp();
95 14 : }
96 :
97 14 : void ScEditFieldObj_Cell::tearDown()
98 : {
99 14 : if (nTest == NUMBER_OF_TESTS)
100 : {
101 2 : mxField.clear();
102 2 : closeDocument(mxComponent);
103 : }
104 :
105 14 : UnoApiTest::tearDown();
106 14 : }
107 :
108 : namespace {
109 :
110 4 : uno::Reference<text::XTextField> getNewField(const uno::Reference<lang::XMultiServiceFactory>& xSM)
111 : {
112 : uno::Reference<text::XTextField> xField(
113 4 : xSM->createInstance("com.sun.star.text.TextField.URL"), UNO_QUERY_THROW);
114 4 : uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
115 4 : xPropSet->setPropertyValue("Representation", uno::makeAny(rtl::OUString("LibreOffice")));
116 4 : xPropSet->setPropertyValue("URL", uno::makeAny(rtl::OUString("http://www.libreoffice.org/")));
117 4 : return xField;
118 : }
119 :
120 : }
121 :
122 38 : uno::Reference<uno::XInterface> ScEditFieldObj_Cell::init()
123 : {
124 : // Return a field that's already in the cell.
125 38 : if (!mxField.is())
126 : {
127 2 : if (!mxComponent.is())
128 : // Load an empty document.
129 2 : mxComponent = loadFromDesktop("private:factory/scalc");
130 :
131 2 : uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
132 :
133 : // Create a new URL field object, and populate it with name and URL.
134 2 : mxField = getNewField(xSM);
135 :
136 : // Insert this field into a cell.
137 2 : uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
138 2 : uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), UNO_QUERY_THROW);
139 2 : uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
140 : // Use cell A1 for this.
141 2 : uno::Reference<table::XCell> xCell = xSheet->getCellByPosition(0, 0);
142 2 : uno::Reference<text::XText> xText(xCell, UNO_QUERY_THROW);
143 :
144 2 : uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
145 2 : uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
146 2 : uno::Reference<text::XTextContent> xContent(mxField, UNO_QUERY_THROW);
147 2 : xText->insertTextContent(xRange, xContent, sal_False);
148 : }
149 38 : return mxField;
150 : }
151 :
152 2 : uno::Reference<text::XTextContent> ScEditFieldObj_Cell::getTextContent()
153 : {
154 : // Return a field object that's not yet inserted.
155 2 : uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
156 2 : return uno::Reference<text::XTextContent>(getNewField(xSM), UNO_QUERY_THROW);
157 : }
158 :
159 2 : uno::Reference<text::XTextRange> ScEditFieldObj_Cell::getTextRange()
160 : {
161 : // Use cell A2 for this.
162 2 : uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
163 2 : uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), UNO_QUERY_THROW);
164 2 : uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
165 2 : uno::Reference<table::XCell> xCell = xSheet->getCellByPosition(0, 1);
166 2 : uno::Reference<text::XText> xText(xCell, UNO_QUERY_THROW);
167 :
168 2 : uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
169 2 : uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
170 2 : return xRange;
171 : }
172 :
173 2 : void ScEditFieldObj_Cell::testEditFieldProperties()
174 : {
175 2 : CPPUNIT_ASSERT_MESSAGE("component doesn't exist.", mxComponent.is());
176 2 : uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
177 :
178 : {
179 : // Test properties of date time field.
180 : uno::Reference<text::XTextField> xField(
181 2 : xSM->createInstance("com.sun.star.text.textfield.DateTime"), UNO_QUERY_THROW);
182 2 : uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
183 :
184 2 : uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
185 2 : CPPUNIT_ASSERT_MESSAGE("failed to retrieve property set info.", xInfo.is());
186 :
187 4 : CPPUNIT_ASSERT_MESSAGE("Calc's date time field should have 'IsFixed' property.",
188 4 : xInfo->hasPropertyByName("IsFixed"));
189 : }
190 :
191 : {
192 : // Test properties of document title field.
193 : uno::Reference<text::XTextField> xField(
194 2 : xSM->createInstance("com.sun.star.text.textfield.docinfo.Title"), UNO_QUERY_THROW);
195 2 : uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
196 :
197 2 : uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
198 2 : CPPUNIT_ASSERT_MESSAGE("failed to retrieve property set info.", xInfo.is());
199 :
200 4 : CPPUNIT_ASSERT_MESSAGE("Calc's title field shouldn't have 'IsFixed' property.",
201 4 : !xInfo->hasPropertyByName("IsFixed"));
202 2 : }
203 2 : }
204 :
205 2 : CPPUNIT_TEST_SUITE_REGISTRATION(ScEditFieldObj_Cell);
206 :
207 2 : CPPUNIT_PLUGIN_IMPLEMENT();
208 :
209 6 : }
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|