1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XMultiSelectionSupplier.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import com
.sun
.star
.container
.NoSuchElementException
;
34 import com
.sun
.star
.container
.XEnumeration
;
35 import com
.sun
.star
.lang
.WrappedTargetException
;
36 import com
.sun
.star
.view
.XMultiSelectionSupplier
;
37 import java
.util
.Comparator
;
38 import lib
.MultiMethodTest
;
40 import lib
.StatusException
;
43 * Testing <code>com.sun.star.view.XSelectionSupplier</code>
46 * <li><code> addSelection()</code></li>
47 * <li><code> removeSelection()</code></li>
48 * <li><code> clearSelection()</code></li>
49 * <li><code> getSelectionCount()</code></li>
50 * <li><code> createSelectionEnumeration()</code></li>
51 * <li><code> createReverseSelectionEnumeration()</code></li>
53 * This test needs the following object relations :
55 * <li> <code>'Selections'</code> of type <code>Object[]</code> :
56 * the array of the instances which can be selected.</li>
57 * <li> <code>'Comparer'</code> of type <code>Comparator</code> :
58 * the interface for comparing of selected instances</li>
60 * Test is <b> NOT </b> multithread compilant. <p>
61 * @see com.sun.star.view.XSelectionSupplier
63 public class _XMultiSelectionSupplier
extends MultiMethodTest
{
65 public XMultiSelectionSupplier oObj
= null;
66 public boolean selectionChanged
= false;
67 Object
[] selections
= null;
68 Comparator ObjCompare
= null;
70 protected void before() {
71 selections
= (Object
[])tEnv
.getObjRelation("Selections");
72 if (selections
== null) {
73 throw new StatusException(Status
.failed(
74 "Couldn't get relation 'Selections'"));
77 ObjCompare
= (Comparator
)tEnv
.getObjRelation("Comparer");
80 protected void after() {
85 * Selects an instance from relation 'First'. <p>
86 * Has <b> OK </b> status if no exceptions were thrown. <p>
88 public void _addSelection() {
92 log
.println("clear selections");
93 oObj
.clearSelection();
95 int count
= oObj
.getSelectionCount();
99 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
101 for(int i
= 0; i
< selections
.length
; i
++) {
103 log
.println("select object from object relation 'selections["+i
+"]'");
104 oObj
.addSelection(selections
[i
]);
105 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
106 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
109 count
= oObj
.getSelectionCount();
111 log
.println("ERROR: add a selection but selection count ("+count
+ ") " +
112 "is not as expected (" + (i
+1) + ") => FAILED");
117 log
.println("try to select object relation 'selections[0]' second time...");
119 count
= oObj
.getSelectionCount();
120 oObj
.addSelection(selections
[0]);
121 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
122 log
.println("ERROR: could not add selection from object relation 'selections[0] a second time': " + ex
.toString());
124 if (count
!= oObj
.getSelectionCount()){
125 log
.println("ERROR: the selected count ("+oObj
.getSelectionCount() +") is not that before (" + count
+ ")");
129 log
.println("try to select invalid object...");
132 oObj
.addSelection(oObj
);
134 log
.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
136 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
137 log
.println("expected exception 'com.sun.star.lang.IllegalArgumentException' => OK");
140 tRes
.tested("addSelection()", bOK
);
143 public void _removeSelection() {
144 requiredMethod("addSelection()");
148 log
.println("clear selections");
149 oObj
.clearSelection();
151 int count
= oObj
.getSelectionCount();
155 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
157 log
.println("add some selections...");
158 for(int i
= 0; i
< selections
.length
; i
++) {
160 log
.println("select object from object relation 'selections["+i
+"]'");
161 oObj
.addSelection(selections
[i
]);
162 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
163 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
166 count
= oObj
.getSelectionCount();
168 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
169 "is not as expected (" + (i
+1) + ") => FAILED");
174 log
.println("try now to remove selections...");
176 count
= oObj
.getSelectionCount();
177 int oldCount
= oObj
.getSelectionCount();
178 for(int i
= 0; i
< selections
.length
; i
++) {
180 log
.println("remove selection for object relation 'selections["+i
+"]'");
181 oObj
.removeSelection(selections
[i
]);
182 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
183 log
.println("ERROR: could not remove selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
186 count
= oObj
.getSelectionCount();
187 if (count
!= (oldCount
- i
- 1)){
188 log
.println("ERROR: removed a selection but selection count ("+count
+ ") " +
189 "is not as expected (" + (oldCount
-i
-1) + ") => FAILED");
194 log
.println("try to remove a removed selection a second time...");
195 count
= oObj
.getSelectionCount();
197 oObj
.removeSelection(selections
[0]);
198 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
199 log
.println("ERROR: could not remove selection from object relation 'selections[0] a second time': " + ex
.toString());
201 if (count
!= oObj
.getSelectionCount()){
202 log
.println("ERROR: the selected count ("+oObj
.getSelectionCount() +") is not that before (" + count
+ ")");
206 log
.println("try to remove invalid object...");
209 oObj
.removeSelection(oObj
);
211 log
.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
213 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
214 log
.println("expected exception 'com.sun.star.lang.IllegalArgumentException' => OK");
217 tRes
.tested("removeSelection()", bOK
);
222 * First test changes selection of the object : if nothing is
223 * currently selected or first instance ('First' relation) is
224 * selected then selects second instance; if second instance
225 * is currently selected then the first instance is selected. <p>
226 * Then <code>getSelection</code> is called and values set and
227 * get are compared. Comparison has some special cases. For
228 * example if selection is a Cell, then the values contained
229 * in cells are compared. <p>
230 * Has <b>OK</b> status if selection changed properly.
232 public void _getSelectionCount() {
233 requiredMethod("addSelection()");
234 tRes
.tested("getSelectionCount()", true);
237 public void _clearSelection() {
238 requiredMethod("addSelection()");
241 log
.println("clear selections");
242 oObj
.clearSelection();
244 int count
= oObj
.getSelectionCount();
248 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
250 log
.println("add some selections...");
251 for(int i
= 0; i
< selections
.length
; i
++) {
253 log
.println("select object from object relation 'selections["+i
+"]'");
254 oObj
.addSelection(selections
[i
]);
255 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
256 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
259 count
= oObj
.getSelectionCount();
261 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
262 "is not as expected (" + (i
+1) + ") => FAILED");
267 count
= oObj
.getSelectionCount();
269 log
.println("clear selections...");
270 oObj
.clearSelection();
272 count
= oObj
.getSelectionCount();
276 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
278 tRes
.tested("clearSelection()", bOK
);
281 public void _createSelectionEnumeration() {
282 requiredMethod("addSelection()");
285 log
.println("clear selections");
286 oObj
.clearSelection();
288 int count
= oObj
.getSelectionCount();
292 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
294 log
.println("add some selections...");
295 for(int i
= 0; i
< selections
.length
; i
++) {
297 log
.println("select object from object relation 'selections["+i
+"]'");
298 oObj
.addSelection(selections
[i
]);
299 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
300 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
303 count
= oObj
.getSelectionCount();
305 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
306 "is not as expected (" + (i
+1) + ") => FAILED");
311 log
.println("create enumeration...");
312 XEnumeration xEnum
= oObj
.createSelectionEnumeration();
314 boolean compRes
= true; //compare result
317 while (xEnum
.hasMoreElements()){
318 log
.println("try to get first element..");
319 Object nextElement
= null;
321 nextElement
= xEnum
.nextElement();
322 } catch (WrappedTargetException ex
) {
323 log
.println("ERROR: could not get nextElement: " + ex
.toString());
325 } catch (NoSuchElementException ex
) {
326 log
.println("ERROR: could not get nextElement: " + ex
.toString());
329 Object shouldElement
= selections
[i
];
332 if (ObjCompare
!= null) {
333 ObjCompare
.compare(shouldElement
, nextElement
);
335 compRes
= util
.ValueComparer
.equalValue(shouldElement
, nextElement
);
338 log
.println("nextElement()-object and expected object 'selections["+i
+"]' are equal: "+compRes
);
341 if ((selections
[i
]) instanceof Object
[]){
342 if (((Object
[])selections
[i
])[0] instanceof Integer
) {
343 log
.println("Getting: "+((Integer
) ((Object
[])shouldElement
)[0]).intValue());
344 log
.println("Expected: "+((Integer
) ((Object
[])selections
[i
])[0]).intValue());
351 tRes
.tested("createSelectionEnumeration()", bOK
);
354 public void _createReverseSelectionEnumeration() {
355 requiredMethod("addSelection()");
358 log
.println("clear selections");
359 oObj
.clearSelection();
361 int count
= oObj
.getSelectionCount();
365 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
367 log
.println("add some selections...");
368 for(int i
= 0; i
< selections
.length
; i
++) {
370 log
.println("select object from object relation 'selections["+i
+"]'");
371 oObj
.addSelection(selections
[i
]);
372 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
373 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
376 count
= oObj
.getSelectionCount();
378 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
379 "is not as expected (" + (i
+1) + ") => FAILED");
384 log
.println("create enumeration...");
385 XEnumeration xEnum
= oObj
.createSelectionEnumeration();
387 boolean compRes
= true; //compare result
388 int i
= selections
.length
- 1;
390 while (xEnum
.hasMoreElements()){
391 log
.println("try to get first element..");
392 Object nextElement
= null;
394 nextElement
= xEnum
.nextElement();
395 } catch (WrappedTargetException ex
) {
396 log
.println("ERROR: could not get nextElement: " + ex
.toString());
398 } catch (NoSuchElementException ex
) {
399 log
.println("ERROR: could not get nextElement: " + ex
.toString());
402 Object shouldElement
= selections
[i
];
405 if (ObjCompare
!= null) {
406 ObjCompare
.compare(shouldElement
, nextElement
);
408 compRes
= util
.ValueComparer
.equalValue(shouldElement
, nextElement
);
411 log
.println("nextElement()-object and expected object 'selections["+i
+"]' are equal: "+compRes
);
414 if ((selections
[i
]) instanceof Object
[]){
415 if (((Object
[])selections
[i
])[0] instanceof Integer
) {
416 log
.println("Getting: "+((Integer
) ((Object
[])shouldElement
)[0]).intValue());
417 log
.println("Expected: "+((Integer
) ((Object
[])selections
[i
])[0]).intValue());
424 tRes
.tested("createReverseSelectionEnumeration()", bOK
);
427 } // finish class _XMultiSelectionSupplier