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

Last change on this file since 1059 was 1059, checked in by fglaser, 11 years ago
  • Further works on NewHTMLLogParser to parse testtrace without errors
  • Test for NewHTMLLogParser added
  • dummy mapping for html added that contains unmapped elements
  • Property svn:mime-type set to text/plain
File size: 4.5 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.Scroll;
26import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
27import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
28import de.ugoe.cs.autoquest.eventcore.guimodel.ITextArea;
29import de.ugoe.cs.autoquest.eventcore.guimodel.ITextField;
30import de.ugoe.cs.util.console.Console;
31
32/**
33 * <p>
34 * TODO comment
35 * </p>
36 *
37 * @author Patrick Harms
38 * @author Fabian Glaser
39 */
40public class HTMLEventTypeFactory {
41   
42    /**  */
43    private static HTMLEventTypeFactory instance = new HTMLEventTypeFactory();
44
45    /**
46     * <p>
47     * TODO: comment
48     * </p>
49     *
50     */
51    private HTMLEventTypeFactory() {}
52
53    /**
54     * <p>
55     * TODO: comment
56     * </p>
57     *
58     * @return
59     */
60    public static HTMLEventTypeFactory getInstance() {
61        return instance;
62    }
63
64    /**
65     * <p>
66     * TODO: comment
67     * </p>
68     *
69     * @param eventName
70     * @param eventParameters
71     * @return
72     */
73    public IEventType getEventType(String eventName, Map<String, String> eventParameters,
74                                   IGUIElement guiElement) {
75        IInteraction result = null;
76
77        if ("onscroll".equals(eventName)) {
78            int[] coordinates = getCoordinateParameter(eventName, eventParameters);
79            result = new Scroll(coordinates[0], coordinates[1]);
80        }
81        else if ("onclick".equals(eventName)) {
82            int[] coordinates = getCoordinateParameter(eventName, eventParameters);
83            result =
84                new MouseClick(MouseButtonInteraction.Button.LEFT, coordinates[0], coordinates[1]);
85        }
86        else if ("onchange".equals(eventName)) {
87            String value = eventParameters.get("selectedValue");
88           
89            if ((guiElement instanceof ITextArea) || (guiElement instanceof ITextField)) {
90                result = new TextInput(value, null);
91            }
92            else {
93                throw new IllegalArgumentException("can not handle onchange events on GUI " +
94                                                   "elements of type " + guiElement.getClass());
95            }
96        }
97        else if ("onfocus".equals(eventName)) {
98            result = new KeyboardFocusChange();
99        }
100        else if ("onunload".equals(eventName) || "onbeforeunload".equals(eventName) ||
101            "onpagehide".equals(eventName) || "onpageshow".equals(eventName))
102        {
103            Console.traceln(Level.FINE, "Ignored event name \"" + eventName + "\"");
104        }
105        else {
106            throw new IllegalArgumentException("unknown event name: \"" + eventName + "\"");
107        }
108        return result;
109    }
110
111    /**
112     * <p>
113     * TODO: comment
114     * </p>
115     *
116     * @param eventName
117     * @param eventParameters
118     * @return
119     */
120    private int[] getCoordinateParameter(String eventName, Map<String, String> eventParameters) {
121        String xCoord = eventParameters.get("X");
122        if (xCoord == null) {
123            throw new IllegalArgumentException("eventParameters do not contain X coordinate.");
124        }
125
126        String yCoord = eventParameters.get("Y");
127        if (yCoord == null) {
128            throw new IllegalArgumentException("eventParameters do not contain Y coordinate.");
129        }
130
131        try {
132            return new int[]
133                { Integer.parseInt(xCoord), Integer.parseInt(yCoord) };
134        }
135        catch (NumberFormatException e) {
136            throw new IllegalArgumentException("the coordinates provided" + " of an " + eventName +
137                " event are no numbers");
138        }
139    }
140   
141   
142   
143}
Note: See TracBrowser for help on using the repository browser.