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 
24 package ifc.view;
25 
26 import com.sun.star.container.NoSuchElementException;
27 import com.sun.star.container.XEnumeration;
28 import com.sun.star.lang.WrappedTargetException;
29 import com.sun.star.view.XMultiSelectionSupplier;
30 import java.util.Comparator;
31 import lib.MultiMethodTest;
32 import lib.Status;
33 import lib.StatusException;
34 
35 /**
36  * Testing <code>com.sun.star.view.XSelectionSupplier</code>
37  * interface methods :
38  * <ul>
39  *  <li><code> addSelection()</code></li>
40  *  <li><code> removeSelection()</code></li>
41  *  <li><code> clearSelection()</code></li>
42  *  <li><code> getSelectionCount()</code></li>
43  *  <li><code> createSelectionEnumeration()</code></li>
44  *  <li><code> createReverseSelectionEnumeration()</code></li>
45  * </ul>
46  * This test needs the following object relations :
47  * <ul>
48  *  <li> <code>'Selections'</code> of type <code>Object[]</code> :
49  *   the array of the instances which can be selected.</li>
50  *  <li> <code>'Comparer'</code> of type <code>Comparator</code> :
51  *   the interface for comparing of selected instances</li>
52  * <ul> <p>
53  * Test is <b> NOT </b> multithread compilant. <p>
54  * @see com.sun.star.view.XSelectionSupplier
55  */
56 public class _XMultiSelectionSupplier extends MultiMethodTest {
57 
58     public XMultiSelectionSupplier oObj = null;
59     public boolean selectionChanged = false;
60     Object[] selections = null;
61     Comparator ObjCompare = null;
62 
before()63     protected void before() {
64         selections = (Object[])tEnv.getObjRelation("Selections");
65         if (selections == null) {
66             throw new StatusException(Status.failed(
67                 "Couldn't get relation 'Selections'"));
68         }
69 
70         ObjCompare = (Comparator)tEnv.getObjRelation("Comparer");
71     }
72 
after()73     protected void after() {
74         disposeEnvironment();
75     }
76 
77     /**
78      * Selects an instance from relation 'First'. <p>
79      * Has <b> OK </b> status if no exceptions were thrown. <p>
80      */
_addSelection()81     public void _addSelection() {
82 
83         boolean bOK = true;
84 
85         log.println("clear selections");
86         oObj.clearSelection();
87 
88         int count = oObj.getSelectionCount();
89 
90         bOK &= (count == 0);
91 
92         if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
93 
94         for(int i = 0; i < selections.length; i++) {
95             try {
96                 log.println("select object from object relation 'selections["+i+"]'");
97                 oObj.addSelection(selections[i]);
98             } catch (com.sun.star.lang.IllegalArgumentException ex) {
99                 log.println("ERROR: could not add selection from object relation 'selections[" + i + "]': " + ex.toString());
100                 bOK = false;
101             }
102             count = oObj.getSelectionCount();
103             if (count != (i+1)){
104                 log.println("ERROR: add a selection but selection count ("+count+ ") " +
105                     "is not as expected (" + (i+1) + ") => FAILED");
106                 bOK = false;
107             }
108         }
109 
110         log.println("try to select object relation 'selections[0]' second time...");
111         try {
112             count = oObj.getSelectionCount();
113             oObj.addSelection(selections[0]);
114         } catch (com.sun.star.lang.IllegalArgumentException ex) {
115             log.println("ERROR: could not add selection from object relation 'selections[0] a second time': " + ex.toString());
116         }
117         if (count != oObj.getSelectionCount()){
118             log.println("ERROR: the selected count ("+oObj.getSelectionCount() +") is not that before (" + count + ")");
119             bOK = false;
120         }
121 
122         log.println("try to select invalid object...");
123         try {
124 
125             oObj.addSelection(oObj);
126 
127             log.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
128             bOK = false;
129         } catch (com.sun.star.lang.IllegalArgumentException ex) {
130             log.println("expected exception 'com.sun.star.lang.IllegalArgumentException' => OK");
131         }
132 
133         tRes.tested("addSelection()", bOK);
134     }
135 
_removeSelection()136     public void _removeSelection() {
137         requiredMethod("addSelection()");
138 
139         boolean bOK = true;
140 
141         log.println("clear selections");
142         oObj.clearSelection();
143 
144         int count = oObj.getSelectionCount();
145 
146         bOK &= (count == 0);
147 
148         if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
149 
150         log.println("add some selections...");
151         for(int i = 0; i < selections.length; i++) {
152             try {
153                 log.println("select object from object relation 'selections["+i+"]'");
154                 oObj.addSelection(selections[i]);
155             } catch (com.sun.star.lang.IllegalArgumentException ex) {
156                 log.println("ERROR: could not add selection from object relation 'selections[" + i + "]': " + ex.toString());
157                 bOK = false;
158             }
159             count = oObj.getSelectionCount();
160             if (count != (i+1)){
161                 log.println("ERROR: added a selection but selection count ("+count+ ") " +
162                     "is not as expected (" + (i+1) + ") => FAILED");
163                 bOK = false;
164             }
165         }
166 
167         log.println("try now to remove selections...");
168 
169         count = oObj.getSelectionCount();
170         int oldCount = oObj.getSelectionCount();
171         for(int i = 0; i < selections.length; i++) {
172             try {
173                 log.println("remove selection for object relation 'selections["+i+"]'");
174                 oObj.removeSelection(selections[i]);
175             } catch (com.sun.star.lang.IllegalArgumentException ex) {
176                 log.println("ERROR: could not remove selection from object relation 'selections[" + i + "]': " + ex.toString());
177                 bOK = false;
178             }
179             count = oObj.getSelectionCount();
180             if (count !=  (oldCount - i - 1)){
181                 log.println("ERROR: removed a selection but selection count ("+count+ ") " +
182                     "is not as expected (" + (oldCount -i -1) + ") => FAILED");
183                 bOK = false;
184             }
185         }
186 
187         log.println("try to remove a removed selection a second time...");
188         count = oObj.getSelectionCount();
189         try {
190             oObj.removeSelection(selections[0]);
191         } catch (com.sun.star.lang.IllegalArgumentException ex) {
192             log.println("ERROR: could not remove selection from object relation 'selections[0] a second time': " + ex.toString());
193         }
194         if (count != oObj.getSelectionCount()){
195             log.println("ERROR: the selected count ("+oObj.getSelectionCount() +") is not that before (" + count + ")");
196             bOK = false;
197         }
198 
199         log.println("try to remove invalid object...");
200         try {
201 
202             oObj.removeSelection(oObj);
203 
204             log.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
205             bOK = false;
206         } catch (com.sun.star.lang.IllegalArgumentException ex) {
207             log.println("expected exception 'com.sun.star.lang.IllegalArgumentException' => OK");
208         }
209 
210         tRes.tested("removeSelection()", bOK);
211     }
212 
213 
214     /**
215      * First test changes selection of the object : if nothing is
216      * currently selected or first instance ('First' relation) is
217      * selected then selects second instance; if second instance
218      * is currently selected then the first instance is selected. <p>
219      * Then <code>getSelection</code> is called and values set and
220      * get are compared. Comparison has some special cases. For
221      * example if selection is a Cell, then the values contained
222      * in cells are compared. <p>
223      * Has <b>OK</b> status if selection changed properly.
224      */
_getSelectionCount()225     public void _getSelectionCount() {
226         requiredMethod("addSelection()");
227         tRes.tested("getSelectionCount()", true);
228     }
229 
_clearSelection()230     public void _clearSelection() {
231         requiredMethod("addSelection()");
232         boolean bOK = true;
233 
234         log.println("clear selections");
235         oObj.clearSelection();
236 
237         int count = oObj.getSelectionCount();
238 
239         bOK &= (count == 0);
240 
241         if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
242 
243         log.println("add some selections...");
244         for(int i = 0; i < selections.length; i++) {
245             try {
246                 log.println("select object from object relation 'selections["+i+"]'");
247                 oObj.addSelection(selections[i]);
248             } catch (com.sun.star.lang.IllegalArgumentException ex) {
249                 log.println("ERROR: could not add selection from object relation 'selections[" + i + "]': " + ex.toString());
250                 bOK = false;
251             }
252             count = oObj.getSelectionCount();
253             if (count != (i+1)){
254                 log.println("ERROR: added a selection but selection count ("+count+ ") " +
255                     "is not as expected (" + (i+1) + ") => FAILED");
256                 bOK = false;
257             }
258         }
259 
260         count = oObj.getSelectionCount();
261 
262         log.println("clear selections...");
263         oObj.clearSelection();
264 
265         count = oObj.getSelectionCount();
266 
267         bOK &= (count == 0);
268 
269         if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
270 
271         tRes.tested("clearSelection()", bOK);
272     }
273 
_createSelectionEnumeration()274     public void _createSelectionEnumeration() {
275         requiredMethod("addSelection()");
276         boolean bOK = true;
277 
278         log.println("clear selections");
279         oObj.clearSelection();
280 
281         int count = oObj.getSelectionCount();
282 
283         bOK &= (count == 0);
284 
285         if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
286 
287         log.println("add some selections...");
288         for(int i = 0; i < selections.length; i++) {
289             try {
290                 log.println("select object from object relation 'selections["+i+"]'");
291                 oObj.addSelection(selections[i]);
292             } catch (com.sun.star.lang.IllegalArgumentException ex) {
293                 log.println("ERROR: could not add selection from object relation 'selections[" + i + "]': " + ex.toString());
294                 bOK = false;
295             }
296             count = oObj.getSelectionCount();
297             if (count != (i+1)){
298                 log.println("ERROR: added a selection but selection count ("+count+ ") " +
299                     "is not as expected (" + (i+1) + ") => FAILED");
300                 bOK = false;
301             }
302         }
303 
304         log.println("create enumeration...");
305         XEnumeration xEnum = oObj.createSelectionEnumeration();
306 
307         boolean compRes = true; //compare result
308         int i = 0;
309 
310         while (xEnum.hasMoreElements()){
311             log.println("try to get first element..");
312             Object nextElement = null;
313             try {
314                 nextElement = xEnum.nextElement();
315             } catch (WrappedTargetException ex) {
316                 log.println("ERROR: could not get nextElement: " + ex.toString());
317                 bOK = false;
318             } catch (NoSuchElementException ex) {
319                 log.println("ERROR: could not get nextElement: " + ex.toString());
320                 bOK = false;
321             }
322             Object shouldElement = selections[i];
323             i++;
324 
325             if (ObjCompare != null) {
326                 ObjCompare.compare(shouldElement, nextElement);
327             } else {
328                 compRes = util.ValueComparer.equalValue(shouldElement, nextElement);
329             }
330 
331             log.println("nextElement()-object and expected object 'selections["+i+"]' are equal: "+compRes);
332 
333             if (!compRes) {
334                 if ((selections[i]) instanceof Object[]){
335                     if (((Object[])selections[i])[0] instanceof Integer) {
336                         log.println("Getting: "+((Integer) ((Object[])shouldElement)[0]).intValue());
337                         log.println("Expected: "+((Integer) ((Object[])selections[i])[0]).intValue());
338                     }
339                 }
340             }
341             bOK &= compRes;
342         }
343 
344         tRes.tested("createSelectionEnumeration()", bOK);
345     }
346 
_createReverseSelectionEnumeration()347     public void _createReverseSelectionEnumeration() {
348         requiredMethod("addSelection()");
349         boolean bOK = true;
350 
351         log.println("clear selections");
352         oObj.clearSelection();
353 
354         int count = oObj.getSelectionCount();
355 
356         bOK &= (count == 0);
357 
358         if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
359 
360         log.println("add some selections...");
361         for(int i = 0; i < selections.length; i++) {
362             try {
363                 log.println("select object from object relation 'selections["+i+"]'");
364                 oObj.addSelection(selections[i]);
365             } catch (com.sun.star.lang.IllegalArgumentException ex) {
366                 log.println("ERROR: could not add selection from object relation 'selections[" + i + "]': " + ex.toString());
367                 bOK = false;
368             }
369             count = oObj.getSelectionCount();
370             if (count != (i+1)){
371                 log.println("ERROR: added a selection but selection count ("+count+ ") " +
372                     "is not as expected (" + (i+1) + ") => FAILED");
373                 bOK = false;
374             }
375         }
376 
377         log.println("create enumeration...");
378         XEnumeration xEnum = oObj.createSelectionEnumeration();
379 
380         boolean compRes = true; //compare result
381         int i = selections.length - 1;
382 
383         while (xEnum.hasMoreElements()){
384             log.println("try to get first element..");
385             Object nextElement = null;
386             try {
387                 nextElement = xEnum.nextElement();
388             } catch (WrappedTargetException ex) {
389                 log.println("ERROR: could not get nextElement: " + ex.toString());
390                 bOK = false;
391             } catch (NoSuchElementException ex) {
392                 log.println("ERROR: could not get nextElement: " + ex.toString());
393                 bOK = false;
394             }
395             Object shouldElement = selections[i];
396             i--;
397 
398             if (ObjCompare != null) {
399                 ObjCompare.compare(shouldElement, nextElement);
400             } else {
401                 compRes = util.ValueComparer.equalValue(shouldElement, nextElement);
402             }
403 
404             log.println("nextElement()-object and expected object 'selections["+i+"]' are equal: "+compRes);
405 
406             if (!compRes) {
407                 if ((selections[i]) instanceof Object[]){
408                     if (((Object[])selections[i])[0] instanceof Integer) {
409                         log.println("Getting: "+((Integer) ((Object[])shouldElement)[0]).intValue());
410                         log.println("Expected: "+((Integer) ((Object[])selections[i])[0]).intValue());
411                     }
412                 }
413             }
414             bOK &= compRes;
415         }
416 
417         tRes.tested("createReverseSelectionEnumeration()", bOK);
418     }
419 
420 }  // finish class _XMultiSelectionSupplier
421 
422 
423 
424