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.JSlider;
034:
035: /**
036: * Test class for testing <code>java.swing.AbstractButton</code> components. The following
037: * criteria are tested by this class:
038: *
039: * <ul>
040: * <li>
041: * <b>slider value</b> - testes whether the value of the tested slider is correct
042: * </li>
043: * <li>
044: * <b>slider min</b> - testes whether the lower bound of the tested slider is correct
045: * </li>
046: * <li>
047: * <b>slider max</b> - testes whether the upper bound of the tested button is correct
048: * </li>
049: * </ul>
050: *
051: *
052: * @author Carola Coch, Uwe A. Krämer
053: * @version 1.0
054: */
055: public class JSliderTest extends JComponentTest {
056: /** Contains the expected value of the tested slider */
057: private int sliderValue;
058:
059: /** Contains the expected lower bound of the tested slider */
060: private int sliderMin;
061:
062: /** Contains the expected upper bound of the tested slider */
063: private int sliderMax;
064:
065: /**
066: * Creates a new test with the specified values.
067: *
068: * @param env env the environment
069: * @param componentName the name of the component
070: * @param isIgnoring if the test result should be ignored
071: * @param isCorrecting if the values of the component should be corrected when the test has
072: * failed
073: * @param hasFocus if the component has the focus
074: * @param isEnabled if the component is set enabled
075: * @param value the value of the slider
076: * @param min the lower bound of the slider
077: * @param max the upper bound of the slider
078: */
079: public JSliderTest(Environment env, String componentName,
080: boolean isIgnoring, boolean isCorrecting, boolean hasFocus,
081: boolean isEnabled, int value, int min, int max) {
082: super (env, componentName, isIgnoring, isCorrecting, hasFocus,
083: isEnabled);
084: setSliderValue(value);
085: setSliderMin(min);
086: setSliderMax(max);
087: }
088:
089: /**
090: * Creates a new test with the values of the given component and default values.
091: *
092: * @param env the environment.
093: * @param components the components instance
094: * @param component the component to test. Must be of type
095: * <code>javax.swing.JToggleButton</code>.
096: */
097: public JSliderTest(Environment env, Components components,
098: Component component) {
099: super (env, components, component);
100: setSliderValue(((JSlider) component).getValue());
101: setSliderMin(((JSlider) component).getMinimum());
102: setSliderMax(((JSlider) component).getMaximum());
103: }
104:
105: /**
106: * Creates a new test with default values and no environment. The environment should be defined
107: * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
108: * before environment instances will be accessed.
109: */
110: public JSliderTest() {
111: this (null, "", false, false, false, true, 0, 0, 0);
112: }
113:
114: /**
115: * @see jacareto.struct.StructureElement#getElementName()
116: */
117: public String getElementName() {
118: return language.getString("Tests.JSliderTest.Name");
119: }
120:
121: /**
122: * @see jacareto.struct.StructureElement#getElementDescription()
123: */
124: public String getElementDescription() {
125: return language.getString("Tests.JSliderTest.Description");
126: }
127:
128: /**
129: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
130: */
131: public boolean evaluate(Components components) {
132: JSlider slider = null;
133: setLastIgnored(isIgnoring());
134: setLastResult(false);
135: setLastCorrected(false);
136:
137: // get the slider with the componentName
138: Component component = components
139: .getComponent(getComponentName());
140:
141: if (component == null) {
142: setEvaluationMessage(language
143: .getString("Tests.Test.Failure.NoComponent"));
144:
145: return false;
146: }
147:
148: if (!(component instanceof JSlider)) {
149: setEvaluationMessage(language
150: .getString("Tests.Test.Failure.WrongComponentType"));
151:
152: return false;
153: }
154:
155: slider = (JSlider) component;
156:
157: if (doTest(slider)) {
158: boolean result = true;
159: Iterator childIter = childrenIterator();
160:
161: while (childIter.hasNext() && result) {
162: result &= evaluateChild((Test) childIter.next(),
163: components);
164: }
165:
166: setLastResult(result);
167:
168: return result;
169: }
170:
171: //If the isCorrecting flag is set, let's set the correct values
172: if (isCorrecting()) {
173: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
174: correct(slider);
175: setLastCorrected(true);
176: }
177:
178: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
179: return false;
180: }
181:
182: /**
183: * @see jacareto.struct.StructureElement#toShortString()
184: */
185: public String toShortString() {
186: return getElementName();
187: }
188:
189: /**
190: * DOCUMENT ME!
191: *
192: * @return The expected value of the tested slider
193: */
194: public int getSliderValue() {
195: return sliderValue;
196: }
197:
198: /**
199: * DOCUMENT ME!
200: *
201: * @param i the expected value of the tested slider
202: */
203: public void setSliderValue(int i) {
204: sliderValue = i;
205: }
206:
207: /**
208: * DOCUMENT ME!
209: *
210: * @return The expected upper bound of the tested of the tested slider
211: */
212: public int getSliderMax() {
213: return sliderMax;
214: }
215:
216: /**
217: * DOCUMENT ME!
218: *
219: * @return The expected lower bound of the tested of the tested slider
220: */
221: public int getSliderMin() {
222: return sliderMin;
223: }
224:
225: /**
226: * DOCUMENT ME!
227: *
228: * @param i the expected upper bound of the tested of the tested slider
229: */
230: public void setSliderMax(int i) {
231: sliderMax = i;
232: }
233:
234: /**
235: * DOCUMENT ME!
236: *
237: * @param i the expected lower bound of the tested of the tested slider
238: */
239: public void setSliderMin(int i) {
240: sliderMin = i;
241: }
242:
243: /**
244: * @see jacareto.test.JComponentTest#doTest(java.awt.Component)
245: */
246: protected boolean doTest(Component component) {
247: if (super .doTest(component)) {
248: JSlider slider = (JSlider) component;
249:
250: //let's check whether slider has correct value
251: if (!(slider.getValue() == getSliderValue())) {
252: setEvaluationMessage(language
253: .getString("Tests.JSliderTest.Failure.WrongValue")
254: + "\n"
255: + language
256: .getString("Tests.Test.Failure.Expected")
257: + ": "
258: + getSliderValue()
259: + "\n"
260: + language
261: .getString("Tests.Test.Failure.Detected")
262: + ": " + slider.getValue());
263:
264: return false;
265: }
266:
267: //let's check whether slider has correct lower bound
268: if (!(slider.getMinimum() == getSliderMin())) {
269: setEvaluationMessage(language
270: .getString("Tests.JSliderTest.Failure.WrongLowerBound")
271: + "\n"
272: + language
273: .getString("Tests.Test.Failure.Expected")
274: + ": "
275: + getSliderMin()
276: + "\n"
277: + language
278: .getString("Tests.Test.Failure.Detected")
279: + ": " + slider.getMinimum());
280:
281: return false;
282: }
283:
284: //let's check whether slider has correct upper bound
285: if (!(slider.getMaximum() == getSliderMax())) {
286: setEvaluationMessage(language
287: .getString("Tests.JSliderTest.Failure.WrongUpperBound")
288: + "\n"
289: + language
290: .getString("Tests.Test.Failure.Expected")
291: + ": "
292: + getSliderMax()
293: + "\n"
294: + language
295: .getString("Tests.Test.Failure.Detected")
296: + ": " + slider.getMaximum());
297:
298: return false;
299: }
300: } else {
301: return false;
302: }
303:
304: return true;
305: }
306:
307: /**
308: * @see jacareto.test.JComponentTest#correct(java.awt.Component)
309: */
310: protected void correct(Component component) {
311: super .correct(component);
312:
313: JSlider slider = (JSlider) component;
314:
315: //check if the target value is less or more than the sliders min or max
316: if ((getSliderValue()) < slider.getMinimum()) {
317: appendToEvaluationMessage("\n"
318: + getElementName()
319: + ": "
320: + language
321: .getString("Tests.JSliderTest.Correct.MinimumFailure"));
322: slider.setValue(slider.getMinimum());
323: } else if (getSliderValue() > slider.getMaximum()) {
324: appendToEvaluationMessage("\n"
325: + getElementName()
326: + ": "
327: + language
328: .getString("Tests.JSliderTest.Correct.MaximumFailure"));
329: slider.setValue(slider.getMaximum());
330: } else {
331: slider.setValue(getSliderValue());
332: appendToEvaluationMessage("\n"
333: + getElementName()
334: + ": "
335: + language
336: .getString("Tests.JSliderTest.Correct.Value"));
337: }
338:
339: if (getSliderMin() <= getSliderMax()) {
340: slider.setMinimum(getSliderMin());
341: slider.setMaximum(getSliderMax());
342: appendToEvaluationMessage("\n"
343: + getElementName()
344: + ": "
345: + language
346: .getString("Tests.JSliderTest.Correct.MinMax"));
347: } else {
348: appendToEvaluationMessage("\n"
349: + getElementName()
350: + ": "
351: + language
352: .getString("Tests.JSliderTest.Correct.MinMaxFailure"));
353: }
354: }
355: }
|