bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / form / _XChangeBroadcaster.java
blobe11a12f0771818cb2db11d9ea5393fdba3fa83f0
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.form;
21 import lib.MultiMethodTest;
22 import lib.Status;
23 import lib.StatusException;
25 import com.sun.star.awt.XTextComponent;
26 import com.sun.star.awt.XWindow;
27 import com.sun.star.form.XChangeBroadcaster;
28 import com.sun.star.form.XChangeListener;
29 import com.sun.star.lang.EventObject;
30 import com.sun.star.uno.UnoRuntime;
32 /**
33 * Testing <code>com.sun.star.form.XChangeBroadcaster</code>
34 * interface methods:
35 * <ul>
36 * <li><code> addChangeListener() </code></li>
37 * <li><code> removeChangeListener() </code></li>
38 * </ul><p>
39 * This test needs the following object relations :
40 * <ul>
41 * <li> <code>'Win1'</code> (of type <code>XWindow</code>):
42 * used to change context when testing interface methods</li>
43 * <li> <code>'Win2'</code> (of type <code>XWindow</code>):
44 * used to change context when testing interface methods </li>
45 * <li> <code>'CONTROL'</code> (of type <code>XControl</code> and
46 * must implement <code>XTextComponent</code> interface):
47 * used to change context when testing interface methods </li>
48 * <li> <code>'XChangeBroadcaster.Changer'</code>
49 * (of type <code>ifc.form._XChangeBroadcaster.Changer</code>)
50 * <b>optional</b> : this relation <b>must be specified</b> when
51 * <code>XTextComponent</code> is not supported by the tested
52 * component. It is used to change some component content
53 * which must cause listener call. </li>
54 * </ul> <p>
56 * <b>Prerequisites:</b> component must implement <code>XTextComponent</code>
57 * interface for changing component's text which must cause listener call.
58 * If the component cann't support the interface, then the relation
59 * <code>'XChangeBroadcaster.Changer'</code> must be passed. <p>
61 * Test is <b> NOT </b> multithread compilant. <p>
62 * @see com.sun.star.form.XChangeBroadcaster
64 public class _XChangeBroadcaster extends MultiMethodTest {
65 public static XChangeBroadcaster oObj = null;
66 protected boolean changed = false;
68 /**
69 * This interface must be implemented by component and passed
70 * in relation if it doesn't support <code>XTextComponent</code>
71 * interface. It used to change the content of component.
73 public static interface Changer {
74 /**
75 * The method must change the component's content to
76 * cause a listener call.
78 public void change() ;
81 /**
82 * Class we need to test methods
84 protected class MyChangeListener implements XChangeListener {
85 public void disposing ( EventObject oEvent ) {}
86 public void changed ( EventObject oEvent ) {
87 System.out.println("Listener called");
88 changed = true;
92 protected XChangeListener listener = new MyChangeListener();
93 protected XTextComponent xText = null ;
94 protected Changer changer = null ;
96 /**
97 * Tries to query the tested component for <code>XTextComponent</code>
98 * interface and retrieves a relation
99 * <code>'XChangeBroadcaster.Changer'</code>.
100 * @throw StatusException If neither relation is found nor interface
101 * is queried.
103 public void before() {
104 xText = UnoRuntime.queryInterface(XTextComponent.class,oObj);
105 changer = (Changer) tEnv.getObjRelation("XChangeBroadcaster.Changer") ;
107 if (xText == null && changer == null)
108 throw new StatusException(Status.failed
109 ("Neither 'XChangeBroadcaster.Changer' relation found " +
110 "nor XTextComponent is supported")) ;
114 * Test calls the method, then object relations 'Win1', 'Win2', 'CONTROL'
115 * are obtained, and context is changed.<p>
116 * Has <b> OK </b> status if listener was called after context has changed.
118 public void _addChangeListener() {
119 log.println("Testing addChangeListener ...");
120 oObj.addChangeListener( listener );
121 XWindow win1 = (XWindow) tEnv.getObjRelation("Win1");
122 XWindow win2 = (XWindow) tEnv.getObjRelation("Win2");
123 win1.setFocus();
125 changeContent() ;
126 shortWait();
128 win2.setFocus();
129 XTextComponent TC = UnoRuntime.queryInterface
130 (XTextComponent.class,tEnv.getObjRelation("CONTROL"));
131 TC.setText("NOXChangeBroadcaster");
132 shortWait();
133 tRes.tested("addChangeListener()", changed);
137 * Test calls the method, then object relations 'Win1', 'Win2', 'CONTROL'
138 * are obtained, and context is changed.<p>
139 * Has <b> OK </b> status if listener was not called after context has
140 * changed.<p>
141 * The following method tests are to be completed successfully before :
142 * <ul>
143 * <li> <code> addChangeListener() </code> : adds the specified listener
144 * to receive the "changed" event</li>
145 * </ul>
147 public void _removeChangeListener() {
148 requiredMethod("addChangeListener()");
149 changed = false;
150 log.println("Testing removeChangeListener ...");
151 oObj.addChangeListener( listener );
152 XWindow win2 = (XWindow) tEnv.getObjRelation("Win2");
153 win2.setFocus();
155 changeContent() ;
157 win2.setFocus();
158 shortWait();
159 tRes.tested("removeChangeListener()", !changed);
163 * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
164 * reset</code> call.
166 private void shortWait() {
167 try {
168 Thread.sleep(2000) ;
169 } catch (InterruptedException e) {
170 log.println("While waiting :" + e) ;
175 * Changes the content of the component depending on whether
176 * <code>XTextComponent</code> is supported or not. If yes
177 * then the text is chahged, if not the relation <code>change()</code>
178 * method is used.
180 protected void changeContent() {
181 if (xText != null) {
182 xText.setText("XChangeBroadcaster".equals(xText.getText()) ?
183 "NoXChangeBroadcaster" : "XChangeBroadcaster") ;
184 } else {
185 changer.change();
189 } // finished class _XChangeBroadcaster