对接连续任务列表接口和连续任务下发,便捷操作中暂停任务的对接,底部导航tab项的设置根据用户角色显示和设置
This commit is contained in:
143
test/mower_overview_status_test.dart
Normal file
143
test/mower_overview_status_test.dart
Normal file
@@ -0,0 +1,143 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:maibu_satabot_v2/core/di/injection.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/data/datasources/mower_realtime_datasource.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/data/repositories/mower_realtime_repository_impl.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/domain/interfaces/mqtt_client.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/domain/models/mqtt_message.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/domain/repositories/mower_realtime_repository.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/device_list/presentation/widgets/overview_device_telemetry.dart';
|
||||
|
||||
class _MqttClient extends Fake implements MqttClient {
|
||||
final messages = StreamController<MqttMessage>.broadcast();
|
||||
final subscribed = <String>[];
|
||||
final unsubscribed = <String>[];
|
||||
|
||||
@override
|
||||
Stream<MqttMessage> get messageStream => messages.stream;
|
||||
|
||||
@override
|
||||
Future<void> subscribe(String topic) async => subscribed.add(topic);
|
||||
|
||||
@override
|
||||
Future<void> unsubscribe(String topic) async => unsubscribed.add(topic);
|
||||
|
||||
void send(String sn, Map<String, Object> payload) {
|
||||
messages.add(MqttMessage(topic: _topic(sn), payload: jsonEncode(payload)));
|
||||
}
|
||||
}
|
||||
|
||||
String _topic(String sn) => 'device/$sn/host/realTime/message';
|
||||
|
||||
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() {
|
||||
test('cancelling the overview stream releases its MQTT topic', () async {
|
||||
final mqtt = _MqttClient();
|
||||
addTearDown(mqtt.messages.close);
|
||||
final datasource = MowerRealtimeDataSourceImpl(mqtt);
|
||||
final first = datasource.watchOverviewStatus('robot-1').listen((_) {});
|
||||
final second = datasource.watchOverviewStatus('robot-12').listen((_) {});
|
||||
|
||||
await first.cancel();
|
||||
expect(mqtt.unsubscribed, [_topic('robot-1')]);
|
||||
await second.cancel();
|
||||
expect(mqtt.unsubscribed, [_topic('robot-1'), _topic('robot-12')]);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'overview uses one subscription per robot and clears missing modes',
|
||||
(tester) async {
|
||||
final mqtt = _MqttClient();
|
||||
sl.registerSingleton<MowerRealtimeRepository>(
|
||||
MowerRealtimeRepositoryImpl(MowerRealtimeDataSourceImpl(mqtt)),
|
||||
);
|
||||
addTearDown(() async {
|
||||
await mqtt.messages.close();
|
||||
await sl.unregister<MowerRealtimeRepository>();
|
||||
});
|
||||
|
||||
final strings = (await tester.runAsync(() async {
|
||||
final strings = AppLocalizations(AppLocalizations.chinese);
|
||||
expect(await strings.load(), isTrue);
|
||||
return strings;
|
||||
}))!;
|
||||
const first = ValueKey('first');
|
||||
const second = ValueKey('second');
|
||||
Widget overview(String? firstSn) => MaterialApp(
|
||||
localizationsDelegates: [_Localizations(strings)],
|
||||
home: Scaffold(
|
||||
body: Column(
|
||||
children: [
|
||||
OverviewDeviceTelemetry(
|
||||
key: first,
|
||||
deviceSn: firstSn,
|
||||
batteryFallback: '--',
|
||||
),
|
||||
const OverviewDeviceTelemetry(
|
||||
key: second,
|
||||
deviceSn: 'robot-12',
|
||||
batteryFallback: '--',
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
Finder cell(Key row, String text) =>
|
||||
find.descendant(of: find.byKey(row), matching: find.text(text));
|
||||
|
||||
await tester.pumpWidget(overview('robot-1'));
|
||||
expect(
|
||||
mqtt.subscribed,
|
||||
unorderedEquals([_topic('robot-1'), _topic('robot-12')]),
|
||||
);
|
||||
|
||||
mqtt.send('robot-1', {
|
||||
'data': {'battery_percentage': 73, 'control_mode': 3},
|
||||
});
|
||||
mqtt.send('robot-12', {'battery_percentage': 27, 'control_mode': '2'});
|
||||
await tester.pumpAndSettle();
|
||||
expect(cell(first, '73.0%'), findsOneWidget);
|
||||
expect(cell(first, '远程控制'), findsOneWidget);
|
||||
expect(cell(second, '27.0%'), findsOneWidget);
|
||||
expect(cell(second, '自主任务'), findsOneWidget);
|
||||
|
||||
mqtt.send('robot-1', {'control_mode': 1});
|
||||
await tester.pumpAndSettle();
|
||||
expect(cell(first, '本地控制'), findsOneWidget);
|
||||
expect(cell(first, '73.0%'), findsOneWidget);
|
||||
|
||||
mqtt.send('robot-1', {'battery_percentage': 72});
|
||||
await tester.pumpAndSettle();
|
||||
expect(cell(first, '--'), findsOneWidget);
|
||||
expect(cell(first, '72.0%'), findsOneWidget);
|
||||
expect(cell(second, '自主任务'), findsOneWidget);
|
||||
|
||||
await tester.pumpWidget(overview(null));
|
||||
await tester.pumpAndSettle();
|
||||
expect(cell(first, '--'), findsNWidgets(2));
|
||||
expect(cell(second, '自主任务'), findsOneWidget);
|
||||
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpAndSettle();
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user