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 Sergey Burlak
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Color;
024: import java.beans.PropertyChangeEvent;
025: import javax.swing.colorchooser.AbstractColorChooserPanel;
026: import javax.swing.colorchooser.ColorChooserComponentFactory;
027: import javax.swing.colorchooser.ColorSelectionModel;
028: import javax.swing.colorchooser.DefaultColorSelectionModel;
029:
030: public class JColorChooserTest extends BasicSwingTestCase {
031: private JColorChooser ch;
032:
033: @Override
034: public void setUp() {
035: ch = new JColorChooser();
036: propertyChangeController = new PropertyChangeController();
037: propertyChangeController.setVerbose(false);
038: ch.addPropertyChangeListener(propertyChangeController);
039: }
040:
041: @Override
042: protected void tearDown() throws Exception {
043: ch = null;
044: }
045:
046: public void testJColorChooser() throws Exception {
047: assertSame(DefaultColorSelectionModel.class, ch
048: .getSelectionModel().getClass());
049: assertNull(ch.accessibleContext);
050: assertEquals(Color.WHITE, ch.getColor());
051: ch = new JColorChooser(Color.BLACK);
052: assertEquals(Color.BLACK, ch.getColor());
053: ColorSelectionModel model = new DefaultColorSelectionModel();
054: ch = new JColorChooser(model);
055: assertFalse(propertyChangeController.isChanged());
056: assertSame(model, ch.getSelectionModel());
057: assertSame(BorderLayout.class, ch.getLayout().getClass());
058: }
059:
060: public void testGetAccessibleContext() throws Exception {
061: assertNull(ch.accessibleContext);
062: assertNotNull(ch.getAccessibleContext());
063: assertNotNull(ch.accessibleContext);
064: assertSame(ch.getAccessibleContext(), ch.getAccessibleContext());
065: assertSame(ch.accessibleContext, ch.getAccessibleContext());
066: assertSame(JColorChooser.AccessibleJColorChooser.class,
067: ch.accessibleContext.getClass());
068: }
069:
070: public void testSetGetSelectionModel() throws Exception {
071: assertNotNull(ch.getSelectionModel());
072: ColorSelectionModel oldModel = ch.getSelectionModel();
073: ColorSelectionModel model = new DefaultColorSelectionModel();
074: ch.setSelectionModel(model);
075: assertTrue(propertyChangeController
076: .isChanged(JColorChooser.SELECTION_MODEL_PROPERTY));
077: assertSame(model,
078: ((PropertyChangeEvent) propertyChangeController
079: .getLastEvent()).getNewValue());
080: assertSame(oldModel,
081: ((PropertyChangeEvent) propertyChangeController
082: .getLastEvent()).getOldValue());
083: ch.setSelectionModel(null);
084: assertNull(ch.getSelectionModel());
085: }
086:
087: public void testSetGetColor() throws Exception {
088: ChangeController changeController = new ChangeController();
089: ch.getSelectionModel().addChangeListener(changeController);
090: assertEquals(Color.WHITE, ch.getColor());
091: ch.setColor(Color.RED);
092: assertFalse(propertyChangeController.isChanged());
093: assertEquals(Color.RED, ch.getColor());
094: assertTrue(changeController.isChanged());
095: changeController.reset();
096: ch.setColor(0, 255, 0);
097: assertFalse(propertyChangeController.isChanged());
098: assertEquals(Color.GREEN, ch.getColor());
099: assertTrue(changeController.isChanged());
100: changeController.reset();
101: ch.setColor(255);
102: assertFalse(propertyChangeController.isChanged());
103: assertEquals(Color.BLUE, ch.getColor());
104: assertTrue(changeController.isChanged());
105: }
106:
107: public void testGetUIClassID() throws Exception {
108: assertEquals("ColorChooserUI", ch.getUIClassID());
109: }
110:
111: public void testSetGetDragEnabled() throws Exception {
112: assertFalse(ch.getDragEnabled());
113: ch.setDragEnabled(true);
114: assertFalse(propertyChangeController.isChanged());
115: assertTrue(ch.getDragEnabled());
116: }
117:
118: public void testSetGetPreviewPanel() throws Exception {
119: ch.addPropertyChangeListener(propertyChangeController);
120: assertNotNull(ch.getPreviewPanel());
121: JButton button = new JButton();
122: ch.setPreviewPanel(button);
123: assertTrue(propertyChangeController
124: .isChanged(JColorChooser.PREVIEW_PANEL_PROPERTY));
125: assertSame(button, ch.getPreviewPanel());
126: assertSame(button,
127: ((PropertyChangeEvent) propertyChangeController
128: .getLastEvent()).getNewValue());
129: propertyChangeController.reset();
130: ch.setPreviewPanel(null);
131: assertTrue(propertyChangeController
132: .isChanged(JColorChooser.PREVIEW_PANEL_PROPERTY));
133: assertNotNull(ch.getPreviewPanel());
134: assertSame(ColorChooserComponentFactory.getPreviewPanel()
135: .getClass(), ch.getPreviewPanel().getClass());
136: }
137:
138: public void testSetGetChooserPanels() throws Exception {
139: AbstractColorChooserPanel[] oldChooserPanels = ch
140: .getChooserPanels();
141: assertEquals(3, oldChooserPanels.length);
142: AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[] {};
143: ch.setChooserPanels(newPanels);
144: assertTrue(propertyChangeController
145: .isChanged(JColorChooser.CHOOSER_PANELS_PROPERTY));
146: assertSame(newPanels,
147: ((PropertyChangeEvent) propertyChangeController
148: .getLastEvent()).getNewValue());
149: assertSame(oldChooserPanels,
150: ((PropertyChangeEvent) propertyChangeController
151: .getLastEvent()).getOldValue());
152: assertSame(newPanels, ch.getChooserPanels());
153: assertEquals(0, ch.getChooserPanels().length);
154: }
155:
156: public void testAddRemoveChooserPanel() throws Exception {
157: AbstractColorChooserPanel[] oldChooserPanels = ch
158: .getChooserPanels();
159: assertEquals(3, oldChooserPanels.length);
160: AbstractColorChooserPanel panel = oldChooserPanels[0];
161: assertSame(panel, ch.removeChooserPanel(panel));
162: assertTrue(propertyChangeController
163: .isChanged(JColorChooser.CHOOSER_PANELS_PROPERTY));
164: assertEquals(2, ch.getChooserPanels().length);
165: propertyChangeController.reset();
166: try {
167: ch.removeChooserPanel(panel);
168: fail("IllegalArgumentException shal be thrown");
169: } catch (IllegalArgumentException e) {
170: }
171: assertFalse(propertyChangeController
172: .isChanged(JColorChooser.CHOOSER_PANELS_PROPERTY));
173: assertEquals(2, ch.getChooserPanels().length);
174: propertyChangeController.reset();
175: ch.addChooserPanel(panel);
176: assertTrue(propertyChangeController
177: .isChanged(JColorChooser.CHOOSER_PANELS_PROPERTY));
178: assertEquals(3, ch.getChooserPanels().length);
179: assertSame(panel, ch.getChooserPanels()[2]);
180: propertyChangeController.reset();
181: try {
182: ch.addChooserPanel(null);
183: fail("NPE shall be thrown");
184: } catch (NullPointerException npe) {
185: }
186: propertyChangeController.reset();
187: try {
188: ch.removeChooserPanel(null);
189: fail("NPE shall be thrown");
190: } catch (NullPointerException npe) {
191: }
192: }
193: }
|