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/container/XNameAccess.hpp>
18 : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
19 : #include <com/sun/star/text/XText.hpp>
20 : #include <com/sun/star/text/XTextField.hpp>
21 : #include <com/sun/star/text/XTextFieldsSupplier.hpp>
22 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
23 : #include <com/sun/star/sheet/XSpreadsheet.hpp>
24 : #include <com/sun/star/sheet/XHeaderFooterContent.hpp>
25 :
26 : #define NUMBER_OF_TESTS 5
27 :
28 : using namespace css;
29 : using namespace css::uno;
30 :
31 : namespace sc_apitest {
32 :
33 20 : class ScEditFieldObj_Header : public CalcUnoApiTest, apitest::XTextContent, apitest::XPropertySet
34 : {
35 : public:
36 : ScEditFieldObj_Header();
37 :
38 : virtual void setUp() SAL_OVERRIDE;
39 : virtual void tearDown() SAL_OVERRIDE;
40 : virtual uno::Reference<uno::XInterface> init() SAL_OVERRIDE;
41 : virtual uno::Reference<text::XTextContent> getTextContent() SAL_OVERRIDE;
42 : virtual uno::Reference<text::XTextRange> getTextRange() SAL_OVERRIDE;
43 2 : virtual bool isAttachSupported() SAL_OVERRIDE { return false; }
44 :
45 4 : CPPUNIT_TEST_SUITE(ScEditFieldObj_Header);
46 :
47 : // XPropertySet
48 2 : CPPUNIT_TEST(testGetPropertySetInfo);
49 2 : CPPUNIT_TEST(testGetPropertyValue);
50 2 : CPPUNIT_TEST(testSetPropertyValue);
51 :
52 : // XTextContent
53 2 : CPPUNIT_TEST(testGetAnchor);
54 2 : CPPUNIT_TEST(testAttach);
55 4 : CPPUNIT_TEST_SUITE_END();
56 :
57 : private:
58 : static sal_Int32 nTest;
59 : static uno::Reference<lang::XComponent> mxComponent;
60 : static uno::Reference<text::XTextField> mxField;
61 : static uno::Reference<text::XText> mxRightText;
62 : };
63 :
64 : sal_Int32 ScEditFieldObj_Header::nTest = 0;
65 2 : uno::Reference<lang::XComponent> ScEditFieldObj_Header::mxComponent;
66 2 : uno::Reference<text::XTextField> ScEditFieldObj_Header::mxField;
67 2 : uno::Reference<text::XText> ScEditFieldObj_Header::mxRightText;
68 :
69 10 : ScEditFieldObj_Header::ScEditFieldObj_Header()
70 10 : : CalcUnoApiTest("/sc/qa/extras/testdocuments")
71 : {
72 10 : }
73 :
74 10 : void ScEditFieldObj_Header::setUp()
75 : {
76 10 : ++nTest;
77 10 : CalcUnoApiTest::setUp();
78 10 : }
79 :
80 10 : void ScEditFieldObj_Header::tearDown()
81 : {
82 10 : if (nTest == NUMBER_OF_TESTS)
83 : {
84 : // Clear these before the component is destroyed. This is important!
85 2 : mxField.clear();
86 2 : mxRightText.clear();
87 2 : closeDocument(mxComponent);
88 2 : mxComponent.clear();
89 : }
90 :
91 10 : CalcUnoApiTest::tearDown();
92 10 : }
93 :
94 36 : uno::Reference<uno::XInterface> ScEditFieldObj_Header::init()
95 : {
96 : // Return a field that's already in the header.
97 36 : if (!mxField.is())
98 : {
99 2 : if (!mxComponent.is())
100 : // Load an empty document.
101 2 : mxComponent = loadFromDesktop("private:factory/scalc");
102 :
103 2 : uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
104 :
105 : // Create a new URL field object, and populate it with name and URL.
106 2 : mxField.set(xSM->createInstance("com.sun.star.text.TextField.Time"), UNO_QUERY_THROW);
107 :
108 4 : uno::Reference<style::XStyleFamiliesSupplier> xSFS(mxComponent, UNO_QUERY_THROW);
109 4 : uno::Reference<container::XNameAccess> xStyleFamilies(xSFS->getStyleFamilies(), UNO_QUERY_THROW);
110 4 : uno::Reference<container::XNameAccess> xPageStyles(xStyleFamilies->getByName("PageStyles"), UNO_QUERY_THROW);
111 4 : uno::Reference<beans::XPropertySet> xPropSet(xPageStyles->getByName("Default"), UNO_QUERY_THROW);
112 :
113 : uno::Reference<sheet::XHeaderFooterContent> xHeaderContent(
114 4 : xPropSet->getPropertyValue("RightPageHeaderContent"), UNO_QUERY_THROW);
115 :
116 : // Use the left header text.
117 4 : uno::Reference<text::XText> xText = xHeaderContent->getLeftText();
118 4 : uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
119 4 : uno::Reference<text::XTextRange> xRange(xCursor, UNO_QUERY_THROW);
120 4 : uno::Reference<text::XTextContent> xContent(mxField, UNO_QUERY_THROW);
121 2 : xText->insertTextContent(xRange, xContent, sal_False);
122 :
123 2 : xPropSet->setPropertyValue("RightPageHeaderContent", uno::makeAny(xHeaderContent));
124 :
125 4 : mxRightText = xHeaderContent->getRightText();
126 : }
127 :
128 36 : return mxField;
129 : }
130 :
131 2 : uno::Reference<text::XTextContent> ScEditFieldObj_Header::getTextContent()
132 : {
133 : // Return a field object that's not yet inserted.
134 2 : uno::Reference<lang::XMultiServiceFactory> xSM(mxComponent, UNO_QUERY_THROW);
135 : uno::Reference<text::XTextContent> xField(
136 2 : xSM->createInstance("com.sun.star.text.TextField.Date"), UNO_QUERY_THROW);
137 2 : return xField;
138 : }
139 :
140 2 : uno::Reference<text::XTextRange> ScEditFieldObj_Header::getTextRange()
141 : {
142 : // Use the right header text for this.
143 2 : uno::Reference<text::XTextRange> xRange(mxRightText, UNO_QUERY_THROW);
144 2 : return xRange;
145 : }
146 :
147 2 : CPPUNIT_TEST_SUITE_REGISTRATION(ScEditFieldObj_Header);
148 :
149 : }
150 :
151 8 : CPPUNIT_PLUGIN_IMPLEMENT();
152 :
153 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|