Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / view / _XMultiSelectionSupplier.java
blobf7c334b30c3c3f1b917bc24078962df53fa39c68
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 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;
58 @Override
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");
69 @Override
70 protected void after() {
71 disposeEnvironment();
74 /**
75 * Selects an instance from relation 'First'. <p>
76 * Has <b> OK </b> status if no exceptions were thrown. <p>
78 public void _addSelection() {
80 boolean bOK = true;
82 log.println("clear selections");
83 oObj.clearSelection();
85 int count = oObj.getSelectionCount();
87 bOK &= (count == 0);
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++) {
92 try {
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());
97 bOK = false;
99 count = oObj.getSelectionCount();
100 if (count != (i+1)){
101 log.println("ERROR: add a selection but selection count ("+count+ ") " +
102 "is not as expected (" + (i+1) + ") => FAILED");
103 bOK = false;
107 log.println("try to select object relation 'selections[0]' second time...");
108 try {
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 + ")");
116 bOK = false;
119 log.println("try to select invalid object...");
120 try {
122 oObj.addSelection(oObj);
124 log.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
125 bOK = false;
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()");
136 boolean bOK = true;
138 log.println("clear selections");
139 oObj.clearSelection();
141 int count = oObj.getSelectionCount();
143 bOK &= (count == 0);
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++) {
149 try {
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());
154 bOK = false;
156 count = oObj.getSelectionCount();
157 if (count != (i+1)){
158 log.println("ERROR: added a selection but selection count ("+count+ ") " +
159 "is not as expected (" + (i+1) + ") => FAILED");
160 bOK = false;
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++) {
169 try {
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());
174 bOK = false;
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");
180 bOK = false;
184 log.println("try to remove a removed selection a second time...");
185 count = oObj.getSelectionCount();
186 try {
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 + ")");
193 bOK = false;
196 log.println("try to remove invalid object...");
197 try {
199 oObj.removeSelection(oObj);
201 log.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
202 bOK = false;
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()");
229 boolean bOK = true;
231 log.println("clear selections");
232 oObj.clearSelection();
234 int count = oObj.getSelectionCount();
236 bOK &= (count == 0);
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++) {
242 try {
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());
247 bOK = false;
249 count = oObj.getSelectionCount();
250 if (count != (i+1)){
251 log.println("ERROR: added a selection but selection count ("+count+ ") " +
252 "is not as expected (" + (i+1) + ") => FAILED");
253 bOK = false;
257 count = oObj.getSelectionCount();
259 log.println("clear selections...");
260 oObj.clearSelection();
262 count = oObj.getSelectionCount();
264 bOK &= (count == 0);
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()");
273 boolean bOK = true;
275 log.println("clear selections");
276 oObj.clearSelection();
278 int count = oObj.getSelectionCount();
280 bOK &= (count == 0);
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++) {
286 try {
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());
291 bOK = false;
293 count = oObj.getSelectionCount();
294 if (count != (i+1)){
295 log.println("ERROR: added a selection but selection count ("+count+ ") " +
296 "is not as expected (" + (i+1) + ") => FAILED");
297 bOK = false;
301 log.println("create enumeration...");
302 XEnumeration xEnum = oObj.createSelectionEnumeration();
304 boolean compRes = true; //compare result
305 int i = 0;
307 while (xEnum.hasMoreElements()){
308 log.println("try to get first element..");
309 Object nextElement = null;
310 try {
311 nextElement = xEnum.nextElement();
312 } catch (WrappedTargetException ex) {
313 log.println("ERROR: could not get nextElement: " + ex.toString());
314 bOK = false;
315 } catch (NoSuchElementException ex) {
316 log.println("ERROR: could not get nextElement: " + ex.toString());
317 bOK = false;
319 Object shouldElement = selections[i];
320 i++;
322 if (ObjCompare != null) {
323 ObjCompare.compare(shouldElement, nextElement);
324 } else {
325 compRes = util.ValueComparer.equalValue(shouldElement, nextElement);
328 log.println("nextElement()-object and expected object 'selections["+i+"]' are equal: "+compRes);
330 if (!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());
338 bOK &= compRes;
341 tRes.tested("createSelectionEnumeration()", bOK);
344 public void _createReverseSelectionEnumeration() {
345 requiredMethod("addSelection()");
346 boolean bOK = true;
348 log.println("clear selections");
349 oObj.clearSelection();
351 int count = oObj.getSelectionCount();
353 bOK &= (count == 0);
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++) {
359 try {
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());
364 bOK = false;
366 count = oObj.getSelectionCount();
367 if (count != (i+1)){
368 log.println("ERROR: added a selection but selection count ("+count+ ") " +
369 "is not as expected (" + (i+1) + ") => FAILED");
370 bOK = false;
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;
383 try {
384 nextElement = xEnum.nextElement();
385 } catch (WrappedTargetException ex) {
386 log.println("ERROR: could not get nextElement: " + ex.toString());
387 bOK = false;
388 } catch (NoSuchElementException ex) {
389 log.println("ERROR: could not get nextElement: " + ex.toString());
390 bOK = false;
392 Object shouldElement = selections[i];
393 i--;
395 if (ObjCompare != null) {
396 ObjCompare.compare(shouldElement, nextElement);
397 } else {
398 compRes = util.ValueComparer.equalValue(shouldElement, nextElement);
401 log.println("nextElement()-object and expected object 'selections["+i+"]' are equal: "+compRes);
403 if (!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());
411 bOK &= compRes;
414 tRes.tested("createReverseSelectionEnumeration()", bOK);
417 } // finish class _XMultiSelectionSupplier