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 Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.event.ItemEvent;
023: import java.awt.event.ItemListener;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * CheckboxTest
029: */
030: @SuppressWarnings("serial")
031: public class CheckboxTest extends TestCase {
032:
033: class TestCheckbox extends Checkbox {
034:
035: public TestCheckbox() throws HeadlessException {
036: super ();
037: }
038:
039: public TestCheckbox(String arg0) throws HeadlessException {
040: super (arg0);
041: }
042:
043: public TestCheckbox(String arg0, boolean arg1)
044: throws HeadlessException {
045: super (arg0, arg1);
046: }
047:
048: public TestCheckbox(String arg0, boolean arg1,
049: CheckboxGroup arg2) throws HeadlessException {
050: super (arg0, arg1, arg2);
051: }
052:
053: public TestCheckbox(String arg0, CheckboxGroup arg1,
054: boolean arg2) throws HeadlessException {
055: super (arg0, arg1, arg2);
056: }
057:
058: @Override
059: public void paint(Graphics g) {
060: super .paint(g);
061: EventQueue.invokeLater(new Runnable() {
062: public void run() {
063: }
064: });
065: }
066: }
067:
068: private TestCheckbox checkbox;
069: private boolean eventProcessed;
070:
071: /*
072: * @see TestCase#setUp()
073: */
074: @Override
075: protected void setUp() throws Exception {
076: super .setUp();
077: checkbox = new TestCheckbox("Checkbox");
078: }
079:
080: /*
081: * @see TestCase#tearDown()
082: */
083: @Override
084: protected void tearDown() throws Exception {
085: super .tearDown();
086: }
087:
088: public void testCheckbox() {
089: checkbox = new TestCheckbox();
090:
091: assertEquals("", checkbox.getLabel());
092: assertFalse(checkbox.getState());
093: assertNull(checkbox.getCheckboxGroup());
094: }
095:
096: public void testCheckboxStringBoolean() {
097: String text = "checkbox";
098: boolean checked = true;
099: checkbox = new TestCheckbox(text, checked);
100: checkCheckbox(text, checked, null);
101: }
102:
103: public void testCheckboxString() {
104: checkCheckbox("Checkbox", false, null);
105: }
106:
107: public void testCheckboxStringBooleanCheckboxGroup() {
108: String text = "checkbox";
109: boolean checked = false;
110: CheckboxGroup group = new CheckboxGroup();
111: checkbox = new TestCheckbox(text, checked, group);
112: checkCheckbox(text, checked, group);
113: }
114:
115: public void testCheckboxStringCheckboxGroupBoolean() {
116: String text = null;
117: boolean checked = true;
118: CheckboxGroup group = new CheckboxGroup();
119: checkbox = new TestCheckbox(text, group, checked);
120: checkCheckbox(text, checked, group);
121: }
122:
123: private void checkCheckbox(String text, boolean state,
124: CheckboxGroup group) {
125: assertEquals(text, checkbox.getLabel());
126: assertEquals(state, checkbox.getState());
127: assertSame(group, checkbox.getCheckboxGroup());
128: }
129:
130: public void testGetSetLabel() {
131: checkbox.setLabel(null);
132: assertNull(checkbox.getLabel());
133:
134: String text = "Checkbox";
135: checkbox.setLabel(text);
136: assertEquals(text, checkbox.getLabel());
137: }
138:
139: public void testGetSetState() {
140: assertFalse(checkbox.getState());
141: checkbox.setState(true);
142: assertTrue(checkbox.getState());
143: checkbox.setState(false);
144: assertFalse(checkbox.getState());
145: }
146:
147: public void testGetSelectedObjects() {
148: assertNull(checkbox.getSelectedObjects());
149: checkbox.setState(true);
150: Object[] selected = checkbox.getSelectedObjects();
151: assertNotNull(selected);
152: assertEquals(1, selected.length);
153: assertTrue(selected[0] instanceof String);
154: String strSelected = (String) selected[0];
155: assertSame(checkbox.getLabel(), strSelected);
156: }
157:
158: public void testGetSetCheckboxGroup() {
159: assertNull(checkbox.getCheckboxGroup());
160: CheckboxGroup group = new CheckboxGroup();
161:
162: checkbox.setCheckboxGroup(group);
163: assertSame(group, checkbox.getCheckboxGroup());
164: checkbox.setCheckboxGroup(null);
165: assertNull(checkbox.getCheckboxGroup());
166:
167: }
168:
169: public void testAddGetRemoveItemListener() {
170: assertEquals(0, checkbox.getItemListeners().length);
171:
172: ItemListener listener = new ItemListener() {
173: public void itemStateChanged(ItemEvent ie) {
174: }
175: };
176: checkbox.addItemListener(listener);
177: assertEquals(1, checkbox.getItemListeners().length);
178: assertSame(listener, checkbox.getItemListeners()[0]);
179:
180: checkbox.removeItemListener(listener);
181: assertEquals(0, checkbox.getItemListeners().length);
182: }
183:
184: public void testProcessItemEvent() {
185: eventProcessed = false;
186: checkbox.addItemListener(new ItemListener() {
187: public void itemStateChanged(ItemEvent ie) {
188: eventProcessed = true;
189: }
190: });
191: checkbox.processEvent(new ItemEvent(checkbox,
192: ItemEvent.ITEM_STATE_CHANGED, checkbox,
193: ItemEvent.SELECTED));
194: assertTrue(eventProcessed);
195: }
196:
197: public void testGetListenersClass() {
198: Class<ItemListener> cls = ItemListener.class;
199: assertEquals(0, checkbox.getListeners(cls).length);
200:
201: ItemListener listener = new ItemListener() {
202:
203: public void itemStateChanged(ItemEvent ie) {
204: }
205: };
206: checkbox.addItemListener(listener);
207: assertEquals(1, checkbox.getListeners(cls).length);
208: assertSame(listener, checkbox.getListeners(cls)[0]);
209:
210: checkbox.removeItemListener(listener);
211: assertEquals(0, checkbox.getListeners(cls).length);
212: }
213:
214: public void testDeadLoop4887() {
215: final int count[] = new int[1];
216: Component c = new Checkbox() {
217: public void paint(Graphics g) {
218: count[0]++;
219: setState(true);
220: setEnabled(true);
221: }
222: };
223:
224: Tools.checkDeadLoop(c, count);
225: }
226:
227: }
|