source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/nodeequality/GUIEventTaskComparisonRule.java @ 1113

Last change on this file since 1113 was 1113, checked in by pharms, 11 years ago
  • added license statement
File size: 14.0 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.tasktrees.nodeequality;
16
17import de.ugoe.cs.autoquest.eventcore.IEventTarget;
18import de.ugoe.cs.autoquest.eventcore.gui.IInteraction;
19import de.ugoe.cs.autoquest.eventcore.gui.MouseClick;
20import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick;
21import de.ugoe.cs.autoquest.eventcore.gui.MouseDragAndDrop;
22import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
23import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection;
24import de.ugoe.cs.autoquest.eventcore.guimodel.IButton;
25import de.ugoe.cs.autoquest.eventcore.guimodel.ICheckBox;
26import de.ugoe.cs.autoquest.eventcore.guimodel.IComboBox;
27import de.ugoe.cs.autoquest.eventcore.guimodel.IImage;
28import de.ugoe.cs.autoquest.eventcore.guimodel.IListBox;
29import de.ugoe.cs.autoquest.eventcore.guimodel.IMenuButton;
30import de.ugoe.cs.autoquest.eventcore.guimodel.IRadioButton;
31import de.ugoe.cs.autoquest.eventcore.guimodel.IShape;
32import de.ugoe.cs.autoquest.eventcore.guimodel.IText;
33import de.ugoe.cs.autoquest.eventcore.guimodel.IToolTip;
34import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
35import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
36
37/**
38 * <p>
39 * This rule compares GUI event tasks (i.e. it is more concrete, than the
40 * {@link EventTaskComparisonRule}). Two GUI event tasks are only equal if their event type and
41 * target are equal. The returned equality is even more fine-grained for events whose type is
42 * {@link TextInput} and {@link ValueSelection}. For text inputs, lexical equality is returned if
43 * the same text is entered using the same key interactions. Syntactical equality is returned if
44 * the same text is entered using different key interactions. Semantical equality is returned if
45 * different text is entered, but into the same event target. Value selections are syntactically
46 * equal, if the same value is selected. Otherwise they are semantically equal.
47 * </p>
48 *
49 * @author Patrick Harms
50 */
51public class GUIEventTaskComparisonRule implements NodeComparisonRule {
52   
53    /*
54     * (non-Javadoc)
55     *
56     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
57     */
58    @Override
59    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
60        if ((!(node1 instanceof IEventTask)) || (!(node2 instanceof IEventTask))) {
61            return null;
62        }
63       
64        IEventTask task1 = (IEventTask) node1;
65        IEventTask task2 = (IEventTask) node2;
66       
67        if ((!(task1.getEventType() instanceof IInteraction)) ||
68            (!(task2.getEventType() instanceof IInteraction)))
69        {
70            return null;
71        }
72
73        if (node1 == node2) {
74            return NodeEquality.IDENTICAL;
75        }
76
77        if (!task1.getEventTarget().equals(task2.getEventTarget())) {
78            return NodeEquality.UNEQUAL;
79        }
80       
81        IInteraction interaction1 = (IInteraction) task1.getEventType();
82        IInteraction interaction2 = (IInteraction) task2.getEventType();
83       
84        return compareInteractions(interaction1, interaction2, task1.getEventTarget());
85    }
86
87    /**
88     * <p>
89     * compares two interactions. The method delegates to other, more specific compare method, e.g.,
90     * {@link #compareTextInputs(TextInput, TextInput)} and
91     * {@link #compareValueSelections(ValueSelection, ValueSelection)}, if any exist for the
92     * concrete interaction types. Otherwise it uses the equals method of the interactions for
93     * comparison. In this case, if the interactions equals method returns true, this method
94     * returns lexical equality.
95     * </p>
96     *
97     * @param interaction1 the first interaction to compare
98     * @param interaction2 the second interaction to compare
99     * @param eventTarget  the event target on which the interactions happened (used within
100     *                     special comparisons like mouse clicks on buttons, where the coordinates
101     *                     can be ignored)
102     *
103     * @return as described
104     */
105    private NodeEquality compareInteractions(IInteraction interaction1,
106                                             IInteraction interaction2,
107                                             IEventTarget eventTarget)
108    {
109        if (interaction1 == interaction2) {
110            return NodeEquality.LEXICALLY_EQUAL;
111        }
112        else if ((interaction1 instanceof TextInput) && (interaction2 instanceof TextInput)) {
113            return compareTextInputs((TextInput) interaction1, (TextInput) interaction2);
114        }
115        else if ((interaction1 instanceof ValueSelection) &&
116                 (interaction2 instanceof ValueSelection))
117        {
118            return compareValueSelections
119                ((ValueSelection<?>) interaction1, (ValueSelection<?>) interaction2);
120        }
121        else if ((interaction1 instanceof MouseClick) &&
122                 (interaction2 instanceof MouseClick))
123        {
124            return compareMouseClicks
125                ((MouseClick) interaction1, (MouseClick) interaction2, eventTarget);
126        }
127        else if ((interaction1 instanceof MouseDoubleClick) &&
128                 (interaction2 instanceof MouseDoubleClick))
129        {
130            return compareMouseDoubleClicks
131                ((MouseDoubleClick) interaction1, (MouseDoubleClick) interaction2, eventTarget);
132        }
133        else if ((interaction1 instanceof MouseDragAndDrop) &&
134                 (interaction2 instanceof MouseDragAndDrop))
135        {
136            return compareMouseDragAndDrops
137                ((MouseDragAndDrop) interaction1, (MouseDragAndDrop) interaction2);
138        }
139        else if (interaction1.equals(interaction2)) {
140            return NodeEquality.LEXICALLY_EQUAL;
141        }
142        else {
143            return NodeEquality.UNEQUAL;
144        }
145    }
146
147    /**
148     * <p>
149     * compares two text inputs. If both text inputs have the same entered text and text input
150     * events, they are lexically equal. If they only have the same entered text, they are
151     * syntactically equal. If they are only both text inputs, they are semantically equal.
152     * (the equality of the event targets is checked beforehand).
153     * </p>
154     *
155     * @param interaction1 the first text input to compare
156     * @param interaction2 the second text input to compare
157     *
158     * @return as described
159     */
160    private NodeEquality compareTextInputs(TextInput interaction1, TextInput interaction2) {
161        if (interaction1.getEnteredText().equals(interaction2.getEnteredText())) {
162            if (interaction1.getTextInputEvents().equals(interaction2.getTextInputEvents())) {
163                return NodeEquality.LEXICALLY_EQUAL;
164            }
165            else {
166                return NodeEquality.SYNTACTICALLY_EQUAL;
167            }
168        }
169        else {
170            return NodeEquality.SEMANTICALLY_EQUAL;
171        }
172    }
173
174    /**
175     * <p>
176     * compares two value selections. If both value selections have the same selected value, they
177     * are syntactically equal, otherwise they are semantically equal.
178     * (the equality of the event targets is checked beforehand).
179     * </p>
180     *
181     * @param interaction1 the first value selection to compare
182     * @param interaction2 the second value selection to compare
183     *
184     * @return as described
185     */
186    private NodeEquality compareValueSelections(ValueSelection<?> interaction1,
187                                                ValueSelection<?> interaction2)
188    {
189        Object value1 = interaction1.getSelectedValue();
190        Object value2 = interaction2.getSelectedValue();
191       
192        if ((value1 == value2) || ((value1 != null) && (value1.equals(value2)))) {
193            return NodeEquality.LEXICALLY_EQUAL;
194        }
195        else {
196            return NodeEquality.SEMANTICALLY_EQUAL;
197        }
198    }
199
200    /**
201     * <p>
202     * compares two mouse clicks. If both clicks have the same coordinates, they are lexically
203     * equal. Otherwise, they are semantically equal. Mouse clicks for which the coordinates make
204     * no lexical difference (see {@link #clickCoordinatesMakeLexicalDifference(IEventTarget)})
205     * are treated as lexically equal.
206     * </p>
207     *
208     * @param interaction1 the first mouse click to compare
209     * @param interaction2 the second mouse click to compare
210     * @param eventTarget  the event target on which the interactions happened (used within
211     *                     special comparisons like mouse clicks on buttons, where the coordinates
212     *                     can be ignored)
213     *
214     * @return as described
215     */
216    private NodeEquality compareMouseClicks(MouseClick   interaction1,
217                                            MouseClick   interaction2,
218                                            IEventTarget eventTarget)
219    {
220        if (interaction1.getButton() != interaction2.getButton()) {
221            return NodeEquality.UNEQUAL;
222        }
223       
224        if (!clickCoordinatesMakeLexicalDifference(eventTarget)) {
225            return NodeEquality.LEXICALLY_EQUAL;
226        }
227       
228        int x1 = interaction1.getX();
229        int x2 = interaction2.getX();
230        int y1 = interaction1.getY();
231        int y2 = interaction2.getY();
232       
233        if ((x1 == x2) && (y1 == y2)) {
234            return NodeEquality.LEXICALLY_EQUAL;
235        }
236        else {
237            return NodeEquality.SEMANTICALLY_EQUAL;
238        }
239    }
240
241    /**
242     * <p>
243     * compares two mouse double clicks. If both double clicks have the same coordinates, they are
244     * lexically equal. Otherwise, they are semantically equal. Double clicks for which the
245     * coordinates make no lexical difference
246     * (see {@link #clickCoordinatesMakeLexicalDifference(IEventTarget)}) are treated as lexically
247     * equal.
248     * </p>
249     *
250     * @param interaction1 the first mouse double click to compare
251     * @param interaction2 the second mouse double click to compare
252     * @param eventTarget  the event target on which the interactions happened (used within
253     *                     special comparisons like mouse clicks on buttons, where the coordinates
254     *                     can be ignored)
255     *
256     * @return as described
257     */
258    private NodeEquality compareMouseDoubleClicks(MouseDoubleClick interaction1,
259                                                  MouseDoubleClick interaction2,
260                                                  IEventTarget     eventTarget)
261    {
262        if (interaction1.getButton() != interaction2.getButton()) {
263            return NodeEquality.UNEQUAL;
264        }
265       
266        if (!clickCoordinatesMakeLexicalDifference(eventTarget)) {
267            return NodeEquality.LEXICALLY_EQUAL;
268        }
269       
270        int x1 = interaction1.getX();
271        int x2 = interaction2.getX();
272        int y1 = interaction1.getY();
273        int y2 = interaction2.getY();
274       
275        if ((x1 == x2) && (y1 == y2)) {
276            return NodeEquality.LEXICALLY_EQUAL;
277        }
278        else {
279            return NodeEquality.SEMANTICALLY_EQUAL;
280        }
281    }
282
283    /**
284     * <p>
285     * compares two mouse drag and drops. If both drag and drops have the same start and end
286     * coordinates, they are lexically equal. Otherwise, they are semantically equal.
287     * </p>
288     *
289     * @param interaction1 the first mouse drag and drop to compare
290     * @param interaction2 the second mouse drag and drop to compare
291     *
292     * @return as described
293     */
294    private NodeEquality compareMouseDragAndDrops(MouseDragAndDrop interaction1,
295                                                  MouseDragAndDrop interaction2)
296    {
297        if (interaction1.getButton() != interaction2.getButton()) {
298            return NodeEquality.UNEQUAL;
299        }
300       
301        int x1 = interaction1.getX();
302        int x1Start = interaction1.getXStart();
303        int x2 = interaction2.getX();
304        int x2Start = interaction2.getXStart();
305        int y1 = interaction1.getY();
306        int y1Start = interaction1.getYStart();
307        int y2 = interaction2.getY();
308        int y2Start = interaction2.getYStart();
309       
310        if ((x1Start == x2Start) && (x1 == x2) && (y1Start == y2Start) && (y1 == y2)) {
311            return NodeEquality.LEXICALLY_EQUAL;
312        }
313        else {
314            return NodeEquality.SEMANTICALLY_EQUAL;
315        }
316    }
317
318    /**
319     * <p>
320     * Checks, if the coordinates of a click or double click on the provided event target makes
321     * a lexical difference. Mouse clicks and double clicks on buttons, check boxes,
322     * combo boxes, images, list boxes, menu buttons, radio buttons, shapes, uneditable text,
323     * and tool tips have no lexical difference as long as they happen on the same event target.
324     * The concrete coordinates are not relevant.
325     * </p>
326     *
327     * @param eventTarget the event target on which the interaction occurred
328     *
329     * @return if the coordinates are important to be considered for clicks and double clicks,
330     *         false else
331     */
332    private boolean clickCoordinatesMakeLexicalDifference(IEventTarget eventTarget) {
333        if ((eventTarget instanceof IButton) ||
334            (eventTarget instanceof ICheckBox) ||
335            (eventTarget instanceof IComboBox) ||
336            (eventTarget instanceof IImage) ||
337            (eventTarget instanceof IListBox) ||
338            (eventTarget instanceof IMenuButton) ||
339            (eventTarget instanceof IRadioButton) ||
340            (eventTarget instanceof IShape) ||
341            (eventTarget instanceof IText) ||
342            (eventTarget instanceof IToolTip))
343        {
344            return false;
345        }
346        else {
347            return true;
348        }
349    }
350
351}
Note: See TracBrowser for help on using the repository browser.