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 Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.event.KeyListener;
023: import javax.swing.event.ChangeEvent;
024: import javax.swing.event.ChangeListener;
025:
026: public class DefaultSingleSelectionModelTest extends SwingTestCase {
027: private DefaultSingleSelectionModel model;
028:
029: private TestChangeListener listener;
030:
031: public DefaultSingleSelectionModelTest(final String name) {
032: super (name);
033: }
034:
035: @Override
036: protected void setUp() throws Exception {
037: model = new DefaultSingleSelectionModel();
038: listener = new TestChangeListener();
039: }
040:
041: @Override
042: protected void tearDown() throws Exception {
043: model = null;
044: }
045:
046: public void testAddRemoveGetChangeListener() throws Exception {
047: assertEquals(0, model.getChangeListeners().length);
048: model.addChangeListener(new TestChangeListener());
049: model.addChangeListener(listener);
050: model.addChangeListener(new TestChangeListener());
051: assertEquals(3, model.getChangeListeners().length);
052: model.removeChangeListener(listener);
053: assertEquals(2, model.getChangeListeners().length);
054: }
055:
056: public void testGetSetIsClearSelection() throws Exception {
057: assertEquals(-1, model.getSelectedIndex());
058: assertFalse(model.isSelected());
059: TestChangeListener listener = new TestChangeListener();
060: model.addChangeListener(listener);
061: model.setSelectedIndex(4);
062: assertEquals(4, model.getSelectedIndex());
063: assertTrue(model.isSelected());
064: assertNotNull(listener.getChangeEvent());
065: listener.reset();
066: model.clearSelection();
067: assertEquals(-1, model.getSelectedIndex());
068: assertFalse(model.isSelected());
069: assertNotNull(listener.getChangeEvent());
070: }
071:
072: public void testGetListeners() throws Exception {
073: assertEquals(0, model.getListeners(ChangeListener.class).length);
074: assertEquals(0, model.getListeners(KeyListener.class).length);
075: model.addChangeListener(listener);
076: assertEquals(1, model.getListeners(ChangeListener.class).length);
077: assertEquals(0, model.getListeners(KeyListener.class).length);
078: }
079:
080: public void testFireStateChanged() throws Exception {
081: TestChangeListener listener2 = new TestChangeListener();
082: model.addChangeListener(listener);
083: model.addChangeListener(listener2);
084: assertNull(model.changeEvent);
085: model.fireStateChanged();
086: assertNotNull(model.changeEvent);
087: assertEquals(model, model.changeEvent.getSource());
088: assertEquals(model.changeEvent, listener.getChangeEvent());
089: assertEquals(model.changeEvent, listener2.getChangeEvent());
090: }
091:
092: private class TestChangeListener implements ChangeListener {
093: private ChangeEvent changeEvent;
094:
095: public void stateChanged(final ChangeEvent e) {
096: changeEvent = e;
097: }
098:
099: public ChangeEvent getChangeEvent() {
100: return changeEvent;
101: }
102:
103: public void reset() {
104: changeEvent = null;
105: }
106: }
107: }
|