bump product version to 4.1.6.2
[LibreOffice.git] / sc / qa / extras / sceditfieldobj-cell.cxx
blob909b1c69062c72b7a86556af73d140eba9a5c4f2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <test/unoapi_test.hxx>
11 #include <test/beans/xpropertyset.hxx>
12 #include <test/text/xtextfield.hxx>
13 #include <test/text/xtextcontent.hxx>
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>
23 #define NUMBER_OF_TESTS 7
25 using namespace css;
26 using namespace css::uno;
28 namespace sc_apitest {
30 class ScEditFieldObj_Cell : public UnoApiTest, apitest::XTextField, apitest::XTextContent, apitest::XPropertySet
32 public:
33 ScEditFieldObj_Cell();
35 virtual void setUp();
36 virtual void tearDown();
37 virtual uno::Reference<uno::XInterface> init();
38 virtual uno::Reference<text::XTextContent> getTextContent();
39 virtual uno::Reference<text::XTextRange> getTextRange();
40 virtual bool isAttachSupported() { return true; }
42 void testEditFieldProperties();
44 CPPUNIT_TEST_SUITE(ScEditFieldObj_Cell);
46 // XPropertySet
47 CPPUNIT_TEST(testGetPropertySetInfo);
48 CPPUNIT_TEST(testGetPropertyValue);
49 CPPUNIT_TEST(testSetPropertyValue);
51 // XTextField
52 CPPUNIT_TEST(testGetPresentation);
54 // XTextContent
55 CPPUNIT_TEST(testGetAnchor);
56 CPPUNIT_TEST(testAttach);
58 // Tests specific to this service implementation.
59 CPPUNIT_TEST(testEditFieldProperties);
61 CPPUNIT_TEST_SUITE_END();
63 private:
64 static sal_Int32 nTest;
65 static uno::Reference<lang::XComponent> mxComponent;
66 static uno::Reference<text::XTextField> mxField;
69 sal_Int32 ScEditFieldObj_Cell::nTest = 0;
70 uno::Reference<lang::XComponent> ScEditFieldObj_Cell::mxComponent;
71 uno::Reference<text::XTextField> ScEditFieldObj_Cell::mxField;
73 ScEditFieldObj_Cell::ScEditFieldObj_Cell()
74 : UnoApiTest("/sc/qa/extras/testdocuments")
78 void ScEditFieldObj_Cell::setUp()
80 ++nTest;
81 UnoApiTest::setUp();
84 void ScEditFieldObj_Cell::tearDown()
86 if (nTest == NUMBER_OF_TESTS)
88 mxField.clear();
89 closeDocument(mxComponent);
92 UnoApiTest::tearDown();
95 namespace {
97 uno::Reference<text::XTextField> getNewField(const uno::Reference<lang::XMultiServiceFactory>& xSM)
99 uno::Reference<text::XTextField> xField(
100 xSM->createInstance("com.sun.star.text.TextField.URL"), UNO_QUERY_THROW);
101 uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
102 xPropSet->setPropertyValue("Representation", uno::makeAny(OUString("LibreOffice")));
103 xPropSet->setPropertyValue("URL", uno::makeAny(OUString("http://www.libreoffice.org/")));
104 return xField;
109 uno::Reference<uno::XInterface> ScEditFieldObj_Cell::init()
111 // Return a field that's already in the cell.
112 if (!mxField.is())
114 if (!mxComponent.is())
115 // Load an empty document.
116 mxComponent = loadFromDesktop("private:factory/scalc");
118 uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
120 // Create a new URL field object, and populate it with name and URL.
121 mxField = getNewField(xSM);
123 // Insert this field into a cell.
124 uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
125 uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), UNO_QUERY_THROW);
126 uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
127 // Use cell A1 for this.
128 uno::Reference<table::XCell> xCell = xSheet->getCellByPosition(0, 0);
129 uno::Reference<text::XText> xText(xCell, UNO_QUERY_THROW);
131 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
132 uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
133 uno::Reference<text::XTextContent> xContent(mxField, UNO_QUERY_THROW);
134 xText->insertTextContent(xRange, xContent, sal_False);
136 return mxField;
139 uno::Reference<text::XTextContent> ScEditFieldObj_Cell::getTextContent()
141 // Return a field object that's not yet inserted.
142 uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
143 return uno::Reference<text::XTextContent>(getNewField(xSM), UNO_QUERY_THROW);
146 uno::Reference<text::XTextRange> ScEditFieldObj_Cell::getTextRange()
148 // Use cell A2 for this.
149 uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, UNO_QUERY_THROW);
150 uno::Reference<container::XIndexAccess> xIA(xDoc->getSheets(), UNO_QUERY_THROW);
151 uno::Reference<sheet::XSpreadsheet> xSheet(xIA->getByIndex(0), UNO_QUERY_THROW);
152 uno::Reference<table::XCell> xCell = xSheet->getCellByPosition(0, 1);
153 uno::Reference<text::XText> xText(xCell, UNO_QUERY_THROW);
155 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
156 uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
157 return xRange;
160 void ScEditFieldObj_Cell::testEditFieldProperties()
162 CPPUNIT_ASSERT_MESSAGE("component doesn't exist.", mxComponent.is());
163 uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
166 // Test properties of date time field.
167 uno::Reference<text::XTextField> xField(
168 xSM->createInstance("com.sun.star.text.textfield.DateTime"), UNO_QUERY_THROW);
169 uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
171 uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
172 CPPUNIT_ASSERT_MESSAGE("failed to retrieve property set info.", xInfo.is());
174 CPPUNIT_ASSERT_MESSAGE("Calc's date time field should have 'IsFixed' property.",
175 xInfo->hasPropertyByName("IsFixed"));
179 // Test properties of document title field.
180 uno::Reference<text::XTextField> xField(
181 xSM->createInstance("com.sun.star.text.textfield.docinfo.Title"), UNO_QUERY_THROW);
182 uno::Reference<beans::XPropertySet> xPropSet(xField, UNO_QUERY_THROW);
184 uno::Reference<beans::XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
185 CPPUNIT_ASSERT_MESSAGE("failed to retrieve property set info.", xInfo.is());
187 CPPUNIT_ASSERT_MESSAGE("Calc's title field shouldn't have 'IsFixed' property.",
188 !xInfo->hasPropertyByName("IsFixed"));
192 CPPUNIT_TEST_SUITE_REGISTRATION(ScEditFieldObj_Cell);
194 CPPUNIT_PLUGIN_IMPLEMENT();
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */