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 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;
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");
68 protected void after() {
73 * Listener implementation which just set flag when listener
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();
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(){
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
);
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() {
114 boolean locRes
= true;
115 boolean compRes
= true;
116 Object oldSelection
= null;
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();
125 if (ObjCompare
!= null) {
126 ObjCompare
.compare(selections
[i
], curSelection
);
128 compRes
= util
.ValueComparer
.equalValue(selections
[i
], curSelection
);
130 log
.println("selected object and current selection are equal: "+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());
141 compRes
= util
.ValueComparer
.equalValue(curSelection
, oldSelection
);
142 log
.println("previous selection and current selection are equal: "+compRes
);
146 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
147 log
.println("Exception occurred during select()");
148 ex
.printStackTrace(log
);
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 :
161 * <li> <code> addSelectionChangeListener() </code> : to have
162 * the listener added. </li>
165 public void _removeSelectionChangeListener() {
167 requiredMethod("addSelectionChangeListener()");
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
);
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