Files
buzz/mobile/test/features/channels/message_content_bubble_cases.dart
Tom Brow 44c8206bea fix(mobile): wrap inline Markdown links with their sentence (#9462)
A link inside a sentence used to be drawn as its own little widget, so a
long label jumped to a line of its own and the words after it started
yet another line. Plain links are now text in the sentence: a long label
breaks mid-label, and the words after it continue on the same line.

Tap still opens the link, a finger on it still tints it, and a hold
still opens the link sheet (Open, Copy, Share) instead of the message
menu. Buzz chips (channel and message links), audio attachments and
repository links keep their current widget look.

| Before | After |
| --- | --- |
| <img alt="before"
src="https://github.com/user-attachments/assets/e77aa97a-6ac5-43b9-bc44-cc43fb169c8f"
width="300"> | <img alt="after"
src="https://github.com/user-attachments/assets/4c76bb23-e6df-4244-bf7e-e1753b727d7b"
width="300"> |

A finger on a text link tints it, across every line of a wrapped label:

<img alt="Pressed tint on a wrapped link"
src="https://github.com/user-attachments/assets/1cde5a1a-39aa-4bc7-90f8-8d95b15ee852"
width="400">


https://github.com/user-attachments/assets/021dd099-d24b-4370-bf75-e60f273e2a92

### Regressions

One tradeoff, accepted so links can wrap:

- **VoiceOver users lose Copy and Share on text links.** VoiceOver still
opens the link, but a text span can only expose a tap to accessibility,
so the link sheet (Copy, Share) can't be reached on text links with
VoiceOver. Chips and repository links keep it. A link nothing can open
(such as mailto) shows its sheet on a tap, so it isn't dead for
VoiceOver users.

Nothing else gets worse. The pressed tint is a translucent paint-only
layer drawn on top of the message's Markdown over the link's text boxes,
so inline code and table header backgrounds can't hide it, so a press
repaints that message and rebuilds nothing; a tint that spans a wrapped
label covers both lines, and it stays inside what's visible (a long link
in a scrolled table cell tints only inside the table). Each message
reuses one recognizer per link across rebuilds and disposes them when
its text changes or the message goes away, and a scroll that starts on a
link cancels the hold, so it never opens the sheet. Only plain links
change shape (including any inline code inside a link label, which now
taps and holds as part of the link); chips, media and code outside links
are untouched.

### Validation

Added tests:
- A long link label in a narrow message breaks across lines, and the
following words continue on the line where it ends.
- Holding a link and then sliding off it before the hold delay opens
nothing (no sheet, no message menu, no open).
- Pressing a text link tints it, and lifting or sliding off clears the
tint; lifting a second finger keeps the tint.
- A pressed link in a horizontally scrolled table cell is tinted inside
the table and nothing past its edge.
- Tapping a link nothing can open (mailto), by finger or screen reader
activate, shows Copy and Share.
- A pressed link in inline code (plain and in a bubble) or in a table
header shows its tint (these fail with the tint drawn underneath).
- Code inside a link label taps and holds as part of the link; a hold
across an edit that removes the link does nothing; a second finger
mid-hold doesn't restart it.

Existing link tests (tap, hold sheet, copy and open from the sheet,
message menu beside a link, swipe to reply from a link, attachment
links) now find links inside their paragraph. They all pass, along with
the full mobile suite and analyze.

Automated simulator run (iPhone 17 Pro, internal community, synthetic
message): pressing a wrapped link tinted all three of its lines; holding
a text link opened its sheet; holding the text right after it opened the
message menu; tapping the second line of a wrapped link opened it in
Safari; a scroll starting on a link scrolled the list and opened
nothing.

---------

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-15 09:06:30 -08:00

437 lines
14 KiB
Dart

