001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.test;
025:
026: import jacareto.comp.Components;
027: import jacareto.system.Environment;
028:
029: import java.awt.Component;
030:
031: import java.util.Iterator;
032:
033: import javax.swing.JRadioButtonMenuItem;
034:
035: /**
036: * Test class for testing <code>java.swing.JRadioButtonMenuItem</code> components. The following
037: * criteria are tested by this class:
038: *
039: * <ul>
040: * <li>
041: * <b>is selected</b> - testes if the radio button menu item is selected
042: * </li>
043: * </ul>
044: *
045: *
046: * @author uak
047: * @version 1.0
048: */
049: public class JRadioButtonMenuItemTest extends JMenuItemTest {
050: /** Contains whether the radio button menu item is expected to be selected */
051: private boolean isSelected;
052:
053: /**
054: * Creates a new test with the specified values.
055: *
056: * @param env env the environment
057: * @param componentName the name of the component
058: * @param isIgnoring if the test result should be ignored
059: * @param isCorrecting if the values of the component should be corrected when the test has
060: * failed
061: * @param hasFocus if the component has the focus
062: * @param isEnabled if the component is set enabled
063: * @param buttonText the text on the button
064: * @param isArmed if the the component is "armed"
065: * @param isSelected if the the component is selected
066: */
067: public JRadioButtonMenuItemTest(Environment env,
068: String componentName, boolean isIgnoring,
069: boolean isCorrecting, boolean hasFocus, boolean isEnabled,
070: String buttonText, boolean isArmed, boolean isSelected) {
071: super (env, componentName, isIgnoring, isCorrecting, hasFocus,
072: isEnabled, buttonText, isArmed);
073: setSelected(isSelected);
074: }
075:
076: /**
077: * Creates a new test with the values of the given component and default values.
078: *
079: * @param env the environment.
080: * @param components the components instance
081: * @param component the component to test. Must be of type
082: * <code>javax.swing.JRadioButtonMenuItem</code>.
083: */
084: public JRadioButtonMenuItemTest(Environment env,
085: Components components, Component component) {
086: super (env, components, component);
087: setSelected(((JRadioButtonMenuItem) component).isSelected());
088: }
089:
090: /**
091: * Creates a new test with default values and no environment. The environment should be defined
092: * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
093: * before environment instances will be accessed.
094: */
095: public JRadioButtonMenuItemTest() {
096: this (null, "", false, false, false, true, "", false, false);
097: }
098:
099: /**
100: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
101: */
102: public boolean evaluate(Components components) {
103: JRadioButtonMenuItem menuItem = null;
104:
105: setLastIgnored(isIgnoring());
106: setLastResult(false);
107: setLastCorrected(false);
108:
109: // get the pane with the componentName
110: Component component = components
111: .getComponent(getComponentName());
112:
113: if (component == null) {
114: setEvaluationMessage(language
115: .getString("Tests.Test.Failure.NoComponent"));
116:
117: return false;
118: }
119:
120: if (!(component instanceof JRadioButtonMenuItem)) {
121: setEvaluationMessage(language
122: .getString("Tests.Test.Failure.WrongComponentType"));
123:
124: return false;
125: }
126:
127: menuItem = (JRadioButtonMenuItem) component;
128:
129: // check if the menu's components are correct
130: if (doTest(menuItem)) {
131: boolean result = true;
132: Iterator childIter = childrenIterator();
133:
134: while (childIter.hasNext() && result) {
135: result &= evaluateChild((Test) childIter.next(),
136: components);
137: }
138:
139: setLastResult(result);
140:
141: return result;
142: }
143:
144: if (isCorrecting()) {
145: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
146: correct(menuItem);
147: setLastCorrected(true);
148: }
149:
150: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
151: return false;
152: }
153:
154: /**
155: * @see jacareto.test.JComponentTest#doTest(java.awt.Component)
156: */
157: protected boolean doTest(Component component) {
158: if (super .doTest(component)) {
159: JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) component;
160:
161: // check if the menu's components are correct
162: if (menuItem.isSelected() != isSelected()) {
163: setEvaluationMessage(language
164: .getString("Tests.JRadioButtonMenuItemTest.Failure.WrongState")
165: + "\n"
166: + language
167: .getString("Tests.Test.Failure.Expected")
168: + ": "
169: + isSelected()
170: + "\n"
171: + language
172: .getString("Tests.Test.Failure.Detected")
173: + ": " + menuItem.isSelected());
174:
175: return false;
176: }
177: } else {
178: return false;
179: }
180:
181: return true;
182: }
183:
184: /**
185: * @see jacareto.test.JComponentTest#correct(java.awt.Component)
186: */
187: protected void correct(Component component) {
188: super .correct(component);
189:
190: JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) component;
191:
192: if (menuItem.isSelected()) {
193: //selection cannot be moved because target button is unknown
194: appendToEvaluationMessage("\n"
195: + getElementName()
196: + ": "
197: + language
198: .getString("Tests.JRadioButtonMenuItemTest.Correct.SelectFailure"));
199: } else {
200: menuItem.setSelected(true);
201: appendToEvaluationMessage("\n"
202: + getElementName()
203: + ": "
204: + language
205: .getString("Tests.JRadioButtonMenuItemTest.Correct.Select"));
206: }
207: }
208:
209: /**
210: * @see jacareto.struct.StructureElement#getElementName()
211: */
212: public String getElementName() {
213: return language
214: .getString("Tests.JRadioButtonMenuItemTest.Name");
215: }
216:
217: /**
218: * @see jacareto.struct.StructureElement#getElementDescription()
219: */
220: public String getElementDescription() {
221: return language
222: .getString("Tests.JRadioButtonMenuItemTest.Description");
223: }
224:
225: /**
226: * DOCUMENT ME!
227: *
228: * @param b whether the tested radio button menu item is expected to be selected
229: */
230: public void setSelected(boolean b) {
231: isSelected = b;
232: }
233:
234: /**
235: * DOCUMENT ME!
236: *
237: * @return <code>true</code> if the tested radio button menu item is expected to be selected,
238: * if not <code>false</code>.
239: */
240: public boolean isSelected() {
241: return isSelected;
242: }
243:
244: /**
245: * @see jacareto.struct.StructureElement#toShortString()
246: */
247: public String toShortString() {
248: return getElementName();
249: }
250: }
|