Files
flutterApp/test/pv_task_progress_test.dart

194 lines
6.2 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
import 'package:maibu_satabot_v2/core/network/mqtt/domain/entities/mower_task_status_entity.dart';
import 'package:maibu_satabot_v2/features/autonomous_work/data/site_device_realtime_subscriptions.dart';
import 'package:maibu_satabot_v2/features/pv_control/presentation/widgets/pv_task_progress_content.dart';
import 'support/region_test_localizations.dart';
class _Subscriptions extends Fake implements SiteDeviceRealtimeSubscriptions {
final controllers = <String, StreamController<MowerTaskStatusEntity?>>{};
final requests = <String>[];
@override
Stream<MowerTaskStatusEntity?> watchTaskStatus({
required int? siteId,
required String deviceSn,
}) {
final key = '$siteId/$deviceSn';
requests.add(key);
return controllers
.putIfAbsent(key, () => StreamController.broadcast())
.stream;
}
void send(
String sn,
Map<String, dynamic> data, {
int site = 1,
DateTime? receivedAt,
}) => controllers['$site/$sn']!.add(
MowerTaskStatusEntity.fromJson({
'sn': sn,
'task_id': '33394',
'data': data,
}, receivedAt: receivedAt),
);
Future<void> close() async {
for (final controller in controllers.values) {
await controller.close();
}
}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUpAll(RegionTestLocalizations.loadAssets);
late _Subscriptions subscriptions;
setUp(() => subscriptions = _Subscriptions());
tearDown(() => subscriptions.close());
Future<void> mount(
WidgetTester tester, {
String sn = 'A',
int site = 1,
bool compact = false,
Locale locale = AppLocalizations.chinese,
}) async {
await tester.pumpWidget(
MaterialApp(
locale: locale,
supportedLocales: AppLocalizations.supportedLocales,
localizationsDelegates: const [
RegionTestLocalizations(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: Scaffold(
body: Align(
alignment: Alignment.topLeft,
child: SizedBox(
width: compact ? 100 : 200,
child: PvTaskProgressContent(
subscriptions: subscriptions,
siteId: site,
deviceSn: sn,
scale: compact ? 0.64 : 1,
),
),
),
),
),
);
await tester.pumpAndSettle();
}
Future<void> flush(WidgetTester tester) async {
await tester.pump();
await tester.pump();
}
testWidgets(
'remote work card replaces demo values with real progress, time and distance',
(tester) async {
await mount(tester);
expect(find.text('未知'), findsWidgets);
expect(find.text('68%'), findsNothing);
expect(find.text('01:42:18'), findsNothing);
subscriptions.send('A', {
'progress': 0.091724,
'elapsed_s': 57.54,
'eta_s': 569.789,
'distance_remaining': 91.36074,
});
await flush(tester);
expect(find.text('9.17%'), findsOneWidget);
expect(find.text('00:00:57'), findsOneWidget);
expect(find.text('00:09:29'), findsOneWidget);
expect(find.text('91.36 m'), findsOneWidget);
expect(find.text('作业面积'), findsOneWidget);
// Area is absent in the protocol and must remain unknown.
expect(find.text('未知'), findsOneWidget);
expect(find.byType(LinearProgressIndicator), findsOneWidget);
expect(
tester
.widget<LinearProgressIndicator>(
find.byType(LinearProgressIndicator),
)
.value,
closeTo(0.091724, 0.000001),
);
await tester.pumpWidget(const SizedBox());
},
);
testWidgets(
'partial reports clear omitted values and real zero values remain visible',
(tester) async {
await mount(tester);
subscriptions.send('A', {'progress': 0.5, 'elapsed_s': 60});
await flush(tester);
subscriptions.send('A', {'progress': 0, 'elapsed_s': 0});
await flush(tester);
expect(find.text('0.00%'), findsOneWidget);
expect(find.text('00:00:00'), findsOneWidget);
subscriptions.send('A', {});
await flush(tester);
expect(find.text('0.00%'), findsNothing);
expect(find.text('00:00:00'), findsNothing);
expect(find.text('未知'), findsWidgets);
await tester.pumpWidget(const SizedBox());
},
);
testWidgets(
'switching device or site clears old task data and detaches old UI listeners',
(tester) async {
await mount(tester);
subscriptions.send('A', {'progress': 0.25});
await flush(tester);
expect(find.text('25.00%'), findsOneWidget);
await mount(tester, sn: 'B');
expect(find.text('25.00%'), findsNothing);
subscriptions.send('A', {'progress': 0.75});
await flush(tester);
expect(find.text('75.00%'), findsNothing);
subscriptions.send('B', {'progress': 0.5});
await flush(tester);
expect(find.text('50.00%'), findsOneWidget);
await mount(tester, sn: 'B', site: 2);
expect(find.text('50.00%'), findsNothing);
expect(subscriptions.controllers['1/B']!.hasListener, isFalse);
await tester.pumpWidget(const SizedBox());
expect(subscriptions.controllers['2/B']!.hasListener, isFalse);
},
);
testWidgets(
'compact English card fits the HUD and labels stale cached data',
(tester) async {
await mount(tester, compact: true, locale: AppLocalizations.english);
subscriptions.send('A', {
'progress': 0.25,
'elapsed_s': 36001,
'eta_s': 1234,
'distance_remaining': 123.45,
}, receivedAt: DateTime.now().subtract(const Duration(seconds: 20)));
await flush(tester);
expect(
find.text('No recent update. Showing the last report.'),
findsOneWidget,
);
expect(find.text('25.00%'), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
},
);
}