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.Graphics;
023: import java.awt.Rectangle;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: public class CellRendererPaneTest extends SwingTestCase {
028: private CellRendererPane pane;
029:
030: public CellRendererPaneTest(final String name) {
031: super (name);
032: }
033:
034: @Override
035: protected void setUp() throws Exception {
036: pane = new CellRendererPane();
037: }
038:
039: @Override
040: protected void tearDown() throws Exception {
041: pane = null;
042: }
043:
044: public void testCellRendererPane() throws Exception {
045: assertNull(pane.accessibleContext);
046: }
047:
048: public void testAddImpl() throws Exception {
049: JComponent c1 = new JPanel();
050: JComponent c2 = new JPanel();
051: JComponent c3 = new JPanel();
052: assertEquals(0, pane.getComponentCount());
053: pane.add(c1);
054: assertEquals(1, pane.getComponentCount());
055: assertSame(c1, pane.getComponent(0));
056: pane.add(c2);
057: assertEquals(2, pane.getComponentCount());
058: assertSame(c1, pane.getComponent(0));
059: assertSame(c2, pane.getComponent(1));
060: pane.add(c3);
061: assertEquals(3, pane.getComponentCount());
062: assertSame(c1, pane.getComponent(0));
063: assertSame(c2, pane.getComponent(1));
064: assertSame(c3, pane.getComponent(2));
065: pane.add(c1);
066: assertEquals(3, pane.getComponentCount());
067: assertSame(c1, pane.getComponent(0));
068: assertSame(c2, pane.getComponent(1));
069: assertSame(c3, pane.getComponent(2));
070: }
071:
072: public void testGetAccessibleContext() throws Exception {
073: assertNull(pane.accessibleContext);
074: assertNotNull(pane.getAccessibleContext());
075: assertEquals(pane.accessibleContext, pane
076: .getAccessibleContext());
077: }
078:
079: public void testInvalidate() throws Exception {
080: final Marker parentInvalidation = new Marker();
081: JPanel parent = new JPanel() {
082: private static final long serialVersionUID = 1L;
083:
084: @Override
085: public void invalidate() {
086: super .invalidate();
087: parentInvalidation.setOccurred();
088: }
089: };
090: final Marker childInvalidation = new Marker();
091: JPanel child = new JPanel() {
092: private static final long serialVersionUID = 1L;
093:
094: @Override
095: public void invalidate() {
096: super .invalidate();
097: childInvalidation.setOccurred();
098: }
099: };
100: parent.add(pane);
101: pane.add(child);
102: parent.validate();
103: parentInvalidation.setOccurred(false);
104: childInvalidation.setOccurred(false);
105: child.invalidate();
106: assertTrue(childInvalidation.isOccurred());
107: assertFalse(parentInvalidation.isOccurred());
108: }
109:
110: public void testPaintComponent() throws Exception {
111: final List<Rectangle> boundsChanges = new ArrayList<Rectangle>();
112: final Marker dbMarker = new Marker();
113: final Marker validationMarker = new Marker();
114: JPanel component = new JPanel() {
115: private static final long serialVersionUID = 1L;
116:
117: @Override
118: public void setBounds(final int x, final int y,
119: final int w, final int h) {
120: boundsChanges.add(new Rectangle(x, y, w, h));
121: super .setBounds(x, y, w, h);
122: }
123:
124: @Override
125: public void validate() {
126: validationMarker.setOccurred();
127: super .validate();
128: }
129:
130: @Override
131: public void paint(final Graphics g) {
132: super .paint(g);
133: dbMarker.setOccurred(isDoubleBuffered());
134: }
135: };
136: component.setDoubleBuffered(true);
137: component.invalidate();
138: Graphics g = createTestGraphics();
139: g.setClip(0, 0, 100, 100);
140: pane.paintComponent(g, component, new JPanel(), 20, 30, 40, 50);
141: assertEquals(pane, component.getParent());
142: assertFalse(dbMarker.isOccurred());
143: assertTrue(component.isDoubleBuffered());
144: assertEquals(2, boundsChanges.size());
145: assertEquals(new Rectangle(20, 30, 40, 50), boundsChanges
146: .get(0));
147: assertEquals(new Rectangle(-40, -50, 0, 0), boundsChanges
148: .get(1));
149: assertEquals(new Rectangle(-40, -50, 0, 0), component
150: .getBounds());
151: assertFalse(validationMarker.isOccurred());
152: pane.paintComponent(g, component, new JPanel(), 20, 30, 40, 50,
153: true);
154: assertTrue(validationMarker.isOccurred());
155: }
156:
157: public void testPaintComponent1() {
158: CellRendererPane c = new CellRendererPane();
159: try {
160: c.paintComponent(null, null, null, 0, 0, 0, 0, false);
161: assertTrue(true);
162: } catch (NullPointerException e) {
163: fail(e + " - was thrown");
164: }
165: }
166: }
|