Merge remote-tracking branch 'origin/dev' into feat/chess

This commit is contained in:
zjs81 committed 2026-11-22 17:00:35 -07:00
commit adead644c7
53 files changed
+2028 -307

No files matched your search

@@ -0,0 +1,498 @@
import 'dart:async';
import 'package:flutter/material.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/l10n/app_localizations.dart';
import 'package:meshcore_open/screens/app_settings_screen.dart';
import 'package:meshcore_open/services/app_settings_service.dart';
import 'package:meshcore_open/services/network_map/network_map_upload_service.dart';
import 'package:meshcore_open/services/notification_service.dart';
import 'package:meshcore_open/services/translation_service.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:meshcore_open/theme/mesh_theme.dart';
import 'package:meshcore_open/utils/platform_info.dart';
import 'package:meshcore_open/widgets/mesh_nav_tile.dart';
import 'package:meshcore_open/widgets/mesh_ui.dart';
import 'package:meshcore_open/widgets/settings_reveal.dart';
Future<AppSettingsService> _createSettingsService({
bool notificationsEnabled = true,
}) async {
SharedPreferences.setMockInitialValues({});
PrefsManager.reset();
await PrefsManager.initialize();
final settings = AppSettingsService();
await settings.loadSettings();
if (!notificationsEnabled) {
await settings.setNotificationsEnabled(false);
}
return settings;
}
Widget _buildSectionTestApp({
required AppSettingsService settings,
ThemeData? theme,
double textScale = 1.0,
Future<bool> Function()? hasPermission,
Future<bool> Function()? requestPermission,
}) {
return ChangeNotifierProvider.value(
value: settings,
child: MaterialApp(
theme: 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: Scaffold(
body: MeshSettingsTileTheme(
child: SingleChildScrollView(
child: MeshCard(
padding: EdgeInsets.zero,
child: NotificationsSettingsSection(
hasPermission: hasPermission,
requestPermission: requestPermission,
),
),
),
),
),
),
);
}
void main() {
setUp(() {
NotificationService().resetForTest();
PlatformInfo.debugIsAndroidOverride = null;
});
tearDown(() {
NotificationService().resetForTest();
PlatformInfo.debugIsAndroidOverride = null;
});
testWidgets(
'notificationsEnabled + OS permission granted shows switch ON, expanded SettingsReveal, and exact row order including Android Background Activity last',
(tester) async {
PlatformInfo.debugIsAndroidOverride = true;
final settings = await _createSettingsService(notificationsEnabled: true);
await tester.pumpWidget(
_buildSectionTestApp(
settings: settings,
hasPermission: () async => true,
),
);
await tester.pumpAndSettle();
final context = tester.element(find.byType(NotificationsSettingsSection));
final l10n = AppLocalizations.of(context);
final enableTile = tester.widget<SwitchListTile>(
find.widgetWithText(
SwitchListTile,
l10n.appSettings_enableNotifications,
),
);
expect(enableTile.value, isTrue);
expect(
find.text(l10n.appSettings_enableNotificationsSubtitle),
findsOneWidget,
);
final reveal = tester.widget<SettingsReveal>(find.byType(SettingsReveal));
expect(reveal.visible, isTrue);
final msgFinder = find.text(l10n.appSettings_messageNotifications);
final channelFinder = find.text(
l10n.appSettings_channelMessageNotifications,
);
final advertFinder = find.text(
l10n.appSettings_advertisementNotifications,
);
final bgActivityFinder = find.text(l10n.appSettings_batteryOptimization);
expect(msgFinder, findsOneWidget);
expect(channelFinder, findsOneWidget);
expect(advertFinder, findsOneWidget);
expect(bgActivityFinder, findsOneWidget);
final msgY = tester.getTopLeft(msgFinder).dy;
final channelY = tester.getTopLeft(channelFinder).dy;
final advertY = tester.getTopLeft(advertFinder).dy;
final bgActivityY = tester.getTopLeft(bgActivityFinder).dy;
expect(msgY, lessThan(channelY));
expect(channelY, lessThan(advertY));
expect(advertY, lessThan(bgActivityY));
},
);
testWidgets(
'notificationsEnabled + OS permission denied shows switch OFF, hides child toggles, shows denied subtitle, and requests permission on tap',
(tester) async {
PlatformInfo.debugIsAndroidOverride = true;
final settings = await _createSettingsService(notificationsEnabled: true);
var requestResult = false;
var requestCalls = 0;
await tester.pumpWidget(
_buildSectionTestApp(
settings: settings,
hasPermission: () async => false,
requestPermission: () async {
requestCalls++;
return requestResult;
},
),
);
await tester.pumpAndSettle();
final context = tester.element(find.byType(NotificationsSettingsSection));
final l10n = AppLocalizations.of(context);
SwitchListTile enableTile() => tester.widget<SwitchListTile>(
find.widgetWithText(
SwitchListTile,
l10n.appSettings_enableNotifications,
),
);
// Even though settings.notificationsEnabled is true, switch is OFF when OS permission is denied.
expect(settings.settings.notificationsEnabled, isTrue);
expect(enableTile().value, isFalse);
expect(
find.text(l10n.appSettings_notificationPermissionDenied),
findsOneWidget,
);
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
expect(
find.text(l10n.appSettings_channelMessageNotifications),
findsNothing,
);
expect(
find.text(l10n.appSettings_advertisementNotifications),
findsNothing,
);
expect(find.text(l10n.appSettings_batteryOptimization), findsNothing);
// No stray Divider when collapsed.
expect(find.byType(Divider), findsNothing);
// Tapping switch while permission request is denied keeps it OFF and shows snackbar.
await tester.tap(find.text(l10n.appSettings_enableNotifications));
await tester.pumpAndSettle();
expect(requestCalls, 1);
expect(enableTile().value, isFalse);
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
expect(
find.descendant(
of: find.byType(SnackBar),
matching: find.text(l10n.appSettings_notificationPermissionDenied),
),
findsOneWidget,
);
// Now grant permission on the next tap: switch turns ON, snackbar appears, and children expand.
requestResult = true;
await tester.tap(find.text(l10n.appSettings_enableNotifications));
await tester.pump();
await tester.pump(RevealOnEnable.duration);
await tester.pumpAndSettle();
expect(requestCalls, 2);
expect(enableTile().value, isTrue);
expect(settings.settings.notificationsEnabled, isTrue);
expect(
find.text(l10n.appSettings_enableNotificationsSubtitle),
findsOneWidget,
);
expect(
find.descendant(
of: find.byType(SnackBar),
matching: find.text(l10n.appSettings_notificationsEnabled),
),
findsOneWidget,
);
expect(find.text(l10n.appSettings_messageNotifications), findsOneWidget);
expect(
find.text(l10n.appSettings_channelMessageNotifications),
findsOneWidget,
);
expect(
find.text(l10n.appSettings_advertisementNotifications),
findsOneWidget,
);
expect(find.text(l10n.appSettings_batteryOptimization), findsOneWidget);
},
);
testWidgets('does not flash ON while async OS permission check is in flight', (
tester,
) async {
final settings = await _createSettingsService(notificationsEnabled: true);
final completer = Completer<bool>();
await tester.pumpWidget(
_buildSectionTestApp(
settings: settings,
hasPermission: () => completer.future,
),
);
final context = tester.element(find.byType(NotificationsSettingsSection));
final l10n = AppLocalizations.of(context);
// While async permission check is in flight, switch is OFF and children are hidden,
// and the normal subtitle is shown rather than flashing an error prematurely.
final initialTile = tester.widget<SwitchListTile>(
find.widgetWithText(SwitchListTile, l10n.appSettings_enableNotifications),
);
expect(initialTile.value, isFalse);
expect(
find.text(l10n.appSettings_enableNotificationsSubtitle),
findsOneWidget,
);
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
completer.complete(false);
await tester.pumpAndSettle();
final settledTile = tester.widget<SwitchListTile>(
find.widgetWithText(SwitchListTile, l10n.appSettings_enableNotifications),
);
expect(settledTile.value, isFalse);
expect(
find.text(l10n.appSettings_notificationPermissionDenied),
findsOneWidget,
);
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
});
testWidgets(
'toggling OFF -> ON collapses children out of tree and expands them back',
(tester) async {
final settings = await _createSettingsService(notificationsEnabled: true);
await tester.pumpWidget(
_buildSectionTestApp(
settings: settings,
hasPermission: () async => true,
requestPermission: () async => true,
),
);
await tester.pumpAndSettle();
final context = tester.element(find.byType(NotificationsSettingsSection));
final l10n = AppLocalizations.of(context);
expect(find.text(l10n.appSettings_messageNotifications), findsOneWidget);
// Toggle OFF
await tester.tap(find.text(l10n.appSettings_enableNotifications));
await tester.pump();
await tester.pump(RevealOnEnable.duration);
await tester.pumpAndSettle();
expect(settings.settings.notificationsEnabled, isFalse);
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
expect(
find.text(l10n.appSettings_channelMessageNotifications),
findsNothing,
);
expect(
find.text(l10n.appSettings_advertisementNotifications),
findsNothing,
);
expect(find.byType(Divider), findsNothing);
expect(
find.descendant(
of: find.byType(SnackBar),
matching: find.text(l10n.appSettings_notificationsDisabled),
),
findsOneWidget,
);
// Toggle back ON
await tester.tap(find.text(l10n.appSettings_enableNotifications));
await tester.pump();
await tester.pump(RevealOnEnable.duration);
await tester.pumpAndSettle();
expect(settings.settings.notificationsEnabled, isTrue);
expect(find.text(l10n.appSettings_messageNotifications), findsOneWidget);
expect(
find.text(l10n.appSettings_channelMessageNotifications),
findsOneWidget,
);
expect(
find.text(l10n.appSettings_advertisementNotifications),
findsOneWidget,
);
},
);
testWidgets(
'AppLifecycleState.resumed re-checks OS permission for revoke and grant',
(tester) async {
final settings = await _createSettingsService(notificationsEnabled: true);
var osPermissionGranted = true;
await tester.pumpWidget(
_buildSectionTestApp(
settings: settings,
hasPermission: () async => osPermissionGranted,
),
);
await tester.pumpAndSettle();
final context = tester.element(find.byType(NotificationsSettingsSection));
final l10n = AppLocalizations.of(context);
SwitchListTile enableTile() => tester.widget<SwitchListTile>(
find.widgetWithText(
SwitchListTile,
l10n.appSettings_enableNotifications,
),
);
expect(enableTile().value, isTrue);
expect(find.text(l10n.appSettings_messageNotifications), findsOneWidget);
// User revokes permission in OS settings and resumes app.
osPermissionGranted = false;
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
await tester.pump();
await tester.pump(RevealOnEnable.duration);
await tester.pumpAndSettle();
expect(enableTile().value, isFalse);
expect(
find.text(l10n.appSettings_notificationPermissionDenied),
findsOneWidget,
);
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
// User grants permission in OS settings and resumes app.
osPermissionGranted = true;
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
await tester.pump();
await tester.pump(RevealOnEnable.duration);
await tester.pumpAndSettle();
expect(enableTile().value, isTrue);
expect(
find.text(l10n.appSettings_enableNotificationsSubtitle),
findsOneWidget,
);
expect(find.text(l10n.appSettings_messageNotifications), findsOneWidget);
},
);
testWidgets(
'AppSettingsScreen integrates NotificationService permission hook',
(tester) async {
final settings = await _createSettingsService(notificationsEnabled: true);
NotificationService().permissionCheckerForTest = () async => false;
tester.view.physicalSize = const Size(800, 4000);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final connector = MeshCoreConnector();
final translation = TranslationService(settings);
final uploads = NetworkMapUploadService(
connector: connector,
settings: settings,
);
addTearDown(connector.dispose);
addTearDown(translation.dispose);
addTearDown(uploads.dispose);
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: settings),
ChangeNotifierProvider.value(value: connector),
ChangeNotifierProvider.value(value: translation),
ChangeNotifierProvider.value(value: uploads),
],
child: MaterialApp(
theme: MeshTheme.light(),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: const AppSettingsScreen(),
),
),
);
await tester.pumpAndSettle();
final context = tester.element(find.byType(AppSettingsScreen));
final l10n = AppLocalizations.of(context);
final enableTile = tester.widget<SwitchListTile>(
find.widgetWithText(
SwitchListTile,
l10n.appSettings_enableNotifications,
),
);
expect(enableTile.value, isFalse);
expect(
find.text(l10n.appSettings_notificationPermissionDenied),
findsOneWidget,
);
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
},
);
for (final isDark in [false, true]) {
for (final scale in [1.3, 2.0]) {
for (final granted in [true, false]) {
testWidgets(
'NotificationsSettingsSection fits 320dp at ${scale}x in ${isDark ? "dark" : "light"} theme when permission=${granted ? "granted" : "denied"}',
(tester) async {
PlatformInfo.debugIsAndroidOverride = true;
tester.view.physicalSize = const Size(320, 568);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
final settings = await _createSettingsService(
notificationsEnabled: true,
);
await tester.pumpWidget(
_buildSectionTestApp(
settings: settings,
theme: isDark ? MeshTheme.dark() : MeshTheme.light(),
textScale: scale,
hasPermission: () async => granted,
),
);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
// Verify 48dp minimum touch target height on all rendered tiles.
final tiles = find.byType(ListTile);
expect(tiles, findsWidgets);
for (final element in tiles.evaluate()) {
final size = tester.getSize(find.byWidget(element.widget));
expect(size.height, greaterThanOrEqualTo(48));
}
},
);
}
}
}
}
@@ -0,0 +1,194 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/services/notification_service.dart';
class _NoopNotificationsPlatform extends FlutterLocalNotificationsPlatform {}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
const channel = MethodChannel('dexterous.com/flutter/local_notifications');
final service = NotificationService();
setUp(() {
service.resetForTest();
debugDefaultTargetPlatformOverride = null;
});
tearDown(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, null);
FlutterLocalNotificationsPlatform.instance = _NoopNotificationsPlatform();
debugDefaultTargetPlatformOverride = null;
service.resetForTest();
});
group('NotificationService.hasNotificationPermission', () {
test('queries Android areNotificationsEnabled and updates cache', () async {
debugDefaultTargetPlatformOverride = TargetPlatform.android;
AndroidFlutterLocalNotificationsPlugin.registerWith();
bool? androidEnabled = true;
var checkCalls = 0;
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
if (call.method == 'initialize') return true;
if (call.method == 'areNotificationsEnabled') {
checkCalls++;
return androidEnabled;
}
return null;
});
expect(await service.hasNotificationPermission(), isTrue);
expect(service.canNotifyCacheForTest, isTrue);
expect(checkCalls, 1);
// Subsequent ensureCanNotify uses the cached grant without re-querying.
expect(await service.ensureCanNotifyForTest(), isTrue);
expect(checkCalls, 1);
// Revoking OS permission invalidates the cached true on the next live check.
androidEnabled = false;
expect(await service.hasNotificationPermission(), isFalse);
expect(service.canNotifyCacheForTest, isNull);
expect(checkCalls, 2);
// _ensureCanNotify now re-queries and returns false instead of stale true.
expect(await service.ensureCanNotifyForTest(), isFalse);
expect(checkCalls, 3);
// Null response from channel is treated as false.
androidEnabled = null;
expect(await service.hasNotificationPermission(), isFalse);
expect(service.canNotifyCacheForTest, isNull);
});
test(
'queries iOS checkPermissions and invalidates cache when revoked',
() async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
IOSFlutterLocalNotificationsPlugin.registerWith();
Map<String, Object?>? permissionsResponse = {
'isEnabled': true,
'isAlertEnabled': true,
'isBadgeEnabled': true,
'isSoundEnabled': true,
'isProvisionalEnabled': false,
'isCriticalEnabled': false,
'isProvidesAppNotificationSettingsEnabled': false,
'isCarPlayEnabled': false,
};
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
if (call.method == 'initialize') return true;
if (call.method == 'checkPermissions') return permissionsResponse;
return null;
});
expect(await service.hasNotificationPermission(), isTrue);
expect(service.canNotifyCacheForTest, isTrue);
expect(await service.ensureCanNotifyForTest(), isTrue);
// Provisional permission also counts as granted.
permissionsResponse = {
'isEnabled': false,
'isAlertEnabled': false,
'isBadgeEnabled': false,
'isSoundEnabled': false,
'isProvisionalEnabled': true,
};
expect(await service.hasNotificationPermission(), isTrue);
expect(service.canNotifyCacheForTest, isTrue);
// Denied in iOS Settings clears the cached grant.
permissionsResponse = {
'isEnabled': false,
'isAlertEnabled': false,
'isBadgeEnabled': false,
'isSoundEnabled': false,
'isProvisionalEnabled': false,
};
expect(await service.hasNotificationPermission(), isFalse);
expect(service.canNotifyCacheForTest, isNull);
expect(await service.ensureCanNotifyForTest(), isFalse);
permissionsResponse = null;
expect(await service.hasNotificationPermission(), isFalse);
},
);
test('queries macOS checkPermissions and updates cache', () async {
debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
MacOSFlutterLocalNotificationsPlugin.registerWith();
Map<String, Object?>? permissionsResponse = {
'isEnabled': true,
'isAlertEnabled': true,
'isBadgeEnabled': true,
'isSoundEnabled': true,
};
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
if (call.method == 'initialize') return true;
if (call.method == 'checkPermissions') return permissionsResponse;
return null;
});
expect(await service.hasNotificationPermission(), isTrue);
expect(service.canNotifyCacheForTest, isTrue);
permissionsResponse = {
'isEnabled': false,
'isAlertEnabled': false,
'isBadgeEnabled': false,
'isSoundEnabled': false,
};
expect(await service.hasNotificationPermission(), isFalse);
expect(service.canNotifyCacheForTest, isNull);
});
test(
'returns true on desktop platforms without an OS permission gate',
() async {
debugDefaultTargetPlatformOverride = TargetPlatform.linux;
FlutterLocalNotificationsPlatform.instance =
_NoopNotificationsPlatform();
expect(await service.hasNotificationPermission(), isTrue);
expect(service.canNotifyCacheForTest, isTrue);
},
);
test('requestPermissions updates and clears canNotify cache', () async {
debugDefaultTargetPlatformOverride = TargetPlatform.android;
AndroidFlutterLocalNotificationsPlugin.registerWith();
var grantResult = true;
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
if (call.method == 'initialize') return true;
if (call.method == 'requestNotificationsPermission') {
return grantResult;
}
if (call.method == 'areNotificationsEnabled') {
return grantResult;
}
return null;
});
expect(await service.requestPermissions(), isTrue);
expect(service.canNotifyCacheForTest, isTrue);
grantResult = false;
expect(await service.requestPermissions(), isFalse);
expect(service.canNotifyCacheForTest, isNull);
expect(await service.ensureCanNotifyForTest(), isFalse);
});
});
}
+185 -7
View File
@@ -65,6 +65,7 @@ Widget _app(
Widget? replyPreview,
bool disableAnimations = false,
Brightness brightness = Brightness.light,
List<ChatComposerAction>? actions,
}) {
final composer = ChatComposer(
controller: h.controller,
@@ -72,13 +73,15 @@ Widget _app(
maxBytes: maxBytes,
onSend: () => h.sends++,
sendTooltip: _l10n.chat_sendMessage,
actionsBuilder: () => [
ChatComposerAction(
id: 'gif',
icon: Icons.gif_box,
label: _l10n.chat_sendGif,
),
],
actionsBuilder: () =>
actions ??
[
ChatComposerAction(
id: 'gif',
icon: Icons.gif_box,
label: _l10n.chat_sendGif,
),
],
onAction: h.actions.add,
replyPreview: replyPreview,
);
@@ -119,6 +122,7 @@ Widget _app(
Finder get _plus => find.byTooltip(_l10n.chat_selectSendAction);
Finder get _send => find.byTooltip(_l10n.chat_sendMessage);
Finder get _menu => find.byKey(ChatComposer.actionMenuKey);
Finder get _bubble => find.byKey(ChatComposer.bubbleKey);
Future<void> _type(WidgetTester tester, String text) async {
@@ -380,6 +384,180 @@ void main() {
);
});
testWidgets('+ menu opens above the button and grows upward from it', (
tester,
) async {
final h = await _harness(tester);
await tester.pumpWidget(_app(h));
await tester.pumpAndSettle();
final plusTop = tester.getTopLeft(_plus).dy;
final plusRight = tester.getTopRight(_plus).dx;
await tester.tap(_plus);
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
final early = tester.getRect(_menu);
await tester.pump(const Duration(milliseconds: 50));
final later = tester.getRect(_menu);
expect(later.top, lessThan(early.top));
expect(later.bottom, closeTo(early.bottom, 0.01));
expect(later.height, greaterThan(early.height));
await tester.pumpAndSettle();
final open = tester.getRect(_menu);
expect(open.bottom, closeTo(early.bottom, 0.01));
expect(open.bottom, lessThanOrEqualTo(plusTop));
// Trailing edges line up (the tap target is a little wider than the
// visible button).
expect(open.right, closeTo(plusRight, 3));
expect(open.height, greaterThanOrEqualTo(kMinInteractiveDimension));
// The card hugs a short label instead of filling the width cap.
expect(open.width, lessThan(280));
await tester.tapAt(const Offset(5, 5));
await tester.pump();
await tester.pump(const Duration(milliseconds: 60));
final closing = tester.getRect(_menu);
expect(closing.bottom, closeTo(open.bottom, 0.01));
expect(closing.height, lessThan(open.height));
await tester.pumpAndSettle();
expect(_menu, findsNothing);
});
testWidgets('+ menu opens at once with animations disabled', (tester) async {
final h = await _harness(tester);
await tester.pumpWidget(_app(h, disableAnimations: true));
await tester.pumpAndSettle();
await tester.tap(_plus);
await tester.pump();
await tester.pump();
expect(find.text(_l10n.chat_sendGif), findsOneWidget);
expect(
tester.getRect(_menu).height,
greaterThanOrEqualTo(kMinInteractiveDimension),
);
expect(
tester.getRect(_menu).bottom,
lessThanOrEqualTo(tester.getTopLeft(_plus).dy),
);
});
testWidgets('+ menu keeps focus on the text field', (tester) async {
final h = await _harness(tester);
await tester.pumpWidget(_app(h));
await tester.pumpAndSettle();
h.focusNode.requestFocus();
await tester.pumpAndSettle();
expect(h.focusNode.hasFocus, isTrue);
await tester.tap(_plus);
await tester.pumpAndSettle();
expect(find.text(_l10n.chat_sendGif), findsOneWidget);
expect(h.focusNode.hasFocus, isTrue);
});
testWidgets('tapping outside the + menu closes it without an action', (
tester,
) async {
final h = await _harness(tester);
await tester.pumpWidget(_app(h));
await tester.pumpAndSettle();
await tester.tap(_plus);
await tester.pumpAndSettle();
expect(_menu, findsOneWidget);
await tester.tapAt(const Offset(5, 5));
await tester.pumpAndSettle();
expect(_menu, findsNothing);
expect(h.actions, isEmpty);
});
for (final scale in [1.3, 2.0]) {
testWidgets('+ menu stays above the button at 320dp, ${scale}x text', (
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.pumpWidget(
_app(
h,
actions: [
for (var i = 0; i < 4; i++)
ChatComposerAction(
id: 'a$i',
icon: Icons.gif_box,
label: 'A rather long action label number $i for the menu',
),
],
),
);
await tester.pumpAndSettle();
await tester.tap(_plus);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
final menu = tester.getRect(_menu);
expect(menu.bottom, lessThanOrEqualTo(tester.getTopLeft(_plus).dy));
expect(menu.left, greaterThanOrEqualTo(0));
expect(menu.right, lessThanOrEqualTo(320));
expect(menu.top, greaterThanOrEqualTo(0));
await tester.tap(find.textContaining('number 3'));
await tester.pumpAndSettle();
expect(h.actions, ['a3']);
});
}
testWidgets('+ menu re-clamps into the viewport when it shrinks while open', (
tester,
) async {
tester.view.physicalSize = const Size(600, 800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final h = await _harness(tester);
await tester.pumpWidget(
_app(
h,
actions: [
for (var i = 0; i < 6; i++)
ChatComposerAction(
id: 'a$i',
icon: Icons.gif_box,
label: 'A rather long action label number $i for the menu',
),
],
),
);
await tester.pumpAndSettle();
await tester.tap(_plus);
await tester.pumpAndSettle();
final before = tester.getRect(_menu);
expect(find.textContaining('number 5'), findsOneWidget);
tester.view.physicalSize = const Size(240, 320);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
final after = tester.getRect(_menu);
expect(after.width, lessThan(before.width));
expect(after.height, lessThan(before.height));
expect(after.left, greaterThanOrEqualTo(0));
expect(after.right, lessThanOrEqualTo(240));
expect(after.top, greaterThanOrEqualTo(0));
expect(after.bottom, lessThanOrEqualTo(tester.getTopLeft(_plus).dy));
await tester.drag(
find.descendant(of: _menu, matching: find.byType(Scrollable)),
const Offset(0, -1000),
);
await tester.pumpAndSettle();
await tester.tap(find.textContaining('number 5'));
await tester.pumpAndSettle();
expect(h.actions, ['a5']);
});
testWidgets('panel is solid without a chat background and transparent '
'over one', (tester) async {
final h = await _harness(tester);
@@ -0,0 +1,247 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/l10n/app_localizations_en.dart';
import 'package:meshcore_open/theme/mesh_theme.dart';
import 'package:meshcore_open/widgets/mesh_ui.dart';
import 'package:meshcore_open/widgets/message_translation_button.dart';
final _l10n = AppLocalizationsEn();
Future<void> _openSheet(
WidgetTester tester, {
Size size = const Size(400, 800),
EdgeInsets padding = const EdgeInsets.only(top: 48, bottom: 34),
EdgeInsets viewPadding = const EdgeInsets.only(top: 48, bottom: 34),
EdgeInsets viewInsets = EdgeInsets.zero,
double textScale = 1.0,
bool initialEnabled = false,
String? initialLanguageCode,
ValueChanged<bool>? onEnabledChanged,
ValueChanged<String?>? onLanguageSelected,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1.0;
tester.view.padding = FakeViewPadding(
left: padding.left,
top: padding.top,
right: padding.right,
bottom: padding.bottom,
);
tester.view.viewPadding = FakeViewPadding(
left: viewPadding.left,
top: viewPadding.top,
right: viewPadding.right,
bottom: viewPadding.bottom,
);
tester.view.viewInsets = FakeViewPadding(
left: viewInsets.left,
top: viewInsets.top,
right: viewInsets.right,
bottom: viewInsets.bottom,
);
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
addTearDown(tester.view.resetPadding);
addTearDown(tester.view.resetViewPadding);
addTearDown(tester.view.resetViewInsets);
await tester.pumpWidget(
MaterialApp(
theme: MeshTheme.light(),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
builder: (context, child) {
final media = MediaQuery.of(context);
return MediaQuery(
data: media.copyWith(
size: size,
padding: padding,
viewPadding: viewPadding,
viewInsets: viewInsets,
textScaler: TextScaler.linear(textScale),
),
child: child!,
);
},
home: Builder(
builder: (context) => Scaffold(
body: Center(
child: MessageTranslationButton(
enabled: initialEnabled,
languageCode: initialLanguageCode,
onPressed: () => showMessageTranslationSheet(
context: context,
enabled: initialEnabled,
selectedLanguageCode: initialLanguageCode,
onEnabledChanged: onEnabledChanged ?? (_) {},
onLanguageSelected: onLanguageSelected ?? (_) {},
),
),
),
),
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byType(MessageTranslationButton));
await tester.pumpAndSettle();
}
void main() {
testWidgets(
'sheet respects top safe area, caps height at <= 85%, and shrinks when filtered',
(tester) async {
const screenSize = Size(400, 800);
await _openSheet(
tester,
size: screenSize,
padding: const EdgeInsets.only(top: 48, bottom: 34),
viewPadding: const EdgeInsets.only(top: 48, bottom: 34),
);
expect(find.byType(BottomSheetHeader), findsOneWidget);
expect(find.text(_l10n.translation_messageTranslation), findsOneWidget);
final sheetFinder = find.byType(BottomSheet);
final sheetRect = tester.getRect(sheetFinder);
final titleTop = tester
.getTopLeft(find.text(_l10n.translation_messageTranslation))
.dy;
// Top edge must stay well below the 48px Android status bar inset.
expect(sheetRect.top, greaterThan(48.0));
expect(sheetRect.top, greaterThanOrEqualTo(screenSize.height * 0.15));
expect(sheetRect.height, lessThanOrEqualTo(screenSize.height * 0.85));
expect(titleTop - sheetRect.top, inInclusiveRange(16.0, 36.0));
final unfilteredHeight = sheetRect.height;
// Filtering down to a single language shrinks the sheet naturally.
await tester.enterText(find.byType(TextField), 'Spanish');
await tester.pumpAndSettle();
final filteredRect = tester.getRect(sheetFinder);
expect(filteredRect.height, lessThan(unfilteredHeight));
expect(filteredRect.top, greaterThan(sheetRect.top));
},
);
testWidgets(
'toggling switch, filtering languages, and selecting target or app language works',
(tester) async {
bool? latestEnabled;
String? latestLanguage = 'fr';
await _openSheet(
tester,
initialEnabled: false,
initialLanguageCode: latestLanguage,
onEnabledChanged: (value) => latestEnabled = value,
onLanguageSelected: (code) => latestLanguage = code,
);
expect(find.text(_l10n.translation_composerDisabledHint), findsOneWidget);
await tester.tap(
find.widgetWithText(
SwitchListTile,
_l10n.translation_translateBeforeSending,
),
);
await tester.pumpAndSettle();
expect(latestEnabled, isTrue);
expect(find.text(_l10n.translation_composerEnabledHint), findsOneWidget);
// Filter by language name and select Spanish ('es').
await tester.enterText(find.byType(TextField), 'Spanish');
await tester.pumpAndSettle();
expect(find.widgetWithText(ListTile, 'Spanish'), findsOneWidget);
expect(find.widgetWithText(ListTile, 'German'), findsNothing);
await tester.tap(find.widgetWithText(ListTile, 'Spanish'));
await tester.pumpAndSettle();
expect(latestLanguage, 'es');
expect(find.byType(BottomSheet), findsNothing);
// Re-open and select "Use app language" (null).
await tester.tap(find.byType(MessageTranslationButton));
await tester.pumpAndSettle();
await tester.tap(
find.widgetWithText(ListTile, _l10n.translation_useAppLanguage),
);
await tester.pumpAndSettle();
expect(latestLanguage, isNull);
expect(find.byType(BottomSheet), findsNothing);
},
);
testWidgets(
'avoids layout overflow at 320dp width with 1.3x and 2.0x text scale and keyboard insets',
(tester) async {
for (final textScale in [1.0, 1.3, 2.0]) {
for (final keyboardInset in [0.0, 300.0]) {
bool? toggled;
String? chosenLanguage;
await _openSheet(
tester,
size: const Size(320, 568),
padding: const EdgeInsets.only(top: 48, bottom: 34),
viewPadding: const EdgeInsets.only(top: 48, bottom: 34),
viewInsets: EdgeInsets.only(bottom: keyboardInset),
textScale: textScale,
onEnabledChanged: (value) => toggled = value,
onLanguageSelected: (code) => chosenLanguage = code,
);
expect(tester.takeException(), isNull);
final sheetRect = tester.getRect(find.byType(BottomSheet));
expect(sheetRect.top, greaterThanOrEqualTo(48.0));
await Scrollable.ensureVisible(
tester.element(find.byType(Switch)),
alignment: 0.5,
);
await tester.pumpAndSettle();
await tester.tap(find.byType(Switch));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(toggled, isTrue);
await Scrollable.ensureVisible(
tester.element(find.byType(TextField)),
alignment: 0.5,
);
await tester.pumpAndSettle();
await tester.enterText(find.byType(TextField), 'de');
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
final germanTile = find.widgetWithText(
ListTile,
'German',
skipOffstage: false,
);
await Scrollable.ensureVisible(
tester.element(germanTile),
alignment: 0.5,
);
await tester.pumpAndSettle();
await tester.tap(germanTile);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(chosenLanguage, 'de');
}
}
},
);
}