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