merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / view / _XMultiSelectionSupplier.java
blob1790809c619b344e37e8faec42e7458b99be17e0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XMultiSelectionSupplier.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.view;
33 import com.sun.star.container.NoSuchElementException;
34 import com.sun.star.container.XEnumeration;
35 import com.sun.star.lang.WrappedTargetException;
36 import com.sun.star.view.XMultiSelectionSupplier;
37 import java.util.Comparator;
38 import lib.MultiMethodTest;
39 import lib.Status;
40 import lib.StatusException;
42 /**
43 * Testing <code>com.sun.star.view.XSelectionSupplier</code>
44 * interface methods :
45 * <ul>
46 * <li><code> addSelection()</code></li>
47 * <li><code> removeSelection()</code></li>
48 * <li><code> clearSelection()</code></li>
49 * <li><code> getSelectionCount()</code></li>
50 * <li><code> createSelectionEnumeration()</code></li>
51 * <li><code> createReverseSelectionEnumeration()</code></li>
52 * </ul>
53 * This test needs the following object relations :
54 * <ul>
55 * <li> <code>'Selections'</code> of type <code>Object[]</code> :
56 * the array of the instances which can be selected.</li>
57 * <li> <code>'Comparer'</code> of type <code>Comparator</code> :
58 * the interface for comparing of selected instances</li>
59 * <ul> <p>
60 * Test is <b> NOT </b> multithread compilant. <p>
61 * @see com.sun.star.view.XSelectionSupplier
63 public class _XMultiSelectionSupplier extends MultiMethodTest {
65 public XMultiSelectionSupplier oObj = null;
66 public boolean selectionChanged = false;
67 Object[] selections = null;
68 Comparator ObjCompare = null;
70 protected void before() {
71 selections = (Object[])tEnv.getObjRelation("Selections");
72 if (selections == null) {
73 throw new StatusException(Status.failed(
74 "Couldn't get relation 'Selections'"));
77 ObjCompare = (Comparator)tEnv.getObjRelation("Comparer");
80 protected void after() {
81 disposeEnvironment();
84 /**
85 * Selects an instance from relation 'First'. <p>
86 * Has <b> OK </b> status if no exceptions were thrown. <p>
88 public void _addSelection() {
90 boolean bOK = true;
92 log.println("clear selections");
93 oObj.clearSelection();
95 int count = oObj.getSelectionCount();
97 bOK &= (count == 0);
99 if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
101 for(int i = 0; i < selections.length; i++) {
102 try {
103 log.println("select object from object relation 'selections["+i+"]'");
104 oObj.addSelection(selections[i]);
105 } catch (com.sun.star.lang.IllegalArgumentException ex) {
106 log.println("ERROR: could not add selection from object relation 'selections[" + i + "]': " + ex.toString());
107 bOK = false;
109 count = oObj.getSelectionCount();
110 if (count != (i+1)){
111 log.println("ERROR: add a selection but selection count ("+count+ ") " +
112 "is not as expected (" + (i+1) + ") => FAILED");
113 bOK = false;
117 log.println("try to select object relation 'selections[0]' second time...");
118 try {
119 count = oObj.getSelectionCount();
120 oObj.addSelection(selections[0]);
121 } catch (com.sun.star.lang.IllegalArgumentException ex) {
122 log.println("ERROR: could not add selection from object relation 'selections[0] a second time': " + ex.toString());
124 if (count != oObj.getSelectionCount()){
125 log.println("ERROR: the selected count ("+oObj.getSelectionCount() +") is not that before (" + count + ")");
126 bOK = false;
129 log.println("try to select invalid object...");
130 try {
132 oObj.addSelection(oObj);
134 log.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
135 bOK = false;
136 } catch (com.sun.star.lang.IllegalArgumentException ex) {
137 log.println("expected exception 'com.sun.star.lang.IllegalArgumentException' => OK");
140 tRes.tested("addSelection()", bOK);
143 public void _removeSelection() {
144 requiredMethod("addSelection()");
146 boolean bOK = true;
148 log.println("clear selections");
149 oObj.clearSelection();
151 int count = oObj.getSelectionCount();
153 bOK &= (count == 0);
155 if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
157 log.println("add some selections...");
158 for(int i = 0; i < selections.length; i++) {
159 try {
160 log.println("select object from object relation 'selections["+i+"]'");
161 oObj.addSelection(selections[i]);
162 } catch (com.sun.star.lang.IllegalArgumentException ex) {
163 log.println("ERROR: could not add selection from object relation 'selections[" + i + "]': " + ex.toString());
164 bOK = false;
166 count = oObj.getSelectionCount();
167 if (count != (i+1)){
168 log.println("ERROR: added a selection but selection count ("+count+ ") " +
169 "is not as expected (" + (i+1) + ") => FAILED");
170 bOK = false;
174 log.println("try now to remove selections...");
176 count = oObj.getSelectionCount();
177 int oldCount = oObj.getSelectionCount();
178 for(int i = 0; i < selections.length; i++) {
179 try {
180 log.println("remove selection for object relation 'selections["+i+"]'");
181 oObj.removeSelection(selections[i]);
182 } catch (com.sun.star.lang.IllegalArgumentException ex) {
183 log.println("ERROR: could not remove selection from object relation 'selections[" + i + "]': " + ex.toString());
184 bOK = false;
186 count = oObj.getSelectionCount();
187 if (count != (oldCount - i - 1)){
188 log.println("ERROR: removed a selection but selection count ("+count+ ") " +
189 "is not as expected (" + (oldCount -i -1) + ") => FAILED");
190 bOK = false;
194 log.println("try to remove a removed selection a second time...");
195 count = oObj.getSelectionCount();
196 try {
197 oObj.removeSelection(selections[0]);
198 } catch (com.sun.star.lang.IllegalArgumentException ex) {
199 log.println("ERROR: could not remove selection from object relation 'selections[0] a second time': " + ex.toString());
201 if (count != oObj.getSelectionCount()){
202 log.println("ERROR: the selected count ("+oObj.getSelectionCount() +") is not that before (" + count + ")");
203 bOK = false;
206 log.println("try to remove invalid object...");
207 try {
209 oObj.removeSelection(oObj);
211 log.println("ERORR: expected exception 'com.sun.star.lang.IllegalArgumentException' was not thrown => FAILED");
212 bOK = false;
213 } catch (com.sun.star.lang.IllegalArgumentException ex) {
214 log.println("expected exception 'com.sun.star.lang.IllegalArgumentException' => OK");
217 tRes.tested("removeSelection()", bOK);
222 * First test changes selection of the object : if nothing is
223 * currently selected or first instance ('First' relation) is
224 * selected then selects second instance; if second instance
225 * is currently selected then the first instance is selected. <p>
226 * Then <code>getSelection</code> is called and values set and
227 * get are compared. Comparison has some special cases. For
228 * example if selection is a Cell, then the values contained
229 * in cells are compared. <p>
230 * Has <b>OK</b> status if selection changed properly.
232 public void _getSelectionCount() {
233 requiredMethod("addSelection()");
234 tRes.tested("getSelectionCount()", true);
237 public void _clearSelection() {
238 requiredMethod("addSelection()");
239 boolean bOK = true;
241 log.println("clear selections");
242 oObj.clearSelection();
244 int count = oObj.getSelectionCount();
246 bOK &= (count == 0);
248 if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
250 log.println("add some selections...");
251 for(int i = 0; i < selections.length; i++) {
252 try {
253 log.println("select object from object relation 'selections["+i+"]'");
254 oObj.addSelection(selections[i]);
255 } catch (com.sun.star.lang.IllegalArgumentException ex) {
256 log.println("ERROR: could not add selection from object relation 'selections[" + i + "]': " + ex.toString());
257 bOK = false;
259 count = oObj.getSelectionCount();
260 if (count != (i+1)){
261 log.println("ERROR: added a selection but selection count ("+count+ ") " +
262 "is not as expected (" + (i+1) + ") => FAILED");
263 bOK = false;
267 count = oObj.getSelectionCount();
269 log.println("clear selections...");
270 oObj.clearSelection();
272 count = oObj.getSelectionCount();
274 bOK &= (count == 0);
276 if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
278 tRes.tested("clearSelection()", bOK);
281 public void _createSelectionEnumeration() {
282 requiredMethod("addSelection()");
283 boolean bOK = true;
285 log.println("clear selections");
286 oObj.clearSelection();
288 int count = oObj.getSelectionCount();
290 bOK &= (count == 0);
292 if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
294 log.println("add some selections...");
295 for(int i = 0; i < selections.length; i++) {
296 try {
297 log.println("select object from object relation 'selections["+i+"]'");
298 oObj.addSelection(selections[i]);
299 } catch (com.sun.star.lang.IllegalArgumentException ex) {
300 log.println("ERROR: could not add selection from object relation 'selections[" + i + "]': " + ex.toString());
301 bOK = false;
303 count = oObj.getSelectionCount();
304 if (count != (i+1)){
305 log.println("ERROR: added a selection but selection count ("+count+ ") " +
306 "is not as expected (" + (i+1) + ") => FAILED");
307 bOK = false;
311 log.println("create enumeration...");
312 XEnumeration xEnum = oObj.createSelectionEnumeration();
314 boolean compRes = true; //compare result
315 int i = 0;
317 while (xEnum.hasMoreElements()){
318 log.println("try to get first element..");
319 Object nextElement = null;
320 try {
321 nextElement = xEnum.nextElement();
322 } catch (WrappedTargetException ex) {
323 log.println("ERROR: could not get nextElement: " + ex.toString());
324 bOK = false;
325 } catch (NoSuchElementException ex) {
326 log.println("ERROR: could not get nextElement: " + ex.toString());
327 bOK = false;
329 Object shouldElement = selections[i];
330 i++;
332 if (ObjCompare != null) {
333 ObjCompare.compare(shouldElement, nextElement);
334 } else {
335 compRes = util.ValueComparer.equalValue(shouldElement, nextElement);
338 log.println("nextElement()-object and expected object 'selections["+i+"]' are equal: "+compRes);
340 if (!compRes) {
341 if ((selections[i]) instanceof Object[]){
342 if (((Object[])selections[i])[0] instanceof Integer) {
343 log.println("Getting: "+((Integer) ((Object[])shouldElement)[0]).intValue());
344 log.println("Expected: "+((Integer) ((Object[])selections[i])[0]).intValue());
348 bOK &= compRes;
351 tRes.tested("createSelectionEnumeration()", bOK);
354 public void _createReverseSelectionEnumeration() {
355 requiredMethod("addSelection()");
356 boolean bOK = true;
358 log.println("clear selections");
359 oObj.clearSelection();
361 int count = oObj.getSelectionCount();
363 bOK &= (count == 0);
365 if ( ! bOK) log.println("ERROR: after clear selection I got a selection count of '" + count + "' => FAILED");
367 log.println("add some selections...");
368 for(int i = 0; i < selections.length; i++) {
369 try {
370 log.println("select object from object relation 'selections["+i+"]'");
371 oObj.addSelection(selections[i]);
372 } catch (com.sun.star.lang.IllegalArgumentException ex) {
373 log.println("ERROR: could not add selection from object relation 'selections[" + i + "]': " + ex.toString());
374 bOK = false;
376 count = oObj.getSelectionCount();
377 if (count != (i+1)){
378 log.println("ERROR: added a selection but selection count ("+count+ ") " +
379 "is not as expected (" + (i+1) + ") => FAILED");
380 bOK = false;
384 log.println("create enumeration...");
385 XEnumeration xEnum = oObj.createSelectionEnumeration();
387 boolean compRes = true; //compare result
388 int i = selections.length - 1;
390 while (xEnum.hasMoreElements()){
391 log.println("try to get first element..");
392 Object nextElement = null;
393 try {
394 nextElement = xEnum.nextElement();
395 } catch (WrappedTargetException ex) {
396 log.println("ERROR: could not get nextElement: " + ex.toString());
397 bOK = false;
398 } catch (NoSuchElementException ex) {
399 log.println("ERROR: could not get nextElement: " + ex.toString());
400 bOK = false;
402 Object shouldElement = selections[i];
403 i--;
405 if (ObjCompare != null) {
406 ObjCompare.compare(shouldElement, nextElement);
407 } else {
408 compRes = util.ValueComparer.equalValue(shouldElement, nextElement);
411 log.println("nextElement()-object and expected object 'selections["+i+"]' are equal: "+compRes);
413 if (!compRes) {
414 if ((selections[i]) instanceof Object[]){
415 if (((Object[])selections[i])[0] instanceof Integer) {
416 log.println("Getting: "+((Integer) ((Object[])shouldElement)[0]).intValue());
417 log.println("Expected: "+((Integer) ((Object[])selections[i])[0]).intValue());
421 bOK &= compRes;
424 tRes.tested("createReverseSelectionEnumeration()", bOK);
427 } // finish class _XMultiSelectionSupplier