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