KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > testapp > interactive > RandomClick


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.testapp.interactive;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.List JavaDoc;
37
38 import nextapp.echo2.app.ApplicationInstance;
39 import nextapp.echo2.app.Component;
40 import nextapp.echo2.app.Window;
41 import nextapp.echo2.app.button.AbstractButton;
42
43 /**
44  * Provides capability to randomly click any on-screen button.
45  */

46 public class RandomClick {
47     
48     /**
49      * A <code>Collection</code> containing the text of buttons which should
50      * not be clicked.
51      */

52     private static final Collection JavaDoc BUTTON_BLACKLIST;
53     static {
54         Collection JavaDoc blacklist = new HashSet JavaDoc();
55         
56         // Ghost test is also protected using other means, but no reason to bother with it.
57
blacklist.add("Push (Ghost Test)");
58         
59         // Exception test deliberately throws exceptions...not what we're looking for.
60
blacklist.add("Exception");
61         
62         // Don't recursively start random click test.
63
blacklist.add("Random Click");
64         
65         // Delay test skews ghost-test based performance test results.
66
blacklist.add("Delay");
67         
68         // Contains buttons to deliberately throw exceptions and invalidate sessions. Not good.
69
blacklist.add("Client Configuration");
70         
71         // Client exceptions are bad too.
72
blacklist.add("Client Exception");
73
74         // Command test might do a redirect, killing the ghost test.
75
blacklist.add("Command");
76         
77         // Demo visitors might think the application broke if the style sheet gets set to null.
78
blacklist.add("No Style Sheet");
79         
80         // Image test skews ghost-test based performance test results (AWTImageReference).
81
blacklist.add("Image");
82         
83         // Text Sync has delay buttons.
84
blacklist.add("Text Sync");
85         
86         // Do not add modal windows.
87
blacklist.add("Add Modal Window");
88         blacklist.add("Add Three Modal Windows");
89         blacklist.add("Add \"Modal Launching\" Component Sampler to Embedded ContentPane");
90         
91         BUTTON_BLACKLIST = Collections.unmodifiableCollection(blacklist);
92     }
93     
94     /**
95      * Retrieves all buttons currently displayed in the user-interface and
96      * programmatically clicks one.
97      */

98     public static void clickRandomButton() {
99         Window window = ApplicationInstance.getActive().getDefaultWindow();
100         List JavaDoc buttonList = new ArrayList JavaDoc();
101         findButtons(buttonList, window);
102         AbstractButton button = (AbstractButton) buttonList.get((int) (buttonList.size() * Math.random()));
103         button.doAction();
104     }
105     
106     /**
107      * Recursively finds <code>Button</code>s in the hierarchy whose parent
108      * is <code>component</code> and adds them to the
109      * <code>foundButtons</code> collection.
110      *
111      * @param foundButtons the <code>Collection</code> to which
112      * <code>Button</code>s will be added
113      * @param component the root <code>Component</code> of the hierarchy to
114      * search
115      */

116     private static void findButtons(Collection JavaDoc foundButtons, Component component) {
117         if (component instanceof AbstractButton && !BUTTON_BLACKLIST.contains(((AbstractButton) component).getText())) {
118             foundButtons.add(component);
119         }
120         Component[] children = component.getComponents();
121         for (int i = 0; i < children.length; ++i) {
122             findButtons(foundButtons, children[i]);
123         }
124     }
125     
126     /** Non-instantiable class. */
127     private RandomClick() { }
128 }
129
Popular Tags