1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package ifc.frame;
24 
25 import com.sun.star.beans.PropertyValue;
26 import com.sun.star.frame.XController;
27 import com.sun.star.frame.XModel;
28 import com.sun.star.view.XSelectionSupplier;
29 
30 import lib.MultiMethodTest;
31 
32 
33 /**
34 * Testing <code>com.sun.star.frame.XModel</code>
35 * interface methods:
36 * <ul>
37 *  <li><code> attachResource() </code></li>
38 *  <li><code> connectController() </code></li>
39 *  <li><code> disconnectController() </code></li>
40 *  <li><code> getArgs() </code></li>
41 *  <li><code> getCurrentController() </code></li>
42 *  <li><code> getCurrentSelection() </code></li>
43 *  <li><code> getURL() </code></li>
44 *  <li><code> hasControllersLocked() </code></li>
45 *  <li><code> lockControllers() </code></li>
46 *  <li><code> setCurrentController() </code></li>
47 *  <li><code> unlockControllers() </code></li>
48 * </ul><p>
49 * This test needs the following object relations :
50 * <ul>
51 *  <li> <code>'CONT2'</code> (of type <code>XController</code>):
52 *  a controller used as an argument for several test methods </li>
53 *  <li> <code>'TOSELECT'</code> (of type <code>Object</code>):
54 *   something, we want to select from document </li>
55 *  <li> <code>'SELSUPP'</code> (of type <code>XSelectionSupplier</code>):
56 *   supplier, we use to select something in a document </li>
57 * </ul> <p>
58 * Test is <b> NOT </b> multithread compilant. <p>
59 * @see com.sun.star.frame.XModel
60 */
61 public class _XModel extends MultiMethodTest {
62     public XModel oObj = null;
63     boolean result = true;
64 
65     /**
66     * Test calls the method. <p>
67     * Has <b> OK </b> status if the method returns true.
68     */
_attachResource()69     public void _attachResource() {
70         log.println("opening DataSourceBrowser");
71 
72         PropertyValue[] noArgs = new PropertyValue[0];
73         result = oObj.attachResource(".component:DB/DataSourceBrowser", noArgs);
74         tRes.tested("attachResource()", result);
75     }
76 
77     /**
78     * After obtaining object relation 'CONT2' and storing old controller,
79     * test calls the method, then result is checked.<p>
80     * Has <b> OK </b> status if controller, gotten after method call is not
81     * equal to a previous controller.
82     */
_connectController()83     public void _connectController() {
84         XController cont2 = (XController) tEnv.getObjRelation("CONT2");
85         XController old = oObj.getCurrentController();
86         result = false;
87 
88         if (cont2 == null) {
89             log.println("No controller no show");
90         } else {
91             oObj.connectController(cont2);
92 
93             String oldFrame = old.getFrame().getName();
94             String newFrame = cont2.getFrame().getName();
95             result = (!oldFrame.equals(newFrame));
96         }
97 
98         tRes.tested("connectController()", result);
99     }
100 
101     /**
102     * After obtaining object relation 'CONT2', test calls the method,
103     * then result is checked.<p>
104     * Has <b> OK </b> status if controller, gotten after method call is not
105     * equal to a controller we use as an argument to method.
106     */
_disconnectController()107     public void _disconnectController() {
108         XController cont2 = (XController) tEnv.getObjRelation("CONT2");
109         result = false;
110 
111         if (cont2 == null) {
112             log.println("No controller no show");
113         } else {
114             oObj.disconnectController(cont2);
115             result = (oObj.getCurrentController() != cont2);
116         }
117 
118         tRes.tested("disconnectController()", result);
119     }
120 
121     /**
122     * Test calls the method. <p>
123     * Has <b> OK </b> status if the method does not return null.
124     */
_getArgs()125     public void _getArgs() {
126         tRes.tested("getArgs()", oObj.getArgs() != null);
127     }
128 
129     /**
130     * Test calls the method. <p>
131     * Has <b> OK </b> status if the method does not return null.
132     */
_getCurrentController()133     public void _getCurrentController() {
134         XController ctrl = oObj.getCurrentController();
135         tRes.tested("getCurrentController()", ctrl != null);
136     }
137 
138     /**
139     * After obtaining object relations 'SELSUPP' and 'TOSELECT', test prepares
140     * selection and calls the method. <p>
141     * Has <b> OK </b> status if the method does not return null.
142     */
_getCurrentSelection()143     public void _getCurrentSelection() {
144         XSelectionSupplier selsupp = (XSelectionSupplier) tEnv.getObjRelation(
145                                              "SELSUPP");
146         Object toSelect = tEnv.getObjRelation("TOSELECT");
147         result = false;
148 
149         if (selsupp == null) {
150             log.println("No Selection Supplier no show");
151         } else {
152             try {
153                 selsupp.select(toSelect);
154             } catch (com.sun.star.lang.IllegalArgumentException e) {
155                 log.println("Exception occured while select:");
156                 e.printStackTrace(log);
157 
158                 return;
159             }
160 
161             Object sel = oObj.getCurrentSelection();
162             result = (sel != null);
163         }
164 
165         tRes.tested("getCurrentSelection()", result);
166     }
167 
168     /**
169     * Test calls the method. <p>
170     * Has <b> OK </b> status if the method does not return null.
171     */
_getURL()172     public void _getURL() {
173         String url = oObj.getURL();
174         log.println("The url: " + url);
175         tRes.tested("getURL()", url != null);
176     }
177 
178     /**
179     * if controller is not locked, test calls the method. <p>
180     * Has <b> OK </b> status if the method returns true. <p>
181     * The following method tests are to be completed successfully before :
182     * <ul>
183     *  <li> <code> lockControllers() </code> : locks controllers </li>
184     * </ul>
185     */
_hasControllersLocked()186     public void _hasControllersLocked() {
187         requiredMethod("lockControllers()");
188         tRes.tested("hasControllersLocked()", oObj.hasControllersLocked());
189     }
190 
191     /**
192     * Test calls the method, then result is checked.<p>
193     * Has <b> OK </b> status if method locks controllers.
194     */
_lockControllers()195     public void _lockControllers() {
196         oObj.lockControllers();
197         result = oObj.hasControllersLocked();
198         tRes.tested("lockControllers()", result);
199     }
200 
201     /**
202     * After obtaining object relation 'CONT2' and storing old controller,
203     * controller cont2 is connected, then this controller is
204     * setting as current.
205     * Sets the old controller as current.
206     * <p>Has <b> OK </b> status if set and gotten controllers are not equal.
207     */
_setCurrentController()208     public void _setCurrentController() {
209         XController cont2 = (XController) tEnv.getObjRelation("CONT2");
210         XController old = oObj.getCurrentController();
211         result = false;
212 
213         if (cont2 == null) {
214             log.println("No controller no show");
215         } else {
216             oObj.connectController(cont2);
217 
218             try {
219                 oObj.setCurrentController(cont2);
220             } catch (com.sun.star.container.NoSuchElementException e) {
221                 log.print("Cannot set current controller:");
222                 e.printStackTrace(log);
223             }
224 
225             result = (oObj.getCurrentController() != old);
226 
227             try {
228                 oObj.setCurrentController(old);
229             } catch (com.sun.star.container.NoSuchElementException e) {
230                 log.print("Cannot set current controller:");
231                 e.printStackTrace(log);
232             }
233         }
234 
235         tRes.tested("setCurrentController()", result);
236     }
237 
238     /**
239     * Test calls the method. <p>
240     * Has <b> OK </b> status if method unlocks controllers.
241     * <p>
242     * The following method tests are to be completed successfully before :
243     * <ul>
244     *  <li> <code> hasControllersLocked() </code> : checks if controllers are
245     * locked </li>
246     * </ul>
247     */
_unlockControllers()248     public void _unlockControllers() {
249         requiredMethod("hasControllersLocked()");
250         oObj.unlockControllers();
251         result = !oObj.hasControllersLocked();
252         tRes.tested("unlockControllers()", result);
253     }
254 }
255