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 Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Canvas;
023: import java.awt.Component;
024: import java.util.Hashtable;
025: import javax.accessibility.AccessibleContext;
026: import javax.accessibility.AccessibleRole;
027:
028: public class JLayeredPaneTest extends SwingTestCase {
029: /*
030: * components to add to the container
031: * they are supposed to have next order:
032: * layer 5 | 4 | 1
033: * --------------------------------------
034: * index | 0 1 2 | 3 4 | 5
035: * comp |5_0 5_1 5_2 | 4_0 4_1 | 1_0
036: * --------------------------------------
037: */
038: private static Component c5_0;
039:
040: private static Component c5_1;
041:
042: private static Component c5_2;
043:
044: private static Component c4_0;
045:
046: private static Component c4_1;
047:
048: private static Component c1_0;
049:
050: private static int i4_0 = 3;
051:
052: /*
053: * This class overload protected methods with public methods
054: */
055: private class TestLayeredPane extends JLayeredPane {
056: private static final long serialVersionUID = 1L;
057:
058: @SuppressWarnings("unchecked")
059: public Hashtable getGetComponentToLayer() {
060: return getComponentToLayer();
061: }
062:
063: @Override
064: public Integer getObjectForLayer(final int layer) {
065: return super .getObjectForLayer(layer);
066: }
067:
068: @Override
069: public int insertIndexForLayer(final int layer,
070: final int position) {
071: return super .insertIndexForLayer(layer, position);
072: }
073:
074: @Override
075: public String paramString() {
076: return super .paramString();
077: }
078: }
079:
080: private JLayeredPane layered;
081: static {
082: c5_0 = new JPanel();
083: c5_1 = new JPanel();
084: c5_2 = new JPanel();
085: c4_0 = new JPanel();
086: c4_1 = new JPanel();
087: c1_0 = new JPanel();
088: }
089:
090: /*
091: * add some components to layered
092: */
093: private void addComponents() {
094: layered.add(c5_0, new Integer(5), -1);
095: layered.add(c5_1, new Integer(5), -1);
096: layered.add(c5_2, new Integer(5), -1);
097: layered.add(c4_0, new Integer(4), -1);
098: layered.add(c4_1, new Integer(4), -1);
099: layered.add(c1_0, new Integer(1), -1);
100: }
101:
102: /*
103: * @see TestCase#setUp()
104: */
105: @Override
106: protected void setUp() throws Exception {
107: super .setUp();
108: layered = new JLayeredPane();
109: }
110:
111: /*
112: * @see TestCase#tearDown()
113: */
114: @Override
115: protected void tearDown() throws Exception {
116: super .tearDown();
117: }
118:
119: /**
120: * Constructor for JLayeredPaneTest.
121: * @param arg0
122: */
123: public JLayeredPaneTest(final String arg0) {
124: super (arg0);
125: }
126:
127: /*
128: * Class under test for void remove(int)
129: */
130: public void testRemoveint() {
131: addComponents();
132: layered.remove(i4_0);
133: assertFalse(layered.isAncestorOf(c4_0));
134: assertEquals(5, layered.getComponentCount());
135: // remove() with incorrect index throws ArrayIndexOutOfBoundsException
136: }
137:
138: /*
139: * Class under test for void removeAll()
140: */
141: public void testRemoveAll() {
142: addComponents();
143: Component comp = new Canvas();
144: layered.setLayer(comp, 5);
145: // comp is not in JLayeredPane
146: assertTrue("not empty", layered.getComponentCount() != 0);
147: assertFalse("componentToLayer is not empty", layered
148: .getComponentToLayer().isEmpty());
149: layered.removeAll();
150: assertTrue("empty", layered.getComponentCount() == 0);
151: assertFalse("componentToLayer is not empty", layered
152: .getComponentToLayer().isEmpty());
153: // comp is in JLayeredPane
154: layered.add(comp);
155: layered.removeAll();
156: assertTrue("componentToLayer is empty", layered
157: .getComponentToLayer().isEmpty());
158: }
159:
160: public void testIsOptimizedDrawingEnabled() {
161: // no components
162: assertTrue(layered.isOptimizedDrawingEnabled());
163: // 1 component
164: layered.add(new JPanel());
165: assertTrue(layered.isOptimizedDrawingEnabled());
166: // many components
167: addComponents();
168: assertFalse(layered.isOptimizedDrawingEnabled());
169: }
170:
171: public void testHighestLayer() {
172: assertEquals(0, layered.highestLayer());
173: addComponents();
174: assertEquals(5, layered.highestLayer());
175: }
176:
177: public void testLowestLayer() {
178: assertEquals(0, layered.lowestLayer());
179: addComponents();
180: assertEquals(1, layered.lowestLayer());
181: }
182:
183: public void testJLayeredPane() {
184: // nothing to test
185: }
186:
187: public void testGetComponentCountInLayer() {
188: assertEquals(0, layered.getComponentCountInLayer(5));
189: addComponents();
190: assertEquals(3, layered.getComponentCountInLayer(5));
191: assertEquals(2, layered.getComponentCountInLayer(4));
192: assertEquals(1, layered.getComponentCountInLayer(1));
193: assertEquals(0, layered.getComponentCountInLayer(6));
194: assertEquals(0, layered.getComponentCountInLayer(2));
195: }
196:
197: /*
198: * Class under test for AccessibleContext getAccessibleContext()
199: */
200: public void testGetAccessibleContext() {
201: AccessibleContext c = layered.getAccessibleContext();
202: assertTrue("instanceof AccessibleJLayeredPane",
203: c instanceof JLayeredPane.AccessibleJLayeredPane);
204: assertTrue("AccessibleRole is ok",
205: c.getAccessibleRole() == AccessibleRole.LAYERED_PANE);
206: assertNull("AccessibleName is ok", c.getAccessibleName());
207: assertNull("AccessibleDescription is ok", c
208: .getAccessibleDescription());
209: assertTrue("AccessibleChildrenCount == 0", c
210: .getAccessibleChildrenCount() == 0);
211: }
212:
213: /*
214: * Class under test for void paint(Graphics)
215: */
216: public void testPaint() {
217: // Note: cannot test paint code here
218: }
219:
220: /*
221: * Class under test for void setLayer(Component, int, int)
222: */
223: public void testSetLayerComponentintint() {
224: Component jcomp = new JPanel();
225: Component comp = new Canvas();
226: // test setLayer() for JComponent outside the container
227: layered.setLayer(jcomp, 4, 0);
228: assertEquals(4, layered.getLayer(jcomp));
229: assertEquals(4, JLayeredPane.getLayer((JComponent) jcomp));
230: assertFalse(layered.isAncestorOf(jcomp));
231: // test setLayer() for Component outside the container
232: layered.setLayer(comp, 4, 5);
233: assertEquals(4, layered.getLayer(comp));
234: assertFalse(layered.isAncestorOf(comp));
235: addComponents();
236: // to the start of existing layer
237: layered.setLayer(c5_2, 1, 0);
238: assertEquals(1, layered.getLayer(c5_2));
239: assertEquals(4, layered.getIndexOf(c5_2));
240: // in the middle of existing layer
241: layered.setLayer(c5_2, 5, 1);
242: assertEquals(5, layered.getLayer(c5_2));
243: assertEquals(1, layered.getIndexOf(c5_2));
244: // to the same position
245: layered.setLayer(c1_0, 1, 1);
246: assertEquals(1, layered.getLayer(c1_0));
247: assertEquals(5, layered.getIndexOf(c1_0));
248: // to the same index with different layer
249: layered.removeAll();
250: layered.add(c1_0);
251: layered.setLayer(c1_0, 5, 0);
252: assertEquals(5, layered.getLayer(c1_0));
253: }
254:
255: /*
256: * Class under test for void setPosition(Component, int)
257: */
258: public void testSetPositionComponentint() {
259: Component comp = new JPanel();
260: layered.setPosition(comp, 1);
261: assertEquals(0, layered.getComponentCount());
262: addComponents();
263: layered.setPosition(c4_0, -1);
264: assertEquals(4, layered.getIndexOf(c4_0));
265: layered.setPosition(c4_0, 0);
266: assertEquals(3, layered.getIndexOf(c4_0));
267: layered.setPosition(c1_0, 0);
268: assertEquals(5, layered.getIndexOf(c1_0));
269: layered.setPosition(c1_0, -1);
270: assertEquals(5, layered.getIndexOf(c1_0));
271: layered.setPosition(c5_2, 100);
272: assertEquals(2, layered.getIndexOf(c5_2));
273: layered.setPosition(c5_0, 100);
274: assertEquals(2, layered.getIndexOf(c5_0));
275: layered.setPosition(c5_0, 1);
276: assertEquals(1, layered.getIndexOf(c5_0));
277: }
278:
279: /*
280: * Class under test for void setLayer(Component, int)
281: */
282: public void testSetLayerComponentint() {
283: Component jcomp = new JPanel();
284: Component comp = new Canvas();
285: // test setLayer() for JComponent outside the container
286: layered.setLayer(jcomp, 4);
287: assertEquals(4, layered.getLayer(jcomp));
288: assertEquals(4, JLayeredPane.getLayer((JComponent) jcomp));
289: assertFalse(layered.isAncestorOf(jcomp));
290: // test setLayer() for Component outside the container
291: layered.setLayer(comp, 4);
292: assertEquals(4, layered.getLayer(comp));
293: assertFalse(layered.isAncestorOf(comp));
294: addComponents();
295: layered.setLayer(c4_0, 3);
296: assertEquals(4, layered.getIndexOf(c4_0));
297: assertEquals(3, layered.getLayer(c4_0));
298: layered.setLayer(c5_1, 1);
299: assertEquals(5, layered.getIndexOf(c5_1));
300: assertEquals(1, layered.getLayer(c5_1));
301: }
302:
303: /*
304: * Class under test for void moveToFront(Component)
305: */
306: public void testMoveToFrontComponent() {
307: addComponents();
308: layered.moveToFront(c4_1);
309: assertEquals(3, layered.getIndexOf(c4_1));
310: layered.moveToFront(c5_2);
311: assertEquals(0, layered.getIndexOf(c5_2));
312: layered.moveToFront(c1_0);
313: assertEquals(5, layered.getIndexOf(c1_0));
314: // moveToFront() does nothing if the component is not from the containter
315: layered.moveToFront(new JPanel());
316: assertEquals(6, layered.getComponentCount());
317: }
318:
319: /*
320: * Class under test for void moveToBack(Component)
321: */
322: public void testMoveToBackComponent() {
323: addComponents();
324: layered.moveToBack(c4_0);
325: assertEquals(4, layered.getIndexOf(c4_0));
326: layered.moveToBack(c5_0);
327: assertEquals(2, layered.getIndexOf(c5_0));
328: layered.moveToBack(c1_0);
329: assertEquals(5, layered.getIndexOf(c1_0));
330: // moveToBack() does nothing if the component is not from the containter
331: layered.moveToBack(new JPanel());
332: assertEquals(6, layered.getComponentCount());
333: try { // Regression test for HARMONY-2279
334: layered.moveToBack(null);
335: fail("NullPointerException should have been thrown");
336: } catch (NullPointerException e) {
337: // Expected
338: }
339: }
340:
341: /*
342: * Class under test for int getPosition(Component)
343: */
344: public void testGetPositionComponent() {
345: addComponents();
346: assertEquals(0, layered.getPosition(c5_0));
347: assertEquals(1, layered.getPosition(c5_1));
348: assertEquals(2, layered.getPosition(c5_2));
349: assertEquals(0, layered.getPosition(c4_0));
350: assertEquals(1, layered.getPosition(c4_1));
351: assertEquals(0, layered.getPosition(c1_0));
352: assertEquals(-1, layered.getPosition(new JPanel()));
353: }
354:
355: /*
356: * Class under test for int getLayer(Component)
357: */
358: public void testGetLayerComponent() {
359: // getLayer() for JComponent that doesn't exist in the container
360: Component c = new JPanel();
361: assertEquals(0, layered.getLayer(c));
362: // getLayer() for JComponent that doesn't exist in the container
363: // but has layer in the property
364: JLayeredPane.putLayer((JComponent) c, 5);
365: assertEquals(5, layered.getLayer(c));
366: // getLayer() for Component that doesn't exist in the container
367: c = new Canvas();
368: assertEquals(0, layered.getLayer(c));
369: // getLayer() for Component from the containter
370: layered.add(c, new Integer(3));
371: assertEquals(3, layered.getLayer(c));
372: // getLayer() for Component removed from the containter
373: layered.remove(c);
374: assertEquals(0, layered.getLayer(c));
375: // layered.getLayer((Component)null) - throws NPE
376: }
377:
378: /*
379: * Class under test for int getIndexOf(Component)
380: */
381: public void testGetIndexOfComponent() {
382: Component comp = new JPanel();
383: assertEquals(-1, layered.getIndexOf(comp));
384: addComponents();
385: assertEquals(-1, layered.getIndexOf(comp));
386: assertEquals(0, layered.getIndexOf(c5_0));
387: assertEquals(1, layered.getIndexOf(c5_1));
388: assertEquals(2, layered.getIndexOf(c5_2));
389: assertEquals(3, layered.getIndexOf(c4_0));
390: assertEquals(4, layered.getIndexOf(c4_1));
391: assertEquals(5, layered.getIndexOf(c1_0));
392: }
393:
394: private boolean belongs(final Component c, final Component[] array) {
395: for (int i = 0; i < array.length; i++) {
396: if (array[i] == c) {
397: return true;
398: }
399: }
400: return false;
401: }
402:
403: /*
404: * Class under test for Component[] getComponentsInLayer(int)
405: */
406: public void testGetComponentsInLayerint() {
407: assertEquals(0, layered.getComponentsInLayer(1).length);
408: addComponents();
409: Component[] components;
410: components = layered.getComponentsInLayer(5);
411: assertEquals(3, components.length);
412: assertTrue(belongs(c5_0, components));
413: assertTrue(belongs(c5_1, components));
414: assertTrue(belongs(c5_2, components));
415: components = layered.getComponentsInLayer(4);
416: assertEquals(2, components.length);
417: assertTrue(belongs(c4_0, components));
418: assertTrue(belongs(c4_1, components));
419: components = layered.getComponentsInLayer(1);
420: assertEquals(1, components.length);
421: assertTrue(belongs(c1_0, components));
422: assertEquals(0, layered.getComponentsInLayer(2).length);
423: }
424:
425: /*
426: * Class under test for javax.swing.JLayeredPane getLayeredPaneAbove(Component)
427: */
428: public void testGetLayeredPaneAboveComponent() {
429: // can't use c5_0 and others because they have
430: // previous JLayeredPane as their parent
431: assertNull(JLayeredPane.getLayeredPaneAbove(new JPanel()));
432: assertNull(JLayeredPane.getLayeredPaneAbove(null));
433: addComponents();
434: assertTrue(JLayeredPane.getLayeredPaneAbove(c5_0) == layered);
435: }
436:
437: /*
438: * Class under test for int getLayer(JComponent)
439: */
440: public void testPutGetLayerJComponent() {
441: JComponent c = new JPanel();
442: assertEquals(0, JLayeredPane.getLayer(c));
443: JLayeredPane.putLayer(c, 5);
444: assertEquals(5, JLayeredPane.getLayer(c));
445: }
446:
447: /*
448: * Class under test for int addImpl(Component comp, Object constraints, int index)
449: */
450: public void testAddImpl() {
451: JComponent comp = new JPanel();
452: addComponents();
453: // test add() with string instead of layer
454: // should be added to layer 0
455: JComponent comp2 = new JPanel();
456: layered.add(comp2, new Integer(0));
457: layered.add(comp, "str", 0);
458: assertEquals(0, JLayeredPane.getLayer(comp));
459: assertEquals(6, layered.getIndexOf(comp));
460: layered.remove(comp);
461: layered.add(comp, "str", -1);
462: assertEquals(0, JLayeredPane.getLayer(comp));
463: assertEquals(7, layered.getIndexOf(comp));
464: layered.remove(comp);
465: layered.remove(comp2);
466: // test if add() sets "layer" property of the added component
467: layered.add(comp, new Integer(4));
468: assertEquals(4, JLayeredPane.getLayer(comp));
469: assertEquals(5, layered.getIndexOf(comp));
470: layered.remove(comp);
471: // test add() to the bottom of the layer
472: layered.add(comp, new Integer(4), -1);
473: assertEquals(5, layered.getIndexOf(comp));
474: layered.remove(comp);
475: // test add() on the top of the layer
476: layered.add(comp, new Integer(4), 0);
477: assertEquals(3, layered.getIndexOf(comp));
478: layered.remove(comp);
479: // test add() to the inner of the layer
480: layered.add(comp, new Integer(4), 1);
481: assertEquals(4, layered.getIndexOf(comp));
482: layered.remove(comp);
483: // test add() to the invalid position of the layer
484: layered.add(comp, new Integer(4), 100);
485: assertEquals(5, layered.getIndexOf(comp));
486: layered.remove(comp);
487: // add component that already in container: the component must be removed first
488: layered.add(comp, new Integer(4), 0);
489: layered.add(comp, new Integer(3), 0);
490: assertEquals(7, layered.getComponentCount());
491: assertEquals(5, layered.getIndexOf(comp));
492: layered.remove(comp);
493: // test add() with layer set in JComponent
494: JLayeredPane.putLayer(comp, 5);
495: layered.add(comp);
496: assertEquals(3, layered.getIndexOf(comp));
497: }
498:
499: /*
500: * Class under test for Hashtable getComponentToLayer()
501: */
502: public void testGetComponentToLayer() {
503: TestLayeredPane pane = new TestLayeredPane();
504: assertTrue(pane.getGetComponentToLayer() != null);
505: }
506:
507: /*
508: * Class under test for Integer getObjectForLayer(int layer)
509: */
510: public void testGetObjectForLayer() {
511: TestLayeredPane pane = new TestLayeredPane();
512: assertEquals(5, pane.getObjectForLayer(5).intValue());
513: }
514:
515: /*
516: * Class under test for int insertIndexForLayer(int layer, int position)
517: */
518: public void testInsertIndexForLayer() {
519: // it is tested in testAddImpl()
520: }
521:
522: /*
523: * Class under test for String paramString()
524: */
525: public void testParamString() {
526: TestLayeredPane pane = new TestLayeredPane();
527: assertTrue(pane.paramString() != null);
528: }
529: }
|