Files
flutterApp/test/device_quick_actions_test.dart

119 lines
3.8 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
import 'package:maibu_satabot_v2/features/v2/device_list/presentation/widgets/device_quick_actions.dart';
import 'package:shared_preferences/shared_preferences.dart';
class _Localizations extends LocalizationsDelegate<AppLocalizations> {
const _Localizations(this.strings);
final AppLocalizations strings;
@override
bool isSupported(Locale locale) => true;
@override
Future<AppLocalizations> load(Locale locale) => SynchronousFuture(strings);
@override
bool shouldReload(_Localizations old) => false;
}
void main() {
testWidgets('moves a tile across rows and keeps the order after saving', (
tester,
) async {
SharedPreferences.setMockInitialValues({
'device_detail_quick_action_order': ['fn_sensor', 'fn_remote', 'fn_fault'],
});
final preferences = await SharedPreferences.getInstance();
final strings = (await tester.runAsync(() async {
final result = AppLocalizations(AppLocalizations.chinese);
expect(await result.load(), isTrue);
return result;
}))!;
const video = 'fn_realtime_video';
const remote = 'fn_remote';
const findRobot = 'fn_find';
const power = 'fn_app_power';
final taps = <String>[];
final actions = {
for (final id in [video, remote, findRobot, power])
id: (icon: Icons.settings, onTap: () => taps.add(id)),
};
Widget app() => MaterialApp(
localizationsDelegates: [_Localizations(strings)],
home: Scaffold(
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
children: [
DeviceQuickActions(actions: actions, preferences: preferences),
const SizedBox(height: 1000),
],
),
),
),
);
Finder tile(String id) =>
find.text(strings.translate('device_detail_v2.$id'));
Offset center(String id) => tester.getCenter(tile(id));
final edit = find.text(strings.translate('device_detail_v2.fn_edit_order'));
final save = find.text(strings.translate('device_detail_v2.fn_save_order'));
Future<void> holdAndDrag(String id, Offset target) async {
final start = center(id);
final gesture = await tester.startGesture(start);
await tester.pump(kLongPressTimeout);
await gesture.moveBy(target - start);
await gesture.up();
await tester.pumpAndSettle();
}
await tester.pumpWidget(app());
await tester.tap(edit);
await tester.pumpAndSettle();
expect(tile('fn_fault'), findsNothing);
expect(tile('fn_sensor'), findsNothing);
final from = center(video);
final to = center(power);
final topLeft = center(remote);
expect(from.dx, greaterThan(topLeft.dx));
expect(to.dx, topLeft.dx);
expect(to.dy, greaterThan(from.dy));
await tester.tap(tile(video));
await tester.drag(tile(video), center(findRobot) - from);
await tester.pumpAndSettle();
expect(taps, isEmpty);
expect(center(video), from);
await holdAndDrag(video, to);
expect(center(findRobot), from);
expect(center(video), to);
await holdAndDrag(video, topLeft);
expect(center(video), topLeft);
await tester.tap(save);
await tester.pumpAndSettle();
expect(edit, findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
await preferences.reload();
await tester.pumpWidget(app());
await tester.pumpAndSettle();
expect(center(video).dy, center(remote).dy);
expect(center(video).dx, lessThan(center(remote).dx));
await tester.tap(tile(video));
expect(taps, [video]);
});
}