Read chat replies aloud with word highlighting

An assistant reply gets Read aloud beside Copy; while it reads it becomes
Pause/Resume and Stop, one reply at a time app-wide. The spoken word is
highlighted in the rendered reply (across multi-part replies) and the
transcript scrolls only when that word leaves the viewport. Reading stops
when its chat closes or is swapped, another reply starts, or the app goes
to the background. Markdown is spoken as sentences; code blocks are
announced as "Code block.".

Speech goes through a SpeechEngine seam with a flutter_tts implementation
(Android, macOS, web; hidden on Linux) and a fake for tests. Settings has
a Read aloud section: a voice picker grouped by language (device language
first, a sample per voice), a rate slider and reset, saved per device.

flutter_tts has no Swift package, so macOS/iOS gain the CocoaPods
integration (Podfiles with the Runner deployment targets); Android
declares the TTS_SERVICE query. The capture fixture gains ?speech=fake.
This commit is contained in:
2026-11-13 01:29:04 +00:00
parent 26e68140fd
commit 6367cce68a
40 changed files with 2643 additions and 29 deletions
@@ -0,0 +1,74 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/speech/speech.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_read_aloud_button.dart';
import '../speech/fake_speech_engine.dart';
import 'chat_thread_fixtures.dart';
Widget _thread(FakeChatGateway gateway, String sessionId) => MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(
body: ChatThreadView(gateway: gateway, sessionId: sessionId, onBack: () {}),
),
);
Future<(FakeSpeechEngine, SpeechController, FakeChatGateway)> _reading(
WidgetTester tester,
) async {
tester.view.physicalSize = const Size(800, 1600);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final engine = FakeSpeechEngine();
final speech = installFakeSpeech(engine);
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage([
chatMessage(
'a1',
'assistant',
parts: [
{'type': 'text', 'text': 'Hello from the agent.'},
],
),
]);
await tester.pumpWidget(_thread(gateway, 's1'));
await tester.pumpAndSettle();
await tester.tap(find.byTooltip(chatReadAloudLabel));
await tester.pump();
engine.onStart!();
expect(engine.spoken, ['Hello from the agent.']);
expect(speech.messageId, startsWith(speechScopePrefix('s1')));
return (engine, speech, gateway);
}
void main() {
testWidgets('switching the pane to another chat stops reading', (
tester,
) async {
final (engine, speech, gateway) = await _reading(tester);
await tester.pumpWidget(_thread(gateway, 's2'));
await tester.pumpAndSettle();
expect(speech.state, SpeechState.idle);
expect(engine.calls, contains('stop'));
});
testWidgets('closing the chat stops reading', (tester) async {
final (engine, speech, _) = await _reading(tester);
await tester.pumpWidget(const MaterialApp(home: SizedBox()));
await tester.pumpAndSettle();
expect(speech.state, SpeechState.idle);
expect(engine.calls.last, 'stop');
});
testWidgets('closing a chat leaves another chat reading', (tester) async {
final (engine, speech, _) = await _reading(tester);
await speech.read(speechMessageId('elsewhere', 'b1'), ['Other reply']);
engine.onStart!();
await tester.pumpWidget(const MaterialApp(home: SizedBox()));
await tester.pumpAndSettle();
expect(speech.state, SpeechState.speaking);
expect(speech.messageId, speechMessageId('elsewhere', 'b1'));
});
}
@@ -0,0 +1,79 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/speech/speech.dart';
/// A scripted [SpeechEngine]: it records calls and a test fires the platform's
/// events. [stop] reports a cancel like every real platform does.
class FakeSpeechEngine extends SpeechEngine {
FakeSpeechEngine({this.supported = true, this.available = const []});
@override
final bool supported;
final List<SpeechVoice> available;
final calls = <String>[];
final spoken = <String>[];
double? rate;
SpeechVoice? voice;
@override
Future<void> speak(String text) async {
calls.add('speak');
spoken.add(text);
}
@override
Future<void> pause() async => calls.add('pause');
@override
Future<void> stop() async {
calls.add('stop');
onCancel?.call();
}
@override
Future<void> setRate(double rate) async => this.rate = rate;
@override
Future<void> setVoice(SpeechVoice? voice) async => this.voice = voice;
@override
Future<List<SpeechVoice>> voices() async => available;
/// Reports [word] (its first occurrence at or after [from]) being spoken.
void speakWord(String word, {int from = 0}) {
final text = spoken.last;
final start = text.indexOf(word, from);
expect(start, greaterThanOrEqualTo(0), reason: '"$word" is not spoken');
onProgress?.call(text, start, start + word.length);
}
}
/// Saved read-aloud settings held in memory, surviving a "reload".
class MemorySpeechPreferences implements SpeechPreferencesStore {
SpeechPreferences? saved;
@override
Future<SpeechPreferences?> load() async => saved;
@override
Future<bool> save(SpeechPreferences preferences) async {
saved = preferences;
return true;
}
}
/// Installs a reader over [engine] as the app's own for one test.
SpeechController installFakeSpeech(
FakeSpeechEngine engine, {
SpeechPreferencesStore? store,
}) {
final controller = SpeechController(
engine: engine,
store: store ?? MemorySpeechPreferences(),
);
debugSpeechController = controller;
addTearDown(() {
debugSpeechController = null;
controller.dispose();
});
return controller;
}
@@ -0,0 +1,168 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/speech/speech.dart';
import 'fake_speech_engine.dart';
void main() {
late FakeSpeechEngine engine;
late SpeechController speech;
setUp(() {
engine = FakeSpeechEngine();
});
SpeechController reader([SpeechPreferencesStore? store]) =>
speech = SpeechController(
engine: engine,
store: store ?? MemorySpeechPreferences(),
);
tearDown(() => speech.dispose());
testWidgets('read, pause, resume and stop move through the states', (
tester,
) async {
reader();
await speech.read('a', ['Hello **there**.']);
expect(speech.state, SpeechState.speaking);
expect(speech.messageId, 'a');
expect(engine.spoken.last, 'Hello there.');
engine.onStart!();
await speech.pause();
expect(speech.state, SpeechState.paused);
expect(engine.calls.last, 'pause');
engine.onPause!();
expect(speech.state, SpeechState.paused);
await speech.resume();
expect(speech.state, SpeechState.speaking);
// Every platform resumes when the paused text is spoken again.
expect(engine.spoken.last, 'Hello there.');
engine.onResume!();
await speech.stop();
expect(speech.state, SpeechState.idle);
expect(speech.messageId, isNull);
expect(speech.document, isNull);
});
testWidgets('starting another reply stops the first', (tester) async {
reader();
await speech.read('a', ['First reply']);
engine.onStart!();
engine.speakWord('reply');
await speech.read('b', ['Second reply']);
expect(engine.calls, ['speak', 'stop', 'speak']);
expect(speech.messageId, 'b');
expect(speech.word.value, isNull);
// A late cancel of the first reply does not end the second.
engine.onCancel!();
expect(speech.messageId, 'b');
expect(speech.state, SpeechState.speaking);
});
testWidgets('progress sets the word and maps back to the source', (
tester,
) async {
reader();
const first = 'Intro **bold** word';
const second = '- item word';
await speech.read('a', [first, second]);
engine.onStart!();
engine.speakWord('bold');
final document = speech.document!;
expect(speech.word.value, isNotNull);
expect(document.sourceRange(0, speech.word.value!), (
start: first.indexOf('bold'),
end: first.indexOf('bold') + 4,
));
expect(document.sourceRange(1, speech.word.value!), isNull);
final spoken = engine.spoken.last;
engine.speakWord('word', from: spoken.lastIndexOf('item'));
expect(document.sourceRange(0, speech.word.value!), isNull);
expect(document.sourceRange(1, speech.word.value!), (
start: second.indexOf('word'),
end: second.length,
));
});
testWidgets('progress after a resumed suffix keeps document offsets', (
tester,
) async {
reader();
await speech.read('a', ['one two three']);
engine.onStart!();
// Android speaks the remaining text after a resume.
engine.onProgress!('three', 0, 5);
expect(speech.word.value, (start: 8, end: 13));
});
testWidgets('completion and cancel clear the reply', (tester) async {
reader();
await speech.read('a', ['Done soon']);
engine.onStart!();
engine.speakWord('soon');
engine.onCompletion!();
expect(speech.state, SpeechState.idle);
expect(speech.messageId, isNull);
expect(speech.word.value, isNull);
await speech.read('b', ['Cancelled']);
engine.onStart!();
engine.onCancel!();
expect(speech.state, SpeechState.idle);
expect(speech.messageId, isNull);
});
testWidgets('going to the background stops reading', (tester) async {
reader();
await speech.read('a', ['Background']);
engine.onStart!();
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
await tester.pump();
expect(speech.state, SpeechState.speaking, reason: 'focus loss only');
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
await tester.pump();
expect(speech.state, SpeechState.idle);
expect(engine.calls.last, 'stop');
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
});
testWidgets('stopScope stops only replies of that conversation', (
tester,
) async {
reader();
await speech.read(speechMessageId('chat-1', 'm1'), ['Hello']);
engine.onStart!();
await speech.stopScope('chat-2');
expect(speech.state, SpeechState.speaking);
await speech.stopScope('chat-1');
expect(speech.state, SpeechState.idle);
});
testWidgets('voice and rate are applied and persist across a reload', (
tester,
) async {
final store = MemorySpeechPreferences();
reader(store);
const voice = SpeechVoice(name: 'Daniel', locale: 'en-GB');
await speech.setVoice(voice);
await speech.setRate(1.5);
await speech.read('a', ['Hello']);
expect(engine.voice, voice);
expect(engine.rate, 1.5);
speech.dispose();
reader(store);
await speech.restore();
expect(speech.voice, voice);
expect(speech.rate, 1.5);
await speech.reset();
expect(speech.voice, isNull);
expect(speech.rate, defaultSpeechRate);
expect(store.saved, (rate: defaultSpeechRate, voice: null));
});
}
@@ -0,0 +1,56 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/speech/speech_document.dart';
void main() {
test('markdown is spoken as sentences and code is announced', () {
final result = markdownToSpeech(
'# Welcome\n\n- **First**\n- [Docs](https://example.com)\n\n```dart\nprint(1);\n```\n\nThat is all.',
);
expect(result.text, 'Welcome. First. Docs. Code block. That is all.');
expect(result.sources.length, result.text.length);
});
test('tilde fences and tables are summarised', () {
final result = markdownToSpeech(
'~~~\nsecret();\n~~~\n\n| A | B |\n|---|---|\n| 1 | 2 |',
);
expect(result.text, 'Code block. Table. A. B. 1. 2');
expect(result.text, isNot(contains('secret')));
});
test('repeated words map to their own occurrence', () {
const markdown = 'First repeat\n\nSecond repeat';
final result = markdownToSpeech(markdown);
final first = result.text.indexOf('repeat');
final second = result.text.lastIndexOf('repeat');
expect(result.sourceRange(0, (start: first, end: first + 6)), (
start: 6,
end: 12,
));
expect(result.sourceRange(0, (start: second, end: second + 6)), (
start: markdown.lastIndexOf('repeat'),
end: markdown.length,
));
});
test('text after a code block maps after the skipped code', () {
const markdown = 'Before repeat\n\n```dart\nrepeat();\n```\n\nAfter repeat';
final result = markdownToSpeech(markdown);
final spoken = result.text.lastIndexOf('repeat');
final range = result.sourceRange(0, (start: spoken, end: spoken + 6))!;
expect(range.start, markdown.lastIndexOf('repeat'));
});
test('parts are trimmed and keep their own sources', () {
final result = speechDocumentFor([' One part\n', '', 'Two part']);
expect(result.parts, ['One part', 'Two part']);
expect(result.text, 'One part. Two part');
expect(result.partOf('\nTwo part '), 1);
final two = result.text.indexOf('Two');
expect(result.sourceRange(1, (start: two, end: two + 3)), (
start: 0,
end: 3,
));
expect(result.sourceRange(0, (start: two, end: two + 3)), isNull);
});
}
@@ -0,0 +1,120 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
import '../../speech/fake_speech_engine.dart';
/// The text of every span drawn with the spoken-word highlight.
List<String> _highlighted(WidgetTester tester, [Finder? within]) {
final fill = Theme.of(
tester.element(find.byType(ChatMarkdownView).first),
).colorScheme.primaryContainer;
final spans = <String>[];
final texts = within == null
? find.byType(RichText)
: find.descendant(of: within, matching: find.byType(RichText));
for (final rich in tester.widgetList<RichText>(texts)) {
rich.text.visitChildren((span) {
if (span is TextSpan &&
span.text != null &&
span.style?.backgroundColor == fill) {
spans.add(span.text!);
}
return true;
});
}
return spans;
}
void main() {
testWidgets('the highlight follows progress and clears on stop', (
tester,
) async {
final engine = FakeSpeechEngine();
final speech = installFakeSpeech(engine);
const first = 'Alpha **beta** gamma';
const second = '- delta beta';
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: Column(
children: [
ChatMarkdownView(text: first, messageId: 'm'),
ChatMarkdownView(text: second, messageId: 'm'),
ChatMarkdownView(text: first, messageId: 'other'),
],
),
),
),
);
await speech.read('m', [first, second]);
engine.onStart!();
await tester.pump();
expect(_highlighted(tester), isEmpty, reason: 'no word reported yet');
engine.speakWord('beta');
await tester.pump();
expect(_highlighted(tester), ['beta']);
engine.speakWord('gamma');
await tester.pump();
expect(_highlighted(tester), ['gamma']);
// The second part's "beta" lights in the second view only.
engine.speakWord('beta', from: engine.spoken.last.indexOf('delta'));
await tester.pump();
expect(_highlighted(tester), ['beta']);
final views = find.byType(ChatMarkdownView);
expect(_highlighted(tester, views.at(1)), ['beta']);
expect(_highlighted(tester, views.at(0)), isEmpty);
await speech.stop();
await tester.pump();
expect(_highlighted(tester), isEmpty);
});
testWidgets('the transcript scrolls only when the word leaves the view', (
tester,
) async {
final engine = FakeSpeechEngine();
final speech = installFakeSpeech(engine);
final text = [
for (var index = 0; index < 40; index += 1) 'Paragraph $index words.',
].join('\n\n');
final scroll = ScrollController();
addTearDown(scroll.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SizedBox(
height: 300,
child: ListView(
controller: scroll,
children: [ChatMarkdownView(text: text, messageId: 'm')],
),
),
),
),
);
await speech.read('m', [text]);
engine.onStart!();
engine.speakWord('Paragraph 2 ');
await tester.pumpAndSettle();
expect(scroll.offset, 0, reason: 'the word is already in view');
engine.speakWord('Paragraph 30');
await tester.pumpAndSettle();
expect(scroll.offset, greaterThan(0));
final viewport = tester.getRect(find.byType(ListView));
final word = find.textContaining('Paragraph 30', findRichText: true);
expect(viewport.contains(tester.getRect(word).bottomLeft), isTrue);
final followed = scroll.offset;
// A word still inside the view does not move the transcript.
final spoken = engine.spoken.last;
engine.speakWord('words', from: spoken.indexOf('Paragraph 30'));
await tester.pumpAndSettle();
expect(scroll.offset, followed);
});
}
@@ -0,0 +1,136 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
import 'package:pipelines_app/features/chat/speech/speech.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_copy_gutter.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_read_aloud_button.dart';
import '../../speech/fake_speech_engine.dart';
Widget _host(Widget child) => MaterialApp(
home: Scaffold(body: Center(child: child)),
);
JsonMap _answer(String id, String text) => transcriptRows([
{
'id': id,
'role': 'assistant',
'created_at': 1000,
'parts': [
{'type': 'text', 'text': text},
],
},
]).single;
final _read = find.byTooltip(chatReadAloudLabel);
final _pause = find.byTooltip(chatPauseReadingLabel);
final _resume = find.byTooltip(chatResumeReadingLabel);
final _stop = find.byTooltip(chatStopReadingLabel);
void main() {
testWidgets('Read aloud becomes Pause and Stop, then Resume', (tester) async {
final engine = FakeSpeechEngine();
final speech = installFakeSpeech(engine);
await tester.pumpWidget(
_host(const ChatReadAloudButton(messageId: 'a', parts: ['Hello'])),
);
expect(_read, findsOneWidget);
expect(_pause, findsNothing);
await tester.tap(_read);
await tester.pump();
expect(engine.spoken, ['Hello']);
expect(_read, findsNothing);
expect(_pause, findsOneWidget);
expect(_stop, findsOneWidget);
await tester.tap(_pause);
await tester.pump();
expect(speech.state, SpeechState.paused);
expect(_resume, findsOneWidget);
expect(_pause, findsNothing);
await tester.tap(_resume);
await tester.pump();
expect(speech.state, SpeechState.speaking);
expect(_pause, findsOneWidget);
await tester.tap(_stop);
await tester.pump();
expect(speech.state, SpeechState.idle);
expect(_read, findsOneWidget);
expect(_stop, findsNothing);
});
testWidgets('only one reply reads at a time', (tester) async {
final engine = FakeSpeechEngine();
installFakeSpeech(engine);
await tester.pumpWidget(
_host(
const Column(
mainAxisSize: MainAxisSize.min,
children: [
ChatReadAloudButton(messageId: 'a', parts: ['First']),
ChatReadAloudButton(messageId: 'b', parts: ['Second']),
],
),
),
);
await tester.tap(_read.first);
await tester.pump();
expect(_stop, findsOneWidget);
await tester.tap(_read);
await tester.pump();
expect(engine.spoken, ['First', 'Second']);
expect(engine.calls, ['speak', 'stop', 'speak']);
expect(_stop, findsOneWidget);
expect(_read, findsOneWidget);
});
testWidgets('hidden where the platform cannot speak', (tester) async {
installFakeSpeech(FakeSpeechEngine(supported: false));
await tester.pumpWidget(
_host(const ChatReadAloudButton(messageId: 'a', parts: ['Hello'])),
);
expect(_read, findsNothing);
});
testWidgets('an answer offers Read aloud beside Copy, footer and gutter', (
tester,
) async {
installFakeSpeech(FakeSpeechEngine());
Future<void> pump(double gutter) => tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ListView(
children: [
ChatCopyGutter(
width: gutter,
child: ChatMessageView(row: _answer('a1', 'The answer')),
),
],
),
),
),
);
final copy = find.byKey(const Key('chat-message-copy'));
await pump(0);
expect(_read, findsOneWidget);
expect(tester.getCenter(_read).dy, tester.getCenter(copy).dy);
expect(tester.getCenter(_read).dx, greaterThan(tester.getCenter(copy).dx));
// The 48 px gutter stacks the buttons; while reading, Pause and Stop sit
// above Copy and nothing overflows.
await pump(48);
expect(tester.getCenter(_read).dx, tester.getCenter(copy).dx);
expect(tester.getCenter(_read).dy, lessThan(tester.getCenter(copy).dy));
await tester.tap(_read);
await tester.pump();
expect(tester.takeException(), isNull);
expect(tester.getCenter(_stop).dy, lessThan(tester.getCenter(copy).dy));
expect(tester.getCenter(_pause).dy, lessThan(tester.getCenter(_stop).dy));
});
}
@@ -0,0 +1,148 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/speech/speech.dart';
import 'package:pipelines_app/features/settings/settings_read_aloud_section.dart';
import 'package:pipelines_app/features/settings/settings_voice_picker.dart';
import '../chat/speech/fake_speech_engine.dart';
const _daniel = SpeechVoice(name: 'Daniel', locale: 'en-GB');
const _samantha = SpeechVoice(name: 'Samantha', locale: 'en-US');
const _thomas = SpeechVoice(name: 'Thomas', locale: 'fr-FR');
const _anna = SpeechVoice(name: 'Anna', locale: 'de-DE');
final _voiceRow = find.byKey(const Key('settings-read-aloud-voice'));
final _slider = find.byKey(const Key('settings-read-aloud-rate'));
final _reset = find.byKey(const Key('settings-read-aloud-reset'));
final _sample = find.byKey(const Key('settings-read-aloud-sample'));
late FakeSpeechEngine _engine;
/// A fresh reader over [store], as after an app reload, and its section.
Future<SpeechController> _open(
WidgetTester tester,
MemorySpeechPreferences store,
) async {
_engine = FakeSpeechEngine(available: [_samantha, _thomas, _anna, _daniel]);
final speech = SpeechController(engine: _engine, store: store);
addTearDown(speech.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ListView(
children: [
SettingsReadAloudSection(key: UniqueKey(), controller: speech),
],
),
),
),
);
await tester.pumpAndSettle();
return speech;
}
void main() {
testWidgets('a picked voice persists across a reload', (tester) async {
tester.platformDispatcher.localeTestValue = const Locale('fr', 'FR');
addTearDown(tester.platformDispatcher.clearLocaleTestValue);
final store = MemorySpeechPreferences();
await _open(tester, store);
expect(find.text('System default'), findsOneWidget);
await tester.tap(_voiceRow);
await tester.pumpAndSettle();
// Grouped by language, the device's language first.
final french = tester.getTopLeft(find.text('French')).dy;
final english = tester.getTopLeft(find.text('English')).dy;
final german = tester.getTopLeft(find.text('German')).dy;
expect(french, lessThan(english));
expect(english, lessThan(german));
expect(
tester.getTopLeft(find.text('Daniel')).dy,
lessThan(tester.getTopLeft(find.text('Samantha')).dy),
);
await tester.tap(find.text('Daniel'));
await tester.pumpAndSettle();
expect(find.text('Daniel · en-GB'), findsOneWidget);
expect(store.saved?.voice, _daniel);
final reloaded = await _open(tester, store);
expect(reloaded.voice, _daniel);
expect(find.text('Daniel · en-GB'), findsOneWidget);
await reloaded.read('m', ['Hello']);
expect(_engine.voice, _daniel);
});
testWidgets('the rate persists across a reload', (tester) async {
final store = MemorySpeechPreferences();
final speech = await _open(tester, store);
expect(find.text('Speaking rate 1.0×'), findsOneWidget);
await tester.drag(_slider, const Offset(400, 0));
await tester.pumpAndSettle();
expect(speech.rate, greaterThan(defaultSpeechRate));
expect(store.saved?.rate, speech.rate);
final reloaded = await _open(tester, store);
expect(reloaded.rate, store.saved!.rate);
expect(
find.text('Speaking rate ${reloaded.rate.toStringAsFixed(1)}×'),
findsOneWidget,
);
await reloaded.read('m', ['Hello']);
expect(_engine.rate, reloaded.rate);
});
testWidgets('reset restores the system voice and normal rate', (
tester,
) async {
final store = MemorySpeechPreferences()
..saved = (rate: 1.5, voice: _thomas);
final speech = await _open(tester, store);
expect(find.text('Thomas · fr-FR'), findsOneWidget);
expect(find.text('Speaking rate 1.5×'), findsOneWidget);
await tester.ensureVisible(_reset);
await tester.tap(_reset);
await tester.pumpAndSettle();
expect(speech.voice, isNull);
expect(speech.rate, defaultSpeechRate);
expect(store.saved, (rate: defaultSpeechRate, voice: null));
expect(find.text('System default'), findsOneWidget);
expect(find.text('Speaking rate 1.0×'), findsOneWidget);
expect(tester.widget<TextButton>(_reset).onPressed, isNull);
});
testWidgets('samples play through the engine', (tester) async {
final store = MemorySpeechPreferences()..saved = (rate: 1, voice: _anna);
final speech = await _open(tester, store);
await tester.ensureVisible(_sample);
await tester.tap(_sample);
await tester.pump();
expect(_engine.spoken.single, contains('read aloud'));
expect(_engine.voice, _anna);
expect(find.text('Stop sample'), findsOneWidget);
await tester.tap(_sample);
await tester.pump();
expect(speech.state, SpeechState.idle);
expect(find.text('Play sample'), findsOneWidget);
// Each voice in the picker has its own sample; it does not choose it.
await tester.tap(_voiceRow);
await tester.pumpAndSettle();
await tester.tap(find.byTooltip('Play sample of Samantha'));
await tester.pump();
expect(_engine.voice, _samantha);
expect(_engine.spoken, hasLength(2));
await tester.tap(find.text('Cancel'));
await tester.pumpAndSettle();
expect(speech.voice, _anna);
expect(speech.state, SpeechState.idle, reason: 'closing stops the sample');
});
test('voices group by language, device language first', () {
final groups = groupSpeechVoices([_samantha, _anna, _daniel], 'de');
expect(groups.map((group) => group.language), ['de', 'en']);
expect(groups.last.voices, [_daniel, _samantha]);
});
}