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 .
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
;
27 import lib
.StatusException
;
31 * Testing <code>com.sun.star.view.XSelectionSupplier</code>
34 * <li><code> select()</code></li>
35 * <li><code> getSelection()</code></li>
36 * <li><code> addSelectionChangeListener()</code></li>
37 * <li><code> removeSelectionChangeListener()</code></li>
39 * This test needs the following object relations :
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>
46 * Test is <b> NOT </b> multithread compilant. <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 protected void before() {
57 selections
= (Object
[])tEnv
.getObjRelation("Selections");
58 if (selections
== null) {
59 throw new StatusException(Status
.failed(
60 "Couldn't get relation 'Selections'"));
63 ObjCompare
= (Comparator
<Object
>)tEnv
.getObjRelation("Comparer");
66 protected void after() {
71 * Listener implementation which just set flag when listener
74 public class MyChangeListener
implements XSelectionChangeListener
{
75 public void disposing( EventObject oEvent
) {}
76 public void selectionChanged(EventObject ev
) {
77 log
.println("listener called");
78 selectionChanged
= true;
83 XSelectionChangeListener listener
= new MyChangeListener();
86 * Test adds listener to the object, then selects first and
87 * then second instances to be sure that selection was changed.<p>
88 * Has <b>OK</b> status if selection lisener was called.
90 public void _addSelectionChangeListener(){
93 selectionChanged
= false;
94 oObj
.addSelectionChangeListener(listener
);
95 oObj
.select(selections
[0]);
96 oObj
.select(selections
[1]);
97 res
= selectionChanged
;
98 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
99 log
.println("Exception occurred during addSelectionChangeListener()");
100 ex
.printStackTrace(log
);
103 tRes
.tested("addSelectionChangeListener()", res
);
107 * Selects an instance from relation 'First'. <p>
108 * Has <b> OK </b> status if no exceptions were thrown. <p>
110 public void _select() {
112 boolean locRes
= true;
113 boolean compRes
= true;
114 Object oldSelection
= null;
116 for(int i
= 0; i
< selections
.length
; i
++) {
117 oldSelection
= oObj
.getSelection();
118 locRes
= oObj
.select(selections
[i
]);
119 log
.println("select #" + i
+ ": " + locRes
);
120 Object curSelection
= oObj
.getSelection();
123 if (ObjCompare
!= null) {
124 ObjCompare
.compare(selections
[i
], curSelection
);
126 compRes
= util
.ValueComparer
.equalValue(selections
[i
], curSelection
);
128 log
.println("selected object and current selection are equal: "+compRes
);
130 if ((selections
[i
]) instanceof Object
[]){
131 if (((Object
[])selections
[i
])[0] instanceof Integer
) {
132 log
.println("Getting: "+((Integer
) ((Object
[])curSelection
)[0]).intValue());
133 log
.println("Expected: "+((Integer
) ((Object
[])selections
[i
])[0]).intValue());
139 compRes
= util
.ValueComparer
.equalValue(curSelection
, oldSelection
);
140 log
.println("previous selection and current selection are equal: "+compRes
);
144 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
145 log
.println("Exception occurred during select()");
146 ex
.printStackTrace(log
);
150 tRes
.tested("select()", res
);
154 * Test removes listener, then selects first and
155 * then second instances to be sure that selection was changed.<p>
156 * Has <b>OK</b> status if selection lisener was not called.
157 * The following method tests are to be completed successfully before :
159 * <li> <code> addSelectionChangeListener() </code> : to have
160 * the listener added. </li>
163 public void _removeSelectionChangeListener() {
165 requiredMethod("addSelectionChangeListener()");
167 selectionChanged
= false;
168 oObj
.removeSelectionChangeListener(listener
);
169 oObj
.select(selections
[0]);
170 oObj
.select(selections
[1]);
171 res
= !selectionChanged
;
172 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
173 log
.println("Exception occurred during removeSelectionChangeListener()");
174 ex
.printStackTrace(log
);
177 tRes
.tested("removeSelectionChangeListener()", res
);
181 * First test changes selection of the object : if nothing is
182 * currently selected or first instance ('First' relation) is
183 * selected then selects second instance; if second instance
184 * is currently selected then the first instance is selected. <p>
185 * Then <code>getSelection</code> is called and values set and
186 * get are compared. Comparison has some special cases. For
187 * example if selection is a Cell, then the values contained
188 * in cells are compared. <p>
189 * Has <b>OK</b> status if selection changed properly.
191 public void _getSelection() {
192 requiredMethod("select()");
193 tRes
.tested("getSelection()", true);
196 } // finish class _XSelectionSupplier