001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.test;
031:
032: import nextapp.echo2.app.event.ChangeEvent;
033: import nextapp.echo2.app.event.ChangeListener;
034: import nextapp.echo2.app.list.DefaultListSelectionModel;
035: import junit.framework.TestCase;
036:
037: /**
038: * Unit test(s) for the <code>nextapp.echo2.app.list.ListSelectionModel</code>
039: * and derivatives.
040: */
041: public class ListSelectionModelTest extends TestCase {
042:
043: public class TestChangeListener implements ChangeListener {
044:
045: private ChangeEvent e;
046:
047: /**
048: * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
049: */
050: public void stateChanged(ChangeEvent e) {
051: this .e = e;
052: }
053: }
054:
055: public void testChangeSelectionMode() {
056: DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
057: TestChangeListener changeListener = new TestChangeListener();
058: selectionModel.addChangeListener(changeListener);
059: selectionModel
060: .setSelectionMode(DefaultListSelectionModel.MULTIPLE_SELECTION);
061: assertEquals(DefaultListSelectionModel.MULTIPLE_SELECTION,
062: selectionModel.getSelectionMode());
063: selectionModel.setSelectedIndex(25, true);
064: selectionModel.setSelectedIndex(27, true);
065: selectionModel.setSelectedIndex(50, true);
066: selectionModel.setSelectedIndex(44, true);
067: assertEquals(25, selectionModel.getMinSelectedIndex());
068: assertEquals(50, selectionModel.getMaxSelectedIndex());
069: changeListener.e = null;
070:
071: selectionModel
072: .setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
073: assertEquals(DefaultListSelectionModel.SINGLE_SELECTION,
074: selectionModel.getSelectionMode());
075: assertEquals(25, selectionModel.getMinSelectedIndex());
076: assertEquals(25, selectionModel.getMaxSelectedIndex());
077: assertNotNull(changeListener.e);
078: assertEquals(selectionModel, changeListener.e.getSource());
079: }
080:
081: public void testInitialState() {
082: DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
083:
084: assertTrue(selectionModel.isSelectionEmpty());
085: assertEquals(-1, selectionModel.getMinSelectedIndex());
086: assertEquals(-1, selectionModel.getMaxSelectedIndex());
087: assertFalse(selectionModel.isSelectedIndex(0));
088: assertFalse(selectionModel.isSelectedIndex(1));
089: assertFalse(selectionModel.isSelectedIndex(2));
090: }
091:
092: public void testMultipleSelection() {
093: DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
094: selectionModel
095: .setSelectionMode(DefaultListSelectionModel.MULTIPLE_SELECTION);
096: assertEquals(DefaultListSelectionModel.MULTIPLE_SELECTION,
097: selectionModel.getSelectionMode());
098:
099: TestChangeListener changeListener = new TestChangeListener();
100: selectionModel.addChangeListener(changeListener);
101:
102: selectionModel.setSelectedIndex(50, true);
103: assertFalse(selectionModel.isSelectionEmpty());
104: assertTrue(selectionModel.isSelectedIndex(50));
105: assertFalse(selectionModel.isSelectedIndex(0));
106: assertFalse(selectionModel.isSelectedIndex(5));
107: assertFalse(selectionModel.isSelectedIndex(49));
108: assertFalse(selectionModel.isSelectedIndex(51));
109: assertEquals(50, selectionModel.getMinSelectedIndex());
110: assertEquals(50, selectionModel.getMaxSelectedIndex());
111: assertNotNull(changeListener.e);
112: assertEquals(selectionModel, changeListener.e.getSource());
113: changeListener.e = null;
114:
115: selectionModel.setSelectedIndex(75, true);
116: assertFalse(selectionModel.isSelectionEmpty());
117: assertTrue(selectionModel.isSelectedIndex(50));
118: assertTrue(selectionModel.isSelectedIndex(75));
119: assertEquals(50, selectionModel.getMinSelectedIndex());
120: assertEquals(75, selectionModel.getMaxSelectedIndex());
121: assertNotNull(changeListener.e);
122: assertEquals(selectionModel, changeListener.e.getSource());
123: changeListener.e = null;
124:
125: selectionModel.setSelectedIndex(67, true);
126: assertFalse(selectionModel.isSelectionEmpty());
127: assertTrue(selectionModel.isSelectedIndex(50));
128: assertTrue(selectionModel.isSelectedIndex(67));
129: assertTrue(selectionModel.isSelectedIndex(75));
130: assertEquals(50, selectionModel.getMinSelectedIndex());
131: assertEquals(75, selectionModel.getMaxSelectedIndex());
132: assertNotNull(changeListener.e);
133: assertEquals(selectionModel, changeListener.e.getSource());
134: changeListener.e = null;
135:
136: selectionModel.setSelectedIndex(21, true);
137: assertFalse(selectionModel.isSelectionEmpty());
138: assertTrue(selectionModel.isSelectedIndex(21));
139: assertTrue(selectionModel.isSelectedIndex(50));
140: assertTrue(selectionModel.isSelectedIndex(67));
141: assertTrue(selectionModel.isSelectedIndex(75));
142: assertEquals(21, selectionModel.getMinSelectedIndex());
143: assertEquals(75, selectionModel.getMaxSelectedIndex());
144: assertNotNull(changeListener.e);
145: assertEquals(selectionModel, changeListener.e.getSource());
146: changeListener.e = null;
147:
148: selectionModel.setSelectedIndex(75, false);
149: assertFalse(selectionModel.isSelectionEmpty());
150: assertTrue(selectionModel.isSelectedIndex(21));
151: assertTrue(selectionModel.isSelectedIndex(50));
152: assertTrue(selectionModel.isSelectedIndex(67));
153: assertFalse(selectionModel.isSelectedIndex(75));
154: assertEquals(21, selectionModel.getMinSelectedIndex());
155: assertEquals(67, selectionModel.getMaxSelectedIndex());
156: assertNotNull(changeListener.e);
157: assertEquals(selectionModel, changeListener.e.getSource());
158: changeListener.e = null;
159:
160: selectionModel.setSelectedIndex(21, false);
161: assertFalse(selectionModel.isSelectionEmpty());
162: assertFalse(selectionModel.isSelectedIndex(21));
163: assertTrue(selectionModel.isSelectedIndex(50));
164: assertTrue(selectionModel.isSelectedIndex(67));
165: assertFalse(selectionModel.isSelectedIndex(75));
166: assertEquals(50, selectionModel.getMinSelectedIndex());
167: assertEquals(67, selectionModel.getMaxSelectedIndex());
168: assertNotNull(changeListener.e);
169: assertEquals(selectionModel, changeListener.e.getSource());
170: changeListener.e = null;
171:
172: selectionModel.clearSelection();
173: assertTrue(selectionModel.isSelectionEmpty());
174: assertFalse(selectionModel.isSelectedIndex(21));
175: assertFalse(selectionModel.isSelectedIndex(50));
176: assertFalse(selectionModel.isSelectedIndex(67));
177: assertFalse(selectionModel.isSelectedIndex(75));
178: assertEquals(-1, selectionModel.getMinSelectedIndex());
179: assertEquals(-1, selectionModel.getMaxSelectedIndex());
180: assertNotNull(changeListener.e);
181: assertEquals(selectionModel, changeListener.e.getSource());
182: changeListener.e = null;
183: }
184:
185: public void testSingleSelection() {
186: DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
187: assertEquals(DefaultListSelectionModel.SINGLE_SELECTION,
188: selectionModel.getSelectionMode());
189: TestChangeListener changeListener = new TestChangeListener();
190: selectionModel.addChangeListener(changeListener);
191:
192: selectionModel.setSelectedIndex(50, true);
193: assertFalse(selectionModel.isSelectionEmpty());
194: assertTrue(selectionModel.isSelectedIndex(50));
195: assertFalse(selectionModel.isSelectedIndex(0));
196: assertFalse(selectionModel.isSelectedIndex(5));
197: assertFalse(selectionModel.isSelectedIndex(49));
198: assertFalse(selectionModel.isSelectedIndex(51));
199: assertEquals(50, selectionModel.getMinSelectedIndex());
200: assertEquals(50, selectionModel.getMaxSelectedIndex());
201: assertNotNull(changeListener.e);
202: assertEquals(selectionModel, changeListener.e.getSource());
203: changeListener.e = null;
204:
205: selectionModel.setSelectedIndex(75, true);
206: assertFalse(selectionModel.isSelectionEmpty());
207: assertFalse(selectionModel.isSelectedIndex(50));
208: assertTrue(selectionModel.isSelectedIndex(75));
209: assertEquals(75, selectionModel.getMinSelectedIndex());
210: assertEquals(75, selectionModel.getMaxSelectedIndex());
211: assertNotNull(changeListener.e);
212: assertEquals(selectionModel, changeListener.e.getSource());
213: changeListener.e = null;
214:
215: selectionModel.clearSelection();
216: assertTrue(selectionModel.isSelectionEmpty());
217: assertFalse(selectionModel.isSelectedIndex(50));
218: assertFalse(selectionModel.isSelectedIndex(75));
219: assertEquals(-1, selectionModel.getMinSelectedIndex());
220: assertEquals(-1, selectionModel.getMaxSelectedIndex());
221: assertNotNull(changeListener.e);
222: assertEquals(selectionModel, changeListener.e.getSource());
223: changeListener.e = null;
224: }
225: }
|