1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.view;
25 
26 import com.sun.star.lang.EventObject;
27 import com.sun.star.view.XSelectionChangeListener;
28 import com.sun.star.view.XSelectionSupplier;
29 import java.util.Comparator;
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import lib.StatusException;
33 
34 
35 /**
36  * Testing <code>com.sun.star.view.XSelectionSupplier</code>
37  * interface methods :
38  * <ul>
39  *  <li><code> select()</code></li>
40  *  <li><code> getSelection()</code></li>
41  *  <li><code> addSelectionChangeListener()</code></li>
42  *  <li><code> removeSelectionChangeListener()</code></li>
43  * </ul>
44  * This test needs the following object relations :
45  * <ul>
46  *  <li> <code>'Selections'</code> of type <code>Object[]</code> :
47  *   the array of the instances which can be selected.</li>
48  *  <li> <code>'Comparer'</code> of type <code>Comparator</code> :
49  *   the interface for comparing of selected instances</li>
50  * <ul> <p>
51  * Test is <b> NOT </b> multithread compilant. <p>
52  * @see com.sun.star.view.XSelectionSupplier
53  */
54 public class _XSelectionSupplier extends MultiMethodTest {
55 
56     public XSelectionSupplier oObj = null;
57     public boolean selectionChanged = false;
58     Object[] selections = null;
59     Comparator ObjCompare = null;
60 
61     protected void before() {
62         selections = (Object[])tEnv.getObjRelation("Selections");
63         if (selections == null) {
64             throw new StatusException(Status.failed(
65                     "Couldn't get relation 'Selections'"));
66         }
67 
68         ObjCompare = (Comparator)tEnv.getObjRelation("Comparer");
69     }
70 
71     protected void after() {
72         disposeEnvironment();
73     }
74 
75     /**
76      * Listener implementation which just set flag when listener
77      * method is called.
78      */
79     public class MyChangeListener implements XSelectionChangeListener {
80         public void disposing( EventObject oEvent ) {}
81         public void selectionChanged(EventObject ev) {
82             log.println("listener called");
83             selectionChanged = true;
84         }
85 
86     }
87 
88     XSelectionChangeListener listener = new MyChangeListener();
89 
90     /**
91      * Test adds listener to the object, then selects first and
92      * then second instances to be sure that selection was changed.<p>
93      * Has <b>OK</b> status if selection lisener was called.
94      */
95     public void _addSelectionChangeListener(){
96         boolean res = true;
97         try {
98             selectionChanged = false;
99             oObj.addSelectionChangeListener(listener);
100             oObj.select(selections[0]);
101             oObj.select(selections[1]);
102             res = selectionChanged;
103         } catch (com.sun.star.lang.IllegalArgumentException ex) {
104             log.println("Exception occured during addSelectionChangeListener()");
105             ex.printStackTrace(log);
106             res = false;
107         }
108         tRes.tested("addSelectionChangeListener()", res);
109     }
110 
111     /**
112      * Selects an instance from relation 'First'. <p>
113      * Has <b> OK </b> status if no exceptions were thrown. <p>
114      */
115     public void _select() {
116         boolean res  = true;
117         boolean locRes = true;
118         boolean compRes = true;
119         Object oldSelection = null;
120         try {
121             for(int i = 0; i < selections.length; i++) {
122                 oldSelection = oObj.getSelection();
123                 locRes = oObj.select(selections[i]);
124                 log.println("select #" + i + ": " + locRes);
125                 Object curSelection = oObj.getSelection();
126                 if (locRes) {
127 
128                     if (ObjCompare != null) {
129                         ObjCompare.compare(selections[i], curSelection);
130                     } else {
131                         compRes = util.ValueComparer.equalValue(selections[i], curSelection);
132                     }
133                     log.println("selected object and current selection are equal: "+compRes);
134                     if (!compRes) {
135                         if ((selections[i]) instanceof Object[]){
136                             if (((Object[])selections[i])[0] instanceof Integer) {
137                                 log.println("Getting: "+((Integer) ((Object[])curSelection)[0]).intValue());
138                                 log.println("Expected: "+((Integer) ((Object[])selections[i])[0]).intValue());
139                             }
140                         }
141                     }
142                     res &= compRes;
143                 } else {
144                     compRes = util.ValueComparer.equalValue(curSelection, oldSelection);
145                     log.println("previous selection and current selection are equal: "+compRes);
146                     res &= compRes;
147                 }
148             }
149         } catch (com.sun.star.lang.IllegalArgumentException ex) {
150             log.println("Exception occured during select()");
151             ex.printStackTrace(log);
152             res = false;
153         }
154 
155         tRes.tested("select()", res);
156     }
157 
158     /**
159      * Test removes listener, then selects first and
160      * then second instances to be sure that selection was changed.<p>
161      * Has <b>OK</b> status if selection lisener was not called.
162      * The following method tests are to be completed successfully before :
163      * <ul>
164      *  <li> <code> addSelectionChangeListener() </code> : to have
165      *   the listener added. </li>
166      * </ul>
167      */
168     public void _removeSelectionChangeListener() {
169         boolean res = false;
170         requiredMethod("addSelectionChangeListener()");
171         try {
172             selectionChanged = false;
173             oObj.removeSelectionChangeListener(listener);
174             oObj.select(selections[0]);
175             oObj.select(selections[1]);
176             res = !selectionChanged;
177         } catch (com.sun.star.lang.IllegalArgumentException ex) {
178             log.println("Exception occured during removeSelectionChangeListener()");
179             ex.printStackTrace(log);
180             res = false;
181         }
182         tRes.tested("removeSelectionChangeListener()", res);
183     }
184 
185     /**
186      * First test changes selection of the object : if nothing is
187      * currently selected or first instance ('First' relation) is
188      * selected then selects second instance; if second instance
189      * is currently selected then the first instance is selected. <p>
190      * Then <code>getSelection</code> is called and values set and
191      * get are compared. Comparison has some special cases. For
192      * example if selection is a Cell, then the values contained
193      * in cells are compared. <p>
194      * Has <b>OK</b> status if selection changed properly.
195      */
196     public void _getSelection() {
197         requiredMethod("select()");
198         tRes.tested("getSelection()", true);
199     }
200 
201 }  // finish class _XSelectionSupplier
202 
203 
204 
205