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.JPopupMenu;
034:
035: /**
036: * Test class for testing <code>java.swing.JPopupMenu</code> components. The following criteria
037: * are tested by this class:
038: *
039: * <ul>
040: * <li>
041: * <b>is visible</b> - testes if the popup menu is visible
042: * </li>
043: * <li>
044: * <b>element count</b> - testes if the number of elements in the tested popup menu is correct
045: * </li>
046: * <li>
047: * <b>menu label</b> - testes if the label text of the tested popup menu is correct
048: * </li>
049: * </ul>
050: *
051: *
052: * @author uak, Christoph Sauer
053: */
054: public class JPopupMenuTest extends JComponentTest {
055: /** Contains whether if the tested component is expected to be visible */
056: private boolean isVisible;
057:
058: /** Contains the expected popup menu label */
059: private String menuLabel;
060:
061: /** Contains the number of expected elements in the popup menu */
062: private int menuElementCount;
063:
064: /**
065: * Creates a new test with the specified values.
066: *
067: * @param env env the environment
068: * @param componentName the name of the component
069: * @param isIgnoring if the test result should be ignored
070: * @param isCorrecting if the values of the component should be corrected when the test has
071: * failed
072: * @param hasFocus if the component has the focus
073: * @param isEnabled if the component is set enabled
074: * @param visible DOCUMENT ME!
075: * @param label DOCUMENT ME!
076: * @param elemCount DOCUMENT ME!
077: */
078: public JPopupMenuTest(Environment env, String componentName,
079: boolean isIgnoring, boolean isCorrecting, boolean hasFocus,
080: boolean isEnabled, boolean visible, String label,
081: int elemCount) {
082: super (env, componentName, isIgnoring, isCorrecting, hasFocus,
083: isEnabled);
084: setMenuLabel(label);
085: setVisible(visible);
086: setMenuElementCount(elemCount);
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 <code>javax.swing.JPopupMenu</code>.
095: */
096: public JPopupMenuTest(Environment env, Components components,
097: Component component) {
098: super (env, components, component);
099: setMenuLabel(((JPopupMenu) component).getLabel());
100: setVisible(((JPopupMenu) component).isVisible());
101: setMenuElementCount(((JPopupMenu) component)
102: .getComponentCount());
103: }
104:
105: /**
106: * Creates a new test with the values of the given component and default values.
107: */
108: public JPopupMenuTest() {
109: this (null, "", false, false, false, true, false, "", 0);
110: }
111:
112: /**
113: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
114: */
115: public boolean evaluate(Components components) {
116: JPopupMenu popupMenu;
117: setLastIgnored(isIgnoring());
118: setLastResult(false);
119: setLastCorrected(false);
120:
121: // get the pane with the componentName
122: Component component = components
123: .getComponent(getComponentName());
124:
125: if (component == null) {
126: setEvaluationMessage(language
127: .getString("Tests.Test.Failure.NoComponent"));
128:
129: return false;
130: }
131:
132: if (!(component instanceof JPopupMenu)) {
133: setEvaluationMessage(language
134: .getString("Tests.Test.Failure.WrongComponentType"));
135:
136: return false;
137: }
138:
139: popupMenu = (JPopupMenu) component;
140:
141: if (doTest(popupMenu)) {
142: boolean result = true;
143: Iterator childIter = childrenIterator();
144:
145: while (childIter.hasNext() && result) {
146: result &= evaluateChild((Test) childIter.next(),
147: components);
148: }
149:
150: setLastResult(result);
151:
152: return result;
153: }
154:
155: // correct the menu items if wanted
156: if (isCorrecting()) {
157: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
158: correct(popupMenu);
159: setLastCorrected(true);
160: }
161:
162: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
163: return false;
164: }
165:
166: /**
167: * @see jacareto.test.JComponentTest#doTest(java.awt.Component)
168: */
169: protected boolean doTest(Component component) {
170: if (super .doTest(component)) {
171: // Has the menu the correct label?
172: JPopupMenu popupMenu = (JPopupMenu) component;
173:
174: if ((popupMenu.getLabel() == null)
175: && (getMenuLabel() != null)) {
176: setEvaluationMessage(language
177: .getString("Tests.JPopupMenuTest.Failure.WrongLabel")
178: + "\n"
179: + language
180: .getString("Tests.Test.Failure.Expected")
181: + ": "
182: + "a label"
183: + "\n"
184: + language
185: .getString("Tests.Test.Failure.Detected")
186: + ": " + "no label");
187:
188: return false;
189: } else if ((popupMenu.getLabel() != null)
190: && (getMenuLabel() == null)) {
191: setEvaluationMessage(language
192: .getString("Tests.JPopupMenuTest.Failure.WrongLabel")
193: + "\n"
194: + language
195: .getString("Tests.Test.Failure.Expected")
196: + ": "
197: + "no label"
198: + "\n"
199: + language
200: .getString("Tests.Test.Failure.Detected")
201: + ": " + "a label");
202:
203: return false;
204: } else if ((popupMenu.getLabel() != null)
205: && !popupMenu.getLabel().equals(getMenuLabel())) {
206: setEvaluationMessage(language
207: .getString("Tests.JPopupMenuTest.Failure.WrongLabel")
208: + "\n"
209: + language
210: .getString("Tests.Test.Failure.Expected")
211: + ": "
212: + getMenuLabel()
213: + "\n"
214: + language
215: .getString("Tests.Test.Failure.Detected")
216: + ": " + popupMenu.getLabel());
217:
218: return false;
219: }
220:
221: // Is the menu visible at present?
222: if (popupMenu.isVisible() != isVisible()) {
223: setEvaluationMessage(language
224: .getString("Tests.JPopupMenuTest.Failure.WrongPopupVisibility")
225: + "\n"
226: + language
227: .getString("Tests.Test.Failure.Expected")
228: + ": "
229: + isVisible()
230: + "\n"
231: + language
232: .getString("Tests.Test.Failure.Detected")
233: + ": " + popupMenu.isVisible());
234:
235: return false;
236: }
237:
238: //has the menu the correct number of elements?
239: if (popupMenu.getComponentCount() != getMenuElementCount()) {
240: setEvaluationMessage(language
241: .getString("Tests.JPopupMenuTest.Failure.WrongElementCount")
242: + "\n"
243: + language
244: .getString("Tests.Test.Failure.Expected")
245: + ": "
246: + getMenuElementCount()
247: + "\n"
248: + language
249: .getString("Tests.Test.Failure.Detected")
250: + ": " + popupMenu.getComponentCount());
251:
252: return false;
253: }
254: } else {
255: return false;
256: }
257:
258: return true;
259: }
260:
261: /**
262: * @see jacareto.test.JComponentTest#correct(java.awt.Component)
263: */
264: protected void correct(Component component) {
265: super .correct(component);
266:
267: JPopupMenu popupMenu = (JPopupMenu) component;
268:
269: // set label
270: popupMenu.setLabel(getMenuLabel());
271: appendToEvaluationMessage("\n" + getElementName() + ": "
272: + language.getString("Tests.JPopupMenu.Correct.Label"));
273:
274: // set visibility
275: popupMenu.setVisible(isVisible);
276: appendToEvaluationMessage("\n"
277: + getElementName()
278: + ": "
279: + language
280: .getString("Tests.JPopupMenu.Correct.Visible"));
281:
282: // number of menu elements
283: if (popupMenu.getComponentCount() != getMenuElementCount()) {
284: appendToEvaluationMessage("\n"
285: + getElementName()
286: + ": "
287: + language
288: .getString("Tests.JPopupMenu.Correct.MenuElementCountFailure"));
289: }
290: }
291:
292: /**
293: * @see jacareto.struct.StructureElement#getElementName()
294: */
295: public String getElementName() {
296: return language.getString("Tests.JPopupMenuTest.Name");
297: }
298:
299: /**
300: * @see jacareto.struct.StructureElement#getElementDescription()
301: */
302: public String getElementDescription() {
303: return language.getString("Tests.JPopupMenuTest.Description");
304: }
305:
306: /**
307: * @see jacareto.struct.StructureElement#toShortString()
308: */
309: public String toShortString() {
310: return getElementName();
311: }
312:
313: /**
314: * DOCUMENT ME!
315: *
316: * @return <code>true</code> if the tested component is expected to be visible, if not
317: * <code>false</code>.
318: */
319: public boolean isVisible() {
320: return isVisible;
321: }
322:
323: /**
324: * DOCUMENT ME!
325: *
326: * @param b if the tested component is expected to be visible
327: */
328: public void setVisible(boolean b) {
329: isVisible = b;
330: }
331:
332: /**
333: * DOCUMENT ME!
334: *
335: * @return The number of expected elements in the popup menu
336: */
337: public int getMenuElementCount() {
338: return menuElementCount;
339: }
340:
341: /**
342: * DOCUMENT ME!
343: *
344: * @param i the number of expected elements in the popup menu
345: */
346: public void setMenuElementCount(int i) {
347: menuElementCount = i;
348: }
349:
350: /**
351: * DOCUMENT ME!
352: *
353: * @return The expected popup menu label
354: */
355: public String getMenuLabel() {
356: return menuLabel;
357: }
358:
359: /**
360: * DOCUMENT ME!
361: *
362: * @param string the expected popup menu label
363: */
364: public void setMenuLabel(String string) {
365: menuLabel = string;
366: }
367: }
|