You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
meshcore-client/test/widgets/link_router_host_test.dart

164 lines
4.9 KiB

// #1289: a share link opens the existing confirmation, waits for a radio
// when none is connected, and never adds anything the user did not confirm.
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/helpers/link_handler.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/models/channel.dart';
import 'package:meshcore_open/services/link_router.dart';
import 'package:meshcore_open/widgets/link_router_host.dart';
const _link = 'meshcore://channel/add?name=%23example';
class _Radio extends MeshCoreConnector {
bool connected = false;
bool busy = false;
final List<Channel> onRadio = [];
final List<(int, String, String)> writes = [];
void change() => notifyListeners();
@override
bool get isConnected => connected;
@override
bool get isChannelTableBusy => busy;
@override
List<Channel> get channels => List.unmodifiable(onRadio);
@override
int get maxChannels => 8;
@override
Future<ChannelWrite> setChannel(
int index,
String name,
Uint8List psk, {
bool refresh = true,
}) async {
writes.add((
index,
name,
Channel(index: index, name: name, psk: psk).pskHex,
));
return ChannelWrite.ok;
}
}
Future<(LinkRouter, BuildContext)> _pump(
WidgetTester tester,
_Radio radio,
) async {
final router = LinkRouter();
late BuildContext inner;
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider<MeshCoreConnector>.value(value: radio),
ChangeNotifierProvider.value(value: router),
],
child: MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: LinkRouterHost(
child: Scaffold(
body: Builder(
builder: (context) {
inner = context;
return const SizedBox.expand();
},
),
),
),
),
),
);
return (router, inner);
}
void main() {
testWidgets('waits for a radio, then confirms, then writes', (tester) async {
final radio = _Radio();
final (router, _) = await _pump(tester, radio);
router.handle(_link);
await tester.pumpAndSettle();
expect(find.textContaining('Connect a radio to add #example'), findsOne);
expect(find.byType(AlertDialog), findsNothing);
radio
..connected = true
..busy = true
..change();
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing, reason: 'table busy');
radio
..busy = false
..change();
await tester.pumpAndSettle();
expect(find.text('Add channel "#example"?'), findsOne);
expect(radio.writes, isEmpty, reason: 'nothing before confirming');
await tester.tap(find.widgetWithText(FilledButton, 'Add'));
await tester.pumpAndSettle();
expect(radio.writes, hasLength(1));
expect(radio.writes.single.$2, '#example');
expect(
radio.writes.single.$3,
Channel(
index: 0,
name: '#example',
psk: Channel.derivePskFromHashtag('example'),
).pskHex,
);
});
testWidgets('cancel writes nothing', (tester) async {
final radio = _Radio()..connected = true;
final (router, _) = await _pump(tester, radio);
router.handle(_link);
await tester.pumpAndSettle();
await tester.tap(find.widgetWithText(TextButton, 'Cancel'));
await tester.pumpAndSettle();
expect(radio.writes, isEmpty);
expect(router.pending, isNull);
});
testWidgets('a channel already on the radio is not offered again', (
tester,
) async {
final radio = _Radio()..connected = true;
radio.onRadio.add(Channel.fromShareUri(_link, index: 1)!);
final (router, _) = await _pump(tester, radio);
router.handle(_link);
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
expect(find.text('"#example" is already added.'), findsOne);
expect(radio.writes, isEmpty);
});
testWidgets('a malformed link says so', (tester) async {
final radio = _Radio()..connected = true;
final (router, _) = await _pump(tester, radio);
router.handle('meshcore://channel/add?name=nope');
await tester.pumpAndSettle();
expect(find.textContaining("isn't a channel or contact"), findsOne);
});
testWidgets('tapping a meshcore:// link in a message routes it here', (
tester,
) async {
final radio = _Radio()..connected = true;
final (_, inner) = await _pump(tester, radio);
LinkHandler.handleLinkTap(inner, _link);
await tester.pumpAndSettle();
// Offband's own confirmation, not the browser's "Open Link" dialog.
expect(find.text('Add channel "#example"?'), findsOne);
expect(find.text('Open Link'), findsNothing);
});
}

Powered by TurnKey Linux.