source: trunk/autoquest-plugin-html/src/main/java/de/ugoe/cs/autoquest/plugin/html/eventcore/HTMLEventTypeFactory.java @ 1239

Last change on this file since 1239 was 1239, checked in by pharms, 11 years ago
  • made parsing of logfiles with missing click coordinates more lenient
  • Property svn:mime-type set to text/plain
File size: 5.7 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.plugin.html.eventcore;
16
17import java.util.Map;
18import java.util.logging.Level;
19
20import de.ugoe.cs.autoquest.eventcore.IEventType;
21import de.ugoe.cs.autoquest.eventcore.gui.IInteraction;
22import de.ugoe.cs.autoquest.eventcore.gui.KeyboardFocusChange;
23import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction;
24import de.ugoe.cs.autoquest.eventcore.gui.MouseClick;
25import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick;
26import de.ugoe.cs.autoquest.eventcore.gui.Scroll;
27import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
28import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection;
29import de.ugoe.cs.autoquest.eventcore.guimodel.ICheckBox;
30import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
31import de.ugoe.cs.autoquest.eventcore.guimodel.IRadioButton;
32import de.ugoe.cs.autoquest.eventcore.guimodel.ITextArea;
33import de.ugoe.cs.autoquest.eventcore.guimodel.ITextField;
34import de.ugoe.cs.util.console.Console;
35
36/**
37 * <p>
38 * TODO comment
39 * </p>
40 *
41 * @author Patrick Harms
42 * @author Fabian Glaser
43 */
44public class HTMLEventTypeFactory {
45   
46    /**  */
47    private static HTMLEventTypeFactory instance = new HTMLEventTypeFactory();
48
49    /**
50     * <p>
51     * TODO: comment
52     * </p>
53     *
54     */
55    private HTMLEventTypeFactory() {}
56
57    /**
58     * <p>
59     * TODO: comment
60     * </p>
61     *
62     * @return
63     */
64    public static HTMLEventTypeFactory getInstance() {
65        return instance;
66    }
67
68    /**
69     * <p>
70     * TODO: comment
71     * </p>
72     *
73     * @param eventName
74     * @param eventParameters
75     * @return
76     */
77    public IEventType getEventType(String eventName, Map<String, String> eventParameters,
78                                   IGUIElement guiElement) {
79        IInteraction result = null;
80
81        if ("onscroll".equals(eventName)) {
82            int[] coordinates =
83                getCoordinateParameter("scrollX", "scrollY", eventName, eventParameters);
84           
85            result = new Scroll(coordinates[0], coordinates[1]);
86        }
87        else if ("onclick".equals(eventName)) {
88            int[] coordinates = getCoordinateParameter("X", "Y", eventName, eventParameters);
89            result =
90                new MouseClick(MouseButtonInteraction.Button.LEFT, coordinates[0], coordinates[1]);
91        }
92        else if ("ondblclick".equals(eventName)) {
93            int[] coordinates = getCoordinateParameter("X", "Y", eventName, eventParameters);
94            result = new MouseDoubleClick
95                (MouseButtonInteraction.Button.LEFT, coordinates[0], coordinates[1]);
96        }
97        else if ("onchange".equals(eventName)) {
98            String value = eventParameters.get("selectedValue");
99           
100            if ((guiElement instanceof ITextArea) || (guiElement instanceof ITextField)) {
101                result = new TextInput(value, null);
102            }
103            else if ((guiElement instanceof IRadioButton) || (guiElement instanceof ICheckBox)) {
104                result = new ValueSelection<String>(value);
105            }
106            else {
107                throw new IllegalArgumentException("can not handle onchange events on GUI " +
108                                                   "elements of type " + guiElement.getClass());
109            }
110        }
111        else if ("onfocus".equals(eventName)) {
112            result = new KeyboardFocusChange();
113        }
114        else if ("onunload".equals(eventName) || "onbeforeunload".equals(eventName) ||
115                 "onpagehide".equals(eventName) || "onpageshow".equals(eventName) ||
116                 "onsubmit".equals(eventName) || "onselect".equals(eventName) ||
117                 "onreset".equals(eventName))
118        {
119            Console.traceln(Level.FINE, "Ignored event name \"" + eventName + "\"");
120        }
121        else {
122            throw new IllegalArgumentException("unknown event name: \"" + eventName + "\"");
123        }
124        return result;
125    }
126
127    /**
128     * <p>
129     * TODO: comment
130     * </p>
131     *
132     * @param eventName
133     * @param eventParameters
134     * @return
135     */
136    private int[] getCoordinateParameter(String              xParamName,
137                                         String              yParamName,
138                                         String              eventName,
139                                         Map<String, String> eventParameters)
140    {
141        String xCoord = eventParameters.get(xParamName);
142        if (xCoord == null) {
143            Console.printerrln("eventParameters do not contain " + xParamName + " coordinate.");
144            xCoord = "0";
145        }
146
147        String yCoord = eventParameters.get(yParamName);
148        if (yCoord == null) {
149            Console.printerrln("eventParameters do not contain " + yParamName + " coordinate.");
150            yCoord = "0";
151        }
152
153        try {
154            return new int[]
155                { Integer.parseInt(xCoord), Integer.parseInt(yCoord) };
156        }
157        catch (NumberFormatException e) {
158            throw new IllegalArgumentException("the coordinates provided by an " + eventName +
159                " event are no numbers");
160        }
161    }
162   
163   
164   
165}
Note: See TracBrowser for help on using the repository browser.