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