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 01.05.2005
021:
022: */package javax.swing;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.image.BufferedImage;
026: import javax.accessibility.AccessibleRole;
027: import javax.swing.plaf.ButtonUI;
028:
029: public class JCheckBoxTest extends SwingTestCase {
030: protected AbstractButton button = null;
031:
032: /*
033: * @see TestCase#setUp()
034: */
035: @Override
036: protected void setUp() throws Exception {
037: super .setUp();
038: button = new JCheckBox();
039: }
040:
041: public void testGetAccessibleContext() {
042: boolean assertedValue = (button.getAccessibleContext() != null && button
043: .getAccessibleContext().getClass().getName().equals(
044: "javax.swing.JCheckBox$AccessibleJCheckBox"));
045: assertTrue("AccessibleContext created properly ", assertedValue);
046: assertEquals("AccessibleRole", AccessibleRole.CHECK_BOX, button
047: .getAccessibleContext().getAccessibleRole());
048: }
049:
050: public void testParamString() {
051: assertTrue("ParamString returns a string ",
052: button.toString() != null);
053: }
054:
055: public void testGetUIClassID() {
056: assertEquals("UI class ID", "CheckBoxUI", button.getUIClassID());
057: }
058:
059: public void testUpdateUI() {
060: ButtonUI ui = new ButtonUI() {
061: };
062: button.setUI(ui);
063: assertEquals(ui, button.getUI());
064: button.updateUI();
065: assertNotSame(ui, button.getUI());
066: }
067:
068: public void testCreateActionPropertyChangeListener() {
069: Object res1 = null;
070: Object res2 = null;
071: AbstractAction action1 = new AbstractAction() {
072: private static final long serialVersionUID = 1L;
073:
074: public void actionPerformed(final ActionEvent event) {
075: }
076: };
077: AbstractAction action2 = new AbstractAction() {
078: private static final long serialVersionUID = 1L;
079:
080: public void actionPerformed(final ActionEvent event) {
081: }
082: };
083: res1 = button.createActionPropertyChangeListener(action1);
084: assertNotNull("returned value is not null", res1);
085: res2 = button.createActionPropertyChangeListener(action2);
086: assertNotNull("returned value is not null", res2);
087: res2 = button.createActionPropertyChangeListener(null);
088: assertNotNull("returned value is not null", res2);
089: }
090:
091: @SuppressWarnings("serial")
092: public void testConfigurePropertiesFromAction() {
093: Icon icon1 = new ImageIcon(new BufferedImage(20, 20,
094: BufferedImage.TYPE_BYTE_GRAY));
095: Icon icon2 = new ImageIcon(new BufferedImage(20, 20,
096: BufferedImage.TYPE_BYTE_GRAY));
097: String text1 = "texttext1";
098: String text2 = "texttext2";
099: String text3 = "texttext3";
100: String text4 = "texttext4";
101: AbstractAction action1 = new AbstractAction(text1, icon1) {
102: public void actionPerformed(final ActionEvent event) {
103: }
104: };
105: AbstractAction action2 = new AbstractAction(text2, icon2) {
106: public void actionPerformed(final ActionEvent event) {
107: }
108: };
109: action1.setEnabled(true);
110: action1.putValue(Action.SHORT_DESCRIPTION, text3);
111: action1.putValue(Action.MNEMONIC_KEY, new Integer(1));
112: button.setAction(action1);
113: assertEquals("action ", action1, button.getAction());
114: assertTrue("enabled ", button.isEnabled());
115: assertTrue("enabled ", action1.isEnabled());
116: action1.setEnabled(false);
117: button.isEnabled();
118: assertFalse("enabled ", button.isEnabled());
119: assertFalse("enabled ", action1.isEnabled());
120: assertNull("icon ", button.getIcon());
121: action1.putValue(Action.SMALL_ICON, icon2);
122: assertNull("icon ", button.getIcon());
123: button.setIcon(icon2);
124: action1.putValue(Action.SMALL_ICON, null);
125: assertEquals("icon ", icon2, button.getIcon());
126: if (isHarmony()) {
127: assertEquals("mnemonic ", 1, button.getMnemonic());
128: action1.putValue(Action.MNEMONIC_KEY, new Integer(27));
129: assertEquals("mnemonic ", 27, button.getMnemonic());
130: }
131: assertEquals("text ", text1, button.getText());
132: action1.putValue(Action.NAME, text2);
133: assertEquals("text ", text2, button.getText());
134: assertEquals("ToolTipText ", text3, button.getToolTipText());
135: action1.putValue(Action.SHORT_DESCRIPTION, text4);
136: assertEquals("ToolTipText ", text4, button.getToolTipText());
137: button.setAction(action2);
138: action1.putValue(Action.SHORT_DESCRIPTION, text4);
139: assertNull("ToolTipText ", button.getToolTipText());
140: action2.putValue(Action.SHORT_DESCRIPTION, text4);
141: assertEquals("ToolTipText ", text4, button.getToolTipText());
142: }
143:
144: /*
145: * Class under test for void JCheckBox()
146: */
147: public void testJCheckBox() {
148: assertNull(button.getIcon());
149: assertEquals("", button.getText());
150: assertFalse(button.isSelected());
151: assertEquals(SwingConstants.LEADING, button
152: .getHorizontalAlignment());
153: assertEquals(SwingConstants.CENTER, button
154: .getVerticalAlignment());
155: }
156:
157: /*
158: * Class under test for void JCheckBox(Action)
159: */
160: @SuppressWarnings("serial")
161: public void testJCheckBoxAction() {
162: final String command = "dnammoc";
163: class MyAction extends AbstractAction {
164: public MyAction(final String text, final Icon icon) {
165: super (text, icon);
166: putValue(Action.ACTION_COMMAND_KEY, command);
167: }
168:
169: public void actionPerformed(final ActionEvent e) {
170: }
171: }
172: ;
173: Icon icon = new ImageIcon(new BufferedImage(20, 20,
174: BufferedImage.TYPE_BYTE_GRAY));
175: String text = "texttext";
176: MyAction action = new MyAction(text, icon);
177: action.setEnabled(false);
178: button = new JCheckBox(action);
179: assertNull("icon ", button.getIcon());
180: assertEquals("text ", text, button.getText());
181: assertEquals("action", action, button.getAction());
182: assertEquals("command ", command, button.getActionCommand());
183: assertFalse("selected ", button.isSelected());
184: assertFalse("enabled ", button.isEnabled());
185: button = new JCheckBox((Action) null);
186: assertNull("icon ", button.getIcon());
187: assertNull("text ", button.getText());
188: assertNull("action", button.getAction());
189: assertNull("command ", button.getActionCommand());
190: assertFalse("selected ", button.isSelected());
191: assertTrue("enabled ", button.isEnabled());
192: }
193:
194: /*
195: * Class under test for void JCheckBox(Icon)
196: */
197: public void testJCheckBoxIcon() {
198: Icon icon1 = new ImageIcon(new BufferedImage(20, 20,
199: BufferedImage.TYPE_BYTE_GRAY));
200: Icon icon2 = new ImageIcon(new BufferedImage(20, 20,
201: BufferedImage.TYPE_BYTE_GRAY));
202: String text1 = "";
203: String text2 = "";
204: boolean state1 = false;
205: boolean state2 = false;
206: button = new JCheckBox(icon1);
207: assertEquals("icon ", icon1, button.getIcon());
208: assertEquals("text ", text1, button.getText());
209: assertEquals("selected ", state1, button.isSelected());
210: button = new JCheckBox(icon2);
211: assertEquals("icon ", icon2, button.getIcon());
212: assertEquals("text ", text2, button.getText());
213: assertEquals("selected ", state2, button.isSelected());
214: }
215:
216: /*
217: * Class under test for void JCheckBox(Icon, boolean)
218: */
219: public void testJCheckBoxIconboolean() {
220: Icon icon1 = new ImageIcon(new BufferedImage(20, 20,
221: BufferedImage.TYPE_BYTE_GRAY));
222: Icon icon2 = new ImageIcon(new BufferedImage(20, 20,
223: BufferedImage.TYPE_BYTE_GRAY));
224: String text1 = "";
225: String text2 = "";
226: boolean state1 = true;
227: boolean state2 = false;
228: button = new JCheckBox(icon1, state1);
229: assertEquals("icon ", icon1, button.getIcon());
230: assertEquals("text ", text1, button.getText());
231: assertEquals("selected ", state1, button.isSelected());
232: button = new JCheckBox(icon2, state2);
233: assertEquals("icon ", icon2, button.getIcon());
234: assertEquals("text ", text2, button.getText());
235: assertEquals("selected ", state2, button.isSelected());
236: }
237:
238: /*
239: * Class under test for void JCheckBox(String)
240: */
241: public void testJCheckBoxString() {
242: Icon icon1 = null;
243: Icon icon2 = null;
244: String text1 = "texttext1";
245: String text2 = "texttext2";
246: boolean state1 = false;
247: boolean state2 = false;
248: button = new JCheckBox(text1);
249: assertEquals("icon ", icon1, button.getIcon());
250: assertEquals("text ", text1, button.getText());
251: assertEquals("selected ", state1, button.isSelected());
252: button = new JCheckBox(text2);
253: assertEquals("icon ", icon2, button.getIcon());
254: assertEquals("text ", text2, button.getText());
255: assertEquals("selected ", state2, button.isSelected());
256: }
257:
258: /*
259: * Class under test for void JCheckBox(String, boolean)
260: */
261: public void testJCheckBoxStringboolean() {
262: Icon icon1 = null;
263: Icon icon2 = null;
264: String text1 = "texttext1";
265: String text2 = "texttext2";
266: boolean state1 = true;
267: boolean state2 = false;
268: button = new JCheckBox(text1, state1);
269: assertEquals("icon ", icon1, button.getIcon());
270: assertEquals("text ", text1, button.getText());
271: assertEquals("selected ", state1, button.isSelected());
272: button = new JCheckBox(text2, state2);
273: assertEquals("icon ", icon2, button.getIcon());
274: assertEquals("text ", text2, button.getText());
275: assertEquals("selected ", state2, button.isSelected());
276: }
277:
278: /*
279: * Class under test for void JCheckBox(String, Icon)
280: */
281: public void testJCheckBoxStringIcon() {
282: Icon icon1 = new ImageIcon(new BufferedImage(20, 20,
283: BufferedImage.TYPE_BYTE_GRAY));
284: Icon icon2 = new ImageIcon(new BufferedImage(20, 20,
285: BufferedImage.TYPE_BYTE_GRAY));
286: String text1 = "texttext1";
287: String text2 = "texttext2";
288: boolean state1 = false;
289: boolean state2 = false;
290: button = new JCheckBox(text1, icon1);
291: assertEquals("icon ", icon1, button.getIcon());
292: assertEquals("text ", text1, button.getText());
293: assertEquals("selected ", state1, button.isSelected());
294: button = new JCheckBox(text2, icon2);
295: assertEquals("icon ", icon2, button.getIcon());
296: assertEquals("text ", text2, button.getText());
297: assertEquals("selected ", state2, button.isSelected());
298: }
299:
300: /*
301: * Class under test for void JCheckBox(String, Icon, boolean)
302: */
303: public void testJCheckBoxStringIconboolean() {
304: Icon icon1 = new ImageIcon(new BufferedImage(20, 20,
305: BufferedImage.TYPE_BYTE_GRAY));
306: Icon icon2 = new ImageIcon(new BufferedImage(20, 20,
307: BufferedImage.TYPE_BYTE_GRAY));
308: String text1 = "texttext1";
309: String text2 = "texttext2";
310: boolean state1 = true;
311: boolean state2 = false;
312: button = new JCheckBox(text1, icon1, state1);
313: assertEquals("icon ", icon1, button.getIcon());
314: assertEquals("text ", text1, button.getText());
315: assertEquals("selected ", state1, button.isSelected());
316: button = new JCheckBox(text2, icon2, state2);
317: assertEquals("icon ", icon2, button.getIcon());
318: assertEquals("text ", text2, button.getText());
319: assertEquals("selected ", state2, button.isSelected());
320: }
321:
322: public void testIsBorderPaintedFlat() {
323: assertFalse("paintedFlat", ((JCheckBox) button)
324: .isBorderPaintedFlat());
325: }
326:
327: public void testSetBorderPaintedFlat() {
328: PropertyChangeController listener1 = new PropertyChangeController();
329: button.addPropertyChangeListener(listener1);
330: ((JCheckBox) button).setBorderPaintedFlat(true);
331: listener1.checkPropertyFired(button, "borderPaintedFlat",
332: Boolean.FALSE, Boolean.TRUE);
333: assertTrue("borderPaintedFlat", ((JCheckBox) button)
334: .isBorderPaintedFlat());
335: listener1.reset();
336: ((JCheckBox) button).setBorderPaintedFlat(false);
337: listener1.checkPropertyFired(button, "borderPaintedFlat",
338: Boolean.TRUE, Boolean.FALSE);
339: assertFalse("borderPaintedFlat", ((JCheckBox) button)
340: .isBorderPaintedFlat());
341: }
342: }
|