Merge pull request #705 from zjs81/feat/compression-menu-page

Move chat compression to the menu with its own page
This commit is contained in:
zjs81 authored and GitHub committed 2026-11-23 20:33:06 -07:00
commit 6748d5f0f2
33 files changed
+798 -846

No files matched your search

+172
View File
@@ -0,0 +1,172 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/helpers/smaz.dart';
int _utf8Len(String s) => utf8.encode(s).length;
Uint8List _raw(String s) =>
Smaz.compressBytes(Uint8List.fromList(utf8.encode(s)));
/// Sent size, raw SMAZ size and base64 wire size for [text].
({int plain, int raw, int wire, int sent}) _measure(String text) {
final raw = _raw(text);
return (
plain: _utf8Len(text),
raw: raw.length,
wire: _utf8Len('s:${base64Encode(raw)}'),
sent: _utf8Len(Smaz.encodeIfSmaller(text)),
);
}
void main() {
group('compression results', () {
test('lowercase English compresses well', () {
const text = 'hello there, how are you doing today?';
final m = _measure(text);
expect((m.plain, m.raw, m.wire, m.sent), (37, 20, 30, 30));
expect(Smaz.encodeIfSmaller(text), startsWith('s:'));
});
test('sentence case compresses, a little less', () {
const text = 'Hello there, how are you doing today?';
final m = _measure(text);
expect((m.plain, m.raw, m.wire, m.sent), (37, 22, 34, 34));
expect(Smaz.encodeIfSmaller(text), startsWith('s:'));
});
test('mixed case with many capitals barely compresses', () {
const mixed = 'Meeting at Headquarters on Monday afternoon';
const lower = 'meeting at headquarters on monday afternoon';
expect(_measure(mixed).wire, 42);
expect(_measure(mixed).plain, 43);
expect(Smaz.encodeIfSmaller(mixed), startsWith('s:'));
// The same words in lowercase save far more.
expect(_measure(lower).wire, 34);
expect(_measure(lower).raw, 24);
// Each capital that is not in the codebook costs 2 bytes.
expect(_measure(mixed).raw - _measure(lower).raw, 4);
});
test('all caps is longer than the plain text, so it is sent plain', () {
const text = 'HELLO THERE, HOW ARE YOU DOING TODAY?';
final m = _measure(text);
expect((m.plain, m.raw, m.wire, m.sent), (37, 50, 70, 37));
expect(Smaz.encodeIfSmaller(text), text);
});
test('a reply prefix costs the same in either case', () {
expect(
_raw('@[Alice] hello there, how are you?').length,
_raw('@[alice] hello there, how are you?').length,
);
// 34 bytes plain, 34 bytes encoded: not strictly smaller, sent plain.
const text = '@[Alice] hello there, how are you?';
expect(_measure(text).wire, _measure(text).plain);
expect(Smaz.encodeIfSmaller(text), text);
});
});
group('non-ASCII text', () {
test('Cyrillic round-trips as raw bytes but is sent plain', () {
const text = 'Привет, как дела сегодня?';
final m = _measure(text);
expect((m.plain, m.raw, m.wire, m.sent), (45, 52, 74, 45));
expect(Smaz.encodeIfSmaller(text), text);
// The byte-level codec is still lossless.
expect(utf8.decode(Smaz.decompressBytes(_raw(text))), text);
});
test('emoji survives a compressed round trip', () {
const text =
'see you soon 😀 the best of luck with the new house and the garden';
final encoded = Smaz.encodeIfSmaller(text);
expect(encoded, startsWith('s:'));
expect(_utf8Len(encoded), lessThan(_utf8Len(text)));
expect(Smaz.tryDecodePrefixed(encoded), text);
});
test('a short emoji message is not smaller, so it is sent plain', () {
const text = 'see you soon 😀 the best';
final m = _measure(text);
expect((m.plain, m.raw, m.wire, m.sent), (26, 17, 26, 26));
expect(Smaz.encodeIfSmaller(text), text);
});
});
group('encodeIfSmaller falls back to plain text', () {
test('short strings', () {
for (final text in ['hi', 'ok', 'Hello', 'the']) {
expect(Smaz.encodeIfSmaller(text), text, reason: text);
}
});
test('empty string', () {
expect(Smaz.encodeIfSmaller(''), '');
});
test('sent form is never longer than the plain text', () {
for (final text in [
'a',
'Hello',
'HELLO THERE, HOW ARE YOU DOING TODAY?',
'the quick brown fox jumps over the lazy dog',
'Привет, как дела?',
'12345 67890',
]) {
expect(
_utf8Len(Smaz.encodeIfSmaller(text)),
lessThanOrEqualTo(_utf8Len(text)),
reason: text,
);
}
});
});
group('round trip', () {
test('tryDecodePrefixed restores what encodeIfSmaller produced', () {
for (final text in [
'hello there, how are you doing today?',
'Hello there, how are you doing today?',
'Meeting at Headquarters on Monday afternoon',
'the quick brown fox jumps over the lazy dog',
'@[Alice] hello there, how are you doing today my friend?',
]) {
final encoded = Smaz.encodeIfSmaller(text);
expect(encoded, startsWith('s:'), reason: text);
expect(Smaz.tryDecodePrefixed(encoded), text, reason: text);
}
});
test('the decoder ignores leading whitespace', () {
const text = 'hello there, how are you doing today?';
expect(Smaz.tryDecodePrefixed(' ${Smaz.encodeIfSmaller(text)}'), text);
});
test('plain text is not a compressed payload', () {
expect(Smaz.tryDecodePrefixed('hello there'), isNull);
expect(Smaz.tryDecodePrefixed('s:'), isNull);
});
});
group('"s:" guard', () {
test('text that already starts with "s:" is never re-encoded', () {
const text = 's:hello there, how are you doing today?';
expect(Smaz.encodeIfSmaller(text), text);
});
test('plain "s:" text is not mistaken for a compressed payload', () {
expect(Smaz.tryDecodePrefixed('s:test'), isNull);
expect(Smaz.tryDecodePrefixed('s:hello'), isNull);
// Valid base64 that is not what the encoder would have produced.
expect(Smaz.tryDecodePrefixed('s:AAAA'), isNull);
});
test('malformed payloads return null instead of throwing', () {
expect(Smaz.tryDecodePrefixed('s:!!!!'), isNull);
// Truncated verbatim escape (0xFE with no byte after it).
expect(Smaz.tryDecodePrefixed('s:${base64Encode([254])}'), isNull);
});
});
}
@@ -0,0 +1,265 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/models/app_settings.dart';
import 'package:meshcore_open/theme/mesh_theme.dart';
import 'package:meshcore_open/screens/chat_compression_screen.dart';
final _l10n = lookupAppLocalizations(const Locale('en'));
final _profiles = [
Cyr2LatProfile(id: 'ru', name: 'Russian', charMap: const {'А': 'A'}),
Cyr2LatProfile(id: 'sr', name: 'Serbian', charMap: const {'З': '3'}),
];
/// Stands in for a chat screen: keeps the "saved" state and applies a change
/// only when the test completes the pending write.
class _Host extends ChangeNotifier {
ChatCompressionMode mode;
String? profileId;
final bool enabled;
final List<ChatCompressionMode> modeCalls = [];
final List<String> profileCalls = [];
final List<Completer<void>> pending = [];
_Host({
this.mode = ChatCompressionMode.off,
this.enabled = true,
this.profileId,
});
ChatCompressionState read() => ChatCompressionState(
mode: mode,
enabled: enabled,
disabledReason: enabled ? null : _l10n.chatCompression_noStorageId,
profiles: _profiles,
profileId: profileId,
defaultProfileId: 'ru',
);
Future<void> _write(void Function() apply) async {
final completer = Completer<void>();
pending.add(completer);
await completer.future;
apply();
notifyListeners();
}
Future<void> setMode(ChatCompressionMode m) {
modeCalls.add(m);
return _write(() => mode = m);
}
Future<void> setProfile(String id) {
profileCalls.add(id);
return _write(() => profileId = id);
}
}
void main() {
late _Host host;
Future<void> pumpPage(
WidgetTester tester, {
ChatCompressionMode mode = ChatCompressionMode.off,
bool enabled = true,
String? profileId,
Size size = const Size(400, 800),
double textScale = 1.0,
}) async {
host = _Host(mode: mode, enabled: enabled, profileId: profileId);
addTearDown(host.dispose);
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await tester.pumpWidget(
MaterialApp(
theme: MeshTheme.light(),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
builder: (context, child) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: child!,
),
home: ChatCompressionScreen(
listenable: host,
read: host.read,
onModeChanged: host.setMode,
onProfileChanged: host.setProfile,
),
),
);
}
Finder optionRow(String label) =>
find.ancestor(of: find.text(label), matching: find.byType(ListTile));
bool isSelected(String label) {
return find
.descendant(
of: optionRow(label),
matching: find.byIcon(Icons.radio_button_checked),
)
.evaluate()
.isNotEmpty;
}
testWidgets(
'lists the three modes with short notes and marks the saved one',
(tester) async {
await pumpPage(tester, mode: ChatCompressionMode.smaz);
expect(find.text(_l10n.chatCompression_title), findsWidgets);
expect(find.text(_l10n.chatCompression_off), findsOneWidget);
expect(find.text(_l10n.chatCompression_smaz), findsOneWidget);
expect(find.text(_l10n.chatCompression_cyr2lat), findsOneWidget);
expect(find.text(_l10n.chatCompression_offDescription), findsOneWidget);
expect(find.text(_l10n.chatCompression_smazDescription), findsOneWidget);
expect(
find.text(_l10n.chatCompression_cyr2latDescription),
findsOneWidget,
);
expect(isSelected(_l10n.chatCompression_smaz), isTrue);
expect(isSelected(_l10n.chatCompression_off), isFalse);
expect(find.text(_l10n.chatCompression_profile), findsNothing);
for (final mode in [
_l10n.chatCompression_off,
_l10n.chatCompression_smaz,
_l10n.chatCompression_cyr2lat,
]) {
expect(
tester.getSize(optionRow(mode)).height,
greaterThanOrEqualTo(48),
reason: mode,
);
}
},
);
testWidgets('a choice shows only after it is saved', (tester) async {
await pumpPage(tester);
await tester.tap(find.text(_l10n.chatCompression_smaz));
await tester.pump();
expect(host.modeCalls, [ChatCompressionMode.smaz]);
// Still saving: old mode shown, options locked against rapid taps.
expect(isSelected(_l10n.chatCompression_off), isTrue);
expect(
tester.widget<ListTile>(optionRow(_l10n.chatCompression_cyr2lat)).enabled,
isFalse,
);
await tester.tap(find.text(_l10n.chatCompression_cyr2lat));
await tester.pump();
expect(host.modeCalls, [ChatCompressionMode.smaz]);
host.pending.single.complete();
await tester.pumpAndSettle();
expect(isSelected(_l10n.chatCompression_smaz), isTrue);
expect(isSelected(_l10n.chatCompression_off), isFalse);
expect(
tester.widget<ListTile>(optionRow(_l10n.chatCompression_cyr2lat)).enabled,
isTrue,
);
});
testWidgets('Cyr2Lat reveals the profile picker', (tester) async {
await pumpPage(tester);
await tester.tap(find.text(_l10n.chatCompression_cyr2lat));
await tester.pump();
host.pending.removeAt(0).complete();
await tester.pumpAndSettle();
expect(isSelected(_l10n.chatCompression_cyr2lat), isTrue);
expect(find.text(_l10n.chatCompression_profile), findsWidgets);
// No profile saved for the chat: the app-wide one is what sending uses.
expect(find.text('Russian'), findsOneWidget);
await tester.tap(find.text('Russian'));
await tester.pumpAndSettle();
await tester.tap(find.text('Serbian').last);
await tester.pump();
expect(host.profileCalls, ['sr']);
host.pending.removeAt(0).complete();
await tester.pumpAndSettle();
expect(find.text('Serbian'), findsOneWidget);
});
testWidgets('a failed save keeps the saved mode and says so', (tester) async {
await pumpPage(tester, mode: ChatCompressionMode.smaz);
await tester.tap(find.text(_l10n.chatCompression_cyr2lat));
await tester.pump();
host.pending.single.completeError(StateError('disk full'));
await tester.pumpAndSettle();
expect(find.text(_l10n.chatCompression_saveFailed), findsOneWidget);
expect(isSelected(_l10n.chatCompression_smaz), isTrue);
expect(isSelected(_l10n.chatCompression_cyr2lat), isFalse);
expect(
tester.widget<ListTile>(optionRow(_l10n.chatCompression_off)).enabled,
isTrue,
);
});
testWidgets('a failed profile save shows the saved profile again', (
tester,
) async {
await pumpPage(tester, mode: ChatCompressionMode.cyr2lat, profileId: 'ru');
await tester.tap(find.text('Russian'));
await tester.pumpAndSettle();
await tester.tap(find.text('Serbian').last);
await tester.pump();
expect(host.profileCalls, ['sr']);
host.pending.single.completeError(StateError('disk full'));
await tester.pumpAndSettle();
expect(find.text(_l10n.chatCompression_saveFailed), findsOneWidget);
await tester.scrollUntilVisible(find.text('Russian'), 100);
expect(find.text('Russian'), findsOneWidget);
expect(find.text('Serbian'), findsNothing);
});
testWidgets('disabled shows the saved mode, says why and allows no change', (
tester,
) async {
await pumpPage(tester, mode: ChatCompressionMode.smaz, enabled: false);
expect(find.text(_l10n.chatCompression_noStorageId), findsOneWidget);
expect(isSelected(_l10n.chatCompression_smaz), isTrue);
for (final mode in [
_l10n.chatCompression_off,
_l10n.chatCompression_smaz,
_l10n.chatCompression_cyr2lat,
]) {
expect(tester.widget<ListTile>(optionRow(mode)).enabled, isFalse);
}
await tester.tap(find.text(_l10n.chatCompression_off), warnIfMissed: false);
await tester.pumpAndSettle();
expect(host.modeCalls, isEmpty);
});
for (final scale in [1.3, 2.0]) {
testWidgets('fits a 320dp phone at ${scale}x text', (tester) async {
await pumpPage(
tester,
mode: ChatCompressionMode.cyr2lat,
size: const Size(320, 568),
textScale: scale,
);
expect(tester.takeException(), isNull);
// The page scrolls to its last option and the profile picker.
await tester.scrollUntilVisible(
find.byType(DropdownButtonFormField<String>),
200,
scrollable: find.byType(Scrollable).first,
);
expect(tester.takeException(), isNull);
});
}
}
+31 -26
View File
@@ -42,9 +42,8 @@ import 'package:meshcore_open/storage/last_radio_store.dart';
import 'package:meshcore_open/storage/message_store.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:meshcore_open/theme/mesh_theme.dart';
import 'package:meshcore_open/widgets/chat_background.dart';
import 'package:meshcore_open/widgets/chat_composer.dart';
import 'package:meshcore_open/widgets/chat_compression_selector.dart';
import 'package:meshcore_open/screens/chat_compression_screen.dart';
import 'package:meshcore_open/widgets/connect_sheet.dart';
final _radioKey = Uint8List.fromList(List<int>.generate(32, (i) => 0xA0 + i));
@@ -349,10 +348,18 @@ void main() {
await tester.pump(const Duration(minutes: 1));
});
group('compression selector', () {
Future<void> choose(WidgetTester tester, String mode) async {
await tester.tap(find.byType(ChatCompressionSelector));
group('compression menu item', () {
Future<void> openMenu(WidgetTester tester) async {
await tester.tap(find.byIcon(Icons.more_vert));
await _settle(tester);
}
/// Opens the page from the overflow menu, picks [mode], and returns.
Future<void> choose(WidgetTester tester, String mode) async {
await openMenu(tester);
await tester.tap(find.textContaining('${_l10n.chatCompression_title}:'));
await _settle(tester);
expect(find.byType(ChatCompressionScreen), findsOneWidget);
await tester.tap(find.text(mode));
await _settle(tester);
// Connector notifications are debounced on a real timer.
@@ -360,9 +367,11 @@ void main() {
() => Future<void>.delayed(const Duration(milliseconds: 200)),
);
await _settle(tester);
await tester.pageBack();
await _settle(tester);
}
testWidgets('DM chat shows it at the top and saves offline', (
testWidgets('DM chat shows it in the menu and saves offline', (
tester,
) async {
final connector = await _offlineConnector(tester);
@@ -373,32 +382,27 @@ void main() {
);
await _settle(tester);
expect(find.byType(ChatCompressionSelector), findsOneWidget);
// No strip above the transcript any more.
expect(find.byType(ChatCompressionScreen), findsNothing);
expect(find.text(_l10n.chatCompression_label('Off')), findsNothing);
await openMenu(tester);
expect(find.text(_l10n.chatCompression_label('Off')), findsOneWidget);
// Above the transcript, outside its scroll view.
expect(
find.ancestor(
of: find.byType(ChatCompressionSelector),
matching: find.byType(Scrollable),
),
findsNothing,
);
expect(
tester.getBottomLeft(find.byType(ChatCompressionSelector)).dy,
lessThanOrEqualTo(tester.getTopLeft(find.byType(ChatBackdrop)).dy),
);
expect(find.byIcon(Icons.compress), findsOneWidget);
await tester.tapAt(const Offset(1, 1));
await _settle(tester);
await choose(tester, _l10n.chatCompression_smaz);
await openMenu(tester);
expect(find.text(_l10n.chatCompression_label('SMAZ')), findsOneWidget);
await tester.tapAt(const Offset(1, 1));
await _settle(tester);
expect(
await tester.runAsync(() => store.loadSmazEnabled(alice.publicKeyHex)),
isTrue,
);
await choose(tester, _l10n.chatCompression_cyr2lat);
// Cyr2Lat keeps the sheet open for its profile picker.
await tester.tap(find.byTooltip('Close'));
await _settle(tester);
await openMenu(tester);
expect(find.text(_l10n.chatCompression_label('Cyr2Lat')), findsOneWidget);
expect(
connector.contactCompressionMode(alice.publicKeyHex),
@@ -416,8 +420,6 @@ void main() {
);
// The settings dialog no longer carries the compression switches.
await tester.tap(find.byIcon(Icons.more_vert));
await _settle(tester);
await tester.tap(find.text(_l10n.contact_settings).last);
await _settle(tester);
expect(find.byType(AlertDialog), findsOneWidget);
@@ -431,7 +433,7 @@ void main() {
await tester.pumpWidget(const SizedBox());
});
testWidgets('channel chat uses the same selector and saves offline', (
testWidgets('channel chat uses the same page and saves offline', (
tester,
) async {
final connector = await _offlineConnector(tester);
@@ -442,10 +444,13 @@ void main() {
);
await _settle(tester);
expect(find.byType(ChatCompressionSelector), findsOneWidget);
await openMenu(tester);
expect(find.text(_l10n.chatCompression_label('Off')), findsOneWidget);
await tester.tapAt(const Offset(1, 1));
await _settle(tester);
await choose(tester, _l10n.chatCompression_smaz);
await openMenu(tester);
expect(find.text(_l10n.chatCompression_label('SMAZ')), findsOneWidget);
expect(
await tester.runAsync(() => store.loadSmazEnabled(channel.storageId!)),
@@ -1,334 +0,0 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/models/app_settings.dart';
import 'package:meshcore_open/theme/mesh_theme.dart';
import 'package:meshcore_open/widgets/chat_compression_selector.dart';
final _l10n = lookupAppLocalizations(const Locale('en'));
final _profiles = [
Cyr2LatProfile(id: 'ru', name: 'Russian', charMap: const {'А': 'A'}),
Cyr2LatProfile(id: 'sr', name: 'Serbian', charMap: const {'З': '3'}),
];
/// Stands in for a chat screen: keeps the "saved" state and applies a change
/// only when the test completes the pending write.
class _Host extends StatefulWidget {
final ChatCompressionMode initialMode;
final bool enabled;
final String? initialProfileId;
final List<ChatCompressionMode> modeCalls;
final List<String> profileCalls;
final List<Completer<void>> pending;
const _Host({
this.initialMode = ChatCompressionMode.off,
this.enabled = true,
this.initialProfileId,
required this.modeCalls,
required this.profileCalls,
required this.pending,
});
@override
State<_Host> createState() => _HostState();
}
class _HostState extends State<_Host> {
late ChatCompressionMode _mode = widget.initialMode;
late String? _profileId = widget.initialProfileId;
Future<void> _write(void Function() apply) async {
final completer = Completer<void>();
widget.pending.add(completer);
await completer.future;
if (mounted) setState(apply);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Chat')),
body: Column(
children: [
ChatCompressionSelector(
mode: _mode,
enabled: widget.enabled,
disabledReason: widget.enabled
? null
: _l10n.chatCompression_noStorageId,
profiles: _profiles,
profileId: _profileId,
defaultProfileId: 'ru',
onModeChanged: (mode) {
widget.modeCalls.add(mode);
return _write(() => _mode = mode);
},
onProfileChanged: (id) {
widget.profileCalls.add(id);
return _write(() => _profileId = id);
},
),
const Expanded(child: Placeholder()),
],
),
);
}
}
void main() {
late List<ChatCompressionMode> modeCalls;
late List<String> profileCalls;
late List<Completer<void>> pending;
setUp(() {
modeCalls = [];
profileCalls = [];
pending = [];
});
Future<void> pumpHost(
WidgetTester tester, {
ChatCompressionMode mode = ChatCompressionMode.off,
bool enabled = true,
String? profileId,
Size size = const Size(400, 800),
double textScale = 1.0,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await tester.pumpWidget(
MaterialApp(
theme: MeshTheme.light(),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
builder: (context, child) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: child!,
),
home: _Host(
initialMode: mode,
enabled: enabled,
initialProfileId: profileId,
modeCalls: modeCalls,
profileCalls: profileCalls,
pending: pending,
),
),
);
}
Future<void> openSheet(WidgetTester tester) async {
await tester.tap(find.byType(ChatCompressionSelector));
await tester.pumpAndSettle();
}
Finder optionRow(String label) =>
find.ancestor(of: find.text(label), matching: find.byType(ListTile));
bool isSelected(WidgetTester tester, String label) {
final row = optionRow(label);
return find
.descendant(of: row, matching: find.byIcon(Icons.radio_button_checked))
.evaluate()
.isNotEmpty;
}
testWidgets('shows the saved mode and announces it', (tester) async {
final semantics = tester.ensureSemantics();
await pumpHost(tester, mode: ChatCompressionMode.smaz);
expect(find.text(_l10n.chatCompression_label('SMAZ')), findsOneWidget);
expect(
tester.getSemantics(
find.bySemanticsLabel(_l10n.chatCompression_label('SMAZ')),
),
isSemantics(
label: _l10n.chatCompression_label('SMAZ'),
hint: _l10n.chatCompression_changeHint,
isButton: true,
isEnabled: true,
hasTapAction: true,
),
);
expect(
tester.getSize(find.byType(ChatCompressionSelector)).height,
greaterThanOrEqualTo(48),
);
semantics.dispose();
});
testWidgets('one sheet lists the three modes with their explanations', (
tester,
) async {
await pumpHost(tester);
await openSheet(tester);
expect(find.text(_l10n.chatCompression_title), findsOneWidget);
expect(find.text(_l10n.chatCompression_off), findsOneWidget);
expect(find.text(_l10n.chatCompression_smaz), findsOneWidget);
expect(find.text(_l10n.chatCompression_cyr2lat), findsOneWidget);
expect(find.text(_l10n.chatCompression_offDescription), findsOneWidget);
expect(find.text(_l10n.chatCompression_smazDescription), findsOneWidget);
expect(find.text(_l10n.chatCompression_cyr2latDescription), findsOneWidget);
expect(isSelected(tester, _l10n.chatCompression_off), isTrue);
expect(isSelected(tester, _l10n.chatCompression_smaz), isFalse);
// No profile picker unless Cyr2Lat is chosen.
expect(find.text(_l10n.chatCompression_profile), findsNothing);
expect(find.byType(Dialog), findsNothing);
});
testWidgets('a choice shows only after it is saved, then the sheet closes', (
tester,
) async {
await pumpHost(tester);
await openSheet(tester);
await tester.tap(find.text(_l10n.chatCompression_smaz));
await tester.pump();
expect(modeCalls, [ChatCompressionMode.smaz]);
// Still saving: old mode shown, options locked against rapid taps.
expect(isSelected(tester, _l10n.chatCompression_off), isTrue);
expect(
tester.widget<ListTile>(optionRow(_l10n.chatCompression_cyr2lat)).enabled,
isFalse,
);
await tester.tap(find.text(_l10n.chatCompression_cyr2lat));
await tester.pump();
expect(modeCalls, [ChatCompressionMode.smaz]);
pending.single.complete();
await tester.pumpAndSettle();
expect(find.text(_l10n.chatCompression_title), findsNothing);
expect(find.text(_l10n.chatCompression_label('SMAZ')), findsOneWidget);
});
testWidgets('Cyr2Lat keeps the sheet open with its profile picker', (
tester,
) async {
await pumpHost(tester);
await openSheet(tester);
await tester.tap(find.text(_l10n.chatCompression_cyr2lat));
await tester.pump();
pending.removeAt(0).complete();
await tester.pumpAndSettle();
expect(find.text(_l10n.chatCompression_title), findsOneWidget);
expect(isSelected(tester, _l10n.chatCompression_cyr2lat), isTrue);
expect(find.text(_l10n.chatCompression_profile), findsOneWidget);
// No profile saved for the chat: the app-wide one is what sending uses.
expect(find.text('Russian'), findsOneWidget);
await tester.ensureVisible(find.text('Russian'));
await tester.pumpAndSettle();
await tester.tap(find.text('Russian'));
await tester.pumpAndSettle();
await tester.tap(find.text('Serbian').last);
await tester.pump();
expect(profileCalls, ['sr']);
pending.removeAt(0).complete();
await tester.pumpAndSettle();
expect(find.text('Serbian'), findsOneWidget);
expect(find.text(_l10n.chatCompression_label('Cyr2Lat')), findsOneWidget);
});
testWidgets('a failed save keeps the saved mode and says so', (tester) async {
await pumpHost(tester, mode: ChatCompressionMode.smaz);
await openSheet(tester);
await tester.tap(find.text(_l10n.chatCompression_cyr2lat));
await tester.pump();
pending.single.completeError(StateError('disk full'));
await tester.pumpAndSettle();
expect(find.text(_l10n.chatCompression_saveFailed), findsOneWidget);
expect(isSelected(tester, _l10n.chatCompression_smaz), isTrue);
expect(isSelected(tester, _l10n.chatCompression_cyr2lat), isFalse);
expect(
tester.widget<ListTile>(optionRow(_l10n.chatCompression_off)).enabled,
isTrue,
);
await tester.tap(find.byTooltip('Close'));
await tester.pumpAndSettle();
expect(find.text(_l10n.chatCompression_label('SMAZ')), findsOneWidget);
});
testWidgets('a failed profile save shows the saved profile again', (
tester,
) async {
await pumpHost(tester, mode: ChatCompressionMode.cyr2lat, profileId: 'ru');
await openSheet(tester);
await tester.ensureVisible(find.text('Russian'));
await tester.pumpAndSettle();
await tester.tap(find.text('Russian'));
await tester.pumpAndSettle();
await tester.tap(find.text('Serbian').last);
await tester.pump();
expect(profileCalls, ['sr']);
pending.single.completeError(StateError('disk full'));
await tester.pumpAndSettle();
expect(find.text(_l10n.chatCompression_saveFailed), findsOneWidget);
expect(find.text('Russian'), findsOneWidget);
expect(find.text('Serbian'), findsNothing);
});
testWidgets('disabled shows the saved mode, and the sheet says why and '
'allows no change', (tester) async {
final semantics = tester.ensureSemantics();
await pumpHost(tester, mode: ChatCompressionMode.smaz, enabled: false);
final label = _l10n.chatCompression_label('SMAZ');
expect(find.text(label), findsOneWidget);
expect(find.byIcon(Icons.lock_outline), findsOneWidget);
expect(
find.bySemanticsLabel('$label. ${_l10n.chatCompression_noStorageId}'),
findsOneWidget,
);
await openSheet(tester);
expect(find.text(_l10n.chatCompression_noStorageId), findsOneWidget);
expect(isSelected(tester, _l10n.chatCompression_smaz), isTrue);
for (final mode in [
_l10n.chatCompression_off,
_l10n.chatCompression_smaz,
_l10n.chatCompression_cyr2lat,
]) {
expect(tester.widget<ListTile>(optionRow(mode)).enabled, isFalse);
}
await tester.tap(find.text(_l10n.chatCompression_off), warnIfMissed: false);
await tester.pumpAndSettle();
expect(modeCalls, isEmpty);
semantics.dispose();
});
for (final scale in [1.3, 2.0]) {
testWidgets('fits a 320dp phone at ${scale}x text', (tester) async {
await pumpHost(
tester,
mode: ChatCompressionMode.cyr2lat,
enabled: true,
size: const Size(320, 568),
textScale: scale,
);
expect(tester.takeException(), isNull);
await openSheet(tester);
expect(tester.takeException(), isNull);
// The sheet scrolls to its last option and the profile picker.
await tester.scrollUntilVisible(
find.text(_l10n.chatCompression_profile),
200,
scrollable: find.byType(Scrollable).last,
);
expect(tester.takeException(), isNull);
});
}
}