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 30.09.2004
021:
022: */package javax.swing;
023:
024: import java.awt.event.KeyEvent;
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027: import java.io.IOException;
028: import java.io.ObjectInputStream;
029: import java.io.ObjectOutputStream;
030:
031: public class InputMapTest extends SwingTestCase {
032: protected InputMap map;
033:
034: protected InputMap parent;
035:
036: protected boolean find(final Object[] array, final Object value) {
037: boolean found = false;
038: if (array != null) {
039: for (int i = 0; i < array.length; i++) {
040: if (array[i].equals(value)) {
041: found = true;
042: break;
043: }
044: }
045: }
046: return found;
047: }
048:
049: /*
050: * @see TestCase#setUp()
051: */
052: @Override
053: protected void setUp() throws Exception {
054: super .setUp();
055: map = new InputMap();
056: parent = new InputMap();
057: }
058:
059: public void testPut() {
060: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(
061: KeyEvent.VK_PAGE_DOWN, 0);
062: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(
063: KeyEvent.VK_PAGE_UP, 0);
064: map.put(keyStroke1, "1");
065: map.put(keyStroke2, "2");
066: assertTrue(map.get(keyStroke1).equals("1"));
067: assertTrue(map.get(keyStroke2).equals("2"));
068: map.put(keyStroke2, "1");
069: map.put(keyStroke1, "2");
070: assertTrue(map.get(keyStroke1).equals("2"));
071: assertTrue(map.get(keyStroke2).equals("1"));
072: map.put(keyStroke1, null);
073: map.put(keyStroke2, null);
074: assertTrue(map.size() == 0);
075: }
076:
077: public void testGet() {
078: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(
079: KeyEvent.VK_PAGE_DOWN, 0);
080: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(
081: KeyEvent.VK_PAGE_UP, 0);
082: assertNull(map.get(keyStroke1));
083: assertNull(map.get(keyStroke2));
084: map.put(keyStroke1, "1");
085: map.put(keyStroke2, "1");
086: assertTrue(map.get(keyStroke1).equals("1"));
087: assertTrue(map.get(keyStroke2).equals("1"));
088: map.put(keyStroke2, "2");
089: assertTrue(map.get(keyStroke2).equals("2"));
090: map.put(keyStroke2, null);
091: assertNull(map.get(keyStroke2));
092: InputMap childMap = new InputMap();
093: childMap.setParent(map);
094: map.put(keyStroke1, "1");
095: KeyStroke keyStroke11 = KeyStroke
096: .getKeyStroke(KeyEvent.VK_1, 0);
097: childMap.put(keyStroke11, "1");
098: assertTrue(childMap.get(keyStroke11).equals("1"));
099: assertTrue(childMap.get(keyStroke1).equals("1"));
100: map.put(keyStroke2, "2");
101: assertTrue(childMap.get(keyStroke2).equals("2"));
102: childMap.put(keyStroke2, "1");
103: assertTrue(childMap.get(keyStroke2).equals("1"));
104: }
105:
106: public void testRemove() {
107: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(
108: KeyEvent.VK_PAGE_DOWN, 0);
109: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(
110: KeyEvent.VK_PAGE_UP, 0);
111: map.put(keyStroke1, "1");
112: map.put(keyStroke2, "2");
113: assertTrue(map.get(keyStroke1).equals("1"));
114: assertTrue(map.get(keyStroke2).equals("2"));
115: map.remove(keyStroke1);
116: assertNull(map.get(keyStroke1));
117: assertTrue(map.get(keyStroke2).equals("2"));
118: map.remove(keyStroke2);
119: assertNull(map.get(keyStroke2));
120: assertTrue(map.size() == 0);
121: }
122:
123: public void testKeys() {
124: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(
125: KeyEvent.VK_PAGE_DOWN, 0);
126: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(
127: KeyEvent.VK_PAGE_UP, 0);
128: KeyStroke keyStroke3 = KeyStroke.getKeyStroke(KeyEvent.VK_1, 0);
129: KeyStroke keyStroke4 = KeyStroke.getKeyStroke(KeyEvent.VK_0, 0);
130: Object[] keys = map.keys();
131: assertEquals(0, map.size());
132: if (isHarmony()) {
133: assertNotNull(keys);
134: assertEquals(0, keys.length);
135: } else {
136: assertNull(keys);
137: }
138: map.put(keyStroke1, "1");
139: map.put(keyStroke2, "2");
140: map.put(keyStroke3, "1");
141: map.put(keyStroke4, "2");
142: keys = map.keys();
143: assertTrue("array size's correct ", keys != null
144: && keys.length == 4);
145: assertTrue(find(keys, keyStroke1));
146: assertTrue(find(keys, keyStroke2));
147: assertTrue(find(keys, keyStroke3));
148: assertTrue(find(keys, keyStroke4));
149: map.put(keyStroke1, null);
150: map.put(keyStroke2, null);
151: map.put(keyStroke3, null);
152: map.put(keyStroke4, null);
153: assertEquals(0, map.size());
154: assertNotNull(keys);
155: }
156:
157: public void testAllKeys() {
158: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(KeyEvent.VK_1, 0);
159: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(KeyEvent.VK_2, 0);
160: KeyStroke keyStroke3 = KeyStroke.getKeyStroke(KeyEvent.VK_3, 0);
161: KeyStroke keyStroke4 = KeyStroke.getKeyStroke(KeyEvent.VK_4, 0);
162: KeyStroke keyStroke5 = KeyStroke.getKeyStroke(KeyEvent.VK_5, 0);
163: KeyStroke keyStroke6 = KeyStroke.getKeyStroke(KeyEvent.VK_6, 0);
164: Object[] keys = map.allKeys();
165: map.setParent(parent);
166: assertEquals(0, map.size());
167: if (isHarmony()) {
168: assertNotNull(keys);
169: assertEquals(0, keys.length);
170: } else {
171: assertNull(keys);
172: }
173: parent.put(keyStroke1, "1");
174: parent.put(keyStroke2, "2");
175: parent.put(keyStroke3, "1");
176: parent.put(keyStroke4, "2");
177: map.put(keyStroke3, "1");
178: map.put(keyStroke4, "2");
179: map.put(keyStroke5, "1");
180: map.put(keyStroke6, "2");
181: keys = map.allKeys();
182: assertTrue(find(keys, keyStroke1));
183: assertTrue(find(keys, keyStroke2));
184: assertTrue(find(keys, keyStroke3));
185: assertTrue(find(keys, keyStroke4));
186: assertTrue(find(keys, keyStroke5));
187: assertTrue(find(keys, keyStroke6));
188: }
189:
190: public void testSetGetParent() {
191: InputMap parent1 = new InputMap();
192: InputMap parent2 = new InputMap();
193: InputMap parent3 = null;
194: assertNull(map.getParent());
195: map.setParent(parent1);
196: assertTrue(map.getParent() == parent1);
197: map.setParent(parent3);
198: assertTrue(map.getParent() == parent3);
199: map.setParent(parent2);
200: assertTrue(map.getParent() == parent2);
201: }
202:
203: public void testClear() {
204: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(
205: KeyEvent.VK_PAGE_DOWN, 0);
206: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(
207: KeyEvent.VK_PAGE_UP, 0);
208: assertTrue(map.size() == 0);
209: map.put(keyStroke1, "1");
210: map.put(keyStroke2, "2");
211: assertTrue(map.size() == 2);
212: map.clear();
213: assertTrue(map.size() == 0);
214: assertTrue("keys", map.keys() != null);
215: assertTrue("keys", map.keys().length == 0);
216: map.put(keyStroke1, "1");
217: assertTrue(map.size() == 1);
218: }
219:
220: public void testSize() {
221: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(KeyEvent.VK_1, 0);
222: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(KeyEvent.VK_2, 0);
223: KeyStroke keyStroke3 = KeyStroke.getKeyStroke(KeyEvent.VK_3, 0);
224: KeyStroke keyStroke4 = KeyStroke.getKeyStroke(KeyEvent.VK_4, 0);
225: assertTrue(map.size() == 0);
226: map.put(keyStroke1, "1");
227: map.put(keyStroke2, "2");
228: assertTrue(map.size() == 2);
229: map.put(keyStroke3, "1");
230: map.put(keyStroke4, "2");
231: assertTrue(map.size() == 4);
232: map.put(keyStroke1, null);
233: map.put(keyStroke2, null);
234: assertTrue(map.size() == 2);
235: }
236:
237: public void testWriteObject() throws IOException {
238: ByteArrayOutputStream output = new ByteArrayOutputStream();
239: ObjectOutputStream so = new ObjectOutputStream(output);
240: so.writeObject(map);
241: so.flush();
242: assertTrue(output.size() > 0);
243: }
244:
245: public void testReadObject() throws Exception {
246: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(KeyEvent.VK_1, 0);
247: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(KeyEvent.VK_2, 0);
248: Object object1 = "object1";
249: Object object2 = "object2";
250: map.setParent(parent);
251: map.put(keyStroke1, object1);
252: map.put(keyStroke2, object2);
253: ByteArrayOutputStream output = new ByteArrayOutputStream();
254: ObjectOutputStream so = new ObjectOutputStream(output);
255: so.writeObject(map);
256: so.flush();
257: ObjectInputStream si = new ObjectInputStream(
258: new ByteArrayInputStream(output.toByteArray()));
259: InputMap ressurectedMap = (InputMap) si.readObject();
260: assertTrue(ressurectedMap.getParent() != null);
261: assertTrue(ressurectedMap.get(keyStroke1).equals(object1));
262: assertTrue(ressurectedMap.get(keyStroke2).equals(object2));
263: }
264: }
|