3164b7c8df
Rank platform voice quality and select the most natural automatic voice while keeping explicit voice choices intact. Surface natural labels, hide novelty voices in the picker, and guide Apple users to download enhanced voices when needed.
224 lines
7.7 KiB
Dart
224 lines
7.7 KiB
Dart
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 Enhanced',
|
||
locale: 'en-US',
|
||
qualityRank: 2,
|
||
);
|
||
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, {
|
||
List<SpeechVoice>? available,
|
||
bool voiceDownloadHint = false,
|
||
bool voiceFailure = false,
|
||
}) async {
|
||
_engine = FakeSpeechEngine(
|
||
available: available ?? [_samantha, _thomas, _anna, _daniel],
|
||
voiceDownloadHint: voiceDownloadHint,
|
||
voiceFailure: voiceFailure,
|
||
);
|
||
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('Automatic (most natural)'), findsOneWidget);
|
||
|
||
await tester.tap(_voiceRow);
|
||
await tester.pumpAndSettle();
|
||
expect(find.text('Natural'), findsOneWidget);
|
||
// 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,
|
||
greaterThan(tester.getTopLeft(find.text('Samantha Enhanced')).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('Automatic (most natural)'), 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 Enhanced'));
|
||
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');
|
||
});
|
||
|
||
testWidgets(
|
||
'shows the Apple download hint only for a non-natural inventory',
|
||
(tester) async {
|
||
tester.platformDispatcher.localeTestValue = const Locale('en', 'US');
|
||
addTearDown(tester.platformDispatcher.clearLocaleTestValue);
|
||
final store = MemorySpeechPreferences();
|
||
await _open(
|
||
tester,
|
||
store,
|
||
available: const [
|
||
SpeechVoice(
|
||
name: 'Samantha Compact',
|
||
locale: 'en-US',
|
||
qualityRank: 1,
|
||
),
|
||
],
|
||
voiceDownloadHint: true,
|
||
);
|
||
expect(
|
||
find.textContaining('download one in System Settings'),
|
||
findsOneWidget,
|
||
);
|
||
final hint = tester.widget<Text>(
|
||
find.descendant(
|
||
of: find.byKey(const Key('settings-read-aloud-voice-hint')),
|
||
matching: find.byType(Text),
|
||
),
|
||
);
|
||
expect(hint.maxLines, 1);
|
||
expect(
|
||
tester
|
||
.getTopLeft(find.byKey(const Key('settings-read-aloud-voice-hint')))
|
||
.dy,
|
||
greaterThan(tester.getTopLeft(_voiceRow).dy),
|
||
);
|
||
expect(
|
||
tester.getTopLeft(find.text('Speaking rate 1.0×')).dy,
|
||
greaterThan(
|
||
tester
|
||
.getTopLeft(
|
||
find.byKey(const Key('settings-read-aloud-voice-hint')),
|
||
)
|
||
.dy,
|
||
),
|
||
);
|
||
},
|
||
);
|
||
|
||
testWidgets('failed voice inventory is safe and dismissible', (tester) async {
|
||
final speech = await _open(
|
||
tester,
|
||
MemorySpeechPreferences(),
|
||
voiceFailure: true,
|
||
);
|
||
await tester.tap(_voiceRow);
|
||
await tester.pumpAndSettle();
|
||
expect(find.textContaining('Voices could not be loaded'), findsOneWidget);
|
||
expect(find.text('This device offers no other voices.'), findsNothing);
|
||
await tester.tap(find.text('Cancel'));
|
||
await tester.pumpAndSettle();
|
||
expect(speech.state, SpeechState.idle);
|
||
});
|
||
|
||
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, [_samantha, _daniel]);
|
||
});
|
||
}
|