Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / view / _XSelectionSupplier.java
blobd601f1f2251b92a062e985c724f05e56069ecd58
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.lang.EventObject;
22 import com.sun.star.view.XSelectionChangeListener;
23 import com.sun.star.view.XSelectionSupplier;
24 import java.util.Comparator;
25 import lib.MultiMethodTest;
26 import lib.Status;
27 import lib.StatusException;
30 /**
31 * Testing <code>com.sun.star.view.XSelectionSupplier</code>
32 * interface methods :
33 * <ul>
34 * <li><code> select()</code></li>
35 * <li><code> getSelection()</code></li>
36 * <li><code> addSelectionChangeListener()</code></li>
37 * <li><code> removeSelectionChangeListener()</code></li>
38 * </ul>
39 * This test needs the following object relations :
40 * <ul>
41 * <li> <code>'Selections'</code> of type <code>Object[]</code> :
42 * the array of the instances which can be selected.</li>
43 * <li> <code>'Comparer'</code> of type <code>Comparator</code> :
44 * the interface for comparing of selected instances</li>
45 * <ul> <p>
46 * Test is <b> NOT </b> multithread compliant. <p>
47 * @see com.sun.star.view.XSelectionSupplier
49 public class _XSelectionSupplier extends MultiMethodTest {
51 public XSelectionSupplier oObj = null;
52 public boolean selectionChanged = false;
53 Object[] selections = null;
54 Comparator<Object> ObjCompare = null;
56 @Override
57 protected void before() {
58 selections = (Object[])tEnv.getObjRelation("Selections");
59 if (selections == null) {
60 throw new StatusException(Status.failed(
61 "Couldn't get relation 'Selections'"));
64 ObjCompare = (Comparator<Object>)tEnv.getObjRelation("Comparer");
67 @Override
68 protected void after() {
69 disposeEnvironment();
72 /**
73 * Listener implementation which just set flag when listener
74 * method is called.
76 public class MyChangeListener implements XSelectionChangeListener {
77 public void disposing( EventObject oEvent ) {}
78 public void selectionChanged(EventObject ev) {
79 log.println("listener called");
80 selectionChanged = true;
85 XSelectionChangeListener listener = new MyChangeListener();
87 /**
88 * Test adds listener to the object, then selects first and
89 * then second instances to be sure that selection was changed.<p>
90 * Has <b>OK</b> status if selection listener was called.
92 public void _addSelectionChangeListener(){
93 boolean res = true;
94 try {
95 selectionChanged = false;
96 oObj.addSelectionChangeListener(listener);
97 oObj.select(selections[0]);
98 oObj.select(selections[1]);
99 res = selectionChanged;
100 } catch (com.sun.star.lang.IllegalArgumentException ex) {
101 log.println("Exception occurred during addSelectionChangeListener()");
102 ex.printStackTrace(log);
103 res = false;
105 tRes.tested("addSelectionChangeListener()", res);
109 * Selects an instance from relation 'First'. <p>
110 * Has <b> OK </b> status if no exceptions were thrown. <p>
112 public void _select() {
113 boolean res = true;
114 boolean locRes = true;
115 boolean compRes = true;
116 Object oldSelection = null;
117 try {
118 for(int i = 0; i < selections.length; i++) {
119 oldSelection = oObj.getSelection();
120 locRes = oObj.select(selections[i]);
121 log.println("select #" + i + ": " + locRes);
122 Object curSelection = oObj.getSelection();
123 if (locRes) {
125 if (ObjCompare != null) {
126 ObjCompare.compare(selections[i], curSelection);
127 } else {
128 compRes = util.ValueComparer.equalValue(selections[i], curSelection);
130 log.println("selected object and current selection are equal: "+compRes);
131 if (!compRes) {
132 if ((selections[i]) instanceof Object[]){
133 if (((Object[])selections[i])[0] instanceof Integer) {
134 log.println("Getting: "+((Integer) ((Object[])curSelection)[0]).intValue());
135 log.println("Expected: "+((Integer) ((Object[])selections[i])[0]).intValue());
139 res &= compRes;
140 } else {
141 compRes = util.ValueComparer.equalValue(curSelection, oldSelection);
142 log.println("previous selection and current selection are equal: "+compRes);
143 res &= compRes;
146 } catch (com.sun.star.lang.IllegalArgumentException ex) {
147 log.println("Exception occurred during select()");
148 ex.printStackTrace(log);
149 res = false;
152 tRes.tested("select()", res);
156 * Test removes listener, then selects first and
157 * then second instances to be sure that selection was changed.<p>
158 * Has <b>OK</b> status if selection listener was not called.
159 * The following method tests are to be completed successfully before :
160 * <ul>
161 * <li> <code> addSelectionChangeListener() </code> : to have
162 * the listener added. </li>
163 * </ul>
165 public void _removeSelectionChangeListener() {
166 boolean res = false;
167 requiredMethod("addSelectionChangeListener()");
168 try {
169 selectionChanged = false;
170 oObj.removeSelectionChangeListener(listener);
171 oObj.select(selections[0]);
172 oObj.select(selections[1]);
173 res = !selectionChanged;
174 } catch (com.sun.star.lang.IllegalArgumentException ex) {
175 log.println("Exception occurred during removeSelectionChangeListener()");
176 ex.printStackTrace(log);
177 res = false;
179 tRes.tested("removeSelectionChangeListener()", res);
183 * First test changes selection of the object : if nothing is
184 * currently selected or first instance ('First' relation) is
185 * selected then selects second instance; if second instance
186 * is currently selected then the first instance is selected. <p>
187 * Then <code>getSelection</code> is called and values set and
188 * get are compared. Comparison has some special cases. For
189 * example if selection is a Cell, then the values contained
190 * in cells are compared. <p>
191 * Has <b>OK</b> status if selection changed properly.
193 public void _getSelection() {
194 requiredMethod("select()");
195 tRes.tested("getSelection()", true);
198 } // finish class _XSelectionSupplier