[927] | 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 | |
---|
[864] | 15 | package de.ugoe.cs.autoquest.htmlmonitor; |
---|
| 16 | |
---|
| 17 | import static org.junit.Assert.*; |
---|
| 18 | |
---|
| 19 | import org.apache.http.HttpEntity; |
---|
| 20 | import org.apache.http.HttpResponse; |
---|
| 21 | import org.apache.http.client.methods.HttpPost; |
---|
| 22 | import org.apache.http.entity.ContentType; |
---|
| 23 | import org.apache.http.entity.StringEntity; |
---|
| 24 | import org.apache.http.impl.client.DefaultHttpClient; |
---|
| 25 | import org.junit.After; |
---|
| 26 | import org.junit.Before; |
---|
| 27 | import org.junit.Test; |
---|
| 28 | |
---|
| 29 | import de.ugoe.cs.util.console.TextConsole; |
---|
| 30 | |
---|
| 31 | /** |
---|
[1078] | 32 | * |
---|
[864] | 33 | * @author Patrick Harms |
---|
| 34 | */ |
---|
| 35 | public class HtmlMonitorServerTest implements HtmlMonitorMessageListener { |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * |
---|
| 39 | */ |
---|
| 40 | public static final TextConsole CONSOLE = new TextConsole(); |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * |
---|
| 44 | */ |
---|
| 45 | private static final int PORT = 19098; |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * |
---|
| 49 | */ |
---|
| 50 | private HtmlEvent[] events; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * |
---|
| 54 | */ |
---|
[1069] | 55 | private HtmlGUIElement guiStructure; |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * |
---|
| 59 | */ |
---|
[864] | 60 | private String eventHandlingError; |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * |
---|
| 64 | */ |
---|
| 65 | private HtmlMonitorServer server; |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * |
---|
| 69 | */ |
---|
| 70 | @Before |
---|
| 71 | public void setUp() throws Exception { |
---|
| 72 | server = new HtmlMonitorServer(PORT, this); |
---|
| 73 | server.init(); |
---|
| 74 | server.start(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * |
---|
| 79 | */ |
---|
| 80 | @After |
---|
| 81 | public void tearDown() throws Exception { |
---|
| 82 | events = null; |
---|
| 83 | eventHandlingError = null; |
---|
| 84 | |
---|
| 85 | if (server != null) { |
---|
| 86 | try { |
---|
| 87 | server.stop(); |
---|
| 88 | } |
---|
| 89 | finally { |
---|
| 90 | server = null; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | * |
---|
| 98 | */ |
---|
| 99 | @Test |
---|
| 100 | public void testSimpleMessage() throws Exception { |
---|
[1069] | 101 | String clientId = "123"; |
---|
| 102 | |
---|
[864] | 103 | String message = |
---|
| 104 | "{" + |
---|
| 105 | " \"message\": {" + |
---|
| 106 | " \"clientInfos\": {" + |
---|
[1069] | 107 | " \"clientId\":\"" + clientId + "\"," + |
---|
[864] | 108 | " \"userAgent\":\"Agent\"," + |
---|
| 109 | " \"title\":\"Title\"," + |
---|
| 110 | " \"url\":\"http://host/path\"" + |
---|
| 111 | " }," + |
---|
[1069] | 112 | " \"guiModel\": {" + |
---|
| 113 | " \"tagName\":\"html\"," + |
---|
| 114 | " \"index\":\"0\"," + |
---|
| 115 | " \"children\":" + |
---|
| 116 | " [ {" + |
---|
| 117 | " \"tagName\":\"head\"," + |
---|
| 118 | " \"index\":\"0\"," + |
---|
| 119 | " }," + |
---|
| 120 | " {" + |
---|
| 121 | " \"tagName\":\"body\"," + |
---|
| 122 | " \"htmlId\":\"gsr\"," + |
---|
| 123 | " }" + |
---|
| 124 | " ]" + |
---|
| 125 | " }," + |
---|
[864] | 126 | " \"events\":" + |
---|
| 127 | " [ {" + |
---|
| 128 | " \"time\":\"12345\"," + |
---|
[1069] | 129 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
| 130 | " \"eventType\":\"onclick\"" + |
---|
| 131 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
[864] | 132 | " }" + |
---|
| 133 | " ]" + |
---|
| 134 | " }" + |
---|
| 135 | "}"; |
---|
| 136 | |
---|
| 137 | sendMessageAndAssertResponse(message); |
---|
| 138 | |
---|
| 139 | if (eventHandlingError != null) { |
---|
| 140 | fail(eventHandlingError); |
---|
| 141 | } |
---|
| 142 | |
---|
[1069] | 143 | // check the GUI structure |
---|
| 144 | HtmlGUIElement parent = null; |
---|
| 145 | HtmlGUIElement element = guiStructure; |
---|
| 146 | |
---|
| 147 | assertGuiElement(element, HtmlServer.class, parent, 1); |
---|
| 148 | |
---|
| 149 | parent = element; |
---|
| 150 | element = parent.getChildren().get(0); |
---|
| 151 | assertGuiElement(element, HtmlDocument.class, parent, 1); |
---|
| 152 | |
---|
| 153 | HtmlDocument document = (HtmlDocument) element; |
---|
| 154 | |
---|
| 155 | parent = element; |
---|
| 156 | element = parent.getChildren().get(0); |
---|
| 157 | assertGuiElement(element, HtmlPageElement.class, parent, 2); |
---|
| 158 | |
---|
| 159 | parent = element; |
---|
| 160 | element = parent.getChildren().get(0); |
---|
| 161 | assertGuiElement(element, HtmlPageElement.class, parent, 0); |
---|
| 162 | element = parent.getChildren().get(1); |
---|
| 163 | assertGuiElement(element, HtmlPageElement.class, parent, 0); |
---|
| 164 | |
---|
| 165 | // check the event |
---|
[864] | 166 | assertNotNull(events); |
---|
| 167 | assertEquals(1, events.length); |
---|
| 168 | |
---|
[1069] | 169 | assertEvent(0, 12345, "/html[0]/body(htmlId=gsr)", element, document, "onclick", |
---|
| 170 | null, null, new Integer[] {194, 7}, |
---|
| 171 | "Title", "Agent", "http://host/path", "123"); |
---|
[864] | 172 | |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * |
---|
| 177 | */ |
---|
| 178 | @Test |
---|
| 179 | public void testComplexMessage() throws Exception { |
---|
[1069] | 180 | String clientId = "123"; |
---|
| 181 | |
---|
[864] | 182 | String message = |
---|
| 183 | "{" + |
---|
| 184 | " \"message\": {" + |
---|
| 185 | " \"clientInfos\": {" + |
---|
[1069] | 186 | " \"clientId\":\"" + clientId + "\"," + |
---|
[864] | 187 | " \"userAgent\":\"Agent\"," + |
---|
| 188 | " \"title\":\"Title\"," + |
---|
| 189 | " \"url\":\"http://host/path\"" + |
---|
| 190 | " }," + |
---|
[1069] | 191 | " \"guiModel\": {" + |
---|
| 192 | " \"tagName\":\"html\"," + |
---|
| 193 | " \"index\":\"0\"," + |
---|
| 194 | " \"children\":" + |
---|
| 195 | " [ {" + |
---|
| 196 | " \"tagName\":\"head\"," + |
---|
| 197 | " \"index\":\"0\"," + |
---|
| 198 | " }," + |
---|
| 199 | " {" + |
---|
| 200 | " \"tagName\":\"body\"," + |
---|
| 201 | " \"htmlId\":\"gsr\"," + |
---|
| 202 | " \"children\":" + |
---|
| 203 | " [ {" + |
---|
| 204 | " \"tagName\":\"input_button\"," + |
---|
| 205 | " \"htmlId\":\"input1\"," + |
---|
| 206 | " }," + |
---|
| 207 | " {" + |
---|
| 208 | " \"tagName\":\"input_button\"," + |
---|
| 209 | " \"htmlId\":\"input2\"," + |
---|
| 210 | " }," + |
---|
| 211 | " {" + |
---|
| 212 | " \"tagName\":\"input_button\"," + |
---|
| 213 | " \"htmlId\":\"input3\"," + |
---|
| 214 | " }," + |
---|
| 215 | " {" + |
---|
| 216 | " \"tagName\":\"input_button\"," + |
---|
| 217 | " \"htmlId\":\"input4\"," + |
---|
| 218 | " }," + |
---|
| 219 | " {" + |
---|
| 220 | " \"tagName\":\"input_button\"," + |
---|
| 221 | " \"htmlId\":\"input5\"," + |
---|
| 222 | " }," + |
---|
| 223 | " {" + |
---|
| 224 | " \"tagName\":\"input_button\"," + |
---|
| 225 | " \"htmlId\":\"input6\"," + |
---|
| 226 | " }," + |
---|
| 227 | " {" + |
---|
| 228 | " \"tagName\":\"input_button\"," + |
---|
| 229 | " \"htmlId\":\"input7\"," + |
---|
| 230 | " }," + |
---|
| 231 | " {" + |
---|
| 232 | " \"tagName\":\"input_button\"," + |
---|
| 233 | " \"htmlId\":\"input8\"," + |
---|
| 234 | " }," + |
---|
| 235 | " {" + |
---|
| 236 | " \"tagName\":\"input_button\"," + |
---|
| 237 | " \"htmlId\":\"input9\"," + |
---|
| 238 | " }," + |
---|
| 239 | " {" + |
---|
| 240 | " \"tagName\":\"input_button\"," + |
---|
| 241 | " \"htmlId\":\"input10\"," + |
---|
| 242 | " }," + |
---|
| 243 | " ]" + |
---|
| 244 | " }" + |
---|
| 245 | " ]" + |
---|
| 246 | " }," + |
---|
[864] | 247 | " \"events\":" + |
---|
[1069] | 248 | " [ {" + |
---|
[864] | 249 | " \"time\":\"1\"," + |
---|
[1069] | 250 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input1)\"," + |
---|
[864] | 251 | " \"eventType\":\"onclick\"," + |
---|
| 252 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
| 253 | " }," + |
---|
| 254 | " {" + |
---|
| 255 | " \"time\":\"2\"," + |
---|
[1069] | 256 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input2)\"," + |
---|
[864] | 257 | " \"eventType\":\"ondblclick\"," + |
---|
| 258 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
| 259 | " }," + |
---|
| 260 | " {" + |
---|
| 261 | " \"time\":\"3\"," + |
---|
[1069] | 262 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input3)\"," + |
---|
[864] | 263 | " \"eventType\":\"onfocus\"" + |
---|
| 264 | " }," + |
---|
| 265 | " {" + |
---|
| 266 | " \"time\":\"4\"," + |
---|
[1069] | 267 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input4)\"," + |
---|
[864] | 268 | " \"eventType\":\"onclick\"," + |
---|
| 269 | " \"coordinates\": [\"125\", \"14\"]" + |
---|
| 270 | " }," + |
---|
| 271 | " {" + |
---|
| 272 | " \"time\":\"5\"," + |
---|
[1069] | 273 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input5)\"," + |
---|
[864] | 274 | " \"eventType\":\"onfocus\"" + |
---|
| 275 | " }," + |
---|
| 276 | " {" + |
---|
| 277 | " \"time\":\"6\"," + |
---|
[1069] | 278 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input6)\"," + |
---|
[864] | 279 | " \"eventType\":\"onfocus\"" + |
---|
| 280 | " }," + |
---|
| 281 | " {" + |
---|
| 282 | " \"time\":\"7\"," + |
---|
[1069] | 283 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input7)\"," + |
---|
[864] | 284 | " \"eventType\":\"onfocus\"" + |
---|
| 285 | " }," + |
---|
| 286 | " {" + |
---|
| 287 | " \"time\":\"8\"," + |
---|
[1069] | 288 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input8)\"," + |
---|
[864] | 289 | " \"eventType\":\"onclick\"," + |
---|
| 290 | " \"coordinates\": [\"255\", \"4\"]" + |
---|
| 291 | " }," + |
---|
| 292 | " {" + |
---|
| 293 | " \"time\":\"9\"," + |
---|
[1069] | 294 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
[864] | 295 | " \"eventType\":\"onscroll\"," + |
---|
[943] | 296 | " \"scrollPosition\": [\"23\", \"567\"]" + |
---|
[864] | 297 | " }," + |
---|
| 298 | " {" + |
---|
| 299 | " \"time\":\"10\"," + |
---|
[1069] | 300 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input10)\"," + |
---|
[864] | 301 | " \"eventType\":\"onclick\"," + |
---|
| 302 | " \"coordinates\": [\"516\", \"154\"]" + |
---|
| 303 | " }" + |
---|
| 304 | " ]" + |
---|
| 305 | " }" + |
---|
| 306 | "}"; |
---|
[1069] | 307 | |
---|
[864] | 308 | sendMessageAndAssertResponse(message); |
---|
| 309 | |
---|
| 310 | if (eventHandlingError != null) { |
---|
| 311 | fail(eventHandlingError); |
---|
| 312 | } |
---|
| 313 | |
---|
[1069] | 314 | // check the GUI structure |
---|
| 315 | HtmlGUIElement parent = null; |
---|
| 316 | HtmlGUIElement element = guiStructure; |
---|
| 317 | |
---|
| 318 | assertGuiElement(element, HtmlServer.class, parent, 1); |
---|
| 319 | |
---|
| 320 | parent = element; |
---|
| 321 | element = parent.getChildren().get(0); |
---|
| 322 | assertGuiElement(element, HtmlDocument.class, parent, 1); |
---|
| 323 | |
---|
| 324 | HtmlDocument document = (HtmlDocument) element; |
---|
| 325 | |
---|
| 326 | parent = element; |
---|
| 327 | element = parent.getChildren().get(0); |
---|
| 328 | assertGuiElement(element, HtmlPageElement.class, parent, 2); |
---|
| 329 | |
---|
| 330 | parent = element; |
---|
| 331 | element = parent.getChildren().get(0); |
---|
| 332 | assertGuiElement(element, HtmlPageElement.class, parent, 0); |
---|
| 333 | element = parent.getChildren().get(1); |
---|
| 334 | assertGuiElement(element, HtmlPageElement.class, parent, 10); |
---|
| 335 | |
---|
| 336 | parent = element; |
---|
| 337 | for (HtmlGUIElement child : parent.getChildren()) { |
---|
| 338 | assertGuiElement(child, HtmlPageElement.class, parent, 0); |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | // check the events |
---|
[864] | 342 | assertNotNull(events); |
---|
| 343 | assertEquals(10, events.length); |
---|
| 344 | |
---|
| 345 | for (int i = 0; i < events.length; i++) { |
---|
[1069] | 346 | String targetDOMPath; |
---|
| 347 | HtmlGUIElement target; |
---|
| 348 | Integer[] scrollPosition = null; |
---|
[864] | 349 | |
---|
| 350 | if (i == 8) { |
---|
[1069] | 351 | targetDOMPath = "/html[0]/body(htmlId=gsr)"; |
---|
| 352 | target = parent; |
---|
| 353 | scrollPosition = new Integer[] { 23, 567 }; |
---|
[864] | 354 | } |
---|
| 355 | else { |
---|
[1069] | 356 | targetDOMPath = |
---|
| 357 | "/html[0]/body(htmlId=gsr)/input_button(htmlId=input" + (i + 1) + ")"; |
---|
| 358 | target = parent.getChildren().get(i); |
---|
[864] | 359 | } |
---|
| 360 | |
---|
[1069] | 361 | String eventType; |
---|
[864] | 362 | if ((i == 1)) { |
---|
[1069] | 363 | eventType = "ondblclick"; |
---|
[864] | 364 | } |
---|
| 365 | else if ((i == 2) || ((4 <= i) && (i <= 6))) { |
---|
[1069] | 366 | eventType = "onfocus"; |
---|
[864] | 367 | } |
---|
| 368 | else if ((i == 8)) { |
---|
[1069] | 369 | eventType = "onscroll"; |
---|
[864] | 370 | } |
---|
| 371 | else { |
---|
[1069] | 372 | eventType = "onclick"; |
---|
[864] | 373 | } |
---|
| 374 | |
---|
| 375 | assertNull("event " + i, events[i].getKey()); |
---|
| 376 | |
---|
[1069] | 377 | Integer[] coordinates = null; |
---|
| 378 | if (i <= 1) { |
---|
| 379 | coordinates = new Integer[] { 194, 7 }; |
---|
[864] | 380 | } |
---|
[1069] | 381 | else if (i == 3) { |
---|
| 382 | coordinates = new Integer[] { 125, 14 }; |
---|
[864] | 383 | } |
---|
[1069] | 384 | else if (i == 7) { |
---|
| 385 | coordinates = new Integer[] { 255, 4 }; |
---|
[864] | 386 | } |
---|
[1069] | 387 | else if (i == 9) { |
---|
| 388 | coordinates = new Integer[] { 516, 154 }; |
---|
[864] | 389 | } |
---|
| 390 | |
---|
[1069] | 391 | assertEvent(i, i + 1, targetDOMPath, target, document, eventType, |
---|
| 392 | null, scrollPosition, coordinates, |
---|
| 393 | "Title", "Agent", "http://host/path", "123"); |
---|
| 394 | |
---|
[864] | 395 | } |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | /** |
---|
| 399 | * |
---|
| 400 | */ |
---|
| 401 | @Test |
---|
| 402 | public void testInvalidMessages() throws Exception { |
---|
| 403 | String message = "}"; |
---|
| 404 | |
---|
| 405 | sendMessageAndAssertResponse(message); |
---|
| 406 | |
---|
| 407 | if (eventHandlingError != null) { |
---|
| 408 | fail(eventHandlingError); |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | assertNull(events); |
---|
| 412 | |
---|
| 413 | message = "blaublbidalslkdjflqkerowercalksdjfdlkkjdjfk"; |
---|
| 414 | |
---|
| 415 | sendMessageAndAssertResponse(message); |
---|
| 416 | |
---|
| 417 | if (eventHandlingError != null) { |
---|
| 418 | fail(eventHandlingError); |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | assertNull(events); |
---|
| 422 | |
---|
| 423 | // the following message doesn't work because of the missing scroll position in the event |
---|
| 424 | message = |
---|
| 425 | "{" + |
---|
| 426 | " \"message\": {" + |
---|
| 427 | " \"clientInfos\": {" + |
---|
| 428 | " \"clientId\":\"123\"," + |
---|
| 429 | " \"userAgent\":\"Agent\"," + |
---|
| 430 | " \"title\":\"Title\"," + |
---|
| 431 | " \"url\":\"http://host/path\"" + |
---|
| 432 | " }," + |
---|
[1069] | 433 | " \"guiModel\": {" + |
---|
| 434 | " \"tagName\":\"html\"," + |
---|
| 435 | " \"index\":\"0\"," + |
---|
| 436 | " \"children\":" + |
---|
| 437 | " [ {" + |
---|
| 438 | " \"tagName\":\"head\"," + |
---|
| 439 | " \"index\":\"0\"," + |
---|
| 440 | " }," + |
---|
| 441 | " {" + |
---|
| 442 | " \"tagName\":\"body\"," + |
---|
| 443 | " \"htmlId\":\"gsr\"," + |
---|
| 444 | " }" + |
---|
| 445 | " ]" + |
---|
| 446 | " }," + |
---|
[864] | 447 | " \"events\":" + |
---|
| 448 | " [ {" + |
---|
[1069] | 449 | " \"time\":\"9\"," + |
---|
| 450 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
| 451 | " \"eventType\":\"onscroll\"," + |
---|
[864] | 452 | " }" + |
---|
| 453 | " ]" + |
---|
| 454 | " }" + |
---|
| 455 | "}"; |
---|
| 456 | |
---|
| 457 | sendMessageAndAssertResponse(message); |
---|
| 458 | |
---|
| 459 | if (eventHandlingError != null) { |
---|
| 460 | fail(eventHandlingError); |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | assertNull(events); |
---|
| 464 | |
---|
| 465 | // the following message doesn't work because of the missing client id |
---|
| 466 | message = |
---|
| 467 | "{" + |
---|
| 468 | " \"message\": {" + |
---|
| 469 | " \"clientInfos\": {" + |
---|
| 470 | " \"userAgent\":\"Agent\"," + |
---|
| 471 | " \"title\":\"Title\"," + |
---|
| 472 | " \"url\":\"http://host/path\"" + |
---|
| 473 | " }," + |
---|
[1069] | 474 | " \"guiModel\": {" + |
---|
| 475 | " \"tagName\":\"html\"," + |
---|
| 476 | " \"index\":\"0\"," + |
---|
| 477 | " \"children\":" + |
---|
| 478 | " [ {" + |
---|
| 479 | " \"tagName\":\"head\"," + |
---|
| 480 | " \"index\":\"0\"," + |
---|
| 481 | " }," + |
---|
| 482 | " {" + |
---|
| 483 | " \"tagName\":\"body\"," + |
---|
| 484 | " \"htmlId\":\"gsr\"," + |
---|
| 485 | " }" + |
---|
| 486 | " ]" + |
---|
| 487 | " }," + |
---|
[864] | 488 | " \"events\":" + |
---|
| 489 | " [ {" + |
---|
| 490 | " \"time\":\"12345\"," + |
---|
[1069] | 491 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
[864] | 492 | " \"eventType\":\"onunload\"" + |
---|
| 493 | " }" + |
---|
| 494 | " ]" + |
---|
| 495 | " }" + |
---|
| 496 | "}"; |
---|
| 497 | |
---|
| 498 | sendMessageAndAssertResponse(message); |
---|
| 499 | |
---|
| 500 | if (eventHandlingError != null) { |
---|
| 501 | fail(eventHandlingError); |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | assertNull(events); |
---|
| 505 | |
---|
| 506 | // the following message doesn't work because of the invalid time stamp |
---|
| 507 | message = |
---|
| 508 | "{" + |
---|
| 509 | " \"message\": {" + |
---|
| 510 | " \"clientInfos\": {" + |
---|
| 511 | " \"clientId\":\"123\"," + |
---|
| 512 | " \"userAgent\":\"Agent\"," + |
---|
| 513 | " \"title\":\"Title\"," + |
---|
| 514 | " \"url\":\"http://host/path\"" + |
---|
| 515 | " }," + |
---|
[1069] | 516 | " \"guiModel\": {" + |
---|
| 517 | " \"tagName\":\"html\"," + |
---|
| 518 | " \"index\":\"0\"," + |
---|
| 519 | " \"children\":" + |
---|
| 520 | " [ {" + |
---|
| 521 | " \"tagName\":\"head\"," + |
---|
| 522 | " \"index\":\"0\"," + |
---|
| 523 | " }," + |
---|
| 524 | " {" + |
---|
| 525 | " \"tagName\":\"body\"," + |
---|
| 526 | " \"htmlId\":\"gsr\"," + |
---|
| 527 | " }" + |
---|
| 528 | " ]" + |
---|
| 529 | " }," + |
---|
[864] | 530 | " \"events\":" + |
---|
| 531 | " [ {" + |
---|
| 532 | " \"time\":\"blub\"," + |
---|
[1069] | 533 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
[864] | 534 | " \"eventType\":\"onunload\"" + |
---|
| 535 | " }" + |
---|
| 536 | " ]" + |
---|
| 537 | " }" + |
---|
| 538 | "}"; |
---|
| 539 | |
---|
| 540 | sendMessageAndAssertResponse(message); |
---|
| 541 | |
---|
| 542 | if (eventHandlingError != null) { |
---|
| 543 | fail(eventHandlingError); |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | assertNull(events); |
---|
| 547 | |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | /* (non-Javadoc) |
---|
[922] | 551 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitoringListener#handleEvents(de.ugoe.cs.autoquest.htmlmonitor.HtmlClientInfos, de.ugoe.cs.autoquest.htmlmonitor.HtmlEvent[]) |
---|
[864] | 552 | */ |
---|
| 553 | @Override |
---|
[1069] | 554 | public void handleMessage(HtmlClientInfos clientInfos, |
---|
| 555 | HtmlGUIElement guiStructure, |
---|
| 556 | HtmlEvent[] events) |
---|
| 557 | { |
---|
[864] | 558 | if (clientInfos == null) { |
---|
| 559 | eventHandlingError = "client infos were null"; |
---|
| 560 | } |
---|
[1069] | 561 | else if (guiStructure == null) { |
---|
| 562 | eventHandlingError = "gui structure was null"; |
---|
| 563 | } |
---|
[864] | 564 | else if (events == null) { |
---|
| 565 | eventHandlingError = "events were null"; |
---|
| 566 | } |
---|
| 567 | else { |
---|
| 568 | for (HtmlEvent event : events) { |
---|
| 569 | if (!clientInfos.equals(event.getClientInfos())) { |
---|
| 570 | eventHandlingError = "one of the events did not have a correct client info"; |
---|
| 571 | } |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | this.events = events; |
---|
[1069] | 575 | this.guiStructure = guiStructure; |
---|
[864] | 576 | } |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | /** |
---|
| 580 | * |
---|
| 581 | */ |
---|
| 582 | private void sendMessageAndAssertResponse(String message) throws Exception { |
---|
| 583 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
---|
| 584 | HttpPost httpPost = new HttpPost("http://localhost:" + PORT + "/"); |
---|
| 585 | HttpEntity entity = new StringEntity(message, ContentType.APPLICATION_JSON); |
---|
| 586 | httpPost.setEntity(entity); |
---|
| 587 | |
---|
| 588 | try { |
---|
| 589 | HttpResponse response = httpclient.execute(httpPost); |
---|
| 590 | |
---|
| 591 | // the monitor always returns 200 without any additional information. The client must |
---|
| 592 | // never get more or less information. This is especially important for preventing |
---|
| 593 | // hackers from finding out more |
---|
| 594 | assertEquals(200, response.getStatusLine().getStatusCode()); |
---|
| 595 | assertTrue |
---|
[1245] | 596 | ((response.getEntity() == null) || (response.getEntity().getContentLength() == 0) || |
---|
| 597 | ((response.getEntity().getContentLength() == 1) && |
---|
| 598 | (response.getEntity().getContent().read() == ' '))); |
---|
[864] | 599 | } |
---|
| 600 | finally { |
---|
| 601 | httpPost.releaseConnection(); |
---|
| 602 | } |
---|
| 603 | } |
---|
| 604 | |
---|
[1069] | 605 | /** |
---|
| 606 | * |
---|
| 607 | */ |
---|
| 608 | private void assertGuiElement(HtmlGUIElement element, |
---|
| 609 | Class<? extends HtmlGUIElement> type, |
---|
| 610 | HtmlGUIElement parent, |
---|
| 611 | int noOfChildren) |
---|
| 612 | { |
---|
| 613 | assertTrue(type.isInstance(element)); |
---|
| 614 | |
---|
| 615 | if (parent != null) { |
---|
[1315] | 616 | assertNotNull(element.getParent()); |
---|
| 617 | assertEquals(parent.getId(), element.getParent().getId()); |
---|
[1069] | 618 | } |
---|
| 619 | else { |
---|
[1315] | 620 | assertNull(element.getParent()); |
---|
[1069] | 621 | } |
---|
| 622 | |
---|
| 623 | assertNotNull(element.getId()); |
---|
| 624 | |
---|
| 625 | if (noOfChildren > 0) { |
---|
| 626 | assertNotNull(element.getChildren()); |
---|
| 627 | assertEquals(noOfChildren, element.getChildren().size()); |
---|
| 628 | } |
---|
| 629 | else { |
---|
| 630 | assertNull(element.getChildren()); |
---|
| 631 | } |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | /** |
---|
| 635 | * |
---|
| 636 | */ |
---|
| 637 | private void assertEvent(int index, |
---|
| 638 | long timestamp, |
---|
| 639 | String targetDOMPath, |
---|
| 640 | HtmlGUIElement target, |
---|
| 641 | HtmlDocument document, |
---|
| 642 | String eventType, |
---|
| 643 | Integer key, |
---|
| 644 | Integer[] scrollPosition, |
---|
| 645 | Integer[] coordinates, |
---|
| 646 | String clientInfoTitle, |
---|
| 647 | String clientInfoAgent, |
---|
| 648 | String clientInfoURL, |
---|
| 649 | String clientId) |
---|
| 650 | { |
---|
| 651 | assertEquals("event " + index, new Long(timestamp), events[index].getTime()); |
---|
| 652 | assertEquals("event " + index, targetDOMPath, events[index].getTargetDOMPath()); |
---|
| 653 | assertEquals("event " + index, target, events[index].getTarget()); |
---|
| 654 | assertEquals("event " + index, document, events[index].getTargetDocument()); |
---|
| 655 | assertEquals("event " + index, eventType, events[index].getEventType()); |
---|
| 656 | |
---|
| 657 | assertEquals("event " + index, key, events[index].getKey()); |
---|
| 658 | assertArrayEquals("event " + index, scrollPosition, events[index].getScrollPosition()); |
---|
| 659 | assertArrayEquals("event " + index, coordinates, events[index].getCoordinates()); |
---|
| 660 | |
---|
| 661 | assertEquals |
---|
| 662 | ("event " + index, clientInfoTitle, events[index].getClientInfos().getTitle()); |
---|
| 663 | assertEquals |
---|
| 664 | ("event " + index, clientInfoAgent, events[index].getClientInfos().getUserAgent()); |
---|
| 665 | assertEquals |
---|
| 666 | ("event " + index, clientInfoURL, events[index].getClientInfos().getUrl().toString()); |
---|
| 667 | assertEquals |
---|
| 668 | ("event " + index, clientId, events[index].getClientInfos().getClientId()); |
---|
| 669 | } |
---|
| 670 | |
---|
[864] | 671 | } |
---|