|
32 | 32 | import com.vaadin.flow.dom.Element; |
33 | 33 | import com.vaadin.flow.internal.JacksonUtils; |
34 | 34 | import com.vaadin.flow.internal.nodefeature.ElementListenerMap; |
| 35 | +import com.vaadin.flow.shared.Registration; |
35 | 36 | import com.vaadin.tests.util.MockUI; |
36 | 37 |
|
37 | 38 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
@@ -497,6 +498,120 @@ void active_signalReflectsResumeAndStop() { |
497 | 498 | assertTrue(tracker.activeSignal().peek()); |
498 | 499 | } |
499 | 500 |
|
| 501 | + // --- addPositionListener() tests --- |
| 502 | + |
| 503 | + @Test |
| 504 | + void addPositionListener_dispatchesPositionAndError() { |
| 505 | + TestComponent component = new TestComponent(); |
| 506 | + ui.add(component); |
| 507 | + |
| 508 | + GeolocationTracker tracker = ui.getGeolocation().track(component); |
| 509 | + List<GeolocationPosition> positions = new ArrayList<>(); |
| 510 | + List<GeolocationError> errors = new ArrayList<>(); |
| 511 | + tracker.addPositionListener(positions::add, errors::add); |
| 512 | + |
| 513 | + ObjectNode posData = JacksonUtils.createObjectNode(); |
| 514 | + ObjectNode posDetail = JacksonUtils.createObjectNode(); |
| 515 | + ObjectNode coords = JacksonUtils.createObjectNode(); |
| 516 | + coords.put("latitude", 60.1699); |
| 517 | + coords.put("longitude", 24.9384); |
| 518 | + coords.put("accuracy", 10.0); |
| 519 | + posDetail.set("coords", coords); |
| 520 | + posDetail.put("timestamp", 1700000000000L); |
| 521 | + posData.set("event.detail", posDetail); |
| 522 | + fireEvent(component.getElement(), "vaadin-geolocation-position", |
| 523 | + posData); |
| 524 | + |
| 525 | + ObjectNode errData = JacksonUtils.createObjectNode(); |
| 526 | + ObjectNode errDetail = JacksonUtils.createObjectNode(); |
| 527 | + errDetail.put("code", GeolocationErrorCode.TIMEOUT.code()); |
| 528 | + errDetail.put("message", "Timeout"); |
| 529 | + errData.set("event.detail", errDetail); |
| 530 | + fireEvent(component.getElement(), "vaadin-geolocation-error", errData); |
| 531 | + |
| 532 | + assertEquals(1, positions.size()); |
| 533 | + assertEquals(60.1699, positions.get(0).coords().latitude()); |
| 534 | + assertEquals(1, errors.size()); |
| 535 | + assertEquals(GeolocationErrorCode.TIMEOUT, errors.get(0).errorCode()); |
| 536 | + } |
| 537 | + |
| 538 | + @Test |
| 539 | + void addPositionListener_registrationStopsDelivery() { |
| 540 | + TestComponent component = new TestComponent(); |
| 541 | + ui.add(component); |
| 542 | + |
| 543 | + GeolocationTracker tracker = ui.getGeolocation().track(component); |
| 544 | + List<GeolocationPosition> positions = new ArrayList<>(); |
| 545 | + Registration registration = tracker.addPositionListener(positions::add, |
| 546 | + err -> { |
| 547 | + }); |
| 548 | + |
| 549 | + registration.remove(); |
| 550 | + |
| 551 | + ObjectNode posData = JacksonUtils.createObjectNode(); |
| 552 | + ObjectNode posDetail = JacksonUtils.createObjectNode(); |
| 553 | + ObjectNode coords = JacksonUtils.createObjectNode(); |
| 554 | + coords.put("latitude", 60.0); |
| 555 | + coords.put("longitude", 25.0); |
| 556 | + coords.put("accuracy", 10.0); |
| 557 | + posDetail.set("coords", coords); |
| 558 | + posDetail.put("timestamp", 1700000000000L); |
| 559 | + posData.set("event.detail", posDetail); |
| 560 | + fireEvent(component.getElement(), "vaadin-geolocation-position", |
| 561 | + posData); |
| 562 | + |
| 563 | + assertTrue(positions.isEmpty(), |
| 564 | + "removed listener must not receive subsequent updates"); |
| 565 | + } |
| 566 | + |
| 567 | + @Test |
| 568 | + void addPositionListener_survivesStopResume() { |
| 569 | + TestComponent component = new TestComponent(); |
| 570 | + ui.add(component); |
| 571 | + |
| 572 | + GeolocationTracker tracker = ui.getGeolocation().track(component); |
| 573 | + List<GeolocationPosition> positions = new ArrayList<>(); |
| 574 | + tracker.addPositionListener(positions::add, err -> { |
| 575 | + }); |
| 576 | + |
| 577 | + tracker.stop(); |
| 578 | + tracker.resume(); |
| 579 | + |
| 580 | + ObjectNode posData = JacksonUtils.createObjectNode(); |
| 581 | + ObjectNode posDetail = JacksonUtils.createObjectNode(); |
| 582 | + ObjectNode coords = JacksonUtils.createObjectNode(); |
| 583 | + coords.put("latitude", 60.0); |
| 584 | + coords.put("longitude", 25.0); |
| 585 | + coords.put("accuracy", 10.0); |
| 586 | + posDetail.set("coords", coords); |
| 587 | + posDetail.put("timestamp", 1700000000000L); |
| 588 | + posData.set("event.detail", posDetail); |
| 589 | + fireEvent(component.getElement(), "vaadin-geolocation-position", |
| 590 | + posData); |
| 591 | + |
| 592 | + assertEquals(1, positions.size()); |
| 593 | + assertEquals(60.0, positions.get(0).coords().latitude()); |
| 594 | + } |
| 595 | + |
| 596 | + @Test |
| 597 | + void addPositionListener_pendingIsSilent() { |
| 598 | + TestComponent component = new TestComponent(); |
| 599 | + ui.add(component); |
| 600 | + |
| 601 | + GeolocationTracker tracker = ui.getGeolocation().track(component); |
| 602 | + List<GeolocationPosition> positions = new ArrayList<>(); |
| 603 | + List<GeolocationError> errors = new ArrayList<>(); |
| 604 | + tracker.addPositionListener(positions::add, errors::add); |
| 605 | + |
| 606 | + // resume() resets the signal to Pending — listeners must stay silent |
| 607 | + // since they only see real outcomes. |
| 608 | + tracker.stop(); |
| 609 | + tracker.resume(); |
| 610 | + |
| 611 | + assertTrue(positions.isEmpty()); |
| 612 | + assertTrue(errors.isEmpty()); |
| 613 | + } |
| 614 | + |
500 | 615 | // --- availability() / availability-change listener tests --- |
501 | 616 |
|
502 | 617 | @Test |
|
0 commit comments