1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, Steve Meyfroidt
4 * All rights reserved.
5 * Email: smeyfroi@users.sourceforge.net
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Neither the name "Scope" nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
37 * $Id: TestSListSelectionModel.java,v 1.5 2002/08/05 13:19:26 ludovicc Exp $
38 */
39
40
41 package test.view.swing;
42
43
44 import java.awt.Dimension;
45 import java.util.HashSet;
46 import javax.swing.JList;
47 import junit.framework.TestCase;
48 import org.apache.commons.logging.LogFactory;import org.apache.commons.logging.Log;
49 import org.scopemvc.core.Selector;
50 import org.scopemvc.view.swing.SList;
51 import org.scopemvc.view.swing.SPanel;
52 import org.scopemvc.view.swing.SListSelectionModel;
53
54
55 /***
56 *
57 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net>Steve Meyfroidt</A>
58 * @version $Revision: 1.5 $ $Date: 2002/08/05 13:19:26 $
59 */
60 public final class TestSListSelectionModel extends TestCase {
61
62
63 private static final Log LOG = LogFactory.getLog(TestSListSelectionModel.class);
64
65
66 private SList list1;
67 private SListSelectionModel listSelection1;
68
69 private SwingDummyController controller;
70 private SPanel view;
71 private SwingDummyModel model;
72
73
74 public TestSListSelectionModel(String inName) {
75 super(inName);
76 }
77
78
79 protected void setUp() throws Exception {
80
81 list1 = new SList();
82 list1.setPreferredSize(new Dimension(50, 70));
83 listSelection1 = new SListSelectionModel(list1);
84 list1.setSelectionModel(listSelection1);
85
86 view = new SPanel();
87 view.add(list1);
88
89 controller = new SwingDummyController();
90 controller.setView(view);
91 controller.startup(); // does showView()
92
93 model = new SwingDummyModel();
94 }
95
96
97 protected void tearDown() {
98 controller.shutdown();
99 }
100
101
102 public void testUnbound1() throws Exception {
103 SuiteViewSwing.waitForAWT();
104 assertNull(list1.getSelectedValue());
105 assertNull(listSelection1.getViewValue());
106 assertTrue(listSelection1.getMinSelectionIndex() == -1);
107
108 listSelection1.setSelector(Selector.fromString("stringProperty"));
109 SuiteViewSwing.waitForAWT();
110 assertEquals(Selector.fromString("stringProperty"), listSelection1.getSelector());
111 assertNull(list1.getSelectedValue());
112 assertNull(listSelection1.getViewValue());
113 assertTrue(listSelection1.getMinSelectionIndex() == -1);
114 }
115
116
117 public void testBind1() throws Exception {
118 listSelection1.setSelector(Selector.fromString("stringProperty"));
119 listSelection1.setBoundModel(model);
120
121 list1.setListModel(new String[] {"abc", "xyz"});
122 SuiteViewSwing.waitForAWT();
123
124 list1.setSelectedIndex(0);
125 SuiteViewSwing.waitForAWT();
126 assertEquals("abc", model.getStringProperty());
127
128 list1.setSelectedIndex(1);
129 SuiteViewSwing.waitForAWT();
130 assertEquals("xyz", model.getStringProperty());
131
132 list1.clearSelection();
133 SuiteViewSwing.waitForAWT();
134 assertNull(model.getStringProperty());
135 }
136
137
138 public void testConvenience() throws Exception {
139 listSelection1.setSelectorString("stringProperty");
140 assertEquals(Selector.fromString("stringProperty"), listSelection1.getSelector());
141 }
142
143
144 public void testBind12() throws Exception {
145 listSelection1.setSelector(Selector.fromString("stringProperty"));
146 listSelection1.setBoundModel(model);
147
148 list1.setListModel(new String[] {"abc", "xyz"});
149 SuiteViewSwing.waitForAWT();
150
151 model.setStringProperty("xyz");
152 SuiteViewSwing.waitForAWT();
153 assertTrue(list1.getSelectedIndex() == 1);
154 assertEquals("xyz", list1.getSelectedValue());
155
156 model.setStringProperty("abc");
157 SuiteViewSwing.waitForAWT();
158 assertTrue(list1.getSelectedIndex() == 0);
159 assertEquals("abc", list1.getSelectedValue());
160
161 model.setStringProperty(null);
162 SuiteViewSwing.waitForAWT();
163 assertTrue(list1.getSelectedIndex() == -1);
164 assertNull(list1.getSelectedValue());
165
166 model.setStringProperty("abc");
167 SuiteViewSwing.waitForAWT();
168 assertTrue(list1.getSelectedIndex() == 0);
169 assertEquals("abc", list1.getSelectedValue());
170
171 model.setStringProperty("def");
172 SuiteViewSwing.waitForAWT();
173 assertTrue("selection: " + list1.getSelectedIndex(), list1.getSelectedIndex() == -1);
174 assertNull(list1.getSelectedValue());
175 }
176
177
178 public void testReadOnlyBind() throws Exception {
179 list1.setListModel(new Boolean[] {Boolean.TRUE, Boolean.FALSE});
180 listSelection1.setSelector(Selector.fromString("booleanReadOnlyProperty"));
181 listSelection1.setBoundModel(model);
182 SuiteViewSwing.waitForAWT();
183
184 assertTrue(! list1.isEnabled());
185 }
186
187
188 public void testBindNoMCE() throws Exception {
189 SwingDummyModelNoMCE model = new SwingDummyModelNoMCE();
190
191 listSelection1.setSelector(Selector.fromString("stringProperty"));
192 listSelection1.setBoundModel(model);
193
194 list1.setListModel(new String[] {"abc", "xyz"});
195 SuiteViewSwing.waitForAWT();
196
197 list1.setSelectedIndex(0);
198 SuiteViewSwing.waitForAWT();
199 assertEquals("abc", model.getStringProperty());
200
201 list1.setSelectedIndex(1);
202 SuiteViewSwing.waitForAWT();
203 assertEquals("xyz", model.getStringProperty());
204
205 list1.clearSelection();
206 SuiteViewSwing.waitForAWT();
207 assertNull(model.getStringProperty());
208
209 list1.setListModel(new String[] {"abc", "xyz"});
210 SuiteViewSwing.waitForAWT();
211
212 model.setStringProperty("xyz");
213 SuiteViewSwing.waitForAWT();
214 assertNull(list1.getSelectedValue());
215
216 listSelection1.refresh();
217 assertTrue(list1.getSelectedIndex() == 1);
218 assertEquals("xyz", list1.getSelectedValue());
219
220 model.setStringProperty("abc");
221 SuiteViewSwing.waitForAWT();
222 assertTrue(list1.getSelectedIndex() == 1);
223 assertEquals("xyz", list1.getSelectedValue());
224
225 listSelection1.refresh();
226 assertTrue(list1.getSelectedIndex() == 0);
227 assertEquals("abc", list1.getSelectedValue());
228
229 model.setStringProperty(null);
230 SuiteViewSwing.waitForAWT();
231 assertTrue(list1.getSelectedIndex() == 0);
232 assertEquals("abc", list1.getSelectedValue());
233
234 listSelection1.refresh();
235 assertTrue(list1.getSelectedIndex() == -1);
236 assertNull(list1.getSelectedValue());
237
238 model.setStringProperty("abc");
239 SuiteViewSwing.waitForAWT();
240 assertTrue(list1.getSelectedIndex() == -1);
241 assertNull(list1.getSelectedValue());
242
243 listSelection1.refresh();
244 assertTrue(list1.getSelectedIndex() == 0);
245 assertEquals("abc", list1.getSelectedValue());
246
247 model.setStringProperty("def");
248 SuiteViewSwing.waitForAWT();
249 assertTrue(list1.getSelectedIndex() == 0);
250 assertEquals("abc", list1.getSelectedValue());
251
252 listSelection1.refresh();
253 assertTrue(list1.getSelectedIndex() == -1);
254 assertNull(list1.getSelectedValue());
255 }
256
257
258 public void testMultipleViewToModel() throws Exception {
259 listSelection1.setSelector(Selector.fromString("selections"));
260 listSelection1.setBoundModel(model);
261
262 list1.setListModel(new String[] {"abc", "def", "ghi", "xyz"});
263 list1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
264 SuiteViewSwing.waitForAWT();
265
266 list1.setSelectedIndex(0);
267 SuiteViewSwing.waitForAWT();
268 assertTrue(model.getSelections() != null);
269 assertTrue(model.getSelections().size() == 1);
270 assertTrue(model.getSelections().contains("abc"));
271
272 list1.setSelectionInterval(1, 2);
273 SuiteViewSwing.waitForAWT();
274 assertTrue(model.getSelections() != null);
275 assertTrue(model.getSelections().size() == 2);
276 assertTrue(model.getSelections().contains("def"));
277 assertTrue(model.getSelections().contains("ghi"));
278
279 list1.clearSelection();
280 SuiteViewSwing.waitForAWT();
281 assertTrue(model.getSelections() != null);
282 assertTrue("selections: " + model.getSelections(), model.getSelections().isEmpty());
283
284 list1.setSelectionInterval(0, 3);
285 SuiteViewSwing.waitForAWT();
286 assertTrue(model.getSelections() != null);
287 assertTrue(model.getSelections().size() == 4);
288 assertTrue(model.getSelections().contains("abc"));
289 assertTrue(model.getSelections().contains("def"));
290 assertTrue(model.getSelections().contains("ghi"));
291 assertTrue(model.getSelections().contains("xyz"));
292 }
293
294
295 public void testMultipleModelToView1() throws Exception {
296 listSelection1.setSelector(Selector.fromString("selections"));
297 listSelection1.setBoundModel(model);
298
299 list1.setListModel(new String[] {"abc", "def", "ghi", "xyz"});
300 list1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
301 SuiteViewSwing.waitForAWT();
302
303 assertTrue(list1.getSelectedIndex() == -1);
304
305 HashSet set = new HashSet();
306 set.add("def");
307 set.add("ghi");
308 model.setSelections(set);
309 SuiteViewSwing.waitForAWT();
310
311 assertTrue(listSelection1.getMinSelectionIndex() == 1);
312 assertTrue(listSelection1.getMaxSelectionIndex() == 2);
313
314 model.setSelections(new HashSet());
315 SuiteViewSwing.waitForAWT();
316
317 assertTrue(list1.getSelectedIndex() == -1);
318
319 set = new HashSet();
320 set.add("xyz");
321 set.add("abc");
322 model.setSelections(set);
323 SuiteViewSwing.waitForAWT();
324
325 assertTrue(listSelection1.getMinSelectionIndex() == 0);
326 assertTrue(listSelection1.getMaxSelectionIndex() == 3);
327
328 model.setSelections(null);
329 SuiteViewSwing.waitForAWT();
330
331 assertTrue(list1.getSelectedIndex() == -1);
332 }
333
334
335 public void testMultipleRefresh() throws Exception {
336 listSelection1.setSelector(Selector.fromString("selections"));
337 listSelection1.setBoundModel(model);
338
339 list1.setListModel(new String[] {"abc", "def", "ghi", "xyz"});
340 list1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
341 model.setSelections(new HashSet());
342 SuiteViewSwing.waitForAWT();
343
344 assertTrue(list1.getSelectedIndex() == -1);
345
346 model.getSelections().add("def");
347 model.getSelections().add("ghi");
348 listSelection1.refresh();
349 SuiteViewSwing.waitForAWT();
350
351 assertTrue(listSelection1.getMinSelectionIndex() == 1);
352 assertTrue(listSelection1.getMaxSelectionIndex() == 2);
353
354 model.getSelections().clear();
355 listSelection1.refresh();
356 SuiteViewSwing.waitForAWT();
357
358 assertTrue(list1.getSelectedIndex() == -1);
359
360 model.getSelections().add("xyz");
361 model.getSelections().add("abc");
362 listSelection1.refresh();
363 SuiteViewSwing.waitForAWT();
364
365 assertTrue(listSelection1.getMinSelectionIndex() == 0);
366 assertTrue(listSelection1.getMaxSelectionIndex() == 3);
367 }
368 }
This page was automatically generated by Maven