[1783] | 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 |
|
---|
[1782] | 15 | package de.ugoe.cs.autoquest.plugin.android;
|
---|
| 16 |
|
---|
[1783] | 17 | import java.io.File;
|
---|
| 18 | import java.io.FileInputStream;
|
---|
| 19 | import java.io.FileNotFoundException;
|
---|
| 20 | import java.io.IOException;
|
---|
| 21 | import java.io.InputStreamReader;
|
---|
| 22 | import java.io.UnsupportedEncodingException;
|
---|
| 23 | import java.util.Collection;
|
---|
| 24 | import java.util.HashMap;
|
---|
| 25 | import java.util.LinkedList;
|
---|
| 26 | import java.util.List;
|
---|
| 27 | import java.util.Map;
|
---|
[1803] | 28 | import java.util.logging.Level;
|
---|
[1782] | 29 |
|
---|
[1783] | 30 | import javax.xml.parsers.ParserConfigurationException;
|
---|
| 31 | import javax.xml.parsers.SAXParser;
|
---|
| 32 | import javax.xml.parsers.SAXParserFactory;
|
---|
| 33 |
|
---|
| 34 | import org.xml.sax.Attributes;
|
---|
| 35 | import org.xml.sax.InputSource;
|
---|
| 36 | import org.xml.sax.SAXException;
|
---|
| 37 | import org.xml.sax.SAXParseException;
|
---|
| 38 | import org.xml.sax.helpers.DefaultHandler;
|
---|
| 39 |
|
---|
| 40 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
[1815] | 41 | import de.ugoe.cs.autoquest.eventcore.gui.CharacterTyped;
|
---|
[1797] | 42 | import de.ugoe.cs.autoquest.eventcore.gui.IInteraction;
|
---|
[1815] | 43 | import de.ugoe.cs.autoquest.eventcore.gui.TouchSingle;
|
---|
[1783] | 44 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIElementTree;
|
---|
| 45 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
|
---|
| 46 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModelException;
|
---|
| 47 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
|
---|
[1797] | 48 | import de.ugoe.cs.autoquest.plugin.android.guimodel.ANDROIDGUIElement;
|
---|
| 49 | import de.ugoe.cs.autoquest.plugin.android.guimodel.ANDROIDGUIElementSpec;
|
---|
[1783] | 50 | import de.ugoe.cs.util.console.Console;
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * <p>
|
---|
[1819] | 54 | * This class provides functionality to parse XML log files generated by the AndroidMonitor of
|
---|
| 55 | * AutoQUEST. The result of parsing a file is a collection of event sequences.
|
---|
[1783] | 56 | * </p>
|
---|
| 57 | *
|
---|
| 58 | * @author Florian Unger
|
---|
| 59 | * @version 1.0
|
---|
| 60 | */
|
---|
| 61 | public class AndroidLogParser extends DefaultHandler {
|
---|
| 62 |
|
---|
[1819] | 63 | /*
|
---|
| 64 | * (non-Javadoc)
|
---|
| 65 | *
|
---|
| 66 | * int java.lang.Object.hashCode() is used in the Androidmonitor Long is used to internally
|
---|
| 67 | * handle and compare the number. e.g. currentGUIElementHash != null
|
---|
| 68 | */
|
---|
| 69 | /**
|
---|
| 70 | * <p>
|
---|
| 71 | * Internal handle to the id of the event that is currently being parsed.
|
---|
| 72 | * </p>
|
---|
| 73 | */
|
---|
| 74 | private String currentEventId = null;
|
---|
[1783] | 75 |
|
---|
[1819] | 76 | /**
|
---|
| 77 | * <p>
|
---|
| 78 | * Internal handle to the parameters of the event currently being parsed.
|
---|
| 79 | * </p>
|
---|
| 80 | */
|
---|
| 81 | private Map<String, String> currentEventParameters;
|
---|
[1783] | 82 |
|
---|
[1819] | 83 | /**
|
---|
| 84 | * <p>
|
---|
| 85 | * Internal handle to the source of the event that is currently being parsed.
|
---|
| 86 | * </p>
|
---|
| 87 | */
|
---|
| 88 | private Long currentEventSource;
|
---|
[1783] | 89 |
|
---|
[1819] | 90 | /**
|
---|
| 91 | * <p>
|
---|
| 92 | * Internal handle to the timestamp of the event that is currently being parsed.
|
---|
| 93 | */
|
---|
| 94 | private Long currentEventTimestamp = -1l;
|
---|
[1783] | 95 |
|
---|
[1819] | 96 | /**
|
---|
| 97 | *
|
---|
| 98 | * <p>
|
---|
| 99 | * Internal handle to the hashcode of the GUI element, that is currently parsed.
|
---|
| 100 | * </p>
|
---|
| 101 | */
|
---|
| 102 | private Long currentGUIElementHash = null;
|
---|
[1783] | 103 |
|
---|
[1819] | 104 | /**
|
---|
| 105 | * <p>
|
---|
| 106 | * Internal handle to the parsed GUI structure, stored in a GUIElementTree
|
---|
| 107 | * </p>
|
---|
| 108 | */
|
---|
| 109 | private GUIElementTree<Long> currentGUIElementTree;
|
---|
[1783] | 110 |
|
---|
[1819] | 111 | /**
|
---|
| 112 | * <p>
|
---|
| 113 | * Internal handle to the specification currently parsed for a GUI element
|
---|
| 114 | * </p>
|
---|
| 115 | */
|
---|
| 116 | private ANDROIDGUIElementSpec currentGUIElementSpec;
|
---|
[1783] | 117 |
|
---|
[1819] | 118 | /**
|
---|
| 119 | *
|
---|
| 120 | * <p>
|
---|
| 121 | * Internal handle to the hashcode of the parent of the GUI element, that is currently parsed.
|
---|
| 122 | * </p>
|
---|
| 123 | */
|
---|
| 124 | private Long currentParentHash;
|
---|
[1783] | 125 |
|
---|
[1819] | 126 | /**
|
---|
[1815] | 127 | * <p>
|
---|
[1819] | 128 | * Internal handle to the event sequence that is currently being parsed.
|
---|
| 129 | * </p>
|
---|
| 130 | */
|
---|
| 131 | private List<Event> currentSequence;
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * <p>
|
---|
[1815] | 135 | * internal handle to the class ancestors
|
---|
| 136 | * </p>
|
---|
| 137 | */
|
---|
[1819] | 138 | private List<String> currentTypeHierarchy;
|
---|
[1783] | 139 |
|
---|
[1819] | 140 | /**
|
---|
| 141 | * <p>
|
---|
| 142 | * Map that holds events that had no registered target GUI element during parsing. Keys are the
|
---|
| 143 | * IDs of the unregistered targets.
|
---|
| 144 | * </p>
|
---|
| 145 | */
|
---|
| 146 | // private Map<Long, List<Event>> eventsWithoutTargets;
|
---|
[1783] | 147 |
|
---|
[1819] | 148 | /**
|
---|
| 149 | * <p>
|
---|
| 150 | * Collection of event sequences that is contained in the log file, which is parsed.
|
---|
| 151 | * </p>
|
---|
| 152 | */
|
---|
| 153 | private Collection<List<Event>> sequences;
|
---|
[1783] | 154 |
|
---|
[1819] | 155 | /**
|
---|
[1869] | 156 | * Show parsed elements to find out failures.
|
---|
| 157 | */
|
---|
[1870] | 158 | private Boolean showSteps = false;
|
---|
[1783] | 159 |
|
---|
[1819] | 160 | /**
|
---|
| 161 | * <p>
|
---|
| 162 | * Constructor. Creates a new AndroidLogParser.
|
---|
| 163 | * </p>
|
---|
| 164 | */
|
---|
| 165 | public AndroidLogParser() {
|
---|
| 166 | sequences = new LinkedList<List<Event>>();
|
---|
| 167 | currentSequence = null;
|
---|
| 168 | }
|
---|
[1783] | 169 |
|
---|
[1819] | 170 | // TODO create a constructor which creates a new AndroidLogParser with a
|
---|
| 171 | // specific event filter.
|
---|
[1783] | 172 |
|
---|
[1819] | 173 | /**
|
---|
| 174 | * <p>
|
---|
| 175 | * Parses a log file written by the JFCMonitor and creates a collection of event sequences.
|
---|
| 176 | * </p>
|
---|
| 177 | *
|
---|
| 178 | * @param filename
|
---|
| 179 | * name and path of the log file
|
---|
| 180 | */
|
---|
| 181 | public void parseFile(String filename) {
|
---|
| 182 | if (filename == null) {
|
---|
| 183 | throw new IllegalArgumentException("filename must not be null");
|
---|
| 184 | }
|
---|
[1783] | 185 |
|
---|
[1819] | 186 | parseFile(new File(filename));
|
---|
| 187 | }
|
---|
[1783] | 188 |
|
---|
[1819] | 189 | /**
|
---|
| 190 | * <p>
|
---|
| 191 | * Parses a log file written by the JFCMonitor and creates a collection of event sequences.
|
---|
| 192 | * </p>
|
---|
| 193 | *
|
---|
| 194 | * @param file
|
---|
| 195 | * name and path of the log file
|
---|
| 196 | */
|
---|
| 197 | public void parseFile(File file) {
|
---|
| 198 | if (file == null) {
|
---|
| 199 | throw new IllegalArgumentException("file must not be null");
|
---|
| 200 | }
|
---|
[1783] | 201 |
|
---|
[1819] | 202 | SAXParserFactory spf = SAXParserFactory.newInstance();
|
---|
| 203 | // set true to validate that the file is well defined
|
---|
| 204 | spf.setValidating(true);
|
---|
[1797] | 205 |
|
---|
[1819] | 206 | SAXParser saxParser = null;
|
---|
| 207 | InputSource inputSource = null;
|
---|
| 208 | try {
|
---|
| 209 | saxParser = spf.newSAXParser();
|
---|
| 210 | inputSource =
|
---|
| 211 | new InputSource(new InputStreamReader(new FileInputStream(file), "UTF-8"));
|
---|
| 212 | }
|
---|
| 213 | catch (UnsupportedEncodingException e) {
|
---|
| 214 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 215 | Console.logException(e);
|
---|
| 216 | return;
|
---|
| 217 | }
|
---|
| 218 | catch (ParserConfigurationException e) {
|
---|
| 219 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 220 | Console.logException(e);
|
---|
| 221 | return;
|
---|
| 222 | }
|
---|
| 223 | catch (SAXException e) {
|
---|
| 224 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 225 | Console.logException(e);
|
---|
| 226 | return;
|
---|
| 227 | }
|
---|
| 228 | catch (FileNotFoundException e) {
|
---|
| 229 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 230 | Console.logException(e);
|
---|
| 231 | return;
|
---|
| 232 | }
|
---|
[1783] | 233 |
|
---|
[1819] | 234 | if (inputSource != null) {
|
---|
| 235 | inputSource.setSystemId("file://" + file.getAbsolutePath());
|
---|
| 236 | try {
|
---|
| 237 | // called a second time to be sure that no error happens
|
---|
| 238 | if (saxParser == null) {
|
---|
| 239 | throw new RuntimeException("SAXParser creation failed");
|
---|
| 240 | }
|
---|
| 241 | saxParser.parse(inputSource, this);
|
---|
| 242 | }
|
---|
| 243 | catch (SAXParseException e) {
|
---|
| 244 | Console.printerrln("Failure parsing file in line " + e.getLineNumber() +
|
---|
| 245 | ", column " + e.getColumnNumber() + ".");
|
---|
| 246 | Console.logException(e);
|
---|
| 247 | return;
|
---|
| 248 | }
|
---|
| 249 | catch (SAXException e) {
|
---|
| 250 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 251 | Console.logException(e);
|
---|
| 252 | return;
|
---|
| 253 | }
|
---|
| 254 | catch (IOException e) {
|
---|
| 255 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 256 | Console.logException(e);
|
---|
| 257 | return;
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
[1783] | 261 |
|
---|
[1819] | 262 | /**
|
---|
| 263 | * <p>
|
---|
| 264 | * Returns the collection of event sequences that is obtained from parsing log files.
|
---|
| 265 | * </p>
|
---|
| 266 | *
|
---|
| 267 | * @return collection of event sequences
|
---|
| 268 | */
|
---|
| 269 | public Collection<List<Event>> getSequences() {
|
---|
| 270 | return sequences;
|
---|
| 271 | }
|
---|
[1783] | 272 |
|
---|
[1819] | 273 | /**
|
---|
| 274 | * <p>
|
---|
| 275 | * Returns the GUI model that is obtained from parsing log files.
|
---|
| 276 | * </p>
|
---|
| 277 | *
|
---|
| 278 | * @return GUIModel
|
---|
| 279 | */
|
---|
| 280 | public GUIModel getGuiModel() {
|
---|
| 281 | return currentGUIElementTree.getGUIModel();
|
---|
| 282 | }
|
---|
[1803] | 283 |
|
---|
[1819] | 284 | /*
|
---|
| 285 | * (non-Javadoc)
|
---|
| 286 | *
|
---|
| 287 | * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String,
|
---|
| 288 | * java.lang.String, org.xml.sax.Attributes)
|
---|
| 289 | */
|
---|
| 290 | public void startElement(String uri, String localName, String qName, Attributes atts)
|
---|
| 291 | throws SAXException
|
---|
| 292 | {
|
---|
| 293 | showSteps("start element: " + qName, true);
|
---|
| 294 | if (qName.equals("sessions")) {
|
---|
| 295 | currentSequence = new LinkedList<Event>();
|
---|
| 296 | if (currentGUIElementTree == null) {
|
---|
| 297 | currentGUIElementTree = new GUIElementTree<Long>();
|
---|
| 298 | }
|
---|
[1783] | 299 |
|
---|
[1819] | 300 | }
|
---|
[1803] | 301 |
|
---|
[1819] | 302 | if (qName.equals("component")) {
|
---|
| 303 | currentGUIElementHash = Long.parseLong(atts.getValue("hash"));
|
---|
| 304 | currentGUIElementSpec = new ANDROIDGUIElementSpec();
|
---|
[1869] | 305 | currentGUIElementSpec.setElementHash((int) currentGUIElementHash.longValue());
|
---|
[1783] | 306 |
|
---|
[1819] | 307 | }
|
---|
| 308 | else if (qName.equals("event")) {
|
---|
| 309 | currentEventId = atts.getValue("id");
|
---|
| 310 | currentEventParameters = new HashMap<String, String>();
|
---|
| 311 |
|
---|
| 312 | }
|
---|
| 313 | else if (qName.equals("param")) {
|
---|
| 314 | if (currentGUIElementHash != null) {
|
---|
| 315 | if ("class".equals(atts.getValue("name"))) {
|
---|
| 316 | currentGUIElementSpec.setType(atts.getValue("value"));
|
---|
| 317 | }
|
---|
[1869] | 318 | else if ("title".equals(atts.getValue("name"))) {
|
---|
| 319 | currentGUIElementSpec.setName(atts.getValue("value"));
|
---|
| 320 | }
|
---|
[1819] | 321 | else if ("path".equals(atts.getValue("name"))) {
|
---|
| 322 | currentGUIElementSpec.setPath(atts.getValue("value"));
|
---|
| 323 | }
|
---|
| 324 | else if ("id".equals(atts.getValue("name"))) {
|
---|
| 325 | currentGUIElementSpec.setIndex(Integer.parseInt(atts.getValue("value")));
|
---|
| 326 | }
|
---|
| 327 | else if ("parent".equals(atts.getValue("name"))) {
|
---|
| 328 | currentParentHash = Long.parseLong(atts.getValue("value"));
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 | else if (currentEventId != null) {
|
---|
| 332 | if ("source".equals(atts.getValue("name"))) {
|
---|
| 333 | currentEventSource = Long.parseLong(atts.getValue("value"));
|
---|
| 334 | }
|
---|
| 335 | if ("timestamp".equals(atts.getValue("name"))) {
|
---|
| 336 | currentEventTimestamp = Long.parseLong(atts.getValue("value"));
|
---|
| 337 | }
|
---|
| 338 | currentEventParameters.put(atts.getValue("name"), atts.getValue("value"));
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 | else if (qName.equals("ancestor")) {
|
---|
[1815] | 342 | currentTypeHierarchy.add(atts.getValue("name"));
|
---|
| 343 | }
|
---|
| 344 | else if (qName.equals("ancestors")) {
|
---|
| 345 | currentTypeHierarchy = new LinkedList<String>();
|
---|
| 346 | }
|
---|
[1819] | 347 | }
|
---|
[1783] | 348 |
|
---|
[1819] | 349 | /*
|
---|
| 350 | * (non-Javadoc)
|
---|
| 351 | *
|
---|
| 352 | * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String,
|
---|
| 353 | * java.lang.String)
|
---|
| 354 | */
|
---|
| 355 | @Override
|
---|
| 356 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
[1803] | 357 |
|
---|
[1819] | 358 | showSteps("end element: " + qName, true);
|
---|
[1803] | 359 |
|
---|
[1819] | 360 | if (qName.equals("sessions")) {
|
---|
| 361 | if (currentSequence != null) {
|
---|
| 362 | sequences.add(currentSequence);
|
---|
| 363 | }
|
---|
| 364 | }
|
---|
| 365 | else if (qName.equals("ancestors")) {
|
---|
[1815] | 366 | currentGUIElementSpec.setTypeHierarchy(currentTypeHierarchy);
|
---|
| 367 | }
|
---|
[1819] | 368 | else if (qName.equals("component") && currentGUIElementHash != null) {
|
---|
| 369 | try {
|
---|
| 370 | currentGUIElementTree.add(currentGUIElementHash, currentParentHash,
|
---|
| 371 | currentGUIElementSpec);
|
---|
| 372 | }
|
---|
| 373 | catch (GUIModelException e) {
|
---|
| 374 | throw new SAXException("could not handle GUI element with hash " +
|
---|
| 375 | currentGUIElementHash + ": " + e.getMessage(), e);
|
---|
| 376 | }
|
---|
| 377 | currentGUIElementHash = null;
|
---|
| 378 | currentParentHash = null;
|
---|
| 379 | currentTypeHierarchy = null;
|
---|
| 380 | }
|
---|
| 381 | else if (currentEventId != null && qName.equals("event")) {
|
---|
[1803] | 382 |
|
---|
[1819] | 383 | IGUIElement currentGUIElement;
|
---|
| 384 | currentGUIElement = currentGUIElementTree.find(currentEventSource);
|
---|
| 385 | Event event;
|
---|
[1783] | 386 |
|
---|
[1819] | 387 | // up to now only onClick events are implemented and each
|
---|
| 388 | // onclick event is processed as a mouse click
|
---|
| 389 | if (currentGUIElement == null) {
|
---|
[1803] | 390 |
|
---|
[1819] | 391 | }
|
---|
| 392 | else {
|
---|
[1803] | 393 |
|
---|
[1819] | 394 | event =
|
---|
| 395 | new Event(instantiateInteraction(currentEventId, currentEventParameters),
|
---|
| 396 | currentGUIElement);
|
---|
| 397 | ANDROIDGUIElement currentEventTarget = (ANDROIDGUIElement) event.getTarget();
|
---|
| 398 | currentEventTarget.markUsed();
|
---|
| 399 | event.setTimestamp(currentEventTimestamp);
|
---|
| 400 | currentSequence.add(event);
|
---|
| 401 | }
|
---|
| 402 | currentEventParameters = null;
|
---|
| 403 | currentEventId = null;
|
---|
| 404 | currentEventTimestamp = -1l;
|
---|
| 405 | }
|
---|
| 406 | }
|
---|
[1783] | 407 |
|
---|
[1819] | 408 | /**
|
---|
| 409 | * <p>
|
---|
| 410 | * depending on the event id and the event parameters, this method instantiates the concrete
|
---|
| 411 | * interaction, that took place, i.e. the event type
|
---|
| 412 | * </p>
|
---|
| 413 | *
|
---|
| 414 | * @param eventId
|
---|
| 415 | * the id of the event
|
---|
| 416 | * @param eventParameters
|
---|
| 417 | * the parameters provided for the event
|
---|
| 418 | *
|
---|
| 419 | * @return as described
|
---|
| 420 | *
|
---|
| 421 | * @throws SAXException
|
---|
| 422 | * thrown if the provided event id is unknown
|
---|
| 423 | */
|
---|
| 424 | private IInteraction instantiateInteraction(String event, Map<String, String> eventParameters)
|
---|
| 425 | throws SAXException
|
---|
| 426 | {
|
---|
[1803] | 427 |
|
---|
[1819] | 428 | switch (event)
|
---|
| 429 | {
|
---|
| 430 | case "onClick":
|
---|
[1797] | 431 |
|
---|
[1819] | 432 | float x = Float.parseFloat(currentEventParameters.get("X"));
|
---|
| 433 | float y = Float.parseFloat(currentEventParameters.get("Y"));
|
---|
[1797] | 434 |
|
---|
[1819] | 435 | return new TouchSingle(x, y);
|
---|
[1803] | 436 |
|
---|
[1819] | 437 | case "text":
|
---|
| 438 | return new CharacterTyped(currentEventParameters.get("message"));
|
---|
[1803] | 439 |
|
---|
[1819] | 440 | default:
|
---|
| 441 | throw new SAXException("unhandled event id " + event);
|
---|
| 442 | }
|
---|
[1803] | 443 |
|
---|
[1819] | 444 | }
|
---|
| 445 |
|
---|
| 446 | private void showSteps(String message, Boolean ln) {
|
---|
| 447 | if (showSteps) {
|
---|
| 448 | if (ln) {
|
---|
| 449 | Console.traceln(Level.INFO, message);
|
---|
| 450 | }
|
---|
| 451 | else {
|
---|
| 452 | Console.trace(Level.INFO, message);
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | }
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | public void setShowSteps(Boolean showSteps) {
|
---|
| 459 | this.showSteps = showSteps;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
[1782] | 462 | }
|
---|