Synthia
Generic and flexible data structure generator
WidgetAction.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package examples.gui;
20 
21 import java.awt.AWTException;
22 import java.awt.Robot;
23 
24 import javax.swing.AbstractButton;
25 import javax.swing.KeyStroke;
26 import javax.swing.text.JTextComponent;
27 
28 import ca.uqac.lif.synthia.Picker;
29 import ca.uqac.lif.synthia.test.Action;
30 import ca.uqac.lif.synthia.util.Delay;
31 
32 /**
33  * A {@link Action} that performs a concrete action on a widget.
34  * @author Sylvain Hallé
35  *
36  * @param <T> The type of the widget on which the action can be performed
37  * @ingroup Examples
38  */
39 public abstract class WidgetAction<T> implements Action
40 {
41  /**
42  * A robot that can be used to interact with a GUI.
43  */
44  protected static Robot s_robot;
45 
46  /**
47  * The object on which the action is performed.
48  */
49  protected T m_object;
50 
51  static
52  {
53  try
54  {
55  s_robot = new Robot();
56  }
57  catch (AWTException e)
58  {
59  s_robot = null;
60  }
61  }
62 
63  /**
64  * Creates a new widget action.
65  * @param o The object on which the action is performed
66  */
67  public WidgetAction(T o)
68  {
69  super();
70  m_object = o;
71  }
72 
73  /**
74  * Action that clicks on a specific button when called. This action can be
75  * done on any {@link javax.swing.AbstractButton AbstractButton}, including
76  * standard buttons and checkboxes.
77  * @ingroup API
78  */
79  public static class ClickAction extends WidgetAction<AbstractButton>
80  {
81  /**
82  * Creates a new click action.
83  * @param o The button on which the action is performed
84  */
85  public ClickAction(AbstractButton o)
86  {
87  super(o);
88  }
89 
90  @Override
91  public void doAction()
92  {
93  m_object.doClick();
94  }
95 
96  @Override
97  public String toString()
98  {
99  return m_object.getText();
100  }
101  }
102 
103  /**
104  * Action that types a string inside a textbox. The action simulates
105  * keyboard typing and sends the string one keystroke at a time. A
106  * configurable delay can be set between each keystroke.
107  * @ingroup API
108  */
109  public static class TypeAction extends WidgetAction<JTextComponent>
110  {
111  /**
112  * A picker providing the text that is to be typed.
113  */
114  protected Picker<String> m_text;
115 
116  /**
117  * A picker deciding on the delay (in seconds) to insert between each
118  * keystroke.
119  */
120  protected Picker<Number> m_delay;
121 
122  /**
123  * The duration of a key press on the keyboard (i.e. the time between key
124  * down and key up).
125  */
126  protected static float s_keyDuration = 0.1f;
127 
128  /**
129  * Creates a new type action.
130  * @param jtc The text component in which the text is to be typed
131  * @param text A picker providing the text that is to be typed
132  * @param delay A picker deciding on the delay (in seconds) to insert
133  * between each keystroke
134  */
135  public TypeAction(JTextComponent jtc, Picker<String> text, Picker<Number> delay)
136  {
137  super(jtc);
138  m_text = text;
139  m_delay = delay;
140  }
141 
142  @Override
143  public void doAction()
144  {
145  String text = m_text.pick();
146  m_object.requestFocusInWindow();
147  Delay.wait(m_delay.pick().floatValue());
148  for (int i = 0; i < text.length(); i++)
149  {
150  String c = text.substring(i, i + 1);
151  KeyStroke ks = KeyStroke.getKeyStroke(c);
152  int code = ks.getKeyCode();
153  s_robot.keyPress(code);
154  Delay.wait(0.05f);
155  s_robot.keyRelease(code);
156  }
157  }
158 
159  @Override
160  public String toString()
161  {
162  return "Type \"" + m_text + "\" into " + m_object;
163  }
164  }
165 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
examples.gui.WidgetAction
A Action that performs a concrete action on a widget.
Definition: WidgetAction.java:39
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
examples.gui.WidgetAction.m_object
T m_object
The object on which the action is performed.
Definition: WidgetAction.java:49
examples.gui.WidgetAction.s_robot
static Robot s_robot
A robot that can be used to interact with a GUI.
Definition: WidgetAction.java:44
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.util.Delay
Picker that does not produce any value, but causes the thread to wait for a moment on every call to p...
Definition: Delay.java:28
ca.uqac.lif
ca.uqac.lif.synthia.test.Action.doAction
void doAction()
Performs an action on a component.
ca
ca.uqac.lif.synthia.test.Action
Interface signaling that an object can perform an "action".
Definition: Action.java:27
ca.uqac.lif.synthia.test
Classes that enable Synthia to operate as a fuzz testing tool.
Definition: Action.java:19
examples.gui.WidgetAction.WidgetAction
WidgetAction(T o)
Creates a new widget action.
Definition: WidgetAction.java:67