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