001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: /**
021: * Tests keyboard events in the {@link DelegatingKeyboardListenerCollection}
022: * class.
023: */
024: public class DelegatingKeyboardListenerCollectionTest extends
025: GWTTestCase implements KeyboardListener {
026:
027: /**
028: * A {@link Widget} that uses the
029: * {@link DelegatingKeyboardListenerCollection} to save its list
030: * of keyboard events.
031: */
032: public class DelegatingWidget extends Widget {
033: // The delegating collection of keyboard listeners
034: private DelegatingKeyboardListenerCollection keyboardListeners;
035:
036: // The owner of all events
037: private Widget eventOwner;
038:
039: /**
040: * Adds a listener interface to receive keyboard events.
041: *
042: * @param listener the listener interface to add
043: */
044: public void addKeyboardListener(KeyboardListener listener) {
045: if (keyboardListeners == null) {
046: this .eventOwner = new Widget();
047: keyboardListeners = new DelegatingKeyboardListenerCollection(
048: this .eventOwner, new TextBox());
049: }
050: keyboardListeners.add(listener);
051: }
052:
053: /**
054: * Get the event owner.
055: *
056: * @return the event owner
057: */
058: public Widget getEventOwner() {
059: return this .eventOwner;
060: }
061:
062: /**
063: * Get the keyboardListener.
064: *
065: * @return the keyboardListener
066: */
067: public DelegatingKeyboardListenerCollection getKeyboardListeners() {
068: return this .keyboardListeners;
069: }
070:
071: /**
072: * Removes a previously added listener interface.
073: *
074: * @param listener the listener interface to remove
075: */
076: public void removeKeyboardListener(KeyboardListener listener) {
077: /*
078: * This method is not used in the test or in the
079: * DelegatingKeyboardListenerCollection constructor
080: */
081: }
082: }
083:
084: // The owner of the events
085: private DelegatingWidget delegatingWidget;
086:
087: // A bit used to verify that some event handler was called
088: private boolean eventHandled;
089:
090: // The name of the last event fired
091: private String lastEventName;
092:
093: public String getModuleName() {
094: return "com.google.gwt.user.User";
095: }
096:
097: /**
098: * Handle the key down event from the ownerOfEvents.
099: *
100: * @param sender the widget sending the event.
101: * @param keyCode the key to send with the event.
102: * @param modifiers the modifier keys pressed at when the event occurred. This
103: * value is a combination of the bits defined by
104: * {@link KeyboardListener#MODIFIER_SHIFT},
105: * {@link KeyboardListener#MODIFIER_CTRL}, and
106: * {@link KeyboardListener#MODIFIER_ALT}.
107: */
108: public void onKeyDown(Widget sender, char keyCode, int modifiers) {
109: this .handleKeyEvent(sender, "onKeyDown");
110: }
111:
112: /**
113: * Handle the key press event from the ownerOfEvents.
114: *
115: * @param sender the widget sending the event.
116: * @param keyCode the key to send with the event.
117: * @param modifiers the modifier keys pressed at when the event occurred. This
118: * value is a combination of the bits defined by
119: * {@link KeyboardListener#MODIFIER_SHIFT},
120: * {@link KeyboardListener#MODIFIER_CTRL}, and
121: * {@link KeyboardListener#MODIFIER_ALT}.
122: */
123: public void onKeyPress(Widget sender, char keyCode, int modifiers) {
124: this .handleKeyEvent(sender, "onKeyPress");
125: }
126:
127: /**
128: * Handle the key up event from the ownerOfEvents.
129: *
130: * @param sender the widget sending the event.
131: * @param keyCode the key to send with the event.
132: * @param modifiers the modifier keys pressed at when the event occurred. This
133: * value is a combination of the bits defined by
134: * {@link KeyboardListener#MODIFIER_SHIFT},
135: * {@link KeyboardListener#MODIFIER_CTRL}, and
136: * {@link KeyboardListener#MODIFIER_ALT}.
137: */
138: public void onKeyUp(Widget sender, char keyCode, int modifiers) {
139: this .handleKeyEvent(sender, "onKeyUp");
140: }
141:
142: /**
143: * Tests that the key event handlers re-fire the correct key events to
144: * the correct owner. The owner of the events is a {@link TextBox}, which
145: * implements {@link SourcesKeyboardEvents} and allows us to check that
146: * the owner is correctly re-firing the events.
147: */
148: public void testKeyEvents() {
149: // Create a keyboard event listener with a DeletagingWidgetknown owner
150: this .delegatingWidget = new DelegatingWidget();
151: this .delegatingWidget.addKeyboardListener(this );
152:
153: // Fire events from through delegate, which should set the correct owner
154: this .fireKeyEvent("onKeyDown");
155: this .fireKeyEvent("onKeyUp");
156: this .fireKeyEvent("onKeyPress");
157: }
158:
159: /**
160: * This helper method simulates the firing of an event by the delegating widget,
161: * with a generic Widget as the source.
162: *
163: * @param eventName the name of the event to fire
164: */
165: private void fireKeyEvent(String eventName) {
166: this .lastEventName = eventName; // Set the name of this event
167: this .eventHandled = false; // Mark that we haven't handled it yet
168:
169: // Fire the actual event through the delegate
170: if (eventName.compareTo("onKeyDown") == 0) {
171: this .delegatingWidget.getKeyboardListeners().onKeyDown(
172: new Widget(), 'a', 0);
173: } else if (eventName.compareTo("onKeyUp") == 0) {
174: this .delegatingWidget.getKeyboardListeners().onKeyUp(
175: new Widget(), 'a', 0);
176: } else if (eventName.compareTo("onKeyPress") == 0) {
177: this .delegatingWidget.getKeyboardListeners().onKeyPress(
178: new Widget(), 'a', 0);
179: } else {
180: fail("The event " + eventName + " is not supported");
181: }
182:
183: // Verify that the event was handled
184: assertTrue(this .eventHandled);
185: }
186:
187: /**
188: * Handle an event from the ownerOfEvents by verifying the the
189: * event the the source are correct.
190: *
191: * @param sender the Widget that fired the event
192: * @param eventName the name of the event
193: */
194: private void handleKeyEvent(Widget sender, String eventName) {
195: assertEquals(this .delegatingWidget.getEventOwner(), sender);
196: assertEquals(this .lastEventName, eventName);
197: this .eventHandled = true;
198: }
199: }
|