Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / frame / _XFrame.java
blob9536cdb2b6c1fe9cea70cd4f4c9485fb932c5c8a
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.frame;
21 import com.sun.star.awt.XWindow;
22 import com.sun.star.frame.FrameAction;
23 import com.sun.star.frame.FrameActionEvent;
24 import com.sun.star.frame.XController;
25 import com.sun.star.frame.XFrame;
26 import com.sun.star.frame.XFrameActionListener;
27 import com.sun.star.frame.XFramesSupplier;
28 import com.sun.star.lang.EventObject;
29 import java.io.PrintWriter;
30 import lib.MultiMethodTest;
31 import lib.TestEnvironment;
33 /**
34 * Testing <code>com.sun.star.frame.XFrame</code>
35 * interface methods:
36 * <ul>
37 * <li><code> getName() </code></li>
38 * <li><code> setName() </code></li>
39 * <li><code> activate() </code></li>
40 * <li><code> deactivate() </code></li>
41 * <li><code> isActive() </code></li>
42 * <li><code> addFrameActionListener() </code></li>
43 * <li><code> removeFrameActionListener() </code></li>
44 * <li><code> getCreator() </code></li>
45 * <li><code> getComponentWindow() </code></li>
46 * <li><code> getContainerWindow() </code></li>
47 * <li><code> getController() </code></li>
48 * <li><code> isTop() </code></li>
49 * <li><code> findFrame() </code></li>
50 * <li><code> contextChanged() </code></li>
51 * <li><code> setCreator() </code></li>
52 * <li><code> setComponent() </code></li>
53 * <li><code> initialize() </code></li>
54 * </ul><p>
55 * This test needs the following object relations :
56 * <ul>
57 * <li> <code>'XFrame'</code> (of type <code>XFrame</code>)
58 * <b>optional</b>: any frame named 'XFrame'.
59 * Could be used by <code>findFrame</code> method to try
60 * to find other frame than itself.</li>
62 * <li> <code>'Desktop'</code> (of type <code>Object</code>):
63 * if exists, then desktop component is tested</li>
64 * </ul> <p>
65 * Test is <b> NOT </b> multithread compliant. <p>
66 * @see com.sun.star.frame.XFrame
68 public class _XFrame extends MultiMethodTest {
69 final FrameAction[] actionEvent = new FrameAction[1] ;
70 final boolean[] listenerCalled = new boolean[] {false} ;
71 final boolean[] activatedCalled = new boolean[] {false} ;
72 final boolean[] deactivatedCalled = new boolean[] {false} ;
73 final TestFrameActionListener listener =
74 new TestFrameActionListener() ;
75 public static XFrame oObj = null;
77 /**
78 * Class used to test listeners.
80 private class TestFrameActionListener
81 implements XFrameActionListener {
83 public void frameAction(FrameActionEvent e) {
84 listenerCalled[0] = true ;
85 activatedCalled[0] |= e.Action == FrameAction.FRAME_ACTIVATED;
86 deactivatedCalled[0] |= e.Action == FrameAction.FRAME_DEACTIVATING;
87 actionEvent[0] = e.Action;
90 public void disposing(EventObject e) {}
94 /**
95 * Test calls the method. <p>
96 * Has <b> OK </b> status if the method does not return null.
98 public void _getName() {
99 String name = oObj.getName() ;
100 if (name == null)
101 log.println("getName() returned null: FAILED") ;
103 tRes.tested("getName()", name!=null) ;
107 * Test calls the method. <p>
108 * Has <b> OK </b> status if set and gotten names are equal.
110 public void _setName() {
111 String sName = "XFrame" ;
113 oObj.setName(sName);
114 String gName = oObj.getName();
115 boolean res = sName.equals(gName);
116 if (! res)
117 log.println("setName('" + sName +
118 "'), but getName() return '" + gName + "'") ;
119 tRes.tested("setName()", res);
123 * Test calls the method. <p>
124 * Has <b> OK </b> status if the method successfully returns
125 * and no exceptions were thrown.
127 public void _activate() {
128 oObj.activate() ;
129 tRes.tested("activate()", true) ;
133 * Test calls the method. <p>
134 * Has <b> OK </b> status if the method successfully returns
135 * and no exceptions were thrown.
137 public void _deactivate() {
138 oObj.deactivate() ;
139 oObj.activate() ;
140 tRes.tested("deactivate()", true) ;
144 * Test calls the method. Then frame is deactivated and method called
145 * again. <p>
146 * Has <b> OK </b> status if isDesktop() returns true or if the method
147 * always display real status of a frame during activation/deactivation.
149 public void _isActive() {
150 boolean result = true;
152 if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
153 log.println("Desktop is always active");
154 tRes.tested("isActive()", oObj.isActive()) ;
155 return;
158 oObj.deactivate();
159 result &= !oObj.isActive();
160 if (oObj.isActive())
161 log.println("after deactivate() method call, isActive() returned true");
162 oObj.activate();
163 result &= oObj.isActive();
164 if (!oObj.isActive())
165 log.println("after activate() method call, isActive() returned false") ;
166 boolean res = isDesktop(log,tEnv,"isActive()");
167 if (res) result=res;
169 tRes.tested("isActive()", result) ;
173 * Test calls the method. Then frame status (activated/deactivated) is
174 * changed, and the listener is checked.<p>
175 * Has <b> OK </b> status if isDesktop() method returns true, or if the
176 * listener was called and frame was activated.
178 public void _addFrameActionListener() throws Exception {
179 boolean result = true ;
181 oObj.addFrameActionListener(listener) ;
182 oObj.activate() ;
183 oObj.deactivate() ;
184 oObj.activate() ;
186 if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
187 log.println("No actions supported by Desktop");
188 tRes.tested("addFrameActionListener()", true) ;
189 return;
192 waitForEventIdle();
194 if (!listenerCalled[0]) {
195 log.println("listener was not called.") ;
196 result = false ;
197 } else {
198 if (!activatedCalled[0]) {
199 log.println("Listener was called, FRAME_ACTIVATED was not") ;
200 result = false ;
202 if (!deactivatedCalled[0]) {
203 log.println("Listener was called, FRAME_DEACTIVATING was not") ;
204 result = false ;
208 boolean res = isDesktop(log, tEnv, "addFrameActionListener()");
209 if (res) result=res;
211 tRes.tested("addFrameActionListener()", result) ;
215 * Test calls the method. Then frame status (activated/deactivated) is
216 * changed, and the listener is checked.<p>
217 * Has <b> OK </b> status if isDesktop() method returns true, or if the
218 * method actually removes listener so it does not react on
219 * activate/deactivate events. <p>
220 * The following method tests are to be completed successfully before :
221 * <ul>
222 * <li> <code> addFrameActionListener() </code>: adds action listener
223 * to a frame </li>
224 * </ul>
226 public void _removeFrameActionListener() {
227 boolean result = true;
229 requiredMethod("addFrameActionListener()");
230 listenerCalled[0] = false;
231 oObj.removeFrameActionListener(listener);
232 oObj.activate();
233 oObj.deactivate();
234 oObj.activate();
235 if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
236 log.println("No actions supported by Desktop");
237 tRes.tested("removeFrameActionListener()", true) ;
238 return;
241 if (listenerCalled[0])
242 log.println("Listener wasn't removed, and was called");
243 boolean res = isDesktop(log, tEnv, "removeFrameActionListener()");
244 if (res) result=res; else result = (!listenerCalled[0]);
246 tRes.tested("removeFrameActionListener()", result);
250 * Test calls the method. <p>
251 * Has <b> OK </b> status if isDesktop() method returns true or if the method
252 * does not return null.
254 public void _getCreator() {
255 boolean result = true;
257 if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
258 log.println("Desktop has no creator");
259 tRes.tested("getCreator()", true) ;
260 return;
263 XFramesSupplier creator = oObj.getCreator() ;
264 if (creator == null)
265 log.println("getCreator() returns null") ;
266 boolean res = isDesktop(log,tEnv,"getCreator()");
267 if (res) result=res; else result = (creator != null);
268 tRes.tested("getCreator()", result) ;
272 * Test calls the method. <p>
273 * Has <b> OK </b> status if isDesktop() method returns true or if the method
274 * does not return null.
276 public void _getComponentWindow() {
277 boolean result = true;
279 XWindow win = oObj.getComponentWindow() ;
281 if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
282 log.println("Desktop has no component window");
283 tRes.tested("getComponentWindow()", true) ;
284 return;
287 if (win == null)
288 log.println("getComponentWindow() returns null") ;
289 boolean res = isDesktop(log,tEnv,"getComponentWindow()");
290 if (res) result=res; else result = (win != null);
291 tRes.tested("getComponentWindow()", result) ;
295 * Test calls the method. <p>
296 * Has <b> OK </b> status if isDesktop() method returns true or if the method
297 * does not return null.
299 public void _getContainerWindow() {
300 boolean result = true;
302 if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
303 log.println("Desktop has no container window");
304 tRes.tested("getContainerWindow()", true) ;
305 return;
308 XWindow win = oObj.getContainerWindow() ;
309 if (win == null)
310 log.println("getContainerWindow() returns null") ;
311 boolean res = isDesktop(log,tEnv,"getContainerWindow()");
312 if (res) result=res; else result = (win != null);
313 tRes.tested("getContainerWindow()", result) ;
317 * Test calls the method. Then returned controller is checked. <p>
318 * Has <b> OK </b> status if isDesktop() method returns true or
319 * if the method returns non-null controller, having frame that's equal to
320 * a (XFrame) oObj.
322 public void _getController() {
323 boolean result = true;
324 XController ctrl = oObj.getController();
326 if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
327 log.println("Desktop has no controller");
328 tRes.tested("getController()", true) ;
329 return;
332 if (ctrl == null) {
333 log.println("getController() returns null");
334 result = false;
335 } else {
336 XFrame frm = ctrl.getFrame();
337 if (!oObj.equals(frm)) {
338 log.println("Frame returned by controller not " +
339 "equals to frame testing");
340 result = false;
343 boolean res = isDesktop(log, tEnv, "getController()");
344 if (res) result=res;
345 tRes.tested("getController()", result) ;
349 * Test calls the method. <p>
350 * Has <b> OK </b> status if the method successfully returns
351 * and no exceptions were thrown.
353 public void _isTop() {
354 log.println("isTop() = " + oObj.isTop());
355 tRes.tested("isTop()", true) ;
359 * After obtaining an object relation 'XFrame', test tries to find a frame
360 * named 'XFrame'. <p>
361 * Has <b> OK </b> status if the method returns non-null object that's equal
362 * to previously obtained object relation.
364 public void _findFrame() {
365 boolean result = true ;
367 XFrame aFrame = (XFrame) tEnv.getObjRelation("XFrame");
369 if (aFrame != null) {
370 log.println("Trying to find a frame with name 'XFrame' ...");
371 XFrame frame = oObj.findFrame("XFrame",
372 com.sun.star.frame.FrameSearchFlag.GLOBAL) ;
373 if (frame == null) {
374 log.println("findFrame(\"XFrame,com.sun.star.frame.FrameSearchFlag.GLOBAL\") returns null") ;
375 result = false ;
376 } else if ( !aFrame.equals(frame) ) {
377 log.println("findFrame(\"XFrame,com.sun.star.frame.FrameSearchFlag.GLOBAL\") "
378 + " returns frame which is not equal to passed in relation") ;
379 result = false ;
383 log.println("Trying to find a frame with name '_self' ...");
384 XFrame frame = oObj.findFrame("_self",
385 com.sun.star.frame.FrameSearchFlag.AUTO) ;
386 if (frame == null) {
387 log.println("findFrame(\"_self\") returns null") ;
388 result = false ;
389 } else if ( !oObj.equals(frame) ) {
390 log.println("findFrame(\"_self\") "
391 + " returns frame which is not equal to tested") ;
392 result = false ;
395 tRes.tested("findFrame()", result) ;
399 * At first new listener is added, then test calls the method and result
400 * is checked. <p>
401 * Has <b> OK </b> status if isDesktop() method returns true or if the
402 * listener was called and proper event past to listener.
404 public void _contextChanged() {
405 boolean result = true;
406 TestFrameActionListener listener = new TestFrameActionListener();
408 if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
409 log.println("Desktop can't change context");
410 tRes.tested("contextChanged()", true) ;
411 return;
414 listenerCalled[0] = false;
415 oObj.addFrameActionListener(listener);
416 try {
417 oObj.contextChanged();
418 if ( !listenerCalled[0] ) {
419 log.println("listener was not called on contextChanged() call.") ;
420 result = false;
421 } else if (actionEvent[0] != FrameAction.CONTEXT_CHANGED) {
422 log.println("listener was called, but Action != CONTEXT_CHANGED") ;
423 result = false;
425 } finally {
426 oObj.removeFrameActionListener(listener);
429 boolean res = isDesktop(log, tEnv, "contextChanged()");
430 if (res) result = res;
431 tRes.tested("contextChanged()", result);
436 * Test calls the method. Remembered old creator is restored at the end. <p>
437 * Has <b> OK </b> status if the method successfully set new value to (XFrame)
438 * oObj object.
440 public void _setCreator() {
441 if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
442 log.println("Desktop has no creator");
443 tRes.tested("setCreator()", true) ;
444 return;
447 XFramesSupplier oldCreator = oObj.getCreator() ;
448 oObj.setCreator(null) ;
449 tRes.tested("setCreator()", oObj.getCreator() == null) ;
450 oObj.setCreator(oldCreator) ;
454 * Test calls the method, then result is checked.<p>
455 * Has <b> OK </b> status if method returns true, and values, set by the
456 * method are nulls, or if method returns false, and values are not changed.
457 * This method destroy the object. Therefore all other methods have to be
458 * executed before :
459 * <ul>
460 * <li> <code> getName() </code>
461 * <li> <code> setName() </code>
462 * <li> <code> activate() </code>
463 * <li> <code> deactivate() </code>
464 * <li> <code> isActive() </code>
465 * <li> <code> addFrameActionListener() </code>
466 * <li> <code> getComponentWindow() </code>
467 * <li> <code> getContainerWindow() </code>
468 * <li> <code> getController() </code>
469 * <li> <code> isTop() </code>
470 * <li> <code> findFrame() </code>
471 * <li> <code> contextChanged() </code>
472 * <li> <code> setCreator() </code>
473 * object</li>
474 * </ul>
476 public void _setComponent() {
477 // setComponent() destr
478 requiredMethod("getName()") ;
479 requiredMethod("setName()") ;
480 requiredMethod("activate()") ;
481 requiredMethod("deactivate()") ;
482 requiredMethod("isActive()") ;
483 requiredMethod("addFrameActionListener()") ;
484 requiredMethod("getComponentWindow()") ;
485 requiredMethod("getContainerWindow()") ;
486 requiredMethod("getController()") ;
487 requiredMethod("isTop()") ;
488 requiredMethod("findFrame()") ;
489 requiredMethod("contextChanged()") ;
490 requiredMethod("setCreator()") ;
492 boolean result = true;
494 XWindow oldWindow = oObj.getComponentWindow();
495 XController oldController = oObj.getController();
496 boolean rs = oObj.setComponent(null, null);
497 if (rs) { // component must be changed
498 result &= oObj.getComponentWindow() == null;
499 result &= oObj.getController() == null;
500 if (!result)
501 log.println("setComponent() returns true, but component is " +
502 "not changed.");
503 } else { // frame is not allowed to change component
504 result &= oObj.getComponentWindow() == oldWindow ;
505 result &= oObj.getController() == oldController ;
506 if (!result)
507 log.println("setComponent() returns false, but component is" +
508 "changed.");
510 tRes.tested("setComponent()", result);
515 * Test calls the method. <p>
516 * Has <b> OK </b> status if the method successfully returns.
517 * In case a frame should initialised twice, a
518 * <CODE>com.sun.star.uno.RuntimeException</CODE> was thrown. This is ok. But since
519 * a com.sun.star.uno.RuntimeException could thrown in any state the message of
520 * the exception must contain a defined string. In this case the test get an
521 * <CODE>OK</CODE> status.
522 * The following method tests are to be completed successfully before :
523 * <ul>
524 * <li> <code> setComponent() </code> : sets window and controller to the
525 * object</li>
526 * </ul>
528 public void _initialize() {
529 requiredMethod("setComponent()") ;
530 XWindow win = oObj.getContainerWindow() ;
531 boolean bOK = true;
532 try {
533 oObj.initialize(win) ;
534 } catch (com.sun.star.uno.RuntimeException e){
535 String message="Frame::initialized() is called more than once, which is not useful nor allowed.";
536 if (e.toString().indexOf(message) != -1){
537 log.println(e.toString());
538 log.println("methods throws exception, but it's OK");
539 }else{
540 log.println(e.toString());
541 bOK=false;
545 tRes.tested("initialize()", bOK) ;
549 * Checks does relation 'Desktop' exist. Returns true if exist.
551 public static boolean isDesktop(PrintWriter log,
552 TestEnvironment env, String method) {
553 Object isD = env.getObjRelation("Desktop");
554 if (isD != null) {
555 log.println("The Desktop doesn't support the method " + method);
556 log.println("It will always return true");
557 return true;
559 else {
560 return false;
565 * Forces environment recreation.
567 @Override
568 public void after() {
569 disposeEnvironment();