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: */package javax.swing;
021:
022: import java.awt.event.ActionEvent;
023: import javax.accessibility.AccessibleRole;
024: import javax.swing.JToggleButton.ToggleButtonModel;
025:
026: public class JCheckBoxMenuItemTest extends JMenuItemTest {
027: @Override
028: protected void setUp() throws Exception {
029: super .setUp();
030: menuItem = new JCheckBoxMenuItem();
031: button = menuItem;
032: }
033:
034: @Override
035: protected void tearDown() throws Exception {
036: super .tearDown();
037: }
038:
039: /*
040: * Test method for 'javax.swing.JCheckBoxMenuItem.JCheckBoxMenuItem()'
041: */
042: public void testJCheckBoxMenuItem() {
043: assertFalse(menuItem.isSelected());
044: assertTrue("default buttonModel ",
045: button.getModel() instanceof ToggleButtonModel);
046: assertNull("icon ", button.getIcon());
047: assertEquals("text ", "", button.getText());
048: assertFalse("default FocusPainted", menuItem.isFocusPainted());
049: assertEquals(SwingConstants.LEADING, button
050: .getHorizontalAlignment());
051: assertEquals(SwingConstants.TRAILING, button
052: .getHorizontalTextPosition());
053: assertEquals(SwingConstants.CENTER, button
054: .getVerticalAlignment());
055: assertEquals(SwingConstants.CENTER, button
056: .getVerticalTextPosition());
057: }
058:
059: /*
060: * Test method for 'javax.swing.JCheckBoxMenuItem.JCheckBoxMenuItem(Icon)'
061: */
062: public void testJCheckBoxMenuItemIcon() {
063: Icon icon = createNewIcon();
064: menuItem = new JCheckBoxMenuItem(icon);
065: assertTrue("default buttonModel ",
066: button.getModel() instanceof ToggleButtonModel);
067: assertEquals("icon ", icon, menuItem.getIcon());
068: assertEquals("text ", "", menuItem.getText());
069: assertFalse("default FocusPainted", menuItem.isFocusPainted());
070: assertEquals(SwingConstants.LEADING, button
071: .getHorizontalAlignment());
072: assertEquals(SwingConstants.TRAILING, button
073: .getHorizontalTextPosition());
074: assertEquals(SwingConstants.CENTER, button
075: .getVerticalAlignment());
076: assertEquals(SwingConstants.CENTER, button
077: .getVerticalTextPosition());
078: }
079:
080: /*
081: * Test method for 'javax.swing.JCheckBoxMenuItem.JCheckBoxMenuItem(String)'
082: */
083: public void testJCheckBoxMenuItemString() {
084: String text = "texttext";
085: menuItem = new JCheckBoxMenuItem(text);
086: assertFalse(menuItem.isSelected());
087: assertTrue("default buttonModel ",
088: button.getModel() instanceof ToggleButtonModel);
089: assertNull("icon ", menuItem.getIcon());
090: assertEquals("text ", text, menuItem.getText());
091: assertEquals(SwingConstants.LEADING, button
092: .getHorizontalAlignment());
093: assertEquals(SwingConstants.TRAILING, button
094: .getHorizontalTextPosition());
095: assertEquals(SwingConstants.CENTER, button
096: .getVerticalAlignment());
097: assertEquals(SwingConstants.CENTER, button
098: .getVerticalTextPosition());
099: }
100:
101: /*
102: * Test method for 'javax.swing.JCheckBoxMenuItem.JCheckBoxMenuItem(String, Icon)'
103: */
104: public void testJCheckBoxMenuItemStringIcon() {
105: Icon icon = createNewIcon();
106: String text = "texttext";
107: menuItem = new JCheckBoxMenuItem(text, icon);
108: assertFalse(menuItem.isSelected());
109: assertTrue("default buttonModel ",
110: button.getModel() instanceof ToggleButtonModel);
111: assertEquals("icon ", icon, menuItem.getIcon());
112: assertEquals("text ", text, menuItem.getText());
113: assertFalse("default FocusPainted", menuItem.isFocusPainted());
114: assertEquals(SwingConstants.LEADING, button
115: .getHorizontalAlignment());
116: assertEquals(SwingConstants.TRAILING, button
117: .getHorizontalTextPosition());
118: assertEquals(SwingConstants.CENTER, button
119: .getVerticalAlignment());
120: assertEquals(SwingConstants.CENTER, button
121: .getVerticalTextPosition());
122: }
123:
124: /*
125: * Test method for 'javax.swing.JCheckBoxMenuItem.JCheckBoxMenuItem(Action)'
126: */
127: public void testJCheckBoxMenuItemAction() {
128: final String command = "dnammoc";
129: final KeyStroke accelerator = KeyStroke.getKeyStroke('a');
130: class MyAction extends AbstractAction {
131: private static final long serialVersionUID = 1L;
132:
133: public MyAction(final String text, final Icon icon) {
134: super (text, icon);
135: putValue(Action.ACTION_COMMAND_KEY, command);
136: putValue(Action.ACCELERATOR_KEY, accelerator);
137: }
138:
139: public void actionPerformed(final ActionEvent e) {
140: }
141: }
142: ;
143: Icon icon = createNewIcon();
144: String text = "texttext";
145: MyAction action = new MyAction(text, icon);
146: menuItem = new JCheckBoxMenuItem(action);
147: assertFalse(menuItem.isSelected());
148: assertEquals("icon ", icon, menuItem.getIcon());
149: assertEquals("text ", text, menuItem.getText());
150: assertEquals("action", action, menuItem.getAction());
151: assertEquals("command ", command, menuItem.getActionCommand());
152: assertFalse("selected ", menuItem.isSelected());
153: assertTrue("enabled ", menuItem.isEnabled());
154: assertEquals("accelerator ", accelerator, menuItem
155: .getAccelerator());
156: action.setEnabled(false);
157: menuItem = new JCheckBoxMenuItem(action);
158: assertEquals("icon ", icon, menuItem.getIcon());
159: assertEquals("text ", text, menuItem.getText());
160: assertEquals("action", action, menuItem.getAction());
161: assertEquals("command ", command, menuItem.getActionCommand());
162: assertFalse("selected ", menuItem.isSelected());
163: assertFalse("enabled ", menuItem.isEnabled());
164: assertFalse("default FocusPainted", menuItem.isFocusPainted());
165: menuItem = new JCheckBoxMenuItem((Action) null);
166: assertNull("icon ", menuItem.getIcon());
167: assertNull("text ", menuItem.getText());
168: assertNull("action", menuItem.getAction());
169: assertNull("command ", menuItem.getActionCommand());
170: assertFalse("selected ", menuItem.isSelected());
171: assertTrue("enabled ", menuItem.isEnabled());
172: assertTrue("default buttonModel ",
173: button.getModel() instanceof ToggleButtonModel);
174: assertEquals(SwingConstants.LEADING, button
175: .getHorizontalAlignment());
176: assertEquals(SwingConstants.TRAILING, button
177: .getHorizontalTextPosition());
178: assertEquals(SwingConstants.CENTER, button
179: .getVerticalAlignment());
180: assertEquals(SwingConstants.CENTER, button
181: .getVerticalTextPosition());
182: }
183:
184: /*
185: * Test method for 'javax.swing.JCheckBoxMenuItem.JCheckBoxMenuItem(String, Icon, boolean)'
186: */
187: public void testJCheckBoxMenuItemStringIconBoolean() {
188: Icon icon = createNewIcon();
189: String text = "texttext";
190: menuItem = new JCheckBoxMenuItem(text, icon, true);
191: assertTrue("default buttonModel ",
192: button.getModel() instanceof ToggleButtonModel);
193: assertEquals("icon ", icon, menuItem.getIcon());
194: assertEquals("text ", text, menuItem.getText());
195: assertFalse("default FocusPainted", menuItem.isFocusPainted());
196: assertTrue(menuItem.isSelected());
197: assertEquals(SwingConstants.LEADING, button
198: .getHorizontalAlignment());
199: assertEquals(SwingConstants.TRAILING, button
200: .getHorizontalTextPosition());
201: assertEquals(SwingConstants.CENTER, button
202: .getVerticalAlignment());
203: assertEquals(SwingConstants.CENTER, button
204: .getVerticalTextPosition());
205: menuItem = new JCheckBoxMenuItem(text, icon, false);
206: assertFalse(menuItem.isSelected());
207: }
208:
209: /*
210: * Test method for 'javax.swing.JCheckBoxMenuItem.getAccessibleContext()'
211: */
212: @Override
213: public void testGetAccessibleContext() {
214: boolean assertedValue = (menuItem.getAccessibleContext() != null && menuItem
215: .getAccessibleContext()
216: .getClass()
217: .getName()
218: .equals(
219: "javax.swing.JCheckBoxMenuItem$AccessibleJCheckBoxMenuItem"));
220: assertTrue("AccessibleContext created properly ", assertedValue);
221: assertEquals("AccessibleRole", AccessibleRole.CHECK_BOX,
222: menuItem.getAccessibleContext().getAccessibleRole());
223: }
224:
225: /*
226: * Test method for 'javax.swing.JCheckBoxMenuItem.getUIClassID()'
227: */
228: @Override
229: public void testGetUIClassID() {
230: assertEquals("CheckBoxMenuItemUI", menuItem.getUIClassID());
231: }
232:
233: /*
234: * Test method for 'javax.swing.JCheckBoxMenuItem.getSelectedObjects()'
235: */
236: @Override
237: public void testGetSelectedObjects() {
238: String text = "texttext";
239: menuItem = new JCheckBoxMenuItem(text);
240: assertNull(((JCheckBoxMenuItem) menuItem).getSelectedObjects());
241: menuItem.setSelected(true);
242: assertEquals(1, ((JCheckBoxMenuItem) menuItem)
243: .getSelectedObjects().length);
244: assertEquals(menuItem.getText(), ((JCheckBoxMenuItem) menuItem)
245: .getSelectedObjects()[0]);
246: }
247:
248: /*
249: * Test method for 'javax.swing.JCheckBoxMenuItem.getState()'
250: */
251: public void testGetSetState() {
252: assertEquals("default Selected", ((JCheckBoxMenuItem) menuItem)
253: .getState(), menuItem.isSelected());
254: menuItem.setSelected(true);
255: assertEquals("Selected", ((JCheckBoxMenuItem) menuItem)
256: .getState(), menuItem.isSelected());
257: ((JCheckBoxMenuItem) menuItem).setState(false);
258: assertEquals("Selected", ((JCheckBoxMenuItem) menuItem)
259: .getState(), menuItem.isSelected());
260: }
261: }
|