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 23.12.2004
021:
022: */package javax.swing;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.util.EventListener;
027:
028: public class TimerTest extends SwingTestCase {
029: Timer timer = null;
030:
031: static int counter = 0;
032:
033: class ConcreteActionListener implements ActionListener {
034: public String name;
035:
036: public ActionEvent action = null;
037:
038: ConcreteActionListener(final String name) {
039: this .name = name;
040: }
041:
042: public void actionPerformed(final ActionEvent action) {
043: this .action = action;
044: }
045: };
046:
047: protected boolean find(final Object[] array, final Object value) {
048: boolean found = false;
049: if (array != null) {
050: for (int i = 0; i < array.length; i++) {
051: if (array[i].equals(value)) {
052: found = true;
053: break;
054: }
055: }
056: }
057: return found;
058: }
059:
060: /*
061: * @see TestCase#setUp()
062: */
063: @Override
064: protected void setUp() throws Exception {
065: super .setUp();
066: timer = new Timer(100, new ActionListener() {
067: public void actionPerformed(final ActionEvent action) {
068: }
069: });
070: }
071:
072: public void testTimer() {
073: ActionListener listener1 = new ConcreteActionListener("1");
074: timer = new Timer(100, listener1);
075: ActionListener[] listeners = timer.getActionListeners();
076: assertTrue("Initially there's one listener ", listeners != null
077: && listeners.length == 1);
078: assertTrue("Initially there's one listener ",
079: listeners[0] == listener1);
080: assertTrue("repeats ", timer.isRepeats());
081: assertEquals("delay is ", 100, timer.getDelay());
082: assertEquals("Initial delay is ", 100, timer.getInitialDelay());
083: assertTrue("Coalesces ", timer.isCoalesce());
084: timer = new Timer(-100, listener1);
085: listeners = timer.getActionListeners();
086: assertTrue("Initially there's one listener ", listeners != null
087: && listeners.length == 1);
088: assertTrue("Initially there's one listener ",
089: listeners[0] == listener1);
090: assertTrue("repeats ", timer.isRepeats());
091: assertEquals("delay is ", -100, timer.getDelay());
092: assertEquals("Initial delay is ", -100, timer.getInitialDelay());
093: assertTrue("Coalesces ", timer.isCoalesce());
094: }
095:
096: public void testGetListeners() {
097: ActionListener listener1 = new ConcreteActionListener("1");
098: ActionListener listener2 = new ConcreteActionListener("2");
099: ActionListener listener3 = new ConcreteActionListener("3");
100: timer = new Timer(100, listener1);
101: timer.addActionListener(listener2);
102: timer.addActionListener(listener3);
103: EventListener[] listeners = timer
104: .getListeners(ActionListener.class);
105: assertTrue("Now there are 3 listeners ", listeners != null
106: && listeners.length == 3);
107: assertTrue("Listener 1 is found", find(listeners, listener1));
108: assertTrue("Listener 2 is found", find(listeners, listener2));
109: assertTrue("Listener 3 is found", find(listeners, listener3));
110: listeners = timer.getListeners(ConcreteActionListener.class);
111: assertTrue("No listeners found", listeners != null
112: && listeners.length == 0);
113: }
114:
115: public void testRemoveActionListener() {
116: ActionListener listener1 = new ConcreteActionListener("1");
117: ActionListener listener2 = new ConcreteActionListener("2");
118: ActionListener listener3 = new ConcreteActionListener("3");
119: ActionListener listener4 = new ConcreteActionListener("3");
120: timer = new Timer(100, listener1);
121: ActionListener[] listeners = timer.getActionListeners();
122: assertTrue("Initially there's one listener ", listeners != null
123: && listeners.length == 1);
124: assertTrue("Initially there's one listener ",
125: listeners[0] == listener1);
126: timer.removeActionListener(listener1);
127: listeners = timer.getActionListeners();
128: assertTrue("now there are no listeners ", listeners != null
129: && listeners.length == 0);
130: timer.addActionListener(listener2);
131: timer.addActionListener(listener3);
132: timer.addActionListener(listener4);
133: timer.addActionListener(listener1);
134: listeners = timer.getActionListeners();
135: assertTrue("Now there are 4 listeners ", listeners != null
136: && listeners.length == 4);
137: assertTrue("Listener 1 is found", find(listeners, listener1));
138: assertTrue("Listener 2 is found", find(listeners, listener2));
139: assertTrue("Listener 3 is found", find(listeners, listener3));
140: assertTrue("Listener 4 is found", find(listeners, listener4));
141: timer.removeActionListener(listener4);
142: listeners = timer.getActionListeners();
143: assertTrue("Now there are 3 listeners ", listeners != null
144: && listeners.length == 3);
145: assertTrue("Listener 1 is found", find(listeners, listener1));
146: assertTrue("Listener 2 is found", find(listeners, listener2));
147: assertTrue("Listener 3 is found", find(listeners, listener3));
148: timer.removeActionListener(listener2);
149: listeners = timer.getActionListeners();
150: assertTrue("Now there are 2 listeners ", listeners != null
151: && listeners.length == 2);
152: assertTrue("Listener 1 is found", find(listeners, listener1));
153: assertTrue("Listener 3 is found", find(listeners, listener3));
154: }
155:
156: public void testAddActionListener() {
157: ActionListener listener1 = new ConcreteActionListener("1");
158: ActionListener listener2 = new ConcreteActionListener("2");
159: ActionListener listener3 = new ConcreteActionListener("3");
160: ActionListener listener4 = new ConcreteActionListener("3");
161: timer = new Timer(100, listener1);
162: ActionListener[] listeners = timer.getActionListeners();
163: assertTrue("Initially there's only one listener ",
164: listeners != null && listeners.length == 1);
165: assertTrue("Initially there's only one listener ",
166: listeners[0] == listener1);
167: timer.addActionListener(listener2);
168: listeners = timer.getActionListeners();
169: assertTrue("Now there are 2 listeners ", listeners != null
170: && listeners.length == 2);
171: assertTrue("Listener 1 is found", find(listeners, listener1));
172: assertTrue("Listener 2 is found", find(listeners, listener2));
173: timer.addActionListener(listener3);
174: listeners = timer.getActionListeners();
175: assertTrue("Now there are 3 listeners ", listeners != null
176: && listeners.length == 3);
177: assertTrue("Listener 1 is found", find(listeners, listener1));
178: assertTrue("Listener 2 is found", find(listeners, listener2));
179: assertTrue("Listener 3 is found", find(listeners, listener3));
180: timer.addActionListener(listener4);
181: timer.addActionListener(null);
182: timer.addActionListener(null);
183: listeners = timer.getActionListeners();
184: assertTrue("Now there are 4 listeners ", listeners != null
185: && listeners.length == 4);
186: assertTrue("Listener 1 is found", find(listeners, listener1));
187: assertTrue("Listener 2 is found", find(listeners, listener2));
188: assertTrue("Listener 3 is found", find(listeners, listener3));
189: assertTrue("Listener 4 is found", find(listeners, listener4));
190: }
191:
192: public void testGetActionListeners() {
193: ActionListener listener1 = new ConcreteActionListener("1");
194: ActionListener listener2 = new ConcreteActionListener("2");
195: ActionListener listener3 = new ConcreteActionListener("3");
196: timer = new Timer(100, listener1);
197: ActionListener[] listeners = timer.getActionListeners();
198: assertTrue("Initially there's only one listener ",
199: listeners != null && listeners.length == 1);
200: assertSame("Initially there's only one listener ", listener1,
201: listeners[0]);
202: timer.addActionListener(listener2);
203: timer.addActionListener(listener3);
204: listeners = timer.getActionListeners();
205: assertTrue("Now there are 3 listeners ", listeners != null
206: && listeners.length == 3);
207: assertTrue("Listener 1 is found", find(listeners, listener1));
208: assertTrue("Listener 2 is found", find(listeners, listener2));
209: assertTrue("Listener 3 is found", find(listeners, listener3));
210: }
211:
212: public void testIsRepeats() {
213: ConcreteActionListener listener1 = new ConcreteActionListener(
214: "1");
215: timer = new Timer(10, listener1);
216: assertTrue("repeats ", timer.isRepeats());
217: timer.setRepeats(false);
218: assertFalse("doesn't repeat ", timer.isRepeats());
219: timer.setRepeats(true);
220: assertTrue("repeats ", timer.isRepeats());
221: }
222:
223: public void testSetCoalesce() {
224: ConcreteActionListener listener = new ConcreteActionListener(
225: "1");
226: timer = new Timer(10, listener);
227: timer.setCoalesce(true);
228: assertTrue("Coalesces ", timer.isCoalesce());
229: timer.setCoalesce(false);
230: assertFalse("doesn't Coalesce ", timer.isCoalesce());
231: timer.setCoalesce(true);
232: assertTrue("Coalesce ", timer.isCoalesce());
233: }
234:
235: public void testIsCoalesce() {
236: ConcreteActionListener listener1 = new ConcreteActionListener(
237: "1");
238: timer = new Timer(10, listener1);
239: assertTrue("Coalesces ", timer.isCoalesce());
240: timer.setCoalesce(false);
241: assertFalse("doesn't Coalesce ", timer.isCoalesce());
242: timer.setCoalesce(true);
243: assertTrue("Coalesce ", timer.isCoalesce());
244: }
245:
246: public void testGetInitialDelay() {
247: ConcreteActionListener listener1 = new ConcreteActionListener(
248: "1");
249: timer = new Timer(10, listener1);
250: assertEquals("Initial delay ", 10, timer.getInitialDelay());
251: timer.setInitialDelay(100);
252: assertEquals("Initial delay ", 100, timer.getInitialDelay());
253: timer.setInitialDelay(300);
254: assertEquals("Initial delay ", 300, timer.getInitialDelay());
255: boolean thrown = false;
256: try {
257: timer.setInitialDelay(-100);
258: } catch (IllegalArgumentException e) {
259: thrown = true;
260: }
261: assertTrue("exception has been thrown", thrown);
262: thrown = false;
263: try {
264: timer.setInitialDelay(0);
265: } catch (IllegalArgumentException e) {
266: thrown = true;
267: }
268: assertEquals("Initial delay ", 0, timer.getInitialDelay());
269: }
270:
271: public void testGetDelay() {
272: ConcreteActionListener listener1 = new ConcreteActionListener(
273: "1");
274: timer = new Timer(10, listener1);
275: assertEquals("delay ", 10, timer.getDelay());
276: timer.setDelay(100);
277: assertEquals("delay ", 100, timer.getDelay());
278: timer.setDelay(300);
279: assertEquals("delay ", 300, timer.getDelay());
280: boolean thrown = false;
281: try {
282: timer.setDelay(-100);
283: } catch (IllegalArgumentException e) {
284: thrown = true;
285: }
286: assertTrue("exception has been thrown", thrown);
287: thrown = false;
288: try {
289: timer.setDelay(0);
290: } catch (IllegalArgumentException e) {
291: thrown = true;
292: }
293: assertEquals("delay ", 0, timer.getDelay());
294: }
295:
296: public void testGetLogTimers() {
297: assertFalse("doesn't log timers ", Timer.getLogTimers());
298: Timer.setLogTimers(true);
299: assertTrue("logs timers ", Timer.getLogTimers());
300: Timer.setLogTimers(false);
301: assertFalse("doesn't log timers ", Timer.getLogTimers());
302: }
303: }
|