part of 'message_content_test.dart';
void bubbleContentTests() {
final bubbles = find.byWidgetPredicate(
(widget) =>
widget.key is ValueKey<String> &&
(widget.key! as ValueKey<String>).value.startsWith(
'message-text-bubble-',
),
);
testWidgets('message shapes remain inside their bounds without tails', (
tester,
) async {
for (final tail in [false, true]) {
await tester.pumpWidget(
_testable(
MessageContent(content: 'Hello', bubble: true, bubbleEnd: tail),
),
);
await tester.pumpAndSettle();
final decoration = tester.widget<LocalSendBubbleSurface>(
find.descendant(
of: bubbles,
matching: find.byType(LocalSendBubbleSurface),
),
);
final bounds = decoration.shape
.getOuterPath(Offset.zero & tester.getSize(bubbles))
.getBounds();
expect(bounds.left, 0);
expect(bounds.bottom, tester.getSize(bubbles).height);
}
});
for (final brightness in Brightness.values) {
testWidgets('bubbles inherit every chat color in $brightness', (
tester,
) async {
for (final choice in chatColorChoices) {
final theme = mobileAppearanceTheme(brightness, choice.wireValue);
for (final own in [false, true]) {
await tester.pumpWidget(
_testable(
MessageContent(
content: 'Themed message',
bubble: true,
ownMessage: own,
),
theme: theme,
),
);
await tester.pumpAndSettle();
final surface = tester.widget<LocalSendBubbleSurface>(
find.byType(LocalSendBubbleSurface),
);
expect(
surface.color,
own
? theme.extension<ChatColors>()!.outgoing
: receivedMessageSurface(brightness),
);
}
}
});
}
for (final dark in [false, true]) {
testWidgets('outgoing headings, mentions and links contrast in dark=$dark', (
tester,
) async {
String? tapped;
await tester.pumpWidget(
_testable(
MessageContent(
content:
'# H1\n\n## H2\n\n### H3\n\n#### H4\n\n##### H5\n\n###### H6\n\n@Alice @Helper [Website](https://example.com)',
bubble: true,
ownMessage: true,
mentionNames: const {'person': 'Alice', 'agent': 'Helper'},
agentMentionPubkeys: const {'agent'},
onMentionTap: (pubkey) => tapped = pubkey,
),
dark: dark,
),
);
await tester.pumpAndSettle();
final foreground = ChatColors.of(
tester.element(find.byType(MessageContent)),
).foreground;
for (final heading in ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']) {
final text = tester.widget<RichText>(_findRich(heading).last);
final colors = <Color?>[];
void inspect(InlineSpan span, TextStyle? inherited) {
final effective = inherited?.merge(span.style) ?? span.style;
if (span is TextSpan) {
if (span.text?.contains(heading) == true) {
colors.add(effective?.color);
}
for (final child in span.children ?? const <InlineSpan>[]) {
inspect(child, effective);
}
}
}
inspect(text.text, null);
expect(colors, [foreground]);
}
final linkColor = ChatColors.of(
tester.element(find.byType(MessageContent)),
).foreground;
for (final label in ['Alice', 'Helper']) {
expect(tester.widget<Text>(find.text(label)).style?.color, linkColor);
}
expect(
tester.widget<BuzzAgentIcon>(find.byType(BuzzAgentIcon)).color,
linkColor,
);
expect(linkStyle(tester, 'Website')?.color, linkColor);
await tester.tap(find.text('Alice'));
expect(tapped, 'person');
await tester.tap(find.text('Helper'));
expect(tapped, 'agent');
});
}
for (final content in [
'- screenshots:\n ![result](https://example.com/result.png)\n explanation',
'1. screenshots:\n ![result](https://example.com/result.png)\n explanation',
'> screenshots:\n> ![result](https://example.com/result.png)\n> explanation',
'> screenshots:\n![result](https://example.com/result.png)\nexplanation',
]) {
testWidgets('nested media retains its Markdown block: $content', (
tester,
) async {
await tester.pumpWidget(
_testable(MessageContent(content: content, bubble: true)),
);
await tester.pumpAndSettle();
expect(bubbles, findsOneWidget);
final markdown = tester.widget<GptMarkdown>(find.byType(GptMarkdown));
expect(markdown.data, content);
final media = find.byKey(
const ValueKey(
'message-media-image-preview:https://example.com/result.png',
),
);
expect(find.descendant(of: bubbles, matching: media), findsOneWidget);
expect(tester.takeException(), isNull);
});
}
testWidgets(
'image corners remain uniform throughout the conversation group',
(tester) async {
for (final grouped in [false, true]) {
await tester.pumpWidget(
_testable(
MessageContent(
content: '![photo](https://example.com/photo.png)',
bubble: true,
bubbleStart: !grouped,
bubbleEnd: !grouped,
),
),
);
await tester.pumpAndSettle();
final frame = tester.widget<Container>(
find.byKey(
const ValueKey(
'message-media-image-preview:https://example.com/photo.png',
),
),
);
final corners =
(frame.decoration! as BoxDecoration).borderRadius! as BorderRadius;
expect(corners.topLeft.x, Radii.container);
expect(corners.bottomLeft.x, Radii.container);
expect(corners.topRight.x, Radii.container);
expect(corners.bottomRight.x, Radii.container);
}
},
);
testWidgets('photo gallery stays outside its caption bubble', (tester) async {
await tester.pumpWidget(
_testable(
const MessageContent(
content:
'Photos\n![one](https://example.com/one.png)\n![two](https://example.com/two.png)',
bubble: true,
),
),
);
await tester.pumpAndSettle();
final gallery = find.byKey(const ValueKey('message-media-stack'));
expect(gallery, findsOneWidget);
expect(bubbles, findsOneWidget);
expect(find.descendant(of: bubbles, matching: gallery), findsNothing);
expect(
tester.getTopLeft(gallery).dy,
greaterThanOrEqualTo(tester.getBottomLeft(bubbles).dy),
);
});
testWidgets('neutral bubbles hug short text and distinguish own messages', (
tester,
) async {
for (final own in [false, true]) {
await tester.pumpWidget(
_testable(
MessageContent(content: 'Hello', bubble: true, ownMessage: own),
),
);
await tester.pumpAndSettle();
expect(bubbles, findsOneWidget);
expect(tester.getSize(bubbles).width, lessThan(160));
final decoration = tester.widget<LocalSendBubbleSurface>(
find.descendant(
of: bubbles,
matching: find.byType(LocalSendBubbleSurface),
),
);
expect(decoration.color, isNotNull);
expect(
find.descendant(of: bubbles, matching: _findRich('Hello')),
findsWidgets,
);
}
});
testWidgets('attachments stay outside bubbles and text order is preserved', (
tester,
) async {
await tester.pumpWidget(
_testable(
const MessageContent(
content:
// The escaped destination must still match its plain imeta URL.
'Before\n\n[file.txt](https://example.com/file\\_a.txt)\n\nAfter',
tags: [
['imeta', 'url https://example.com/file_a.txt', 'm text/plain'],
],
bubble: true,
),
),
);
await tester.pumpAndSettle();
expect(bubbles, findsNWidgets(2));
expect(
find.descendant(of: bubbles, matching: _findRich('file.txt')),
findsNothing,
);
expect(
tester.getTopLeft(_findRich('Before')).dy,
lessThan(tester.getTopLeft(_findRich('file.txt')).dy),
);
expect(
tester.getTopLeft(_findRich('file.txt')).dy,
lessThan(tester.getTopLeft(_findRich('After')).dy),
);
});
testWidgets('fenced attachment examples remain in the text bubble', (
tester,
) async {
await tester.pumpWidget(
_testable(
const MessageContent(
content: '```text\n![example](https://example.com/image.png)\n```',
bubble: true,
),
),
);
await tester.pumpAndSettle();
expect(bubbles, findsOneWidget);
expect(
find.descendant(
of: bubbles,
matching: _findRich('![example](https://example.com/image.png)'),
),
findsWidgets,
);
expect(tester.takeException(), isNull);
});
testWidgets('bubble text reflows at narrow width and large text', (
tester,
) async {
_setSurfaceSize(tester, const Size(320, 800));
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
_testable(
MediaQuery(
data: const MediaQueryData(
size: Size(320, 800),
textScaler: TextScaler.linear(2),
),
child: const SizedBox(
width: 230,
child: MessageContent(
content:
'A longer message that wraps across multiple lines without clipping.',
bubble: true,
),
),
),
),
);
await tester.pumpAndSettle();
expect(tester.getSize(bubbles).width, lessThanOrEqualTo(230));
expect(tester.getSize(bubbles).height, greaterThan(80));
expect(tester.takeException(), isNull);
});
}
void editedMarkerTests() {
Iterable<String> plainTexts(WidgetTester tester) => tester
.widgetList<RichText>(find.byType(RichText))
.map((text) => text.text.toPlainText());
TextSpan? markerSpan(WidgetTester tester) {
TextSpan? found;
for (final text in tester.widgetList<RichText>(find.byType(RichText))) {
text.text.visitChildren((span) {
if (span is TextSpan && span.text == '(edited)') found = span;
return found == null;
});
}
return found;
}
testWidgets('an edit is marked after the body, never inside other text', (
tester,
) async {
Future<void> show(String content, {bool edited = true, bool own = false}) =>
tester
.pumpWidget(
_testable(
MessageContent(
content: content,
bubble: true,
edited: edited,
ownMessage: own,
),
),
)
.then((_) => tester.pumpAndSettle());
// Prose: inline after the last line, in the muted metadata colour.
await show('First\n\nHi how are ya.');
expect(plainTexts(tester), contains('Hi how are ya. (edited)'));
final context = tester.element(find.byType(MessageContent));
expect(markerSpan(tester)?.style?.color, context.colors.onSurfaceVariant);
// Your own accent bubble draws it in the bubble's ink.
await show('Mine', own: true);
expect(plainTexts(tester), contains('Mine (edited)'));
expect(
markerSpan(tester)?.style?.color,
ChatColors.of(tester.element(find.byType(MessageContent))).foreground,
);
// Bodies the slot could restyle render exactly as unedited, plus the
// marker on its own line inside the same text bubble.
for (final content in [
'```npm install```',
'Run this:\n```npm install```',
'Intro\n\n```\ncode\n```',
'````\ncode\n````',
'```\nunclosed',
'| a | b |\n| - | - |\n| 1 | 2 |',
'Above\n\n---',
'ok\n-',
'ok\n#',
'todo\n[x]',
'# Heading',
'ok\n\nhttps://example.com/file.pdf',
]) {
await show(content, edited: false);
final unedited = plainTexts(tester).toList();
await show(content);
expect(plainTexts(tester), [...unedited, '(edited)'], reason: content);
expect(
find.descendant(
of: find.byKey(const ValueKey('message-text-bubble-0')),
matching: find.text('(edited)'),
),
findsOneWidget,
reason: content,
);
}
// An escaped bracket is text, not display math, so the marker joins it.
await show(r'see \[x\]');
expect(plainTexts(tester), contains('see [x] (edited)'));
// Inside an unclosed <u> or a heading the marker keeps its own style.
await show('see <u>this');
expect(markerSpan(tester)?.style?.decoration, TextDecoration.none);
// Trailing media: the marker follows it, outside every text bubble.
await show('![](https://example.com/a.png)');
expect(find.text('(edited)'), findsOneWidget);
expect(
find.descendant(
of: find.byWidgetPredicate(
(widget) =>
widget.key is ValueKey<String> &&
(widget.key! as ValueKey<String>).value.startsWith(
'message-text-bubble-',
),
),
matching: find.text('(edited)'),
),
findsNothing,
);
// Unedited bodies stay unmarked. An authored slot character is drawn as
// written, and only an edit adds the marker, on its own line.
bool shows(String text) => tester
.widgetList<RichText>(find.byType(RichText))
.any((rich) => rich.text.toPlainText().contains(text));
await show('Plain', edited: false);
expect(markerSpan(tester), isNull);
await show('Sneaky $editedSlot', edited: false);
expect(shows('Sneaky $editedSlot'), isTrue);
expect(markerSpan(tester), isNull);
await show('Sneaky $editedSlot');
expect(shows('Sneaky $editedSlot'), isTrue);
expect(find.text('(edited)'), findsOneWidget);
// Also when the authored slot is in an earlier part of the message.
await show('Sneaky $editedSlot\n![](https://example.com/a.png)\nafter');
expect(shows('Sneaky $editedSlot'), isTrue);
expect(find.text('(edited)'), findsOneWidget);
});
}