|
1 | 1 | package com.github.kklisura.cdtp.services.impl; |
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.github.kklisura.cdtp.protocol.support.types.EventHandler; |
| 5 | +import com.github.kklisura.cdtp.protocol.support.types.EventListener; |
4 | 6 | import com.github.kklisura.cdtp.services.WebSocketService; |
5 | 7 | import com.github.kklisura.cdtp.services.exceptions.ChromeDevToolsInvocationException; |
6 | 8 | import com.github.kklisura.cdtp.services.exceptions.WebSocketServiceException; |
7 | 9 | import com.github.kklisura.cdtp.services.types.ChromeTab; |
| 10 | +import com.github.kklisura.cdtp.services.types.EventListenerImpl; |
8 | 11 | import com.github.kklisura.cdtp.services.types.MethodInvocation; |
9 | 12 | import com.github.kklisura.cdtp.services.utils.ProxyUtils; |
10 | 13 | import org.easymock.Capture; |
@@ -352,6 +355,123 @@ public void testClose() { |
352 | 355 | verify(chromeService); |
353 | 356 | } |
354 | 357 |
|
| 358 | + @Test |
| 359 | + public void testAddEventListener() { |
| 360 | + EventHandler<String> eventHandler = event -> {}; |
| 361 | + |
| 362 | + EventListener eventListener = service.addEventListener("domain", "event", eventHandler, |
| 363 | + String.class); |
| 364 | + assertNotNull(eventListener); |
| 365 | + |
| 366 | + assertEquals(eventHandler, ((EventListenerImpl) eventListener).getHandler()); |
| 367 | + assertEquals("domain.event", ((EventListenerImpl) eventListener).getKey()); |
| 368 | + assertEquals(String.class, ((EventListenerImpl) eventListener).getParamType()); |
| 369 | + |
| 370 | + service.removeEventListener(eventListener); |
| 371 | + } |
| 372 | + |
| 373 | + @Test |
| 374 | + public void testEventReceivedWithOff() throws InterruptedException { |
| 375 | + // Non existing event handler |
| 376 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 377 | + |
| 378 | + Capture<TestMessage> testMessageCapture = Capture.newInstance(); |
| 379 | + |
| 380 | + EventHandler<TestMessage> eventHandler = testMessageCapture::setValue; |
| 381 | + |
| 382 | + EventListener eventListener = service.addEventListener("Domain", "name", eventHandler, TestMessage.class); |
| 383 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 384 | + |
| 385 | + assertNotNull(testMessageCapture.getValue()); |
| 386 | + assertEquals("testValue", testMessageCapture.getValue().getTestProperty()); |
| 387 | + |
| 388 | + testMessageCapture.reset(); |
| 389 | + |
| 390 | + eventListener.off(); |
| 391 | + |
| 392 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 393 | + |
| 394 | + assertFalse(testMessageCapture.hasCaptured()); |
| 395 | + } |
| 396 | + |
| 397 | + @Test |
| 398 | + public void testEventReceivedWithUnsubscribe() throws InterruptedException { |
| 399 | + // Non existing event handler |
| 400 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 401 | + |
| 402 | + Capture<TestMessage> testMessageCapture = Capture.newInstance(); |
| 403 | + |
| 404 | + EventHandler<TestMessage> eventHandler = testMessageCapture::setValue; |
| 405 | + |
| 406 | + EventListener eventListener = service.addEventListener("Domain", "name", eventHandler, TestMessage.class); |
| 407 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 408 | + |
| 409 | + assertNotNull(testMessageCapture.getValue()); |
| 410 | + assertEquals("testValue", testMessageCapture.getValue().getTestProperty()); |
| 411 | + |
| 412 | + testMessageCapture.reset(); |
| 413 | + |
| 414 | + eventListener.unsubscribe(); |
| 415 | + |
| 416 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 417 | + |
| 418 | + assertFalse(testMessageCapture.hasCaptured()); |
| 419 | + } |
| 420 | + |
| 421 | + @Test |
| 422 | + public void testEventReceivedWithUnsubscribeOnService() throws InterruptedException { |
| 423 | + // Non existing event handler |
| 424 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 425 | + |
| 426 | + Capture<TestMessage> testMessageCapture = Capture.newInstance(); |
| 427 | + |
| 428 | + EventHandler<TestMessage> eventHandler = testMessageCapture::setValue; |
| 429 | + |
| 430 | + EventListener eventListener = service.addEventListener("Domain", "name", eventHandler, TestMessage.class); |
| 431 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 432 | + |
| 433 | + assertNotNull(testMessageCapture.getValue()); |
| 434 | + assertEquals("testValue", testMessageCapture.getValue().getTestProperty()); |
| 435 | + |
| 436 | + service.removeEventListener(eventListener); |
| 437 | + |
| 438 | + testMessageCapture.reset(); |
| 439 | + |
| 440 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 441 | + |
| 442 | + assertFalse(testMessageCapture.hasCaptured()); |
| 443 | + } |
| 444 | + |
| 445 | + @Test |
| 446 | + public void testEventReceivedHandlerThrowsExeption() throws InterruptedException { |
| 447 | + // Non existing event handler |
| 448 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 449 | + |
| 450 | + Capture<TestMessage> testMessageCapture = Capture.newInstance(); |
| 451 | + |
| 452 | + EventHandler<TestMessage> eventHandlerThrowsException = event -> { |
| 453 | + throw new RuntimeException("test"); |
| 454 | + }; |
| 455 | + EventHandler<TestMessage> eventHandler = testMessageCapture::setValue; |
| 456 | + |
| 457 | + EventListener eventListenerWithException = service.addEventListener("Domain", "name", eventHandlerThrowsException, TestMessage.class); |
| 458 | + EventListener eventListener = service.addEventListener("Domain", "name", eventHandler, TestMessage.class); |
| 459 | + |
| 460 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 461 | + |
| 462 | + assertNotNull(testMessageCapture.getValue()); |
| 463 | + assertEquals("testValue", testMessageCapture.getValue().getTestProperty()); |
| 464 | + |
| 465 | + service.removeEventListener(eventListenerWithException); |
| 466 | + |
| 467 | + testMessageCapture.reset(); |
| 468 | + |
| 469 | + service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}"); |
| 470 | + |
| 471 | + assertNotNull(testMessageCapture.getValue()); |
| 472 | + assertEquals("testValue", testMessageCapture.getValue().getTestProperty()); |
| 473 | + } |
| 474 | + |
355 | 475 | private void resolveMessage(String message) { |
356 | 476 | new Thread(() -> { |
357 | 477 | try { |
|
0 commit comments