001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 15.04.2005
021:
022: */package javax.swing;
023:
024: import java.util.Enumeration;
025: import junit.framework.TestCase;
026:
027: public class ButtonGroupTest extends TestCase {
028: protected ButtonGroup group;
029:
030: protected AbstractButton[] buttons;
031:
032: @Override
033: protected void setUp() throws Exception {
034: super .setUp();
035: group = new ButtonGroup();
036: buttons = new AbstractButton[5];
037: buttons[0] = new JCheckBox();
038: buttons[1] = new JCheckBox();
039: buttons[2] = new JRadioButton();
040: buttons[3] = new JRadioButton();
041: buttons[4] = new JButton();
042: }
043:
044: public void testButtonGroup() {
045: assertEquals("initial number of buttons", 0, group
046: .getButtonCount());
047: assertNull("initial selection", group.getSelection());
048: assertTrue("initial enumeration isn't null", group
049: .getElements() != null);
050: assertFalse("initial enumeration is empty", group.getElements()
051: .hasMoreElements());
052: }
053:
054: public void testAdd() {
055: buttons[0].setSelected(true);
056: final DefaultButtonModel model = (DefaultButtonModel) buttons[0]
057: .getModel();
058: assertNull(model.getGroup());
059: group.add(buttons[0]);
060: assertSame(group, model.getGroup());
061: assertEquals("number of buttons", 1, group.getButtonCount());
062: assertEquals("selection", buttons[0].getModel(), group
063: .getSelection());
064: assertTrue("selected model", buttons[0].getModel().isSelected());
065: group.add(buttons[0]);
066: assertEquals("number of buttons", 2, group.getButtonCount());
067: assertEquals("selection", buttons[0].getModel(), group
068: .getSelection());
069: assertTrue("selected model", buttons[0].getModel().isSelected());
070: buttons[3].setSelected(true);
071: group.add(buttons[3]);
072: assertEquals("number of buttons", 3, group.getButtonCount());
073: assertEquals("selection", buttons[0].getModel(), group
074: .getSelection());
075: assertTrue("selected model", buttons[0].getModel().isSelected());
076: assertFalse("unselected model", buttons[3].getModel()
077: .isSelected());
078: buttons[4].setSelected(true);
079: group.add(buttons[4]);
080: assertEquals("number of buttons", 4, group.getButtonCount());
081: assertEquals("selection", buttons[0].getModel(), group
082: .getSelection());
083: assertTrue("selected model", buttons[0].getModel().isSelected());
084: assertFalse("unselected model", buttons[4].getModel()
085: .isSelected());
086: assertFalse("unselected model", buttons[3].getModel()
087: .isSelected());
088: group.add(null);
089: }
090:
091: public void testRemove() {
092: group.add(buttons[0]);
093: group.add(buttons[0]);
094: group.add(buttons[3]);
095: group.add(buttons[4]);
096: final DefaultButtonModel model = (DefaultButtonModel) buttons[0]
097: .getModel();
098: assertSame(group, model.getGroup());
099: assertEquals("number of buttons", 4, group.getButtonCount());
100: buttons[0].setSelected(true);
101: assertEquals("selection", buttons[0].getModel(), group
102: .getSelection());
103: group.remove(buttons[0]);
104: assertEquals("number of buttons", 3, group.getButtonCount());
105: assertNull("selection", group.getSelection());
106: buttons[0].setSelected(true);
107: assertNull("selection", group.getSelection());
108: group.remove(buttons[0]);
109: assertEquals("number of buttons", 2, group.getButtonCount());
110: assertNull(model.getGroup());
111: group.remove(buttons[2]);
112: assertEquals("number of buttons", 2, group.getButtonCount());
113: group.remove(buttons[3]);
114: assertNull("selection", group.getSelection());
115: group.remove(buttons[4]);
116: assertEquals("number of buttons", 0, group.getButtonCount());
117: group.remove(null);
118: }
119:
120: /**
121: * method is being tested by other testcases
122: */
123: public void testGetButtonCount() {
124: }
125:
126: public void testGetElements() {
127: group.add(buttons[0]);
128: group.add(buttons[0]);
129: group.add(buttons[3]);
130: group.add(buttons[4]);
131: Enumeration<AbstractButton> i = group.getElements();
132: assertEquals("button", buttons[0], i.nextElement());
133: assertEquals("button", buttons[0], i.nextElement());
134: assertEquals("button", buttons[3], i.nextElement());
135: assertEquals("button", buttons[4], i.nextElement());
136: assertFalse("no more buttons", i.hasMoreElements());
137: }
138:
139: public void testGetSelection() {
140: group.add(buttons[0]);
141: group.add(buttons[1]);
142: group.add(buttons[3]);
143: group.add(buttons[4]);
144: assertNull("initial selection", group.getSelection());
145: group.setSelected(buttons[0].getModel(), false);
146: assertNull("selection", group.getSelection());
147: group.setSelected(buttons[4].getModel(), true);
148: assertEquals("selection", buttons[4].getModel(), group
149: .getSelection());
150: assertFalse("unselected model", buttons[0].getModel()
151: .isSelected());
152: assertFalse("unselected model", buttons[1].getModel()
153: .isSelected());
154: assertFalse("unselected model", buttons[2].getModel()
155: .isSelected());
156: assertFalse("unselected model", buttons[3].getModel()
157: .isSelected());
158: assertTrue("selected model", buttons[4].getModel().isSelected());
159: group.setSelected(buttons[0].getModel(), true);
160: assertEquals("selection", buttons[0].getModel(), group
161: .getSelection());
162: assertTrue("selected model", buttons[0].getModel().isSelected());
163: assertFalse("unselected model", buttons[1].getModel()
164: .isSelected());
165: assertFalse("unselected model", buttons[2].getModel()
166: .isSelected());
167: assertFalse("unselected model", buttons[3].getModel()
168: .isSelected());
169: assertFalse("unselected model", buttons[4].getModel()
170: .isSelected());
171: group.setSelected(buttons[0].getModel(), false);
172: assertEquals("selection", buttons[0].getModel(), group
173: .getSelection());
174: assertTrue("selected model", buttons[0].getModel().isSelected());
175: assertFalse("unselected model", buttons[1].getModel()
176: .isSelected());
177: assertFalse("unselected model", buttons[2].getModel()
178: .isSelected());
179: assertFalse("unselected model", buttons[3].getModel()
180: .isSelected());
181: assertFalse("unselected model", buttons[4].getModel()
182: .isSelected());
183: buttons[0].setSelected(false);
184: assertEquals("selection", buttons[0].getModel(), group
185: .getSelection());
186: assertTrue("selected model", buttons[0].getModel().isSelected());
187: assertFalse("unselected model", buttons[1].getModel()
188: .isSelected());
189: assertFalse("unselected model", buttons[2].getModel()
190: .isSelected());
191: assertFalse("unselected model", buttons[3].getModel()
192: .isSelected());
193: assertFalse("unselected model", buttons[4].getModel()
194: .isSelected());
195: group.setSelected(buttons[3].getModel(), true);
196: assertEquals("selection", buttons[3].getModel(), group
197: .getSelection());
198: assertFalse("unselected model", buttons[0].getModel()
199: .isSelected());
200: assertFalse("unselected model", buttons[1].getModel()
201: .isSelected());
202: assertFalse("unselected model", buttons[2].getModel()
203: .isSelected());
204: assertTrue("selected model", buttons[3].getModel().isSelected());
205: assertFalse("unselected model", buttons[4].getModel()
206: .isSelected());
207: group.setSelected(buttons[2].getModel(), true);
208: assertEquals("selection", buttons[2].getModel(), group
209: .getSelection());
210: assertFalse("unselected model", buttons[0].getModel()
211: .isSelected());
212: assertFalse("unselected model", buttons[1].getModel()
213: .isSelected());
214: assertTrue("selected model", buttons[2].getModel().isSelected());
215: assertFalse("unselected model", buttons[3].getModel()
216: .isSelected());
217: assertFalse("unselected model", buttons[4].getModel()
218: .isSelected());
219: buttons[1].setSelected(true);
220: assertEquals("selection", buttons[1].getModel(), group
221: .getSelection());
222: assertFalse("unselected model", buttons[0].getModel()
223: .isSelected());
224: assertTrue("selected model", buttons[1].getModel().isSelected());
225: assertFalse("unselected model", buttons[2].getModel()
226: .isSelected());
227: assertFalse("unselected model", buttons[3].getModel()
228: .isSelected());
229: assertFalse("unselected model", buttons[4].getModel()
230: .isSelected());
231: buttons[4].setSelected(true);
232: assertEquals("selection", buttons[1].getModel(), group
233: .getSelection());
234: assertFalse("unselected model", buttons[0].getModel()
235: .isSelected());
236: assertTrue("selected model", buttons[1].getModel().isSelected());
237: assertFalse("unselected model", buttons[2].getModel()
238: .isSelected());
239: assertFalse("unselected model", buttons[3].getModel()
240: .isSelected());
241: assertTrue("unselected model", buttons[4].getModel()
242: .isSelected());
243: }
244:
245: public void testIsSelected() {
246: buttons[0].setSelected(true);
247: group.add(buttons[0]);
248: buttons[3].setSelected(true);
249: group.add(buttons[3]);
250: assertTrue("selected model", group.isSelected(buttons[0]
251: .getModel()));
252: assertFalse("unselected model", group.isSelected(buttons[3]
253: .getModel()));
254: assertEquals("selection", buttons[0].getModel(), group
255: .getSelection());
256: assertTrue("selected model", buttons[0].getModel().isSelected());
257: assertFalse("unselected model", buttons[3].getModel()
258: .isSelected());
259: buttons[1].setSelected(true);
260: buttons[2].setSelected(true);
261: assertFalse("selected model", group.isSelected(buttons[1]
262: .getModel()));
263: assertFalse("selected model", group.isSelected(buttons[2]
264: .getModel()));
265: }
266:
267: public void testSetSelected() {
268: buttons[0].setSelected(true);
269: group.add(buttons[0]);
270: group.setSelected(null, false);
271: group.setSelected(null, true);
272: }
273: }
|