tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / qadevOOo / tests / java / ifc / view / _XMultiSelectionSupplier.java
blob310e37e94ed33d3f2cd4f0186ff1ca9dc5892c0b
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 Object[] selections = null;
55 Comparator<Object> ObjCompare = null;
57 @SuppressWarnings("unchecked")
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("ERROR: 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("ERROR: 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 (nextElement != null)
324 if (ObjCompare != null) {
325 compRes = ObjCompare.compare(shouldElement, nextElement) == 0;
326 } else {
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());
338 bOK &= compRes;
340 } else {
341 bOK = false;
345 tRes.tested("createSelectionEnumeration()", bOK);
348 public void _createReverseSelectionEnumeration() {
349 requiredMethod("addSelection()");
350 boolean bOK = true;
352 log.println("clear selections");
353 oObj.clearSelection();
355 int count = oObj.getSelectionCount();
357 bOK &= (count == 0);
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++) {
363 try {
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());
368 bOK = false;
370 count = oObj.getSelectionCount();
371 if (count != (i+1)){
372 log.println("ERROR: added a selection but selection count ("+count+ ") " +
373 "is not as expected (" + (i+1) + ") => FAILED");
374 bOK = false;
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;
387 try {
388 nextElement = xEnum.nextElement();
389 } catch (WrappedTargetException ex) {
390 log.println("ERROR: could not get nextElement: " + ex.toString());
391 bOK = false;
392 } catch (NoSuchElementException ex) {
393 log.println("ERROR: could not get nextElement: " + ex.toString());
394 bOK = false;
396 Object shouldElement = selections[i];
397 i--;
399 if (nextElement != null) {
400 if (ObjCompare != null) {
401 compRes = ObjCompare.compare(shouldElement, nextElement) == 0;
402 } else {
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());
414 bOK &= compRes;
416 } else {
417 bOK = false;
422 tRes.tested("createReverseSelectionEnumeration()", bOK);
425 } // finish class _XMultiSelectionSupplier