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 public boolean selectionChanged
= false;
55 Object
[] selections
= null;
56 Comparator
<Object
> ObjCompare
= null;
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("ERORR: 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("ERORR: 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 (ObjCompare
!= null) {
323 ObjCompare
.compare(shouldElement
, nextElement
);
325 compRes
= util
.ValueComparer
.equalValue(shouldElement
, nextElement
);
328 log
.println("nextElement()-object and expected object 'selections["+i
+"]' are equal: "+compRes
);
331 if ((selections
[i
]) instanceof Object
[]){
332 if (((Object
[])selections
[i
])[0] instanceof Integer
) {
333 log
.println("Getting: "+((Integer
) ((Object
[])shouldElement
)[0]).intValue());
334 log
.println("Expected: "+((Integer
) ((Object
[])selections
[i
])[0]).intValue());
341 tRes
.tested("createSelectionEnumeration()", bOK
);
344 public void _createReverseSelectionEnumeration() {
345 requiredMethod("addSelection()");
348 log
.println("clear selections");
349 oObj
.clearSelection();
351 int count
= oObj
.getSelectionCount();
355 if ( ! bOK
) log
.println("ERROR: after clear selection I got a selection count of '" + count
+ "' => FAILED");
357 log
.println("add some selections...");
358 for(int i
= 0; i
< selections
.length
; i
++) {
360 log
.println("select object from object relation 'selections["+i
+"]'");
361 oObj
.addSelection(selections
[i
]);
362 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
363 log
.println("ERROR: could not add selection from object relation 'selections[" + i
+ "]': " + ex
.toString());
366 count
= oObj
.getSelectionCount();
368 log
.println("ERROR: added a selection but selection count ("+count
+ ") " +
369 "is not as expected (" + (i
+1) + ") => FAILED");
374 log
.println("create enumeration...");
375 XEnumeration xEnum
= oObj
.createSelectionEnumeration();
377 boolean compRes
= true; //compare result
378 int i
= selections
.length
- 1;
380 while (xEnum
.hasMoreElements()){
381 log
.println("try to get first element..");
382 Object nextElement
= null;
384 nextElement
= xEnum
.nextElement();
385 } catch (WrappedTargetException ex
) {
386 log
.println("ERROR: could not get nextElement: " + ex
.toString());
388 } catch (NoSuchElementException ex
) {
389 log
.println("ERROR: could not get nextElement: " + ex
.toString());
392 Object shouldElement
= selections
[i
];
395 if (ObjCompare
!= null) {
396 ObjCompare
.compare(shouldElement
, nextElement
);
398 compRes
= util
.ValueComparer
.equalValue(shouldElement
, nextElement
);
401 log
.println("nextElement()-object and expected object 'selections["+i
+"]' are equal: "+compRes
);
404 if ((selections
[i
]) instanceof Object
[]){
405 if (((Object
[])selections
[i
])[0] instanceof Integer
) {
406 log
.println("Getting: "+((Integer
) ((Object
[])shouldElement
)[0]).intValue());
407 log
.println("Expected: "+((Integer
) ((Object
[])selections
[i
])[0]).intValue());
414 tRes
.tested("createReverseSelectionEnumeration()", bOK
);
417 } // finish class _XMultiSelectionSupplier