001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.testapp.interactive;
031:
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Collections;
035: import java.util.HashSet;
036: import java.util.List;
037:
038: import nextapp.echo2.app.ApplicationInstance;
039: import nextapp.echo2.app.Component;
040: import nextapp.echo2.app.Window;
041: import nextapp.echo2.app.button.AbstractButton;
042:
043: /**
044: * Provides capability to randomly click any on-screen button.
045: */
046: public class RandomClick {
047:
048: /**
049: * A <code>Collection</code> containing the text of buttons which should
050: * not be clicked.
051: */
052: private static final Collection BUTTON_BLACKLIST;
053: static {
054: Collection blacklist = new HashSet();
055:
056: // Ghost test is also protected using other means, but no reason to bother with it.
057: blacklist.add("Push (Ghost Test)");
058:
059: // Exception test deliberately throws exceptions...not what we're looking for.
060: blacklist.add("Exception");
061:
062: // Don't recursively start random click test.
063: blacklist.add("Random Click");
064:
065: // Delay test skews ghost-test based performance test results.
066: blacklist.add("Delay");
067:
068: // Contains buttons to deliberately throw exceptions and invalidate sessions. Not good.
069: blacklist.add("Client Configuration");
070:
071: // Client exceptions are bad too.
072: blacklist.add("Client Exception");
073:
074: // Command test might do a redirect, killing the ghost test.
075: blacklist.add("Command");
076:
077: // Demo visitors might think the application broke if the style sheet gets set to null.
078: blacklist.add("No Style Sheet");
079:
080: // Image test skews ghost-test based performance test results (AWTImageReference).
081: blacklist.add("Image");
082:
083: // Text Sync has delay buttons.
084: blacklist.add("Text Sync");
085:
086: // Do not add modal windows.
087: blacklist.add("Add Modal Window");
088: blacklist.add("Add Three Modal Windows");
089: blacklist
090: .add("Add \"Modal Launching\" Component Sampler to Embedded ContentPane");
091:
092: BUTTON_BLACKLIST = Collections
093: .unmodifiableCollection(blacklist);
094: }
095:
096: /**
097: * Retrieves all buttons currently displayed in the user-interface and
098: * programmatically clicks one.
099: */
100: public static void clickRandomButton() {
101: Window window = ApplicationInstance.getActive()
102: .getDefaultWindow();
103: List buttonList = new ArrayList();
104: findButtons(buttonList, window);
105: AbstractButton button = (AbstractButton) buttonList
106: .get((int) (buttonList.size() * Math.random()));
107: button.doAction();
108: }
109:
110: /**
111: * Recursively finds <code>Button</code>s in the hierarchy whose parent
112: * is <code>component</code> and adds them to the
113: * <code>foundButtons</code> collection.
114: *
115: * @param foundButtons the <code>Collection</code> to which
116: * <code>Button</code>s will be added
117: * @param component the root <code>Component</code> of the hierarchy to
118: * search
119: */
120: private static void findButtons(Collection foundButtons,
121: Component component) {
122: if (component instanceof AbstractButton
123: && !BUTTON_BLACKLIST
124: .contains(((AbstractButton) component)
125: .getText())) {
126: foundButtons.add(component);
127: }
128: Component[] children = component.getComponents();
129: for (int i = 0; i < children.length; ++i) {
130: findButtons(foundButtons, children[i]);
131: }
132: }
133:
134: /** Non-instantiable class. */
135: private RandomClick() {
136: }
137: }
|