|
26 | 26 | import com.vaadin.flow.component.messages.testbench.MessageElement; |
27 | 27 | import com.vaadin.flow.component.messages.testbench.MessageListElement; |
28 | 28 | import com.vaadin.flow.testutil.TestPath; |
| 29 | +import com.vaadin.testbench.TestBenchElement; |
29 | 30 | import com.vaadin.tests.AbstractComponentIT; |
30 | 31 |
|
31 | 32 | @TestPath("vaadin-messages/message-list-test") |
@@ -342,6 +343,234 @@ public void clickImageAttachment_eventFired() { |
342 | 343 | clickedAttachment.getText()); |
343 | 344 | } |
344 | 345 |
|
| 346 | + @Test |
| 347 | + public void showTyping_typingIndicatorRendered() { |
| 348 | + clickElementWithJs("showTyping"); |
| 349 | + |
| 350 | + Assert.assertEquals("Unexpected typing users", List.of("Alice"), |
| 351 | + messageList.getTypingUserNames()); |
| 352 | + Assert.assertTrue("Unexpected typing indicator text", |
| 353 | + getTypingIndicatorText().contains("Typing")); |
| 354 | + } |
| 355 | + |
| 356 | + @Test |
| 357 | + public void showTyping_typingIndicatorNotIncludedInMessages() { |
| 358 | + clickElementWithJs("showTyping"); |
| 359 | + |
| 360 | + Assert.assertEquals("Typing indicator must not be listed as a message", |
| 361 | + 2, messageList.getMessageElements().size()); |
| 362 | + } |
| 363 | + |
| 364 | + @Test |
| 365 | + public void showTwoTyping_bothTypingUsersRendered() { |
| 366 | + clickElementWithJs("showTwoTyping"); |
| 367 | + |
| 368 | + Assert.assertEquals("Unexpected typing users", List.of("Alice", "Bob"), |
| 369 | + messageList.getTypingUserNames()); |
| 370 | + } |
| 371 | + |
| 372 | + @Test |
| 373 | + public void hideTyping_typingIndicatorRemoved() { |
| 374 | + clickElementWithJs("showTyping"); |
| 375 | + Assert.assertNotNull(getTypingIndicator()); |
| 376 | + |
| 377 | + clickElementWithJs("hideTyping"); |
| 378 | + |
| 379 | + Assert.assertNull("Expected no typing indicator", getTypingIndicator()); |
| 380 | + Assert.assertTrue(messageList.getTypingUserNames().isEmpty()); |
| 381 | + } |
| 382 | + |
| 383 | + @Test |
| 384 | + public void setI18n_showTyping_typingIndicatorTextTranslated() { |
| 385 | + clickElementWithJs("setI18n"); |
| 386 | + clickElementWithJs("showTyping"); |
| 387 | + |
| 388 | + Assert.assertTrue("Unexpected typing indicator text", |
| 389 | + getTypingIndicatorText().contains("is thinking")); |
| 390 | + } |
| 391 | + |
| 392 | + @Test |
| 393 | + public void showTyping_setI18n_visibleTypingIndicatorTextTranslated() { |
| 394 | + clickElementWithJs("showTyping"); |
| 395 | + Assert.assertTrue("Expected the default text", |
| 396 | + getTypingIndicatorText().contains("Typing")); |
| 397 | + |
| 398 | + clickElementWithJs("setI18n"); |
| 399 | + |
| 400 | + Assert.assertTrue("Unexpected typing indicator text", |
| 401 | + getTypingIndicatorText().contains("is thinking")); |
| 402 | + } |
| 403 | + |
| 404 | + @Test |
| 405 | + public void showTyping_setEllipsisType_visibleTypingIndicatorUpdated() { |
| 406 | + clickElementWithJs("showTyping"); |
| 407 | + Assert.assertEquals("Expected the default type", "", |
| 408 | + getTypingIndicatorType()); |
| 409 | + |
| 410 | + clickElementWithJs("setEllipsisTypingIndicator"); |
| 411 | + Assert.assertEquals("Unexpected typing indicator type", "ellipsis", |
| 412 | + getTypingIndicatorType()); |
| 413 | + |
| 414 | + clickElementWithJs("setDefaultTypingIndicator"); |
| 415 | + Assert.assertEquals("Expected the default type", "", |
| 416 | + getTypingIndicatorType()); |
| 417 | + } |
| 418 | + |
| 419 | + @Test |
| 420 | + public void showTypingWithImageHandler_avatarImageLoaded() { |
| 421 | + clickElementWithJs("showTypingWithImageHandler"); |
| 422 | + var avatar = getTypingUserAvatar(); |
| 423 | + |
| 424 | + var imageUrl = avatar.getPropertyString("img"); |
| 425 | + Assert.assertNotNull("Expected an avatar image URL", imageUrl); |
| 426 | + Assert.assertTrue( |
| 427 | + "Image URL should start with 'VAADIN/dynamic', was " + imageUrl, |
| 428 | + imageUrl.startsWith("VAADIN/dynamic")); |
| 429 | + |
| 430 | + // The image only decodes if the download handler is actually served |
| 431 | + waitUntil(driver -> isAvatarImageLoaded(avatar)); |
| 432 | + } |
| 433 | + |
| 434 | + @Test |
| 435 | + public void showTypingWithUserProperties_userPropertiesRenderedOnAvatar() { |
| 436 | + clickElementWithJs("showTypingWithUserProperties"); |
| 437 | + |
| 438 | + Assert.assertEquals("Unexpected typing users", List.of("Carol"), |
| 439 | + messageList.getTypingUserNames()); |
| 440 | + |
| 441 | + var avatar = getTypingUserAvatar(); |
| 442 | + Assert.assertEquals("Unexpected avatar name", "Carol", |
| 443 | + avatar.getPropertyString("name")); |
| 444 | + Assert.assertEquals("Unexpected avatar abbreviation", "CA", |
| 445 | + avatar.getPropertyString("abbr")); |
| 446 | + Assert.assertEquals("Unexpected avatar image", |
| 447 | + MessageListPage.TYPING_USER_IMAGE, |
| 448 | + avatar.getPropertyString("img")); |
| 449 | + Assert.assertEquals("Unexpected avatar color index", Integer.valueOf(4), |
| 450 | + avatar.getPropertyInteger("colorIndex")); |
| 451 | + Assert.assertTrue("Expected the class names of the user on the avatar", |
| 452 | + avatar.getClassNames().containsAll(List.of("carol", "typing"))); |
| 453 | + |
| 454 | + waitUntil(driver -> isAvatarImageLoaded(avatar)); |
| 455 | + } |
| 456 | + |
| 457 | + @Test |
| 458 | + public void showTypingWithUserProperties_updateUser_avatarUpdated() { |
| 459 | + clickElementWithJs("showTypingWithUserProperties"); |
| 460 | + |
| 461 | + clickElementWithJs("updateTypingUserProperties"); |
| 462 | + |
| 463 | + Assert.assertEquals("Unexpected typing users", List.of("Carol Carter"), |
| 464 | + messageList.getTypingUserNames()); |
| 465 | + |
| 466 | + var avatar = getTypingUserAvatar(); |
| 467 | + Assert.assertEquals("Unexpected avatar name", "Carol Carter", |
| 468 | + avatar.getPropertyString("name")); |
| 469 | + Assert.assertEquals("Unexpected avatar abbreviation", "CC", |
| 470 | + avatar.getPropertyString("abbr")); |
| 471 | + Assert.assertFalse("Expected the avatar image to be removed", |
| 472 | + isAvatarImageVisible(avatar)); |
| 473 | + Assert.assertEquals("Unexpected avatar color index", Integer.valueOf(5), |
| 474 | + avatar.getPropertyInteger("colorIndex")); |
| 475 | + Assert.assertTrue("Expected the remaining class name on the avatar", |
| 476 | + avatar.hasClassName("carol")); |
| 477 | + Assert.assertFalse("Expected the removed class name to be gone", |
| 478 | + avatar.hasClassName("typing")); |
| 479 | + } |
| 480 | + |
| 481 | + @Test |
| 482 | + public void showTypingWithImageUrl_nameAndImageRenderedOnAvatar() { |
| 483 | + clickElementWithJs("showTypingWithImageUrl"); |
| 484 | + |
| 485 | + Assert.assertEquals("Unexpected typing users", List.of("Dave"), |
| 486 | + messageList.getTypingUserNames()); |
| 487 | + |
| 488 | + var avatar = getTypingUserAvatar(); |
| 489 | + Assert.assertEquals("Unexpected avatar name", "Dave", |
| 490 | + avatar.getPropertyString("name")); |
| 491 | + Assert.assertEquals("Unexpected avatar image", |
| 492 | + MessageListPage.TYPING_USER_IMAGE, |
| 493 | + avatar.getPropertyString("img")); |
| 494 | + |
| 495 | + waitUntil(driver -> isAvatarImageLoaded(avatar)); |
| 496 | + } |
| 497 | + |
| 498 | + @Test |
| 499 | + public void showTypingWithAbbreviationOnly_abbreviationShownAsName() { |
| 500 | + clickElementWithJs("showTypingWithAbbreviationOnly"); |
| 501 | + |
| 502 | + Assert.assertEquals("Unexpected typing users", List.of("EV"), |
| 503 | + messageList.getTypingUserNames()); |
| 504 | + Assert.assertTrue("Unexpected typing indicator text", |
| 505 | + getTypingIndicatorText().contains("EV")); |
| 506 | + } |
| 507 | + |
| 508 | + @Test |
| 509 | + public void showTwoTyping_changeLocale_typingIndicatorRetained() { |
| 510 | + clickElementWithJs("showTwoTyping"); |
| 511 | + |
| 512 | + clickElementWithJs("setLocale"); |
| 513 | + |
| 514 | + // The locale change re-renders the messages, which must not drop the |
| 515 | + // typing indicator that is rendered along with them |
| 516 | + Assert.assertTrue( |
| 517 | + "Expected the messages to be rendered in the new " + "locale", |
| 518 | + getFirstMessage(messageList).getTime() |
| 519 | + .matches("1 gen 2021, [0-9]+:[0-9]+")); |
| 520 | + Assert.assertEquals("Unexpected typing users", List.of("Alice", "Bob"), |
| 521 | + messageList.getTypingUserNames()); |
| 522 | + Assert.assertTrue("Unexpected typing indicator text", |
| 523 | + getTypingIndicatorText().contains("Typing")); |
| 524 | + } |
| 525 | + |
| 526 | + @Test |
| 527 | + public void showTyping_reattachList_typingIndicatorRestored() { |
| 528 | + clickElementWithJs("showTyping"); |
| 529 | + clickElementWithJs("detachList"); |
| 530 | + clickElementWithJs("attachList"); |
| 531 | + |
| 532 | + messageList = $(MessageListElement.class).first(); |
| 533 | + Assert.assertEquals("Expected the typing indicator to be restored", |
| 534 | + List.of("Alice"), messageList.getTypingUserNames()); |
| 535 | + } |
| 536 | + |
| 537 | + /** |
| 538 | + * Gets the element rendered as the typing indicator, or {@code null} if no |
| 539 | + * user is typing. Intentionally relies on how the web component renders the |
| 540 | + * indicator, as the public TestBench API only exposes the user names. |
| 541 | + */ |
| 542 | + private TestBenchElement getTypingIndicator() { |
| 543 | + var indicators = messageList.$(MessageElement.class) |
| 544 | + .withAttribute("slot", "typing-indicator").all(); |
| 545 | + return indicators.isEmpty() ? null : indicators.get(0); |
| 546 | + } |
| 547 | + |
| 548 | + private TestBenchElement getTypingUserAvatar() { |
| 549 | + return getTypingIndicator().$("vaadin-avatar").first(); |
| 550 | + } |
| 551 | + |
| 552 | + private boolean isAvatarImageVisible(TestBenchElement avatar) { |
| 553 | + return (Boolean) executeScript(""" |
| 554 | + const img = arguments[0].shadowRoot.querySelector('img'); |
| 555 | + return !!img && !img.hidden; |
| 556 | + """, avatar); |
| 557 | + } |
| 558 | + |
| 559 | + private boolean isAvatarImageLoaded(TestBenchElement avatar) { |
| 560 | + return (Boolean) executeScript(""" |
| 561 | + const img = arguments[0].shadowRoot.querySelector('img'); |
| 562 | + return !!(img && img.complete && img.naturalWidth > 0); |
| 563 | + """, avatar); |
| 564 | + } |
| 565 | + |
| 566 | + private String getTypingIndicatorText() { |
| 567 | + return getTypingIndicator().getPropertyString("textContent"); |
| 568 | + } |
| 569 | + |
| 570 | + private String getTypingIndicatorType() { |
| 571 | + return getTypingIndicator().getDomAttribute("typing-indicator"); |
| 572 | + } |
| 573 | + |
345 | 574 | private MessageElement getFirstMessage(MessageListElement list) { |
346 | 575 | return list.getMessageElements().get(0); |
347 | 576 | } |
|
0 commit comments