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 : #include "basictest.hxx"
10 : #include <osl/file.hxx>
11 : #include <osl/process.h>
12 :
13 : #include <basic/sbmod.hxx>
14 : #include <basic/sbmeth.hxx>
15 : #include <com/sun/star/awt/WindowDescriptor.hpp>
16 : #include <com/sun/star/table/TableBorder.hpp>
17 : #include <basic/sbuno.hxx>
18 :
19 : namespace
20 : {
21 : using namespace com::sun::star;
22 36 : class Nested_Struct : public test::BootstrapFixture
23 : {
24 : public:
25 18 : Nested_Struct(): BootstrapFixture(true, false) {};
26 : void testAssign1();
27 : void testAssign1Alt(); // result is uno-ised and tested
28 : void testOldAssign();
29 : void testOldAssignAlt(); // result is uno-ised and tested
30 : void testUnfixedVarAssign();
31 : void testUnfixedVarAssignAlt(); // result is uno-ised and tested
32 : void testFixedVarAssign();
33 : void testFixedVarAssignAlt(); // result is uno-ised and tested
34 : void testUnoAccess(); // fdo#60117 specific test
35 :
36 : // Adds code needed to register the test suite
37 4 : CPPUNIT_TEST_SUITE(Nested_Struct);
38 :
39 : // Declares the method as a test to call
40 2 : CPPUNIT_TEST(testAssign1);
41 2 : CPPUNIT_TEST(testAssign1Alt);
42 2 : CPPUNIT_TEST(testOldAssign);
43 2 : CPPUNIT_TEST(testOldAssignAlt);
44 2 : CPPUNIT_TEST(testUnfixedVarAssign);
45 2 : CPPUNIT_TEST(testUnfixedVarAssignAlt);
46 2 : CPPUNIT_TEST(testFixedVarAssign);
47 2 : CPPUNIT_TEST(testFixedVarAssignAlt);
48 2 : CPPUNIT_TEST(testUnoAccess);
49 :
50 : // End of test suite definition
51 4 : CPPUNIT_TEST_SUITE_END();
52 : };
53 :
54 : // tests the new behaviour, we should be able to
55 : // directly modify the value of the nested 'HorizontalLine' struct
56 4 : OUString sTestSource1(
57 : "Function doUnitTest() as Integer\n"
58 : "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
59 : "b0.HorizontalLine.OuterLineWidth = 9\n"
60 : "doUnitTest = b0.HorizontalLine.OuterLineWidth\n"
61 : "End Function\n"
62 2 : );
63 :
64 4 : OUString sTestSource1Alt(
65 : "Function doUnitTest() as Object\n"
66 : "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
67 : "b0.HorizontalLine.OuterLineWidth = 9\n"
68 : "doUnitTest = b0\n"
69 : "End Function\n"
70 2 : );
71 :
72 : // tests the old behaviour, we should still be able
73 : // to use the old workaround of
74 : // a) creating a new instance BorderLine,
75 : // b) cloning the new instance with the value of b0.HorizontalLine
76 : // c) modifying the new instance
77 : // d) setting b0.HorizontalLine with the value of the new instance
78 4 : OUString sTestSource2(
79 : "Function doUnitTest()\n"
80 : "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
81 : "l = b0.HorizontalLine\n"
82 : "l.OuterLineWidth = 9\n"
83 : "b0.HorizontalLine = l\n"
84 : "doUnitTest = b0.HorizontalLine.OuterLineWidth\n"
85 : "End Function\n"
86 2 : );
87 :
88 4 : OUString sTestSource2Alt(
89 : "Function doUnitTest()\n"
90 : "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
91 : "l = b0.HorizontalLine\n"
92 : "l.OuterLineWidth = 9\n"
93 : "b0.HorizontalLine = l\n"
94 : "doUnitTest = b0\n"
95 : "End Function\n"
96 2 : );
97 : // it should be legal to assign a variant to a struct ( and copy by val )
98 : // make sure we aren't copying by reference, we make sure that l is not
99 : // a reference copy of b0.HorizontalLine, each one should have an
100 : // OuterLineWidth of 4 & 9 respectively and we should be returning
101 : // 13 the sum of the two ( hopefully unique values if we haven't copied by reference )
102 4 : OUString sTestSource3(
103 : "Function doUnitTest()\n"
104 : "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
105 : "l = b0.HorizontalLine\n"
106 : "l.OuterLineWidth = 9\n"
107 : "b0.HorizontalLine = l\n"
108 : "l.OuterLineWidth = 4\n"
109 : "doUnitTest = b0.HorizontalLine.OuterLineWidth + l.OuterLineWidth\n"
110 : "End Function\n"
111 2 : );
112 :
113 4 : OUString sTestSource3Alt(
114 : "Function doUnitTest()\n"
115 : "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
116 : "l = b0.HorizontalLine\n"
117 : "l.OuterLineWidth = 9\n"
118 : "b0.HorizontalLine = l\n"
119 : "l.OuterLineWidth = 4\n"
120 : "Dim result(1)\n"
121 : "result(0) = b0\n"
122 : "result(1) = l\n"
123 : "doUnitTest = result\n"
124 : "End Function\n"
125 2 : );
126 :
127 : // nearly the same as above but this time for a fixed type
128 : // variable
129 4 : OUString sTestSource4(
130 : "Function doUnitTest()\n"
131 : "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
132 : "l = b0.HorizontalLine\n"
133 : "l.OuterLineWidth = 9\n"
134 : "b0.HorizontalLine = l\n"
135 : "l.OuterLineWidth = 4\n"
136 : "doUnitTest = b0.HorizontalLine.OuterLineWidth + l.OuterLineWidth\n"
137 : "End Function\n"
138 2 : );
139 :
140 4 : OUString sTestSource4Alt(
141 : "Function doUnitTest()\n"
142 : "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
143 : "l = b0.HorizontalLine\n"
144 : "l.OuterLineWidth = 9\n"
145 : "b0.HorizontalLine = l\n"
146 : "l.OuterLineWidth = 4\n"
147 : "Dim result(1)\n"
148 : "result(0) = b0\n"
149 : "result(1) = l\n"
150 : "doUnitTest = result\n"
151 : "End Function\n"
152 2 : );
153 :
154 : // Although basic might appear to correctly change nested struct elements
155 : // fdo#60117 shows that basic can be fooled ( and even the watch(ed) variable
156 : // in the debugger shows the expected values )
157 : // We need to additionally check the actual uno struct to see if the
158 : // changes made are *really* reflected in the object
159 4 : OUString sTestSource5(
160 : "Function doUnitTest() as Object\n"
161 : "Dim aWinDesc as new \"com.sun.star.awt.WindowDescriptor\"\n"
162 : "Dim aRect as new \"com.sun.star.awt.Rectangle\"\n"
163 : "aRect.X = 200\n"
164 : "aWinDesc.Bounds = aRect\n"
165 : "doUnitTest = aWinDesc\n"
166 : "End Function\n"
167 2 : );
168 :
169 :
170 2 : void Nested_Struct::testAssign1()
171 : {
172 2 : MacroSnippet myMacro( sTestSource1 );
173 2 : myMacro.Compile();
174 2 : CPPUNIT_ASSERT_MESSAGE("testAssign1 fails with compile error",!myMacro.HasError() );
175 4 : SbxVariableRef pNew = myMacro.Run();
176 4 : CPPUNIT_ASSERT(pNew->GetInteger() == 9 );
177 2 : }
178 :
179 2 : void Nested_Struct::testAssign1Alt()
180 : {
181 2 : MacroSnippet myMacro( sTestSource1Alt );
182 2 : myMacro.Compile();
183 2 : CPPUNIT_ASSERT_MESSAGE("testAssign1Alt fails with compile error",!myMacro.HasError() );
184 4 : SbxVariableRef pNew = myMacro.Run();
185 4 : uno::Any aRet = sbxToUnoValue( pNew );
186 2 : table::TableBorder aBorder;
187 2 : aRet >>= aBorder;
188 :
189 2 : int result = aBorder.HorizontalLine.OuterLineWidth;
190 4 : CPPUNIT_ASSERT_EQUAL( 9, result );
191 2 : }
192 :
193 2 : void Nested_Struct::testOldAssign()
194 : {
195 2 : MacroSnippet myMacro( sTestSource2 );
196 2 : myMacro.Compile();
197 2 : CPPUNIT_ASSERT_MESSAGE("testOldAssign fails with compile error",!myMacro.HasError() );
198 4 : SbxVariableRef pNew = myMacro.Run();
199 4 : CPPUNIT_ASSERT(pNew->GetInteger() == 9 );
200 2 : }
201 :
202 2 : void Nested_Struct::testOldAssignAlt()
203 : {
204 2 : MacroSnippet myMacro( sTestSource2Alt );
205 2 : myMacro.Compile();
206 2 : CPPUNIT_ASSERT_MESSAGE("testOldAssign fails with compile error",!myMacro.HasError() );
207 4 : SbxVariableRef pNew = myMacro.Run();
208 4 : uno::Any aRet = sbxToUnoValue( pNew );
209 2 : table::TableBorder aBorder;
210 2 : aRet >>= aBorder;
211 :
212 2 : int result = aBorder.HorizontalLine.OuterLineWidth;
213 4 : CPPUNIT_ASSERT_EQUAL( 9, result );
214 2 : }
215 :
216 2 : void Nested_Struct::testUnfixedVarAssign()
217 : {
218 2 : MacroSnippet myMacro( sTestSource3 );
219 2 : myMacro.Compile();
220 2 : CPPUNIT_ASSERT_MESSAGE("testUnfixedVarAssign fails with compile error",!myMacro.HasError() );
221 : // forces a broadcast
222 4 : SbxVariableRef pNew = myMacro.Run();
223 4 : CPPUNIT_ASSERT(pNew->GetInteger() == 13 );
224 2 : }
225 :
226 2 : void Nested_Struct::testUnfixedVarAssignAlt()
227 : {
228 2 : MacroSnippet myMacro( sTestSource3Alt );
229 2 : myMacro.Compile();
230 2 : CPPUNIT_ASSERT_MESSAGE("testUnfixedVarAssignAlt fails with compile error",!myMacro.HasError() );
231 4 : SbxVariableRef pNew = myMacro.Run();
232 4 : uno::Any aRet = sbxToUnoValue( pNew );
233 :
234 4 : uno::Sequence< uno::Any > aResult;
235 2 : bool bRes = aRet >>= aResult;
236 2 : CPPUNIT_ASSERT_EQUAL(true, bRes );
237 :
238 2 : int result = aResult.getLength();
239 : // should have 2 elements in a sequence returned
240 2 : CPPUNIT_ASSERT_EQUAL(2, result );
241 :
242 2 : table::TableBorder aBorder;
243 2 : aResult[0] >>= aBorder;
244 :
245 2 : table::BorderLine aBorderLine;
246 2 : aResult[1] >>= aBorderLine;
247 2 : result = aBorder.HorizontalLine.OuterLineWidth;
248 2 : CPPUNIT_ASSERT_EQUAL(9, result );
249 2 : result = aBorderLine.OuterLineWidth;
250 4 : CPPUNIT_ASSERT_EQUAL(4, result );
251 2 : }
252 :
253 2 : void Nested_Struct::testFixedVarAssign()
254 : {
255 2 : MacroSnippet myMacro( sTestSource4 );
256 2 : myMacro.Compile();
257 2 : CPPUNIT_ASSERT_MESSAGE("testFixedVarAssign fails with compile error",!myMacro.HasError() );
258 4 : SbxVariableRef pNew = myMacro.Run();
259 4 : CPPUNIT_ASSERT(pNew->GetInteger() == 13 );
260 2 : }
261 :
262 2 : void Nested_Struct::testFixedVarAssignAlt()
263 : {
264 2 : MacroSnippet myMacro( sTestSource4Alt );
265 2 : myMacro.Compile();
266 2 : CPPUNIT_ASSERT_MESSAGE("testFixedVarAssignAlt fails with compile error",!myMacro.HasError() );
267 4 : SbxVariableRef pNew = myMacro.Run();
268 4 : uno::Any aRet = sbxToUnoValue( pNew );
269 :
270 4 : uno::Sequence< uno::Any > aResult;
271 2 : bool bRes = aRet >>= aResult;
272 2 : CPPUNIT_ASSERT_EQUAL(true, bRes );
273 :
274 2 : int result = aResult.getLength();
275 : // should have 2 elements in a sequence returned
276 2 : CPPUNIT_ASSERT_EQUAL(2, result );
277 :
278 2 : table::TableBorder aBorder;
279 2 : aResult[0] >>= aBorder;
280 :
281 2 : table::BorderLine aBorderLine;
282 2 : aResult[1] >>= aBorderLine;
283 2 : result = aBorder.HorizontalLine.OuterLineWidth;
284 2 : CPPUNIT_ASSERT_EQUAL(9, result );
285 2 : result = aBorderLine.OuterLineWidth;
286 4 : CPPUNIT_ASSERT_EQUAL(4, result );
287 2 : }
288 :
289 2 : void Nested_Struct::testUnoAccess()
290 : {
291 2 : MacroSnippet myMacro( sTestSource5 );
292 2 : myMacro.Compile();
293 2 : CPPUNIT_ASSERT_MESSAGE("testUnoAccess fails with compile error",!myMacro.HasError() );
294 4 : SbxVariableRef pNew = myMacro.Run();
295 4 : uno::Any aRet = sbxToUnoValue( pNew );
296 4 : awt::WindowDescriptor aWinDesc;
297 2 : aRet >>= aWinDesc;
298 :
299 2 : int result = aWinDesc.Bounds.X;
300 4 : CPPUNIT_ASSERT_EQUAL(200, result );
301 2 : }
302 :
303 : // Put the test suite in the registry
304 2 : CPPUNIT_TEST_SUITE_REGISTRATION(Nested_Struct);
305 : } // namespace
306 8 : CPPUNIT_PLUGIN_IMPLEMENT();
307 :
308 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|