1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XSelectionSupplier.java,v $
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 ************************************************************************/
33 import com
.sun
.star
.lang
.EventObject
;
34 import com
.sun
.star
.view
.XSelectionChangeListener
;
35 import com
.sun
.star
.view
.XSelectionSupplier
;
36 import java
.util
.Comparator
;
37 import lib
.MultiMethodTest
;
39 import lib
.StatusException
;
43 * Testing <code>com.sun.star.view.XSelectionSupplier</code>
46 * <li><code> select()</code></li>
47 * <li><code> getSelection()</code></li>
48 * <li><code> addSelectionChangeListener()</code></li>
49 * <li><code> removeSelectionChangeListener()</code></li>
51 * This test needs the following object relations :
53 * <li> <code>'Selections'</code> of type <code>Object[]</code> :
54 * the array of the instances which can be selected.</li>
55 * <li> <code>'Comparer'</code> of type <code>Comparator</code> :
56 * the interface for comparing of selected instances</li>
58 * Test is <b> NOT </b> multithread compilant. <p>
59 * @see com.sun.star.view.XSelectionSupplier
61 public class _XSelectionSupplier
extends MultiMethodTest
{
63 public XSelectionSupplier oObj
= null;
64 public boolean selectionChanged
= false;
65 Object
[] selections
= null;
66 Comparator ObjCompare
= null;
68 protected void before() {
69 selections
= (Object
[])tEnv
.getObjRelation("Selections");
70 if (selections
== null) {
71 throw new StatusException(Status
.failed(
72 "Couldn't get relation 'Selections'"));
75 ObjCompare
= (Comparator
)tEnv
.getObjRelation("Comparer");
78 protected void after() {
83 * Listener implementation which just set flag when listener
86 public class MyChangeListener
implements XSelectionChangeListener
{
87 public void disposing( EventObject oEvent
) {}
88 public void selectionChanged(EventObject ev
) {
89 log
.println("listener called");
90 selectionChanged
= true;
95 XSelectionChangeListener listener
= new MyChangeListener();
98 * Test adds listener to the object, then selects first and
99 * then second instances to be sure that selection was changed.<p>
100 * Has <b>OK</b> status if selection lisener was called.
102 public void _addSelectionChangeListener(){
105 selectionChanged
= false;
106 oObj
.addSelectionChangeListener(listener
);
107 oObj
.select(selections
[0]);
108 oObj
.select(selections
[1]);
109 res
= selectionChanged
;
110 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
111 log
.println("Exception occured during addSelectionChangeListener()");
112 ex
.printStackTrace(log
);
115 tRes
.tested("addSelectionChangeListener()", res
);
119 * Selects an instance from relation 'First'. <p>
120 * Has <b> OK </b> status if no exceptions were thrown. <p>
122 public void _select() {
124 boolean locRes
= true;
125 boolean compRes
= true;
126 Object oldSelection
= null;
128 for(int i
= 0; i
< selections
.length
; i
++) {
129 oldSelection
= oObj
.getSelection();
130 locRes
= oObj
.select(selections
[i
]);
131 log
.println("select #" + i
+ ": " + locRes
);
132 Object curSelection
= oObj
.getSelection();
135 if (ObjCompare
!= null) {
136 ObjCompare
.compare(selections
[i
], curSelection
);
138 compRes
= util
.ValueComparer
.equalValue(selections
[i
], curSelection
);
140 log
.println("selected object and current selection are equal: "+compRes
);
142 if ((selections
[i
]) instanceof Object
[]){
143 if (((Object
[])selections
[i
])[0] instanceof Integer
) {
144 log
.println("Getting: "+((Integer
) ((Object
[])curSelection
)[0]).intValue());
145 log
.println("Expected: "+((Integer
) ((Object
[])selections
[i
])[0]).intValue());
151 compRes
= util
.ValueComparer
.equalValue(curSelection
, oldSelection
);
152 log
.println("previous selection and current selection are equal: "+compRes
);
156 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
157 log
.println("Exception occured during select()");
158 ex
.printStackTrace(log
);
162 tRes
.tested("select()", res
);
166 * Test removes listener, then selects first and
167 * then second instances to be sure that selection was changed.<p>
168 * Has <b>OK</b> status if selection lisener was not called.
169 * The following method tests are to be completed successfully before :
171 * <li> <code> addSelectionChangeListener() </code> : to have
172 * the listener added. </li>
175 public void _removeSelectionChangeListener() {
177 requiredMethod("addSelectionChangeListener()");
179 selectionChanged
= false;
180 oObj
.removeSelectionChangeListener(listener
);
181 oObj
.select(selections
[0]);
182 oObj
.select(selections
[1]);
183 res
= !selectionChanged
;
184 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
185 log
.println("Exception occured during removeSelectionChangeListener()");
186 ex
.printStackTrace(log
);
189 tRes
.tested("removeSelectionChangeListener()", res
);
193 * First test changes selection of the object : if nothing is
194 * currently selected or first instance ('First' relation) is
195 * selected then selects second instance; if second instance
196 * is currently selected then the first instance is selected. <p>
197 * Then <code>getSelection</code> is called and values set and
198 * get are compared. Comparison has some special cases. For
199 * example if selection is a Cell, then the values contained
200 * in cells are compared. <p>
201 * Has <b>OK</b> status if selection changed properly.
203 public void _getSelection() {
204 requiredMethod("select()");
205 tRes
.tested("getSelection()", true);
208 } // finish class _XSelectionSupplier