Merge origin/dev into feat/group-editor-members-first
This commit is contained in:
commit
5e4e9bfce5
67 files changed
+5651
-1056
No files matched your search
@@ -0,0 +1,512 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'package:meshcore_open/connector/meshcore_connector.dart';
|
||||
import 'package:meshcore_open/helpers/gif_helper.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations.dart';
|
||||
import 'package:meshcore_open/models/chat_background.dart';
|
||||
import 'package:meshcore_open/services/app_settings_service.dart';
|
||||
import 'package:meshcore_open/services/chat_background_service.dart';
|
||||
import 'package:meshcore_open/services/chat_background_image_store.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:meshcore_open/theme/mesh_theme.dart';
|
||||
import 'package:meshcore_open/widgets/byte_count_input.dart';
|
||||
import 'package:meshcore_open/widgets/chat_background.dart';
|
||||
import 'package:meshcore_open/widgets/chat_composer.dart';
|
||||
import 'package:meshcore_open/widgets/connect_sheet.dart';
|
||||
import 'package:meshcore_open/widgets/gif_message.dart';
|
||||
import 'package:meshcore_open/widgets/message_translation_button.dart';
|
||||
|
||||
final _l10n = lookupAppLocalizations(const Locale('en'));
|
||||
|
||||
class _Connector extends MeshCoreConnector {
|
||||
bool connected = true;
|
||||
|
||||
@override
|
||||
bool get isConnected => connected;
|
||||
}
|
||||
|
||||
class _Harness {
|
||||
final controller = TextEditingController();
|
||||
final focusNode = FocusNode();
|
||||
final connector = _Connector();
|
||||
late final AppSettingsService settings;
|
||||
late final ChatBackgroundService backgrounds;
|
||||
int sends = 0;
|
||||
final actions = <String>[];
|
||||
|
||||
void dispose() {
|
||||
controller.dispose();
|
||||
focusNode.dispose();
|
||||
connector.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Future<_Harness> _harness(WidgetTester tester) async {
|
||||
final h = _Harness();
|
||||
// A date with no holiday nearby, so no holiday chat background applies.
|
||||
h.settings = AppSettingsService(clock: () => DateTime(2026, 7, 1));
|
||||
await tester.runAsync(h.settings.loadSettings);
|
||||
h.backgrounds = ChatBackgroundService(
|
||||
h.settings,
|
||||
imageStore: InMemoryChatBackgroundImageStore(),
|
||||
);
|
||||
addTearDown(h.dispose);
|
||||
return h;
|
||||
}
|
||||
|
||||
Widget _app(
|
||||
_Harness h, {
|
||||
int maxBytes = 160,
|
||||
Widget? replyPreview,
|
||||
bool disableAnimations = false,
|
||||
Brightness brightness = Brightness.light,
|
||||
}) {
|
||||
final composer = ChatComposer(
|
||||
controller: h.controller,
|
||||
focusNode: h.focusNode,
|
||||
maxBytes: maxBytes,
|
||||
onSend: () => h.sends++,
|
||||
sendTooltip: _l10n.chat_sendMessage,
|
||||
actionsBuilder: () => [
|
||||
ChatComposerAction(
|
||||
id: 'gif',
|
||||
icon: Icons.gif_box,
|
||||
label: _l10n.chat_sendGif,
|
||||
),
|
||||
],
|
||||
onAction: h.actions.add,
|
||||
replyPreview: replyPreview,
|
||||
);
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider<MeshCoreConnector>.value(value: h.connector),
|
||||
ChangeNotifierProvider.value(value: h.settings),
|
||||
ChangeNotifierProvider.value(value: h.backgrounds),
|
||||
],
|
||||
child: MaterialApp(
|
||||
theme: brightness == Brightness.light
|
||||
? MeshTheme.light()
|
||||
: MeshTheme.dark(),
|
||||
locale: const Locale('en'),
|
||||
localizationsDelegates: const [
|
||||
AppLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
builder: (context, child) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(disableAnimations: disableAnimations),
|
||||
child: child!,
|
||||
),
|
||||
home: Scaffold(
|
||||
body: ChatBackdrop(
|
||||
chatKey: 'contact:test',
|
||||
footer: composer,
|
||||
child: const SizedBox.expand(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Finder get _plus => find.byTooltip(_l10n.chat_selectSendAction);
|
||||
Finder get _send => find.byTooltip(_l10n.chat_sendMessage);
|
||||
Finder get _bubble => find.byKey(ChatComposer.bubbleKey);
|
||||
|
||||
Future<void> _type(WidgetTester tester, String text) async {
|
||||
await tester.enterText(find.byType(TextField), text);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
void main() {
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
});
|
||||
|
||||
testWidgets('right circle is + when empty and Send with text', (
|
||||
tester,
|
||||
) async {
|
||||
final h = await _harness(tester);
|
||||
await tester.pumpWidget(_app(h));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// No separate "+" on the left: one round button only.
|
||||
expect(_plus, findsOneWidget);
|
||||
expect(_send, findsNothing);
|
||||
expect(find.byIcon(Icons.add_circle_outline), findsNothing);
|
||||
|
||||
// "+" opens the screen's action menu.
|
||||
await tester.tap(_plus);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(_l10n.chat_sendGif));
|
||||
await tester.pumpAndSettle();
|
||||
expect(h.actions, ['gif']);
|
||||
|
||||
// Whitespace doesn't count as text.
|
||||
await _type(tester, ' ');
|
||||
expect(_plus, findsOneWidget);
|
||||
|
||||
await _type(tester, 'On my way');
|
||||
expect(_plus, findsNothing);
|
||||
expect(_send, findsOneWidget);
|
||||
await tester.tap(_send);
|
||||
expect(h.sends, 1);
|
||||
|
||||
await _type(tester, '');
|
||||
expect(_plus, findsOneWidget);
|
||||
expect(_send, findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('round button has a 48dp touch target and sits right of the '
|
||||
'bubble', (tester) async {
|
||||
final h = await _harness(tester);
|
||||
await tester.pumpWidget(_app(h));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final plus = find.widgetWithIcon(IconButton, Icons.add);
|
||||
final size = tester.getSize(plus);
|
||||
expect(size.width, greaterThanOrEqualTo(48));
|
||||
expect(size.height, greaterThanOrEqualTo(48));
|
||||
expect(
|
||||
tester.getTopLeft(plus).dx,
|
||||
greaterThanOrEqualTo(tester.getTopRight(_bubble).dx),
|
||||
);
|
||||
// Single-line bubble is about one touch target tall.
|
||||
expect(tester.getSize(_bubble).height, inInclusiveRange(44, 52));
|
||||
});
|
||||
|
||||
testWidgets('swap animates, and is instant with animations disabled', (
|
||||
tester,
|
||||
) async {
|
||||
final h = await _harness(tester);
|
||||
await tester.pumpWidget(_app(h));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(find.byType(TextField), 'hi');
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
// Mid-transition both icons are on screen.
|
||||
expect(_plus, findsOneWidget);
|
||||
expect(_send, findsOneWidget);
|
||||
await tester.pumpAndSettle();
|
||||
expect(_plus, findsNothing);
|
||||
|
||||
final reduced = await _harness(tester);
|
||||
await tester.pumpWidget(_app(reduced, disableAnimations: true));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(find.byType(TextField), 'hi');
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(_plus, findsNothing);
|
||||
expect(_send, findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('field grows to 5 lines then scrolls, buttons stay at the '
|
||||
'bottom', (tester) async {
|
||||
final h = await _harness(tester);
|
||||
await tester.pumpWidget(_app(h, maxBytes: 1000));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final field = tester.widget<TextField>(find.byType(TextField));
|
||||
expect(field.minLines, 1);
|
||||
expect(field.maxLines, kComposerMaxLines);
|
||||
|
||||
await _type(tester, 'one');
|
||||
final oneLine = tester.getSize(find.byType(TextField)).height;
|
||||
final sendBottom = tester.getBottomLeft(_send).dy;
|
||||
final bubbleBottom = tester.getBottomLeft(_bubble).dy;
|
||||
|
||||
await _type(tester, List.filled(5, 'line').join('\n'));
|
||||
final fiveLines = tester.getSize(find.byType(TextField)).height;
|
||||
expect(fiveLines, greaterThan(oneLine * 2));
|
||||
|
||||
await _type(tester, List.filled(12, 'line').join('\n'));
|
||||
expect(tester.getSize(find.byType(TextField)).height, fiveLines);
|
||||
|
||||
// The panel grows upwards; the send button keeps its place at the
|
||||
// bottom of the bubble.
|
||||
expect(tester.getBottomLeft(_send).dy, sendBottom);
|
||||
expect(tester.getBottomLeft(_bubble).dy, bubbleBottom);
|
||||
});
|
||||
|
||||
testWidgets('a short window still lets the field wrap onto two lines', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(600, 260);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
tester.platformDispatcher.textScaleFactorTestValue = 2.0;
|
||||
addTearDown(tester.view.reset);
|
||||
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
|
||||
final h = await _harness(tester);
|
||||
await tester.pumpWidget(_app(h, maxBytes: 1000));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
tester.widget<TextField>(find.byType(TextField)).maxLines,
|
||||
greaterThanOrEqualTo(2),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('byte counter shows only past the warning threshold and '
|
||||
'takes no space before', (tester) async {
|
||||
final h = await _harness(tester);
|
||||
await tester.pumpWidget(_app(h, maxBytes: 20));
|
||||
await tester.pumpAndSettle();
|
||||
final emptyHeight = tester.getSize(_bubble).height;
|
||||
|
||||
await _type(tester, 'a' * 14); // 70 %: not past the threshold
|
||||
expect(find.text('14 / 20'), findsNothing);
|
||||
expect(tester.getSize(_bubble).height, emptyHeight);
|
||||
|
||||
await _type(tester, 'a' * 15);
|
||||
final counter = find.text('15 / 20');
|
||||
expect(counter, findsOneWidget);
|
||||
expect(find.descendant(of: _bubble, matching: counter), findsOneWidget);
|
||||
// Bottom-right of the bubble.
|
||||
expect(
|
||||
tester.getCenter(counter).dx,
|
||||
greaterThan(tester.getCenter(_bubble).dx),
|
||||
);
|
||||
expect(
|
||||
tester.getCenter(counter).dy,
|
||||
greaterThan(tester.getCenter(find.byType(TextField)).dy),
|
||||
);
|
||||
|
||||
// The byte limit itself is unchanged.
|
||||
await _type(tester, 'a' * 30);
|
||||
expect(h.controller.text.length, 20);
|
||||
});
|
||||
|
||||
testWidgets('ByteCountedTextField defaults are unchanged for other '
|
||||
'screens', (tester) async {
|
||||
final controller = TextEditingController();
|
||||
addTearDown(controller.dispose);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ByteCountedTextField(maxBytes: 20, controller: controller),
|
||||
),
|
||||
),
|
||||
);
|
||||
final field = tester.widget<TextField>(find.byType(TextField));
|
||||
expect(field.maxLines, isNull);
|
||||
// The counter row keeps its place while hidden.
|
||||
expect(find.text('0 / 20'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('reply preview renders inside the top of the bubble and can '
|
||||
'be dismissed', (tester) async {
|
||||
final h = await _harness(tester);
|
||||
var dismissed = false;
|
||||
await tester.pumpWidget(
|
||||
_app(
|
||||
h,
|
||||
replyPreview: ChatComposerQuote(
|
||||
title: _l10n.chat_replyingTo('Bob'),
|
||||
content: const Text('see you at camp'),
|
||||
onDismiss: () => dismissed = true,
|
||||
dismissTooltip: _l10n.chat_cancelReply,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final quote = find.byType(ChatComposerQuote);
|
||||
expect(find.descendant(of: _bubble, matching: quote), findsOneWidget);
|
||||
expect(find.text(_l10n.chat_replyingTo('Bob')), findsOneWidget);
|
||||
expect(
|
||||
tester.getBottomLeft(quote).dy,
|
||||
lessThanOrEqualTo(tester.getTopLeft(find.byType(TextField)).dy),
|
||||
);
|
||||
|
||||
await tester.tap(find.byTooltip(_l10n.chat_cancelReply));
|
||||
expect(dismissed, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('GIF preview sits in the bubble, sends on Enter and can be '
|
||||
'removed', (tester) async {
|
||||
final h = await _harness(tester);
|
||||
await tester.pumpWidget(_app(h));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
h.controller.text = GifHelper.encodeGif('abc123');
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
|
||||
expect(
|
||||
find.descendant(of: _bubble, matching: find.byType(GifMessage)),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.byType(TextField), findsNothing);
|
||||
expect(_send, findsOneWidget);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
expect(h.sends, 1);
|
||||
|
||||
await tester.tap(find.byTooltip(_l10n.chat_removeGif));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
expect(h.controller.text, isEmpty);
|
||||
expect(find.byType(TextField), findsOneWidget);
|
||||
expect(_plus, findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('translate button sits inside the bubble when enabled', (
|
||||
tester,
|
||||
) async {
|
||||
final h = await _harness(tester);
|
||||
await tester.runAsync(() => h.settings.setTranslationEnabled(true));
|
||||
await tester.pumpWidget(_app(h));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final translate = find.byType(MessageTranslationButton);
|
||||
expect(find.descendant(of: _bubble, matching: translate), findsOneWidget);
|
||||
expect(
|
||||
tester.getCenter(translate).dx,
|
||||
greaterThan(tester.getCenter(find.byType(TextField)).dx),
|
||||
);
|
||||
expect(
|
||||
find.descendant(of: translate, matching: find.byType(Tooltip)),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('panel is solid without a chat background and transparent '
|
||||
'over one', (tester) async {
|
||||
final h = await _harness(tester);
|
||||
await tester.pumpWidget(_app(h));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final scheme = MeshTheme.light().colorScheme;
|
||||
Color bubbleColor() =>
|
||||
((tester.widget<DecoratedBox>(_bubble).decoration as BoxDecoration)
|
||||
.color)
|
||||
as Color;
|
||||
final panelFill = find.ancestor(
|
||||
of: _bubble,
|
||||
matching: find.byWidgetPredicate(
|
||||
(w) => w is ColoredBox && w.color == scheme.surface,
|
||||
),
|
||||
);
|
||||
expect(panelFill, findsOneWidget);
|
||||
expect(bubbleColor(), scheme.surfaceContainerHigh);
|
||||
|
||||
await tester.runAsync(
|
||||
() => h.backgrounds.setChatBackground(
|
||||
'contact:test',
|
||||
const ChatBackground.color(Color(0xFF1E3A8A)),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(ChatBackgroundLayer), findsOneWidget);
|
||||
expect(panelFill, findsNothing);
|
||||
// Incoming-bubble colour keeps the text readable over the background.
|
||||
expect(bubbleColor(), scheme.surfaceContainerLow);
|
||||
});
|
||||
|
||||
testWidgets('offline bar uses the same bubble and height as the composer', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1200, 600);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final h = await _harness(tester);
|
||||
await tester.pumpWidget(_app(h));
|
||||
await tester.pumpAndSettle();
|
||||
final composerTop = tester.getTopLeft(_bubble).dy;
|
||||
final composerHeight = tester.getSize(_bubble).height;
|
||||
|
||||
h.connector.connected = false;
|
||||
h.connector.notifyListeners();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(OfflineComposerBar), findsOneWidget);
|
||||
expect(find.byType(TextField), findsNothing);
|
||||
expect(find.byType(ConnectButton), findsOneWidget);
|
||||
expect(find.text(_l10n.chat_composerOffline), findsOneWidget);
|
||||
final decoration =
|
||||
tester.widget<DecoratedBox>(_bubble).decoration as BoxDecoration;
|
||||
expect(
|
||||
decoration.borderRadius,
|
||||
BorderRadius.circular(kComposerBubbleRadius),
|
||||
);
|
||||
expect(tester.getSize(_bubble).height, composerHeight);
|
||||
expect(tester.getTopLeft(_bubble).dy, composerTop);
|
||||
|
||||
// The draft survives and comes back with the radio.
|
||||
h.controller.text = 'draft';
|
||||
h.connector.connected = true;
|
||||
h.connector.notifyListeners();
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester.widget<TextField>(find.byType(TextField)).controller?.text,
|
||||
'draft',
|
||||
);
|
||||
});
|
||||
|
||||
for (final scale in [1.3, 2.0]) {
|
||||
for (final brightness in Brightness.values) {
|
||||
testWidgets('fits 320dp at ${scale}x text (${brightness.name})', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(320, 568);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
tester.platformDispatcher.textScaleFactorTestValue = scale;
|
||||
addTearDown(tester.view.reset);
|
||||
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
|
||||
final h = await _harness(tester);
|
||||
await tester.runAsync(() => h.settings.setTranslationEnabled(true));
|
||||
await tester.pumpWidget(
|
||||
_app(
|
||||
h,
|
||||
maxBytes: 20,
|
||||
brightness: brightness,
|
||||
replyPreview: ChatComposerQuote(
|
||||
title: _l10n.chat_replyingTo('A very long sender name here'),
|
||||
content: const Text(
|
||||
'a long quoted message that must ellipsize',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
onDismiss: () {},
|
||||
dismissTooltip: _l10n.chat_cancelReply,
|
||||
// A long region chip, as a channel reply shows.
|
||||
trailing: Text(
|
||||
_l10n.channels_regionSetTo('north-west-valley'),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(find.byTooltip(_l10n.chat_cancelReply), findsOneWidget);
|
||||
await _type(tester, 'a' * 19);
|
||||
expect(tester.takeException(), isNull);
|
||||
|
||||
// With the keyboard up, a long draft still fits: the field caps its
|
||||
// lines to the room left.
|
||||
tester.view.viewInsets = const FakeViewPadding(bottom: 260);
|
||||
await _type(tester, List.filled(8, 'line').join('\n'));
|
||||
expect(tester.takeException(), isNull);
|
||||
tester.view.resetViewInsets();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
h.connector.connected = false;
|
||||
h.connector.notifyListeners();
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations_en.dart';
|
||||
import 'package:meshcore_open/models/contact.dart';
|
||||
import 'package:meshcore_open/widgets/contact_visuals.dart';
|
||||
|
||||
Contact _contact(int seed, {required DateTime lastSeen, DateTime? message}) =>
|
||||
Contact(
|
||||
publicKey: Uint8List.fromList(List.filled(32, seed)),
|
||||
name: 'c$seed',
|
||||
type: 1,
|
||||
pathLength: -1,
|
||||
path: Uint8List(0),
|
||||
lastSeen: lastSeen,
|
||||
lastMessageAt: message,
|
||||
);
|
||||
|
||||
void main() {
|
||||
final epoch = DateTime.fromMillisecondsSinceEpoch(0);
|
||||
final now = DateTime.now();
|
||||
|
||||
test('added time orders a contact the radio has no advert time for', () {
|
||||
final added = now.subtract(const Duration(minutes: 1));
|
||||
final fromCard = _contact(1, lastSeen: epoch);
|
||||
final heardHourAgo = _contact(
|
||||
2,
|
||||
lastSeen: now.subtract(const Duration(hours: 1)),
|
||||
);
|
||||
final addedAt = {fromCard: added};
|
||||
final sorted = [heardHourAgo, fromCard]
|
||||
..sort(
|
||||
(a, b) => resolveContactLastSeen(
|
||||
b,
|
||||
addedAt: addedAt[b],
|
||||
).compareTo(resolveContactLastSeen(a, addedAt: addedAt[a])),
|
||||
);
|
||||
|
||||
expect(resolveContactLastSeen(fromCard, addedAt: added), added);
|
||||
expect(sorted.first, fromCard);
|
||||
});
|
||||
|
||||
test('a real advert time is never replaced by the added time', () {
|
||||
final advert = now.subtract(const Duration(days: 30));
|
||||
final contact = _contact(3, lastSeen: advert);
|
||||
|
||||
expect(resolveContactLastSeen(contact, addedAt: now), advert);
|
||||
});
|
||||
|
||||
test('a newer message still wins over the added time', () {
|
||||
final message = now;
|
||||
final contact = _contact(4, lastSeen: epoch, message: message);
|
||||
|
||||
expect(
|
||||
resolveContactLastSeen(
|
||||
contact,
|
||||
addedAt: now.subtract(const Duration(hours: 1)),
|
||||
),
|
||||
message,
|
||||
);
|
||||
});
|
||||
|
||||
test('a contact never heard shows "not heard yet", not decades ago', () {
|
||||
final l10n = AppLocalizationsEn();
|
||||
|
||||
expect(formatContactLastSeen(l10n, epoch), l10n.contacts_lastSeenNever);
|
||||
expect(
|
||||
resolveContactLastSeen(_contact(5, lastSeen: epoch)),
|
||||
epoch,
|
||||
reason: 'display label is driven by the advert/message time only',
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user