Skip to content

Commit e1486b6

Browse files
tomivirkkiclaude
andauthored
feat: add typing indicator API to MessageList (#10116)
## Summary - Chat and assistant views need to show who is currently typing. - Typing users are modelled as a `MessageListUser` type instead of flat fields, because the indicator shows an avatar: name, abbreviation, image and color index, with `DownloadHandler` images registered as a resource of the list. - The whole API is experimental behind `com.vaadin.experimental.messageListTypingIndicator`, or the `aiComponents` umbrella flag, so its shape can still change while the AI components adopt it. --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 8f6b28b commit e1486b6

18 files changed

Lines changed: 2246 additions & 2 deletions

File tree

‎vaadin-messages-flow-parent/vaadin-messages-flow-integration-tests/src/main/java/com/vaadin/flow/component/messages/tests/MessageListPage.java‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
import com.vaadin.flow.component.html.NativeButton;
2424
import com.vaadin.flow.component.html.Span;
2525
import com.vaadin.flow.component.messages.MessageList;
26+
import com.vaadin.flow.component.messages.MessageListI18n;
2627
import com.vaadin.flow.component.messages.MessageListItem;
28+
import com.vaadin.flow.component.messages.MessageListTypingIndicatorType;
29+
import com.vaadin.flow.component.messages.MessageListUser;
2730
import com.vaadin.flow.router.Route;
2831
import com.vaadin.flow.server.Command;
2932
import com.vaadin.flow.server.StreamResource;
@@ -32,6 +35,11 @@
3235
@Route("vaadin-messages/message-list-test")
3336
public class MessageListPage extends Div {
3437

38+
// A 1x1 transparent GIF, so that the avatar image loads without the test
39+
// depending on a served resource
40+
static final String TYPING_USER_IMAGE = "data:image/gif;base64,"
41+
+ "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
42+
3543
public MessageListPage() {
3644
UI.getCurrent().setLocale(Locale.ENGLISH);
3745

@@ -110,6 +118,57 @@ public MessageListPage() {
110118
"#agenda.pdf", "application/pdf"));
111119
});
112120

121+
var alice = new MessageListUser("Alice");
122+
alice.setColorIndex(2);
123+
var bob = new MessageListUser("Bob");
124+
bob.setColorIndex(3);
125+
126+
addButton("showTyping", () -> messageList.setTypingUsers(alice));
127+
addButton("showTwoTyping",
128+
() -> messageList.setTypingUsers(alice, bob));
129+
addButton("hideTyping", () -> messageList.setTypingUsers());
130+
addButton("setI18n", () -> messageList.setI18n(
131+
new MessageListI18n().setTypingIndicatorText("is thinking")));
132+
addButton("setEllipsisTypingIndicator",
133+
() -> messageList.setTypingIndicatorType(
134+
MessageListTypingIndicatorType.ELLIPSIS));
135+
addButton("setDefaultTypingIndicator",
136+
() -> messageList.setTypingIndicatorType(
137+
MessageListTypingIndicatorType.DEFAULT));
138+
addButton("showTypingWithImageHandler", () -> {
139+
alice.setImageHandler(DownloadHandler.forClassResource(getClass(),
140+
"/META-INF/resources/images/avatar.png",
141+
"typing-user-img"));
142+
messageList.setTypingUsers(alice);
143+
});
144+
145+
var carol = new MessageListUser();
146+
carol.setName("Carol");
147+
carol.setAbbreviation("CA");
148+
carol.setImage(TYPING_USER_IMAGE);
149+
carol.setColorIndex(4);
150+
carol.addClassNames("carol", "typing");
151+
152+
var dave = new MessageListUser("Dave", TYPING_USER_IMAGE);
153+
154+
addButton("showTypingWithUserProperties",
155+
() -> messageList.setTypingUsers(carol));
156+
addButton("updateTypingUserProperties", () -> {
157+
carol.setName("Carol Carter");
158+
carol.setAbbreviation("CC");
159+
carol.setImage(null);
160+
carol.setColorIndex(5);
161+
carol.removeClassNames("typing");
162+
});
163+
addButton("showTypingWithImageUrl",
164+
() -> messageList.setTypingUsers(dave));
165+
166+
var eve = new MessageListUser();
167+
eve.setAbbreviation("EV");
168+
169+
addButton("showTypingWithAbbreviationOnly",
170+
() -> messageList.setTypingUsers(eve));
171+
113172
// Output section for test verification
114173
Div outputSection = new Div();
115174
outputSection.getStyle().set("margin-top", "20px");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2000-2026 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.messages.tests;
17+
18+
import java.util.List;
19+
20+
import com.vaadin.flow.component.html.Div;
21+
import com.vaadin.flow.component.html.NativeButton;
22+
import com.vaadin.flow.component.messages.MessageList;
23+
import com.vaadin.flow.component.messages.MessageListItem;
24+
import com.vaadin.flow.component.messages.MessageListUser;
25+
import com.vaadin.flow.router.Route;
26+
import com.vaadin.flow.server.Command;
27+
import com.vaadin.flow.signals.local.ValueSignal;
28+
29+
@Route("vaadin-messages/message-list-typing-signal-test")
30+
public class MessageListTypingSignalPage extends Div {
31+
32+
public MessageListTypingSignalPage() {
33+
var alice = new ValueSignal<>(new MessageListUser("Alice"));
34+
var typingUsers = new ValueSignal<List<ValueSignal<MessageListUser>>>(
35+
List.of());
36+
37+
var messageList = new MessageList(new MessageListItem("foo", "sender"));
38+
messageList.bindTypingUsers(typingUsers);
39+
add(messageList);
40+
41+
addButton("showTyping", () -> typingUsers.set(List.of(alice)));
42+
addButton("hideTyping", () -> typingUsers.set(List.of()));
43+
addButton("renameTypingUser",
44+
() -> alice.set(new MessageListUser("Alice Smith")));
45+
}
46+
47+
private void addButton(String id, Command action) {
48+
var button = new NativeButton(id, e -> action.execute());
49+
button.setId(id);
50+
add(button);
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.vaadin.experimental.messageListTypingIndicator=true

‎vaadin-messages-flow-parent/vaadin-messages-flow-integration-tests/src/test/java/com/vaadin/flow/component/messages/tests/MessageListIT.java‎

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.vaadin.flow.component.messages.testbench.MessageElement;
2727
import com.vaadin.flow.component.messages.testbench.MessageListElement;
2828
import com.vaadin.flow.testutil.TestPath;
29+
import com.vaadin.testbench.TestBenchElement;
2930
import com.vaadin.tests.AbstractComponentIT;
3031

3132
@TestPath("vaadin-messages/message-list-test")
@@ -342,6 +343,234 @@ public void clickImageAttachment_eventFired() {
342343
clickedAttachment.getText());
343344
}
344345

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+
345574
private MessageElement getFirstMessage(MessageListElement list) {
346575
return list.getMessageElements().get(0);
347576
}

0 commit comments

Comments
 (0)