bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / view / _XMultiSelectionSupplier.java
blob3ca7861c0db0a5b28aea45578cc720c5b85ae8a1
1 /*
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 .
19 package ifc.view;
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;
27 import lib.Status;
28 import lib.StatusException;
30 /**
31 * Testing <code>com.sun.star.view.XSelectionSupplier</code>
32 * interface methods :
33 * <ul>
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>
40 * </ul>
41 * This test needs the following object relations :
42 * <ul>
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>
47 * <ul> <p>
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() {
69 disposeEnvironment();
72 /**
73 * Selects an instance from relation 'First'. <p>
74 * Has <b> OK </b> status if no exceptions were thrown. <p>
76 public void _addSelection() {
78 boolean bOK = true;
80 log.println("clear selections");
81 oObj.clearSelection();
83 int count = oObj.getSelectionCount();
85 bOK &= (count == 0);
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++) {
90 try {
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());
95 bOK = false;
97 count = oObj.getSelectionCount();
98 if (count != (i+1)){
99 log.println("ERROR: add a selection but selection count ("+count+ ") " +
100 "is not as expected (" + (i+1) + ") => FAILED");
101 bOK = false;
105 log.println("try to select object relation 'selections[0]' second time...");
106 try {
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 + ")");
114 bOK = false;
117 log.println("try to select invalid object...");
118 try {
120 oObj.addSelection(oObj);
122 log.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
123 bOK = false;
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()");
134 boolean bOK = true;
136 log.println("clear selections");
137 oObj.clearSelection();
139 int count = oObj.getSelectionCount();
141 bOK &= (count == 0);
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++) {
147 try {
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());
152 bOK = false;
154 count = oObj.getSelectionCount();
155 if (count != (i+1)){
156 log.println("ERROR: added a selection but selection count ("+count+ ") " +
157 "is not as expected (" + (i+1) + ") => FAILED");
158 bOK = false;
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++) {
167 try {
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());
172 bOK = false;
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");
178 bOK = false;
182 log.println("try to remove a removed selection a second time...");
183 count = oObj.getSelectionCount();
184 try {
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 + ")");
191 bOK = false;
194 log.println("try to remove invalid object...");
195 try {
197 oObj.removeSelection(oObj);
199 log.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
200 bOK = false;
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()");
227 boolean bOK = true;
229 log.println("clear selections");
230 oObj.clearSelection();
232 int count = oObj.getSelectionCount();
234 bOK &= (count == 0);
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++) {
240 try {
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());
245 bOK = false;
247 count = oObj.getSelectionCount();
248 if (count != (i+1)){
249 log.println("ERROR: added a selection but selection count ("+count+ ") " +
250 "is not as expected (" + (i+1) + ") => FAILED");
251 bOK = false;
255 count = oObj.getSelectionCount();
257 log.println("clear selections...");
258 oObj.clearSelection();
260 count = oObj.getSelectionCount();
262 bOK &= (count == 0);
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()");
271 boolean bOK = true;
273 log.println("clear selections");
274 oObj.clearSelection();
276 int count = oObj.getSelectionCount();
278 bOK &= (count == 0);
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++) {
284 try {
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());
289 bOK = false;
291 count = oObj.getSelectionCount();
292 if (count != (i+1)){
293 log.println("ERROR: added a selection but selection count ("+count+ ") " +
294 "is not as expected (" + (i+1) + ") => FAILED");
295 bOK = false;
299 log.println("create enumeration...");
300 XEnumeration xEnum = oObj.createSelectionEnumeration();
302 boolean compRes = true; //compare result
303 int i = 0;
305 while (xEnum.hasMoreElements()){
306 log.println("try to get first element..");
307 Object nextElement = null;
308 try {
309 nextElement = xEnum.nextElement();
310 } catch (WrappedTargetException ex) {
311 log.println("ERROR: could not get nextElement: " + ex.toString());
312 bOK = false;
313 } catch (NoSuchElementException ex) {
314 log.println("ERROR: could not get nextElement: " + ex.toString());
315 bOK = false;
317 Object shouldElement = selections[i];
318 i++;
320 if (ObjCompare != null) {
321 ObjCompare.compare(shouldElement, nextElement);
322 } else {
323 compRes = util.ValueComparer.equalValue(shouldElement, nextElement);
326 log.println("nextElement()-object and expected object 'selections["+i+"]' are equal: "+compRes);
328 if (!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());
336 bOK &= compRes;
339 tRes.tested("createSelectionEnumeration()", bOK);
342 public void _createReverseSelectionEnumeration() {
343 requiredMethod("addSelection()");
344 boolean bOK = true;
346 log.println("clear selections");
347 oObj.clearSelection();
349 int count = oObj.getSelectionCount();
351 bOK &= (count == 0);
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++) {
357 try {
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());
362 bOK = false;
364 count = oObj.getSelectionCount();
365 if (count != (i+1)){
366 log.println("ERROR: added a selection but selection count ("+count+ ") " +
367 "is not as expected (" + (i+1) + ") => FAILED");
368 bOK = false;
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;
381 try {
382 nextElement = xEnum.nextElement();
383 } catch (WrappedTargetException ex) {
384 log.println("ERROR: could not get nextElement: " + ex.toString());
385 bOK = false;
386 } catch (NoSuchElementException ex) {
387 log.println("ERROR: could not get nextElement: " + ex.toString());
388 bOK = false;
390 Object shouldElement = selections[i];
391 i--;
393 if (ObjCompare != null) {
394 ObjCompare.compare(shouldElement, nextElement);
395 } else {
396 compRes = util.ValueComparer.equalValue(shouldElement, nextElement);
399 log.println("nextElement()-object and expected object 'selections["+i+"]' are equal: "+compRes);
401 if (!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());
409 bOK &= compRes;
412 tRes.tested("createReverseSelectionEnumeration()", bOK);
415 } // finish class _XMultiSelectionSupplier