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 .
21 import com
.sun
.star
.container
.NoSuchElementException
;
22 import com
.sun
.star
.container
.XEnumeration
;
23 import com
.sun
.star
.lang
.WrappedTargetException
;
24 import com
.sun
.star
.view
.XMultiSelectionSupplier
;
25 import java
.util
.Comparator
;
26 import lib
.MultiMethodTest
;
28 import lib
.StatusException
;
31 * Testing <code>com.sun.star.view.XSelectionSupplier</code>
34 * <li><code> addSelection()</code></li>
35 * <li><code> removeSelection()</code></li>
36 * <li><code> clearSelection()</code></li>
37 * <li><code> getSelectionCount()</code></li>
38 * <li><code> createSelectionEnumeration()</code></li>
39 * <li><code> createReverseSelectionEnumeration()</code></li>
41 * This test needs the following object relations :
43 * <li> <code>'Selections'</code> of type <code>Object[]</code> :
44 * the array of the instances which can be selected.</li>
45 * <li> <code>'Comparer'</code> of type <code>Comparator</code> :
46 * the interface for comparing of selected instances</li>
48 * Test is <b> NOT </b> multithread compilant. <p>
49 * @see com.sun.star.view.XSelectionSupplier
51 public class _XMultiSelectionSupplier
extends MultiMethodTest
{
53 public XMultiSelectionSupplier oObj
= null;
54 public boolean selectionChanged
= false;
55 Object
[] selections
= null;
56 Comparator
<Object
> ObjCompare
= null;
58 protected void before() {
59 selections
= (Object
[])tEnv
.getObjRelation("Selections");
60 if (selections
== null) {
61 throw new StatusException(Status
.failed(
62 "Couldn't get relation 'Selections'"));
65 ObjCompare
= (Comparator
<Object
>)tEnv
.getObjRelation("Comparer");
68 protected void after() {
73 * Selects an instance from relation 'First'. <p>
74 * Has <b> OK </b> status if no exceptions were thrown. <p>
76 public void _addSelection() {
80 log
.println("clear selections");
81 oObj
.clearSelection();
83 int count
= oObj
.getSelectionCount();
87 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
89 for(int i
= 0; i
< selections
.length
; i
++) {
91 log
.println("select object from object relation 'selections["+i
+"]'");
92 oObj
.addSelection(selections
[i
]);
93 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
94 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
97 count
= oObj
.getSelectionCount();
99 log
.println("ERROR: add a selection but selection count ("+count
+ ") " +
100 "is not as expected (" + (i
+1) + ") => FAILED");
105 log
.println("try to select object relation 'selections[0]' second time...");
107 count
= oObj
.getSelectionCount();
108 oObj
.addSelection(selections
[0]);
109 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
110 log
.println("ERROR: could not add selection from object relation 'selections[0] a second time': " + ex
.toString());
112 if (count
!= oObj
.getSelectionCount()){
113 log
.println("ERROR: the selected count ("+oObj
.getSelectionCount() +") is not that before (" + count
+ ")");
117 log
.println("try to select invalid object...");
120 oObj
.addSelection(oObj
);
122 log
.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
124 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
125 log
.println("expected exception 'com.sun.star.lang.IllegalArgumentException' => OK");
128 tRes
.tested("addSelection()", bOK
);
131 public void _removeSelection() {
132 requiredMethod("addSelection()");
136 log
.println("clear selections");
137 oObj
.clearSelection();
139 int count
= oObj
.getSelectionCount();
143 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
145 log
.println("add some selections...");
146 for(int i
= 0; i
< selections
.length
; i
++) {
148 log
.println("select object from object relation 'selections["+i
+"]'");
149 oObj
.addSelection(selections
[i
]);
150 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
151 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
154 count
= oObj
.getSelectionCount();
156 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
157 "is not as expected (" + (i
+1) + ") => FAILED");
162 log
.println("try now to remove selections...");
164 count
= oObj
.getSelectionCount();
165 int oldCount
= oObj
.getSelectionCount();
166 for(int i
= 0; i
< selections
.length
; i
++) {
168 log
.println("remove selection for object relation 'selections["+i
+"]'");
169 oObj
.removeSelection(selections
[i
]);
170 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
171 log
.println("ERROR: could not remove selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
174 count
= oObj
.getSelectionCount();
175 if (count
!= (oldCount
- i
- 1)){
176 log
.println("ERROR: removed a selection but selection count ("+count
+ ") " +
177 "is not as expected (" + (oldCount
-i
-1) + ") => FAILED");
182 log
.println("try to remove a removed selection a second time...");
183 count
= oObj
.getSelectionCount();
185 oObj
.removeSelection(selections
[0]);
186 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
187 log
.println("ERROR: could not remove selection from object relation 'selections[0] a second time': " + ex
.toString());
189 if (count
!= oObj
.getSelectionCount()){
190 log
.println("ERROR: the selected count ("+oObj
.getSelectionCount() +") is not that before (" + count
+ ")");
194 log
.println("try to remove invalid object...");
197 oObj
.removeSelection(oObj
);
199 log
.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
201 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
202 log
.println("expected exception 'com.sun.star.lang.IllegalArgumentException' => OK");
205 tRes
.tested("removeSelection()", bOK
);
210 * First test changes selection of the object : if nothing is
211 * currently selected or first instance ('First' relation) is
212 * selected then selects second instance; if second instance
213 * is currently selected then the first instance is selected. <p>
214 * Then <code>getSelection</code> is called and values set and
215 * get are compared. Comparison has some special cases. For
216 * example if selection is a Cell, then the values contained
217 * in cells are compared. <p>
218 * Has <b>OK</b> status if selection changed properly.
220 public void _getSelectionCount() {
221 requiredMethod("addSelection()");
222 tRes
.tested("getSelectionCount()", true);
225 public void _clearSelection() {
226 requiredMethod("addSelection()");
229 log
.println("clear selections");
230 oObj
.clearSelection();
232 int count
= oObj
.getSelectionCount();
236 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
238 log
.println("add some selections...");
239 for(int i
= 0; i
< selections
.length
; i
++) {
241 log
.println("select object from object relation 'selections["+i
+"]'");
242 oObj
.addSelection(selections
[i
]);
243 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
244 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
247 count
= oObj
.getSelectionCount();
249 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
250 "is not as expected (" + (i
+1) + ") => FAILED");
255 count
= oObj
.getSelectionCount();
257 log
.println("clear selections...");
258 oObj
.clearSelection();
260 count
= oObj
.getSelectionCount();
264 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
266 tRes
.tested("clearSelection()", bOK
);
269 public void _createSelectionEnumeration() {
270 requiredMethod("addSelection()");
273 log
.println("clear selections");
274 oObj
.clearSelection();
276 int count
= oObj
.getSelectionCount();
280 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
282 log
.println("add some selections...");
283 for(int i
= 0; i
< selections
.length
; i
++) {
285 log
.println("select object from object relation 'selections["+i
+"]'");
286 oObj
.addSelection(selections
[i
]);
287 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
288 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
291 count
= oObj
.getSelectionCount();
293 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
294 "is not as expected (" + (i
+1) + ") => FAILED");
299 log
.println("create enumeration...");
300 XEnumeration xEnum
= oObj
.createSelectionEnumeration();
302 boolean compRes
= true; //compare result
305 while (xEnum
.hasMoreElements()){
306 log
.println("try to get first element..");
307 Object nextElement
= null;
309 nextElement
= xEnum
.nextElement();
310 } catch (WrappedTargetException ex
) {
311 log
.println("ERROR: could not get nextElement: " + ex
.toString());
313 } catch (NoSuchElementException ex
) {
314 log
.println("ERROR: could not get nextElement: " + ex
.toString());
317 Object shouldElement
= selections
[i
];
320 if (ObjCompare
!= null) {
321 ObjCompare
.compare(shouldElement
, nextElement
);
323 compRes
= util
.ValueComparer
.equalValue(shouldElement
, nextElement
);
326 log
.println("nextElement()-object and expected object 'selections["+i
+"]' are equal: "+compRes
);
329 if ((selections
[i
]) instanceof Object
[]){
330 if (((Object
[])selections
[i
])[0] instanceof Integer
) {
331 log
.println("Getting: "+((Integer
) ((Object
[])shouldElement
)[0]).intValue());
332 log
.println("Expected: "+((Integer
) ((Object
[])selections
[i
])[0]).intValue());
339 tRes
.tested("createSelectionEnumeration()", bOK
);
342 public void _createReverseSelectionEnumeration() {
343 requiredMethod("addSelection()");
346 log
.println("clear selections");
347 oObj
.clearSelection();
349 int count
= oObj
.getSelectionCount();
353 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
355 log
.println("add some selections...");
356 for(int i
= 0; i
< selections
.length
; i
++) {
358 log
.println("select object from object relation 'selections["+i
+"]'");
359 oObj
.addSelection(selections
[i
]);
360 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
361 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
364 count
= oObj
.getSelectionCount();
366 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
367 "is not as expected (" + (i
+1) + ") => FAILED");
372 log
.println("create enumeration...");
373 XEnumeration xEnum
= oObj
.createSelectionEnumeration();
375 boolean compRes
= true; //compare result
376 int i
= selections
.length
- 1;
378 while (xEnum
.hasMoreElements()){
379 log
.println("try to get first element..");
380 Object nextElement
= null;
382 nextElement
= xEnum
.nextElement();
383 } catch (WrappedTargetException ex
) {
384 log
.println("ERROR: could not get nextElement: " + ex
.toString());
386 } catch (NoSuchElementException ex
) {
387 log
.println("ERROR: could not get nextElement: " + ex
.toString());
390 Object shouldElement
= selections
[i
];
393 if (ObjCompare
!= null) {
394 ObjCompare
.compare(shouldElement
, nextElement
);
396 compRes
= util
.ValueComparer
.equalValue(shouldElement
, nextElement
);
399 log
.println("nextElement()-object and expected object 'selections["+i
+"]' are equal: "+compRes
);
402 if ((selections
[i
]) instanceof Object
[]){
403 if (((Object
[])selections
[i
])[0] instanceof Integer
) {
404 log
.println("Getting: "+((Integer
) ((Object
[])shouldElement
)[0]).intValue());
405 log
.println("Expected: "+((Integer
) ((Object
[])selections
[i
])[0]).intValue());
412 tRes
.tested("createReverseSelectionEnumeration()", bOK
);
415 } // finish class _XMultiSelectionSupplier