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.JToggleButton;
034:
035: /**
036: * Test class for testing <code>java.swing.JToggleButton</code> components. The following criteria
037: * are tested by this class:
038: *
039: * <ul>
040: * <li>
041: * <b>is selected</b> - testes if the text on the tested button is correct
042: * </li>
043: * </ul>
044: *
045: *
046: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
047: * @version 1.2
048: */
049: public class JToggleButtonTest extends AbstractButtonTest {
050: /** Contains whether or not the toggle button 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 btnText the text on the button
064: * @param isSelected whether or not the button should be selected
065: */
066: public JToggleButtonTest(Environment env, String componentName,
067: boolean isIgnoring, boolean isCorrecting, boolean hasFocus,
068: boolean isEnabled, String btnText, boolean isSelected) {
069: super (env, componentName, isIgnoring, isCorrecting, hasFocus,
070: isEnabled, btnText);
071: setSelected(isSelected);
072: }
073:
074: /**
075: * Creates a new test with the values of the given component and default values.
076: *
077: * @param env the environment.
078: * @param components the components instance
079: * @param component the component to test. Must be of type
080: * <code>javax.swing.JToggleButton</code>.
081: */
082: public JToggleButtonTest(Environment env, Components components,
083: Component component) {
084: super (env, components, component);
085: setSelected(((JToggleButton) component).isSelected());
086: }
087:
088: /**
089: * Creates a new test with default values and no environment. The environment should be defined
090: * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
091: * before environment instances will be accessed.
092: */
093: public JToggleButtonTest() {
094: this (null, "", false, false, false, true, "", false);
095: }
096:
097: /**
098: * DOCUMENT ME!
099: *
100: * @param isSelected whether the tested toggle button is expected to be selected
101: */
102: public void setSelected(boolean isSelected) {
103: this .isSelected = isSelected;
104: }
105:
106: /**
107: * DOCUMENT ME!
108: *
109: * @return <code>true</code> if the tested toggle button is expected to be selected, if not
110: * <code>false</code>.
111: */
112: public boolean isSelected() {
113: return isSelected;
114: }
115:
116: /**
117: * @see jacareto.struct.StructureElement#getElementName()
118: */
119: public String getElementName() {
120: return language.getString("Tests.JToggleButtonTest.Name");
121: }
122:
123: /**
124: * @see jacareto.struct.StructureElement#getElementDescription()
125: */
126: public String getElementDescription() {
127: return language
128: .getString("Tests.JToggleButtonTest.Description");
129: }
130:
131: /**
132: * @see jacareto.struct.StructureElement#toShortString()
133: */
134: public String toShortString() {
135: return getElementName();
136: }
137:
138: /**
139: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
140: */
141: public boolean evaluate(Components components) {
142: JToggleButton button = null;
143: setLastIgnored(isIgnoring());
144: setLastResult(false);
145: setLastCorrected(false);
146:
147: // get the button with the componentName
148: Component component = components
149: .getComponent(getComponentName());
150:
151: if (component == null) {
152: setEvaluationMessage(language
153: .getString("Tests.Test.Failure.NoComponent"));
154:
155: return false;
156: }
157:
158: if (!(component instanceof JToggleButton)) {
159: setEvaluationMessage(language
160: .getString("Tests.Test.Failure.WrongComponentType"));
161:
162: return false;
163: }
164:
165: button = (JToggleButton) component;
166:
167: if (doTest(button)) {
168: boolean result = true;
169: Iterator childIter = childrenIterator();
170:
171: while (childIter.hasNext() && result) {
172: result &= evaluateChild((Test) childIter.next(),
173: components);
174: }
175:
176: setLastResult(result);
177:
178: return result;
179: }
180:
181: if (isCorrecting()) {
182: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
183: correct(button);
184: setLastCorrected(true);
185: }
186:
187: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
188: return false;
189: }
190:
191: /**
192: * @see jacareto.test.JComponentTest#doTest(java.awt.Component)
193: */
194: protected boolean doTest(Component component) {
195: if (super .doTest(component)) {
196: JToggleButton button = (JToggleButton) component;
197:
198: // JToggleButton specific evaluation logic
199: if (button.isSelected() != isSelected()) {
200: setEvaluationMessage(language
201: .getString("Tests.JToggleButtonTest.Failure.WrongStatus")
202: + "\n"
203: + language
204: .getString("Tests.Test.Failure.Expected")
205: + ": "
206: + isSelected()
207: + "\n"
208: + language
209: .getString("Tests.Test.Failure.Detected")
210: + ": " + button.isSelected());
211:
212: return false;
213: }
214: } else {
215: return false;
216: }
217:
218: return true;
219: }
220:
221: /**
222: * @see jacareto.test.JComponentTest#correct(java.awt.Component)
223: */
224: protected void correct(Component component) {
225: super .correct(component);
226:
227: JToggleButton button = (JToggleButton) component;
228:
229: if (button.isSelected()) {
230: //selection cannot be moved because target button is unknown
231: appendToEvaluationMessage("\n"
232: + getElementName()
233: + ": "
234: + language
235: .getString("Tests.JToggleButtonTest.Correct.SelectFailure"));
236: } else {
237: button.setSelected(true);
238: appendToEvaluationMessage("\n"
239: + getElementName()
240: + ": "
241: + language
242: .getString("Tests.JToggleButtonTest.Correct.Select"));
243: }
244: }
245: }
|