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 31.01.2005
021:
022: */package javax.swing;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.awt.event.ItemEvent;
027: import java.awt.event.ItemListener;
028: import java.io.ByteArrayInputStream;
029: import java.io.ByteArrayOutputStream;
030: import java.io.InputStream;
031: import java.io.ObjectInputStream;
032: import java.io.ObjectOutputStream;
033: import java.util.EventListener;
034: import javax.swing.event.ChangeEvent;
035: import javax.swing.event.ChangeListener;
036:
037: public class DefaultButtonModelTest extends SwingTestCase {
038: protected int find(final Object[] array, final Object value) {
039: int found = 0;
040: if (array != null) {
041: for (int i = 0; i < array.length; i++) {
042: if (array[i].equals(value)) {
043: found++;
044: }
045: }
046: }
047: return found;
048: }
049:
050: class ConcreteActionListener implements ActionListener {
051: public ActionEvent eventHappened = null;
052:
053: public void actionPerformed(final ActionEvent event) {
054: eventHappened = event;
055: }
056: };
057:
058: class ConcreteItemListener implements ItemListener {
059: public ItemEvent eventHappened = null;
060:
061: public void itemStateChanged(final ItemEvent event) {
062: eventHappened = event;
063: }
064: };
065:
066: class ConcreteChangeListener implements ChangeListener {
067: public ChangeEvent eventHappened = null;
068:
069: public void stateChanged(final ChangeEvent event) {
070: eventHappened = event;
071: }
072: };
073:
074: protected DefaultButtonModel buttonModel;
075:
076: @Override
077: protected void setUp() throws Exception {
078: super .setUp();
079: buttonModel = new DefaultButtonModel();
080: }
081:
082: @Override
083: protected void tearDown() throws Exception {
084: super .tearDown();
085: buttonModel = null;
086: }
087:
088: public void testDefaultButtonModel() {
089: assertEquals("default mnemonic ", 0, buttonModel.getMnemonic());
090: assertNull("default action command ", buttonModel
091: .getActionCommand());
092: assertEquals("default actionListeners array ", 0, buttonModel
093: .getActionListeners().length);
094: assertEquals("default changeListeners array ", 0, buttonModel
095: .getChangeListeners().length);
096: assertEquals("default itemListeners array ", 0, buttonModel
097: .getItemListeners().length);
098: assertNull("default group ", buttonModel.getGroup());
099: assertNull("default selected objects array ", buttonModel
100: .getSelectedObjects());
101: assertFalse("default armed state ", buttonModel.isArmed());
102: assertTrue("default enabled state ", buttonModel.isEnabled());
103: assertFalse("default pressed state ", buttonModel.isPressed());
104: assertFalse("default rollover state ", buttonModel.isRollover());
105: assertFalse("default selected state ", buttonModel.isSelected());
106: }
107:
108: public void testGetListeners() {
109: ChangeListener listener1 = new ConcreteChangeListener();
110: ChangeListener listener2 = new ConcreteChangeListener();
111: ChangeListener listener3 = new ConcreteChangeListener();
112: ItemListener listener4 = new ConcreteItemListener();
113: ItemListener listener5 = new ConcreteItemListener();
114: ItemListener listener6 = new ConcreteItemListener();
115: ActionListener listener7 = new ConcreteActionListener();
116: ActionListener listener8 = new ConcreteActionListener();
117: ActionListener listener9 = new ConcreteActionListener();
118: EventListener[] listeners = buttonModel
119: .getListeners(ChangeListener.class);
120: assertTrue("listener's array has the proper size ",
121: listeners != null && listeners.length == 0);
122: buttonModel.addChangeListener(listener1);
123: buttonModel.addChangeListener(listener2);
124: listeners = buttonModel.getListeners(ChangeListener.class);
125: assertTrue("listener's array has the proper size ",
126: listeners != null && listeners.length == 2);
127: assertTrue("listener's added successfully ", find(listeners,
128: listener1) == 1);
129: assertTrue("listener's added successfully ", find(listeners,
130: listener2) == 1);
131: buttonModel.addChangeListener(listener3);
132: buttonModel.addItemListener(listener5);
133: buttonModel.addItemListener(listener4);
134: listeners = buttonModel.getListeners(ChangeListener.class);
135: assertTrue("listener's array has the proper size ",
136: listeners != null && listeners.length == 3);
137: assertTrue("listener's added successfully ", find(listeners,
138: listener1) == 1);
139: assertTrue("listener's added successfully ", find(listeners,
140: listener2) == 1);
141: assertTrue("listener's added successfully ", find(listeners,
142: listener3) == 1);
143: listeners = buttonModel.getListeners(ItemListener.class);
144: assertTrue("listener's array has the proper size ",
145: listeners != null && listeners.length == 2);
146: assertTrue("listener's added successfully ", find(listeners,
147: listener4) == 1);
148: assertTrue("listener's added successfully ", find(listeners,
149: listener5) == 1);
150: buttonModel.addItemListener(listener6);
151: buttonModel.addActionListener(listener7);
152: buttonModel.addActionListener(listener8);
153: buttonModel.addActionListener(listener9);
154: listeners = buttonModel.getListeners(ChangeListener.class);
155: assertTrue("listener's array has the proper size ",
156: listeners != null && listeners.length == 3);
157: assertTrue("listener's added successfully ", find(listeners,
158: listener1) == 1);
159: assertTrue("listener's added successfully ", find(listeners,
160: listener2) == 1);
161: assertTrue("listener's added successfully ", find(listeners,
162: listener3) == 1);
163: listeners = buttonModel.getListeners(ItemListener.class);
164: assertTrue("listener's array has the proper size ",
165: listeners != null && listeners.length == 3);
166: assertTrue("listener's added successfully ", find(listeners,
167: listener4) == 1);
168: assertTrue("listener's added successfully ", find(listeners,
169: listener5) == 1);
170: assertTrue("listener's added successfully ", find(listeners,
171: listener6) == 1);
172: listeners = buttonModel.getListeners(ActionListener.class);
173: assertTrue("listener's array has the proper size ",
174: listeners != null && listeners.length == 3);
175: assertTrue("listener's added successfully ", find(listeners,
176: listener7) == 1);
177: assertTrue("listener's added successfully ", find(listeners,
178: listener8) == 1);
179: assertTrue("listener's added successfully ", find(listeners,
180: listener9) == 1);
181: }
182:
183: public void testRemoveChangeListener() {
184: ChangeListener listener1 = new ConcreteChangeListener();
185: ChangeListener listener2 = new ConcreteChangeListener();
186: ChangeListener listener3 = new ConcreteChangeListener();
187: buttonModel.addChangeListener(listener1);
188: buttonModel.addChangeListener(listener2);
189: buttonModel.addChangeListener(listener2);
190: buttonModel.addChangeListener(listener3);
191: EventListener[] listeners = buttonModel
192: .getListeners(ChangeListener.class);
193: assertTrue("listener's array has the proper size ",
194: listeners != null && listeners.length == 4);
195: buttonModel.removeChangeListener(listener1);
196: listeners = buttonModel.getListeners(ChangeListener.class);
197: assertTrue("listener's array has the proper size ",
198: listeners != null && listeners.length == 3);
199: assertTrue("listener's removed successfully ", find(listeners,
200: listener3) == 1);
201: assertTrue("listener's removed successfully ", find(listeners,
202: listener2) == 2);
203: buttonModel.removeChangeListener(listener2);
204: listeners = buttonModel.getListeners(ChangeListener.class);
205: assertTrue("listener's array has the proper size ",
206: listeners != null && listeners.length == 2);
207: assertTrue("listener's removed successfully ", find(listeners,
208: listener3) == 1);
209: assertTrue("listener's removed successfully ", find(listeners,
210: listener2) == 1);
211: buttonModel.removeChangeListener(listener2);
212: listeners = buttonModel.getListeners(ChangeListener.class);
213: assertTrue("listener's array has the proper size ",
214: listeners != null && listeners.length == 1);
215: assertTrue("listener's removed successfully ", find(listeners,
216: listener3) == 1);
217: assertTrue("listener's removed successfully ", find(listeners,
218: listener2) == 0);
219: buttonModel.removeChangeListener(listener2);
220: listeners = buttonModel.getListeners(ChangeListener.class);
221: assertTrue("listener's array has the proper size ",
222: listeners != null && listeners.length == 1);
223: assertTrue("listener's removed successfully ", find(listeners,
224: listener3) == 1);
225: assertTrue("listener's removed successfully ", find(listeners,
226: listener2) == 0);
227: buttonModel.removeChangeListener(listener3);
228: listeners = buttonModel.getListeners(ChangeListener.class);
229: assertTrue("listener's array has the proper size ",
230: listeners != null && listeners.length == 0);
231: assertTrue("listener's removed successfully ", find(listeners,
232: listener3) == 0);
233: }
234:
235: public void testAddChangeListener() {
236: ChangeListener listener1 = new ConcreteChangeListener();
237: ChangeListener listener2 = new ConcreteChangeListener();
238: buttonModel.addChangeListener(listener1);
239: EventListener[] listeners = buttonModel
240: .getListeners(ChangeListener.class);
241: assertTrue("listener's array has the proper size ",
242: listeners != null && listeners.length == 1);
243: assertEquals("listener's added successfully ", listeners[0],
244: listener1);
245: buttonModel.addChangeListener(listener2);
246: listeners = buttonModel.getListeners(ChangeListener.class);
247: assertTrue("listener's array has the proper size ",
248: listeners != null && listeners.length == 2);
249: assertTrue("listener's added successfully ", find(listeners,
250: listener1) == 1);
251: assertTrue("listener's added successfully ", find(listeners,
252: listener2) == 1);
253: buttonModel.addChangeListener(listener2);
254: listeners = buttonModel.getListeners(ChangeListener.class);
255: assertTrue("listener's array has the proper size ",
256: listeners != null && listeners.length == 3);
257: assertTrue("listener's added successfully ", find(listeners,
258: listener1) == 1);
259: assertTrue("listener's added successfully ", find(listeners,
260: listener2) == 2);
261: }
262:
263: /**
264: * this method is being tested mostly with testAddChangeListener()
265: * and testRemoveChangeListener()
266: */
267: public void testGetChangeListeners() {
268: EventListener[] listeners = buttonModel
269: .getListeners(ChangeListener.class);
270: assertTrue("listener's array has the proper size ",
271: listeners != null && listeners.length == 0);
272: }
273:
274: public void testSetGroup() {
275: ButtonGroup group1 = new ButtonGroup();
276: ButtonGroup group2 = new ButtonGroup();
277: ButtonGroup group3 = new ButtonGroup();
278: assertNull("default group ", buttonModel.getGroup());
279: buttonModel.setGroup(group1);
280: assertEquals("group ", group1, buttonModel.getGroup());
281: buttonModel.setGroup(group2);
282: assertEquals("group ", group2, buttonModel.getGroup());
283: buttonModel.setGroup(group3);
284: assertEquals("group ", group3, buttonModel.getGroup());
285: }
286:
287: public void testGetGroup() {
288: assertNull("default group ", buttonModel.getGroup());
289: }
290:
291: public void testSetActionCommand() {
292: String command1 = "command1";
293: String command2 = "command2";
294: String command3 = "command3";
295: assertNull("default action command ", buttonModel
296: .getActionCommand());
297: buttonModel.setActionCommand(command1);
298: assertEquals("action command ", command1, buttonModel
299: .getActionCommand());
300: buttonModel.setActionCommand(command2);
301: assertEquals("action command ", command2, buttonModel
302: .getActionCommand());
303: buttonModel.setActionCommand(command3);
304: assertEquals("action command ", command3, buttonModel
305: .getActionCommand());
306: }
307:
308: public void testGetActionCommand() {
309: assertNull("default action command ", buttonModel
310: .getActionCommand());
311: }
312:
313: public void testGetSelectedObjects() {
314: assertNull("selected objects ", buttonModel
315: .getSelectedObjects());
316: }
317:
318: public void testRemoveItemListener() {
319: ItemListener listener1 = new ConcreteItemListener();
320: ItemListener listener2 = new ConcreteItemListener();
321: ItemListener listener3 = new ConcreteItemListener();
322: buttonModel.addItemListener(listener1);
323: buttonModel.addItemListener(listener2);
324: buttonModel.addItemListener(listener2);
325: buttonModel.addItemListener(listener3);
326: EventListener[] listeners = buttonModel
327: .getListeners(ItemListener.class);
328: assertTrue("listener's array has the proper size ",
329: listeners != null && listeners.length == 4);
330: buttonModel.removeItemListener(listener1);
331: listeners = buttonModel.getListeners(ItemListener.class);
332: assertTrue("listener's array has the proper size ",
333: listeners != null && listeners.length == 3);
334: assertTrue("listener's removed successfully ", find(listeners,
335: listener3) == 1);
336: assertTrue("listener's removed successfully ", find(listeners,
337: listener2) == 2);
338: buttonModel.removeItemListener(listener2);
339: listeners = buttonModel.getListeners(ItemListener.class);
340: assertTrue("listener's array has the proper size ",
341: listeners != null && listeners.length == 2);
342: assertTrue("listener's removed successfully ", find(listeners,
343: listener3) == 1);
344: assertTrue("listener's removed successfully ", find(listeners,
345: listener2) == 1);
346: buttonModel.removeItemListener(listener2);
347: listeners = buttonModel.getListeners(ItemListener.class);
348: assertTrue("listener's array has the proper size ",
349: listeners != null && listeners.length == 1);
350: assertTrue("listener's removed successfully ", find(listeners,
351: listener3) == 1);
352: assertTrue("listener's removed successfully ", find(listeners,
353: listener2) == 0);
354: buttonModel.removeItemListener(listener2);
355: listeners = buttonModel.getListeners(ItemListener.class);
356: assertTrue("listener's array has the proper size ",
357: listeners != null && listeners.length == 1);
358: assertTrue("listener's removed successfully ", find(listeners,
359: listener3) == 1);
360: assertTrue("listener's removed successfully ", find(listeners,
361: listener2) == 0);
362: buttonModel.removeItemListener(listener3);
363: listeners = buttonModel.getListeners(ItemListener.class);
364: assertTrue("listener's array has the proper size ",
365: listeners != null && listeners.length == 0);
366: assertTrue("listener's removed successfully ", find(listeners,
367: listener3) == 0);
368: }
369:
370: public void testAddItemListener() {
371: ItemListener listener1 = new ConcreteItemListener();
372: ItemListener listener2 = new ConcreteItemListener();
373: buttonModel.addItemListener(listener1);
374: EventListener[] listeners = buttonModel
375: .getListeners(ItemListener.class);
376: assertTrue("listener's array has the proper size ",
377: listeners != null && listeners.length == 1);
378: assertEquals("listener's added successfully ", listeners[0],
379: listener1);
380: buttonModel.addItemListener(listener2);
381: listeners = buttonModel.getListeners(ItemListener.class);
382: assertTrue("listener's array has the proper size ",
383: listeners != null && listeners.length == 2);
384: assertTrue("listener's added successfully ", find(listeners,
385: listener1) == 1);
386: assertTrue("listener's added successfully ", find(listeners,
387: listener2) == 1);
388: buttonModel.addItemListener(listener2);
389: listeners = buttonModel.getListeners(ItemListener.class);
390: assertTrue("listener's array has the proper size ",
391: listeners != null && listeners.length == 3);
392: assertTrue("listener's added successfully ", find(listeners,
393: listener1) == 1);
394: assertTrue("listener's added successfully ", find(listeners,
395: listener2) == 2);
396: }
397:
398: /**
399: * this method is being tested mostly with testAddItemListener()
400: * and testRemoveItemListener()
401: */
402: public void testGetItemListeners() {
403: EventListener[] listeners = buttonModel
404: .getListeners(ItemListener.class);
405: assertTrue("listener's array has the proper size ",
406: listeners != null && listeners.length == 0);
407: }
408:
409: public void testFireItemStateChanged() {
410: Object item1 = "item1";
411: Object item2 = "item2";
412: ItemEvent event1 = new ItemEvent(buttonModel, 11, item1, 2);
413: ItemEvent event2 = new ItemEvent(buttonModel, 111, item2, 1);
414: ConcreteItemListener listener1 = new ConcreteItemListener();
415: ConcreteItemListener listener2 = new ConcreteItemListener();
416: buttonModel.addItemListener(listener1);
417: buttonModel.addItemListener(listener2);
418: buttonModel.fireItemStateChanged(event1);
419: assertEquals("event fired ", event1, listener1.eventHappened);
420: assertEquals("event fired ", event1, listener2.eventHappened);
421: buttonModel.fireItemStateChanged(event2);
422: assertEquals("event fired ", event2, listener1.eventHappened);
423: assertEquals("event fired ", event2, listener2.eventHappened);
424: buttonModel.fireItemStateChanged(null);
425: assertNull("event fired ", listener1.eventHappened);
426: assertNull("event fired ", listener2.eventHappened);
427: }
428:
429: public void testFireActionPerformed1() {
430: String command1 = "command1";
431: String command2 = "command2";
432: ActionEvent event1 = new ActionEvent(buttonModel, 11, command1,
433: 2);
434: ActionEvent event2 = new ActionEvent(buttonModel, 111,
435: command2, 1);
436: ConcreteActionListener listener1 = new ConcreteActionListener();
437: ConcreteActionListener listener2 = new ConcreteActionListener();
438: buttonModel.addActionListener(listener1);
439: buttonModel.addActionListener(listener2);
440: buttonModel.fireActionPerformed(event1);
441: assertEquals("event fired ", event1, listener1.eventHappened);
442: assertEquals("event fired ", event1, listener2.eventHappened);
443: buttonModel.fireActionPerformed(event2);
444: assertEquals("event fired ", event2, listener1.eventHappened);
445: assertEquals("event fired ", event2, listener2.eventHappened);
446: buttonModel.fireActionPerformed(null);
447: assertNull("event fired ", listener1.eventHappened);
448: assertNull("event fired ", listener2.eventHappened);
449: }
450:
451: public void testFireActionPerformed2() {
452: ConcreteActionListener listener1 = new ConcreteActionListener();
453: ConcreteActionListener listener2 = new ConcreteActionListener();
454: buttonModel.addActionListener(listener1);
455: buttonModel.addActionListener(listener2);
456: buttonModel.setPressed(true);
457: buttonModel.setArmed(true);
458: assertNull("actionListeners aren't triggered",
459: listener1.eventHappened);
460: assertNull("actionListeners aren't triggered",
461: listener1.eventHappened);
462: buttonModel.setPressed(false);
463: assertTrue("actionListeners are triggered",
464: listener1.eventHappened != null);
465: assertTrue("actionListeners are triggered",
466: listener2.eventHappened != null);
467: }
468:
469: public void testFireStateChanged() {
470: ChangeEvent event1 = null;
471: ChangeEvent event2 = null;
472: ConcreteChangeListener listener1 = new ConcreteChangeListener();
473: ConcreteChangeListener listener2 = new ConcreteChangeListener();
474: buttonModel.addChangeListener(listener1);
475: buttonModel.addChangeListener(listener2);
476: buttonModel.fireStateChanged();
477: event1 = listener1.eventHappened;
478: assertTrue("event fired ", listener1.eventHappened != null);
479: assertTrue("event fired ", listener2.eventHappened != null);
480: assertTrue("one event fired ",
481: listener1.eventHappened == listener2.eventHappened);
482: assertEquals("event fired properly ", ChangeEvent.class,
483: listener1.eventHappened.getClass());
484: assertEquals("event fired properly ", ChangeEvent.class,
485: listener2.eventHappened.getClass());
486: assertEquals("event fired properly ", buttonModel,
487: listener1.eventHappened.getSource());
488: assertEquals("event fired properly ", buttonModel,
489: listener2.eventHappened.getSource());
490: buttonModel.fireStateChanged();
491: event2 = listener1.eventHappened;
492: assertTrue("event fired ", listener1.eventHappened != null);
493: assertTrue("event fired ", listener2.eventHappened != null);
494: assertTrue("one event fired ",
495: listener1.eventHappened == listener2.eventHappened);
496: assertEquals("event fired properly ", ChangeEvent.class,
497: listener1.eventHappened.getClass());
498: assertEquals("event fired properly ", ChangeEvent.class,
499: listener2.eventHappened.getClass());
500: assertEquals("event fired properly ", buttonModel,
501: listener1.eventHappened.getSource());
502: assertEquals("event fired properly ", buttonModel,
503: listener2.eventHappened.getSource());
504: assertTrue("the same event is fired always ", event1 == event2);
505: }
506:
507: public void testRemoveActionListener() {
508: ActionListener listener1 = new ConcreteActionListener();
509: ActionListener listener2 = new ConcreteActionListener();
510: ActionListener listener3 = new ConcreteActionListener();
511: buttonModel.addActionListener(listener1);
512: buttonModel.addActionListener(listener2);
513: buttonModel.addActionListener(listener2);
514: buttonModel.addActionListener(listener3);
515: EventListener[] listeners = buttonModel
516: .getListeners(ActionListener.class);
517: assertTrue("listener's array has the proper size ",
518: listeners != null && listeners.length == 4);
519: buttonModel.removeActionListener(listener1);
520: listeners = buttonModel.getListeners(ActionListener.class);
521: assertTrue("listener's array has the proper size ",
522: listeners != null && listeners.length == 3);
523: assertTrue("listener's removed successfully ", find(listeners,
524: listener3) == 1);
525: assertTrue("listener's removed successfully ", find(listeners,
526: listener2) == 2);
527: buttonModel.removeActionListener(listener2);
528: listeners = buttonModel.getListeners(ActionListener.class);
529: assertTrue("listener's array has the proper size ",
530: listeners != null && listeners.length == 2);
531: assertTrue("listener's removed successfully ", find(listeners,
532: listener3) == 1);
533: assertTrue("listener's removed successfully ", find(listeners,
534: listener2) == 1);
535: buttonModel.removeActionListener(listener2);
536: listeners = buttonModel.getListeners(ActionListener.class);
537: assertTrue("listener's array has the proper size ",
538: listeners != null && listeners.length == 1);
539: assertTrue("listener's removed successfully ", find(listeners,
540: listener3) == 1);
541: assertTrue("listener's removed successfully ", find(listeners,
542: listener2) == 0);
543: buttonModel.removeActionListener(listener2);
544: listeners = buttonModel.getListeners(ActionListener.class);
545: assertTrue("listener's array has the proper size ",
546: listeners != null && listeners.length == 1);
547: assertTrue("listener's removed successfully ", find(listeners,
548: listener3) == 1);
549: assertTrue("listener's removed successfully ", find(listeners,
550: listener2) == 0);
551: buttonModel.removeActionListener(listener3);
552: listeners = buttonModel.getListeners(ActionListener.class);
553: assertTrue("listener's array has the proper size ",
554: listeners != null && listeners.length == 0);
555: assertTrue("listener's removed successfully ", find(listeners,
556: listener3) == 0);
557: }
558:
559: public void testAddActionListener() {
560: ActionListener listener1 = new ConcreteActionListener();
561: ActionListener listener2 = new ConcreteActionListener();
562: buttonModel.addActionListener(listener1);
563: EventListener[] listeners = buttonModel
564: .getListeners(ActionListener.class);
565: assertTrue("listener's array has the proper size ",
566: listeners != null && listeners.length == 1);
567: assertEquals("listener's added successfully ", listeners[0],
568: listener1);
569: buttonModel.addActionListener(listener2);
570: listeners = buttonModel.getListeners(ActionListener.class);
571: assertTrue("listener's array has the proper size ",
572: listeners != null && listeners.length == 2);
573: assertTrue("listener's added successfully ", find(listeners,
574: listener1) == 1);
575: assertTrue("listener's added successfully ", find(listeners,
576: listener2) == 1);
577: buttonModel.addActionListener(listener2);
578: listeners = buttonModel.getListeners(ActionListener.class);
579: assertTrue("listener's array has the proper size ",
580: listeners != null && listeners.length == 3);
581: assertTrue("listener's added successfully ", find(listeners,
582: listener1) == 1);
583: assertTrue("listener's added successfully ", find(listeners,
584: listener2) == 2);
585: }
586:
587: /**
588: * this method is being tested mostly with testAddActionListener()
589: * and testRemoveActionListener()
590: */
591: public void testGetActionListeners() {
592: EventListener[] listeners = buttonModel
593: .getListeners(ActionListener.class);
594: assertTrue("listener's array has the proper size ",
595: listeners != null && listeners.length == 0);
596: }
597:
598: public void testSetSelected() {
599: boolean value1 = true;
600: boolean value2 = false;
601: ConcreteChangeListener listener1 = new ConcreteChangeListener();
602: ConcreteChangeListener listener2 = new ConcreteChangeListener();
603: ConcreteItemListener listener3 = new ConcreteItemListener();
604: ConcreteItemListener listener4 = new ConcreteItemListener();
605: buttonModel.addChangeListener(listener1);
606: buttonModel.addChangeListener(listener2);
607: buttonModel.addItemListener(listener3);
608: buttonModel.addItemListener(listener4);
609: buttonModel.setSelected(value1);
610: assertEquals("Selected ", value1, buttonModel.isSelected());
611: assertEquals("event fired source ", buttonModel,
612: listener1.eventHappened.getSource());
613: assertEquals("event fired class ", ChangeEvent.class,
614: listener1.eventHappened.getClass());
615: assertEquals("event fired source ", buttonModel,
616: listener2.eventHappened.getSource());
617: assertEquals("event fired class ", ChangeEvent.class,
618: listener2.eventHappened.getClass());
619: assertEquals("event fired source ", buttonModel,
620: listener3.eventHappened.getItem());
621: assertEquals("event fired value ", 1, listener3.eventHappened
622: .getStateChange());
623: assertEquals("event fired source ", buttonModel,
624: listener4.eventHappened.getItem());
625: assertEquals("event fired value ", 1, listener4.eventHappened
626: .getStateChange());
627: buttonModel.setSelected(value2);
628: assertEquals("Selected ", value2, buttonModel.isSelected());
629: assertEquals("event fired source ", buttonModel,
630: listener1.eventHappened.getSource());
631: assertEquals("event fired class ", ChangeEvent.class,
632: listener1.eventHappened.getClass());
633: assertEquals("event fired source ", buttonModel,
634: listener2.eventHappened.getSource());
635: assertEquals("event fired class ", ChangeEvent.class,
636: listener2.eventHappened.getClass());
637: assertEquals("event fired source ", buttonModel,
638: listener3.eventHappened.getItem());
639: assertEquals("event fired value ", 2, listener3.eventHappened
640: .getStateChange());
641: assertEquals("event fired source ", buttonModel,
642: listener4.eventHappened.getItem());
643: assertEquals("event fired value ", 2, listener4.eventHappened
644: .getStateChange());
645: buttonModel.setSelected(value1);
646: assertEquals("Selected ", value1, buttonModel.isSelected());
647: assertEquals("event fired source ", buttonModel,
648: listener1.eventHappened.getSource());
649: assertEquals("event fired class ", ChangeEvent.class,
650: listener1.eventHappened.getClass());
651: assertEquals("event fired source ", buttonModel,
652: listener2.eventHappened.getSource());
653: assertEquals("event fired class ", ChangeEvent.class,
654: listener2.eventHappened.getClass());
655: assertEquals("event fired source ", buttonModel,
656: listener3.eventHappened.getItem());
657: assertEquals("event fired value ", 1, listener3.eventHappened
658: .getStateChange());
659: assertEquals("event fired source ", buttonModel,
660: listener4.eventHappened.getItem());
661: assertEquals("event fired value ", 1, listener4.eventHappened
662: .getStateChange());
663: buttonModel.setSelected(value2);
664: assertEquals("Selected ", value2, buttonModel.isSelected());
665: assertEquals("event fired source ", buttonModel,
666: listener1.eventHappened.getSource());
667: assertEquals("event fired class ", ChangeEvent.class,
668: listener1.eventHappened.getClass());
669: assertEquals("event fired source ", buttonModel,
670: listener2.eventHappened.getSource());
671: assertEquals("event fired class ", ChangeEvent.class,
672: listener2.eventHappened.getClass());
673: assertEquals("event fired source ", buttonModel,
674: listener3.eventHappened.getItem());
675: assertEquals("event fired value ", 2, listener3.eventHappened
676: .getStateChange());
677: assertEquals("event fired source ", buttonModel,
678: listener4.eventHappened.getItem());
679: assertEquals("event fired value ", 2, listener4.eventHappened
680: .getStateChange());
681: listener1.eventHappened = null;
682: listener2.eventHappened = null;
683: listener3.eventHappened = null;
684: listener4.eventHappened = null;
685: buttonModel.setSelected(value2);
686: assertEquals("Selected ", value2, buttonModel.isSelected());
687: assertNull("event wasn't fired ", listener1.eventHappened);
688: assertNull("event wasn't fired ", listener2.eventHappened);
689: assertNull("event wasn't fired ", listener3.eventHappened);
690: assertNull("event wasn't fired ", listener4.eventHappened);
691: buttonModel.setEnabled(true);
692: buttonModel.setSelected(false);
693: buttonModel.setEnabled(false);
694: buttonModel.setSelected(true);
695: assertTrue(buttonModel.isSelected());
696: }
697:
698: public void testSetRollover() {
699: boolean value1 = true;
700: boolean value2 = false;
701: ConcreteChangeListener listener1 = new ConcreteChangeListener();
702: ConcreteChangeListener listener2 = new ConcreteChangeListener();
703: buttonModel.addChangeListener(listener1);
704: buttonModel.addChangeListener(listener2);
705: buttonModel.setRollover(value1);
706: assertEquals("Rollover ", value1, buttonModel.isRollover());
707: assertEquals("event fired source ", buttonModel,
708: listener1.eventHappened.getSource());
709: assertEquals("event fired class ", ChangeEvent.class,
710: listener1.eventHappened.getClass());
711: assertEquals("event fired source ", buttonModel,
712: listener2.eventHappened.getSource());
713: assertEquals("event fired class ", ChangeEvent.class,
714: listener2.eventHappened.getClass());
715: buttonModel.setRollover(value2);
716: assertEquals("Rollover ", value2, buttonModel.isRollover());
717: assertEquals("event fired source ", buttonModel,
718: listener1.eventHappened.getSource());
719: assertEquals("event fired class ", ChangeEvent.class,
720: listener1.eventHappened.getClass());
721: assertEquals("event fired source ", buttonModel,
722: listener2.eventHappened.getSource());
723: assertEquals("event fired class ", ChangeEvent.class,
724: listener2.eventHappened.getClass());
725: listener1.eventHappened = null;
726: listener2.eventHappened = null;
727: buttonModel.setRollover(value2);
728: assertEquals("Rollover ", value2, buttonModel.isRollover());
729: assertNull("event wasn't fired ", listener1.eventHappened);
730: assertNull("event wasn't fired ", listener2.eventHappened);
731: buttonModel.setEnabled(true);
732: buttonModel.setRollover(false);
733: buttonModel.setEnabled(false);
734: buttonModel.setRollover(true);
735: assertFalse(buttonModel.isRollover());
736: }
737:
738: public void testSetPressed() {
739: boolean value1 = true;
740: boolean value2 = false;
741: ConcreteChangeListener listener1 = new ConcreteChangeListener();
742: ConcreteChangeListener listener2 = new ConcreteChangeListener();
743: ConcreteActionListener listener3 = new ConcreteActionListener();
744: buttonModel.addChangeListener(listener1);
745: buttonModel.addChangeListener(listener2);
746: buttonModel.addActionListener(listener3);
747: buttonModel.setPressed(value1);
748: assertEquals("Pressed ", value1, buttonModel.isPressed());
749: assertEquals("event fired source ", buttonModel,
750: listener1.eventHappened.getSource());
751: assertEquals("event fired class ", ChangeEvent.class,
752: listener1.eventHappened.getClass());
753: assertEquals("event fired source ", buttonModel,
754: listener2.eventHappened.getSource());
755: assertEquals("event fired class ", ChangeEvent.class,
756: listener2.eventHappened.getClass());
757: buttonModel.setPressed(value2);
758: assertEquals("Pressed ", value2, buttonModel.isPressed());
759: assertEquals("event fired source ", buttonModel,
760: listener1.eventHappened.getSource());
761: assertEquals("event fired class ", ChangeEvent.class,
762: listener1.eventHappened.getClass());
763: assertEquals("event fired source ", buttonModel,
764: listener2.eventHappened.getSource());
765: assertEquals("event fired class ", ChangeEvent.class,
766: listener2.eventHappened.getClass());
767: listener1.eventHappened = null;
768: listener2.eventHappened = null;
769: buttonModel.setPressed(value2);
770: assertEquals("Pressed ", value2, buttonModel.isPressed());
771: assertNull("event wasn't fired ", listener1.eventHappened);
772: assertNull("event wasn't fired ", listener2.eventHappened);
773: buttonModel.setArmed(true);
774: buttonModel.setPressed(true);
775: listener3.eventHappened = null;
776: buttonModel.setPressed(false);
777: assertNotNull(listener3.eventHappened);
778: buttonModel.setEnabled(true);
779: buttonModel.setPressed(false);
780: buttonModel.setEnabled(false);
781: buttonModel.setPressed(true);
782: assertFalse(buttonModel.isPressed());
783: }
784:
785: public void testSetEnabled1() {
786: boolean value1 = false;
787: boolean value2 = true;
788: ConcreteChangeListener listener1 = new ConcreteChangeListener();
789: ConcreteChangeListener listener2 = new ConcreteChangeListener();
790: buttonModel.addChangeListener(listener1);
791: buttonModel.addChangeListener(listener2);
792: buttonModel.setEnabled(value1);
793: assertEquals("Enabled ", value1, buttonModel.isEnabled());
794: assertEquals("event fired source ", buttonModel,
795: listener1.eventHappened.getSource());
796: assertEquals("event fired class ", ChangeEvent.class,
797: listener1.eventHappened.getClass());
798: assertEquals("event fired source ", buttonModel,
799: listener2.eventHappened.getSource());
800: assertEquals("event fired class ", ChangeEvent.class,
801: listener2.eventHappened.getClass());
802: buttonModel.setEnabled(value2);
803: assertEquals("Enabled ", value2, buttonModel.isEnabled());
804: assertEquals("event fired source ", buttonModel,
805: listener1.eventHappened.getSource());
806: assertEquals("event fired class ", ChangeEvent.class,
807: listener1.eventHappened.getClass());
808: assertEquals("event fired source ", buttonModel,
809: listener2.eventHappened.getSource());
810: assertEquals("event fired class ", ChangeEvent.class,
811: listener2.eventHappened.getClass());
812: listener1.eventHappened = null;
813: listener2.eventHappened = null;
814: buttonModel.setEnabled(value2);
815: assertEquals("Enabled ", value2, buttonModel.isEnabled());
816: assertNull("event wasn't fired ", listener1.eventHappened);
817: assertNull("event wasn't fired ", listener2.eventHappened);
818: buttonModel.setEnabled(true);
819: }
820:
821: public void testSetEnabled2() {
822: buttonModel.setEnabled(true);
823: buttonModel.setPressed(true);
824: buttonModel.setRollover(true);
825: buttonModel.setSelected(true);
826: buttonModel.setArmed(true);
827: buttonModel.setEnabled(false);
828: assertFalse(buttonModel.isArmed());
829: assertFalse(buttonModel.isPressed());
830: assertTrue(buttonModel.isSelected());
831: if (isHarmony()) {
832: assertFalse(buttonModel.isRollover());
833: } else {
834: assertTrue(buttonModel.isRollover());
835: }
836: }
837:
838: public void testSetArmed() {
839: boolean value1 = true;
840: boolean value2 = false;
841: ConcreteChangeListener listener1 = new ConcreteChangeListener();
842: ConcreteChangeListener listener2 = new ConcreteChangeListener();
843: buttonModel.addChangeListener(listener1);
844: buttonModel.addChangeListener(listener2);
845: buttonModel.setArmed(value1);
846: assertEquals("Armed ", value1, buttonModel.isArmed());
847: assertEquals("event fired source ", buttonModel,
848: listener1.eventHappened.getSource());
849: assertEquals("event fired class ", ChangeEvent.class,
850: listener1.eventHappened.getClass());
851: assertEquals("event fired source ", buttonModel,
852: listener2.eventHappened.getSource());
853: assertEquals("event fired class ", ChangeEvent.class,
854: listener2.eventHappened.getClass());
855: buttonModel.setArmed(value2);
856: assertEquals("Armed ", value2, buttonModel.isArmed());
857: assertEquals("event fired source ", buttonModel,
858: listener1.eventHappened.getSource());
859: assertEquals("event fired class ", ChangeEvent.class,
860: listener1.eventHappened.getClass());
861: assertEquals("event fired source ", buttonModel,
862: listener2.eventHappened.getSource());
863: assertEquals("event fired class ", ChangeEvent.class,
864: listener2.eventHappened.getClass());
865: listener1.eventHappened = null;
866: listener2.eventHappened = null;
867: buttonModel.setArmed(value2);
868: assertEquals("Armed ", value2, buttonModel.isArmed());
869: assertNull("event wasn't fired ", listener1.eventHappened);
870: assertNull("event wasn't fired ", listener2.eventHappened);
871: buttonModel.setEnabled(true);
872: buttonModel.setArmed(false);
873: buttonModel.setEnabled(false);
874: buttonModel.setArmed(true);
875: assertFalse(buttonModel.isArmed());
876: }
877:
878: public void testSetMnemonic() {
879: int value1 = 123;
880: int value2 = 234;
881: ConcreteChangeListener listener1 = new ConcreteChangeListener();
882: ConcreteChangeListener listener2 = new ConcreteChangeListener();
883: buttonModel.addChangeListener(listener1);
884: buttonModel.addChangeListener(listener2);
885: buttonModel.setMnemonic(value1);
886: assertEquals("mnemonic ", value1, buttonModel.getMnemonic());
887: assertEquals("event fired source ", buttonModel,
888: listener1.eventHappened.getSource());
889: assertEquals("event fired class ", ChangeEvent.class,
890: listener1.eventHappened.getClass());
891: assertEquals("event fired source ", buttonModel,
892: listener2.eventHappened.getSource());
893: assertEquals("event fired class ", ChangeEvent.class,
894: listener2.eventHappened.getClass());
895: buttonModel.setMnemonic(value2);
896: assertEquals("mnemonic ", value2, buttonModel.getMnemonic());
897: assertEquals("event fired source ", buttonModel,
898: listener1.eventHappened.getSource());
899: assertEquals("event fired class ", ChangeEvent.class,
900: listener1.eventHappened.getClass());
901: assertEquals("event fired source ", buttonModel,
902: listener2.eventHappened.getSource());
903: assertEquals("event fired class ", ChangeEvent.class,
904: listener2.eventHappened.getClass());
905: listener1.eventHappened = null;
906: listener2.eventHappened = null;
907: buttonModel.setMnemonic(value2);
908: assertEquals("mnemonic ", value2, buttonModel.getMnemonic());
909: if (!isHarmony()) {
910: assertEquals("event fired source ", buttonModel,
911: listener1.eventHappened.getSource());
912: assertEquals("event fired class ", ChangeEvent.class,
913: listener1.eventHappened.getClass());
914: assertEquals("event fired source ", buttonModel,
915: listener2.eventHappened.getSource());
916: assertEquals("event fired class ", ChangeEvent.class,
917: listener2.eventHappened.getClass());
918: } else {
919: assertNull("event hasn't fired", listener1.eventHappened);
920: assertNull("event hasn't fired", listener2.eventHappened);
921: }
922: }
923:
924: public void testIsSelected() {
925: assertFalse("default selected state ", buttonModel.isSelected());
926: }
927:
928: public void testIsRollover() {
929: assertFalse("default rollover state ", buttonModel.isRollover());
930: }
931:
932: public void testIsPressed() {
933: assertFalse("default pressed state ", buttonModel.isPressed());
934: }
935:
936: public void testIsEnabled() {
937: assertTrue("default enabled state ", buttonModel.isEnabled());
938: }
939:
940: public void testIsArmed() {
941: assertFalse("default armed state ", buttonModel.isArmed());
942: }
943:
944: public void testGetMnemonic() {
945: assertEquals("default mnemonic ", 0, buttonModel.getMnemonic());
946: }
947:
948: public void testWriteObject() throws Exception {
949: ByteArrayOutputStream fo = new ByteArrayOutputStream();
950: ObjectOutputStream so = new ObjectOutputStream(fo);
951: so.writeObject(buttonModel);
952: so.flush();
953: }
954:
955: public void testReadObject() throws Exception {
956: ByteArrayOutputStream fo = new ByteArrayOutputStream();
957: ObjectOutputStream so = new ObjectOutputStream(fo);
958: so.writeObject(buttonModel);
959: so.flush();
960: InputStream fi = new ByteArrayInputStream(fo.toByteArray());
961: ObjectInputStream si = new ObjectInputStream(fi);
962: DefaultButtonModel ressurectedButtonModel = (DefaultButtonModel) si
963: .readObject();
964: assertNotNull(ressurectedButtonModel);
965: }
966: }
|