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 compliant. <p>
49 * @see com.sun.star.view.XSelectionSupplier
51 public class _XMultiSelectionSupplier
extends MultiMethodTest
{
53 public XMultiSelectionSupplier oObj
= null;
54 Object
[] selections
= null;
55 Comparator
<Object
> ObjCompare
= null;
57 @SuppressWarnings("unchecked")
59 protected void before() {
60 selections
= (Object
[])tEnv
.getObjRelation("Selections");
61 if (selections
== null) {
62 throw new StatusException(Status
.failed(
63 "Couldn't get relation 'Selections'"));
66 ObjCompare
= (Comparator
<Object
>)tEnv
.getObjRelation("Comparer");
70 protected void after() {
75 * Selects an instance from relation 'First'. <p>
76 * Has <b> OK </b> status if no exceptions were thrown. <p>
78 public void _addSelection() {
82 log
.println("clear selections");
83 oObj
.clearSelection();
85 int count
= oObj
.getSelectionCount();
89 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
91 for(int i
= 0; i
< selections
.length
; i
++) {
93 log
.println("select object from object relation 'selections["+i
+"]'");
94 oObj
.addSelection(selections
[i
]);
95 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
96 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
99 count
= oObj
.getSelectionCount();
101 log
.println("ERROR: add a selection but selection count ("+count
+ ") " +
102 "is not as expected (" + (i
+1) + ") => FAILED");
107 log
.println("try to select object relation 'selections[0]' second time...");
109 count
= oObj
.getSelectionCount();
110 oObj
.addSelection(selections
[0]);
111 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
112 log
.println("ERROR: could not add selection from object relation 'selections[0] a second time': " + ex
.toString());
114 if (count
!= oObj
.getSelectionCount()){
115 log
.println("ERROR: the selected count ("+oObj
.getSelectionCount() +") is not that before (" + count
+ ")");
119 log
.println("try to select invalid object...");
122 oObj
.addSelection(oObj
);
124 log
.println("ERROR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
126 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
127 log
.println("expected exception 'com.sun.star.lang.IllegalArgumentException' => OK");
130 tRes
.tested("addSelection()", bOK
);
133 public void _removeSelection() {
134 requiredMethod("addSelection()");
138 log
.println("clear selections");
139 oObj
.clearSelection();
141 int count
= oObj
.getSelectionCount();
145 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
147 log
.println("add some selections...");
148 for(int i
= 0; i
< selections
.length
; i
++) {
150 log
.println("select object from object relation 'selections["+i
+"]'");
151 oObj
.addSelection(selections
[i
]);
152 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
153 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
156 count
= oObj
.getSelectionCount();
158 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
159 "is not as expected (" + (i
+1) + ") => FAILED");
164 log
.println("try now to remove selections...");
166 count
= oObj
.getSelectionCount();
167 int oldCount
= oObj
.getSelectionCount();
168 for(int i
= 0; i
< selections
.length
; i
++) {
170 log
.println("remove selection for object relation 'selections["+i
+"]'");
171 oObj
.removeSelection(selections
[i
]);
172 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
173 log
.println("ERROR: could not remove selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
176 count
= oObj
.getSelectionCount();
177 if (count
!= (oldCount
- i
- 1)){
178 log
.println("ERROR: removed a selection but selection count ("+count
+ ") " +
179 "is not as expected (" + (oldCount
-i
-1) + ") => FAILED");
184 log
.println("try to remove a removed selection a second time...");
185 count
= oObj
.getSelectionCount();
187 oObj
.removeSelection(selections
[0]);
188 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
189 log
.println("ERROR: could not remove selection from object relation 'selections[0] a second time': " + ex
.toString());
191 if (count
!= oObj
.getSelectionCount()){
192 log
.println("ERROR: the selected count ("+oObj
.getSelectionCount() +") is not that before (" + count
+ ")");
196 log
.println("try to remove invalid object...");
199 oObj
.removeSelection(oObj
);
201 log
.println("ERROR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
203 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
204 log
.println("expected exception 'com.sun.star.lang.IllegalArgumentException' => OK");
207 tRes
.tested("removeSelection()", bOK
);
212 * First test changes selection of the object : if nothing is
213 * currently selected or first instance ('First' relation) is
214 * selected then selects second instance; if second instance
215 * is currently selected then the first instance is selected. <p>
216 * Then <code>getSelection</code> is called and values set and
217 * get are compared. Comparison has some special cases. For
218 * example if selection is a Cell, then the values contained
219 * in cells are compared. <p>
220 * Has <b>OK</b> status if selection changed properly.
222 public void _getSelectionCount() {
223 requiredMethod("addSelection()");
224 tRes
.tested("getSelectionCount()", true);
227 public void _clearSelection() {
228 requiredMethod("addSelection()");
231 log
.println("clear selections");
232 oObj
.clearSelection();
234 int count
= oObj
.getSelectionCount();
238 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
240 log
.println("add some selections...");
241 for(int i
= 0; i
< selections
.length
; i
++) {
243 log
.println("select object from object relation 'selections["+i
+"]'");
244 oObj
.addSelection(selections
[i
]);
245 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
246 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
249 count
= oObj
.getSelectionCount();
251 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
252 "is not as expected (" + (i
+1) + ") => FAILED");
257 count
= oObj
.getSelectionCount();
259 log
.println("clear selections...");
260 oObj
.clearSelection();
262 count
= oObj
.getSelectionCount();
266 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
268 tRes
.tested("clearSelection()", bOK
);
271 public void _createSelectionEnumeration() {
272 requiredMethod("addSelection()");
275 log
.println("clear selections");
276 oObj
.clearSelection();
278 int count
= oObj
.getSelectionCount();
282 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
284 log
.println("add some selections...");
285 for(int i
= 0; i
< selections
.length
; i
++) {
287 log
.println("select object from object relation 'selections["+i
+"]'");
288 oObj
.addSelection(selections
[i
]);
289 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
290 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
293 count
= oObj
.getSelectionCount();
295 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
296 "is not as expected (" + (i
+1) + ") => FAILED");
301 log
.println("create enumeration...");
302 XEnumeration xEnum
= oObj
.createSelectionEnumeration();
304 boolean compRes
= true; //compare result
307 while (xEnum
.hasMoreElements()){
308 log
.println("try to get first element..");
309 Object nextElement
= null;
311 nextElement
= xEnum
.nextElement();
312 } catch (WrappedTargetException ex
) {
313 log
.println("ERROR: could not get nextElement: " + ex
.toString());
315 } catch (NoSuchElementException ex
) {
316 log
.println("ERROR: could not get nextElement: " + ex
.toString());
319 Object shouldElement
= selections
[i
];
322 if (nextElement
!= null)
324 if (ObjCompare
!= null) {
325 compRes
= ObjCompare
.compare(shouldElement
, nextElement
) == 0;
327 compRes
= util
.ValueComparer
.equalValue(shouldElement
, nextElement
);
330 log
.println("nextElement()-object and expected object 'selections["+i
+"]' are equal: "+compRes
);
332 if (!compRes
&& (selections
[i
]) instanceof Object
[]) {
333 if (((Object
[])selections
[i
])[0] instanceof Integer
) {
334 log
.println("Getting: "+((Integer
) ((Object
[])shouldElement
)[0]).intValue());
335 log
.println("Expected: "+((Integer
) ((Object
[])selections
[i
])[0]).intValue());
345 tRes
.tested("createSelectionEnumeration()", bOK
);
348 public void _createReverseSelectionEnumeration() {
349 requiredMethod("addSelection()");
352 log
.println("clear selections");
353 oObj
.clearSelection();
355 int count
= oObj
.getSelectionCount();
359 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
361 log
.println("add some selections...");
362 for(int i
= 0; i
< selections
.length
; i
++) {
364 log
.println("select object from object relation 'selections["+i
+"]'");
365 oObj
.addSelection(selections
[i
]);
366 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
367 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
370 count
= oObj
.getSelectionCount();
372 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
373 "is not as expected (" + (i
+1) + ") => FAILED");
378 log
.println("create enumeration...");
379 XEnumeration xEnum
= oObj
.createSelectionEnumeration();
381 boolean compRes
= true; //compare result
382 int i
= selections
.length
- 1;
384 while (xEnum
.hasMoreElements()){
385 log
.println("try to get first element..");
386 Object nextElement
= null;
388 nextElement
= xEnum
.nextElement();
389 } catch (WrappedTargetException ex
) {
390 log
.println("ERROR: could not get nextElement: " + ex
.toString());
392 } catch (NoSuchElementException ex
) {
393 log
.println("ERROR: could not get nextElement: " + ex
.toString());
396 Object shouldElement
= selections
[i
];
399 if (nextElement
!= null) {
400 if (ObjCompare
!= null) {
401 compRes
= ObjCompare
.compare(shouldElement
, nextElement
) == 0;
403 compRes
= util
.ValueComparer
.equalValue(shouldElement
, nextElement
);
406 log
.println("nextElement()-object and expected object 'selections["+i
+"]' are equal: "+compRes
);
408 if (!compRes
&& (selections
[i
]) instanceof Object
[]){
409 if (((Object
[])selections
[i
])[0] instanceof Integer
) {
410 log
.println("Getting: "+((Integer
) ((Object
[])shouldElement
)[0]).intValue());
411 log
.println("Expected: "+((Integer
) ((Object
[])selections
[i
])[0]).intValue());
422 tRes
.tested("createReverseSelectionEnumeration()", bOK
);
425 } // finish class _XMultiSelectionSupplier