Add interactive GPS coordinates in messages
This commit is contained in:
1 parent
2f8fcb87d7
commit
05b47a037b
33 files changed
+1985
-84
No files matched your search
@@ -0,0 +1,219 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_linkify/flutter_linkify.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/helpers/coordinate_parser.dart';
|
||||
|
||||
void main() {
|
||||
group('CoordinateParser.parse', () {
|
||||
test('parses positive and negative decimal pairs', () {
|
||||
const inputs = [
|
||||
('37.7749,-122.4194', 37.7749, -122.4194),
|
||||
('38.722,-9.139', 38.722, -9.139),
|
||||
('-33.86882, 151.20929', -33.86882, 151.20929),
|
||||
('51.5074, -0.1278', 51.5074, -0.1278),
|
||||
('0.0, 0.0', 0.0, 0.0),
|
||||
('45.515232, -122.678385', 45.515232, -122.678385),
|
||||
('37.77491234, -122.41945678', 37.77491234, -122.41945678),
|
||||
];
|
||||
|
||||
for (final (text, expectedLat, expectedLon) in inputs) {
|
||||
final parsed = CoordinateParser.parse(text);
|
||||
expect(parsed, hasLength(1), reason: 'Failed for "$text"');
|
||||
expect(parsed.first.latitude, closeTo(expectedLat, 1e-12));
|
||||
expect(parsed.first.longitude, closeTo(expectedLon, 1e-12));
|
||||
expect(parsed.first.rawText, text);
|
||||
expect(parsed.first.start, 0);
|
||||
expect(parsed.first.end, text.length);
|
||||
}
|
||||
});
|
||||
|
||||
test('parses explicit + signs and optional whitespace after comma', () {
|
||||
final parsed = CoordinateParser.parse(
|
||||
'Meet at +37.7749,+122.4194 or +45.5152,\t-122.6784',
|
||||
);
|
||||
expect(parsed, hasLength(2));
|
||||
expect(parsed[0].rawText, '+37.7749,+122.4194');
|
||||
expect(parsed[0].latitude, 37.7749);
|
||||
expect(parsed[0].longitude, 122.4194);
|
||||
expect(parsed[1].rawText, '+45.5152,\t-122.6784');
|
||||
expect(parsed[1].latitude, 45.5152);
|
||||
expect(parsed[1].longitude, -122.6784);
|
||||
});
|
||||
|
||||
test('accepts WGS84 boundary coordinates and rejects out-of-range', () {
|
||||
final validBounds = CoordinateParser.parse(
|
||||
'90.0, 180.0 and -90.0, -180.0',
|
||||
);
|
||||
expect(validBounds, hasLength(2));
|
||||
expect(validBounds[0].latitude, 90.0);
|
||||
expect(validBounds[0].longitude, 180.0);
|
||||
expect(validBounds[1].latitude, -90.0);
|
||||
expect(validBounds[1].longitude, -180.0);
|
||||
|
||||
const invalidBounds = [
|
||||
'90.0001, 0.0',
|
||||
'-90.1, 0.0',
|
||||
'0.0, 180.0001',
|
||||
'0.0, -180.1',
|
||||
'990.0, -180.0',
|
||||
'123.45, 67.89',
|
||||
];
|
||||
for (final text in invalidBounds) {
|
||||
expect(
|
||||
CoordinateParser.parse(text),
|
||||
isEmpty,
|
||||
reason: 'Should reject "$text"',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('rejects plain integer pairs and mixed integer/decimal pairs', () {
|
||||
const invalid = [
|
||||
'1,2',
|
||||
'10, 20',
|
||||
'-45, 122',
|
||||
'37,-122.4194',
|
||||
'37.7749,-122',
|
||||
];
|
||||
for (final text in invalid) {
|
||||
expect(
|
||||
CoordinateParser.parse(text),
|
||||
isEmpty,
|
||||
reason: 'Should reject "$text"',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('rejects version numbers and digits inside words or identifiers', () {
|
||||
const invalid = [
|
||||
'1.2.3,4.5',
|
||||
'1.2,3.4.5',
|
||||
'1.2, 3.4.5',
|
||||
'1.2.3, 4.5.6',
|
||||
'v1.2,3.4',
|
||||
'foo37.7749,-122.4194',
|
||||
'37.7749,-122.4194bar',
|
||||
'id_37.7749,-122.4194',
|
||||
'37.7749,-122.4194_end',
|
||||
'--37.7749,-122.4194',
|
||||
'+-37.7749,-122.4194',
|
||||
'1.2,3.4-5',
|
||||
];
|
||||
for (final text in invalid) {
|
||||
expect(
|
||||
CoordinateParser.parse(text),
|
||||
isEmpty,
|
||||
reason: 'Should reject "$text"',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('handles punctuation boundaries around coordinates', () {
|
||||
const text =
|
||||
'Camp (37.7749, -122.4194), ridge [45.5152,-122.6784], end 38.722,-9.139.';
|
||||
final parsed = CoordinateParser.parse(text);
|
||||
expect(parsed, hasLength(3));
|
||||
expect(parsed[0].rawText, '37.7749, -122.4194');
|
||||
expect(text.substring(parsed[0].start, parsed[0].end), parsed[0].rawText);
|
||||
expect(parsed[1].rawText, '45.5152,-122.6784');
|
||||
expect(text.substring(parsed[1].start, parsed[1].end), parsed[1].rawText);
|
||||
expect(parsed[2].rawText, '38.722,-9.139');
|
||||
expect(text.substring(parsed[2].start, parsed[2].end), parsed[2].rawText);
|
||||
});
|
||||
|
||||
test('parses multiple coordinates and #wardriving messages', () {
|
||||
const wardriving =
|
||||
'#wardriving 37.7749,-122.4194 SNR: 8.5dB -> (37.7812, -122.4111) '
|
||||
'and 38.722,-9.139, 51.5074, -0.1278';
|
||||
final parsed = CoordinateParser.parse(wardriving);
|
||||
expect(parsed, hasLength(4));
|
||||
expect(parsed.map((c) => c.rawText).toList(), [
|
||||
'37.7749,-122.4194',
|
||||
'37.7812, -122.4111',
|
||||
'38.722,-9.139',
|
||||
'51.5074, -0.1278',
|
||||
]);
|
||||
});
|
||||
|
||||
test('does not misparse coordinates inside URLs or emails', () {
|
||||
const urlOnly = [
|
||||
'https://maps.google.com/?q=37.7749,-122.4194',
|
||||
'https://www.openstreetmap.org/?mlat=37.7749&mlon=-122.4194#map=16/37.7749/-122.4194',
|
||||
'https://example.com/@37.7749,-122.4194,15z',
|
||||
'geo:37.7749,-122.4194',
|
||||
'maps.google.com/?q=37.7749,-122.4194',
|
||||
];
|
||||
for (final text in urlOnly) {
|
||||
expect(
|
||||
CoordinateParser.parse(text),
|
||||
isEmpty,
|
||||
reason: 'Should not parse coordinate inside "$text"',
|
||||
);
|
||||
}
|
||||
|
||||
const mixed =
|
||||
'Route https://maps.google.com/?q=10.0,20.0 meet at 37.7749, -122.4194';
|
||||
final parsed = CoordinateParser.parse(mixed);
|
||||
expect(parsed, hasLength(1));
|
||||
expect(parsed.first.rawText, '37.7749, -122.4194');
|
||||
|
||||
final elements = CoordinateParser.linkifyWithCoordinates(mixed);
|
||||
expect(elements.whereType<UrlElement>(), hasLength(1));
|
||||
expect(elements.whereType<CoordinateElement>(), hasLength(1));
|
||||
});
|
||||
});
|
||||
|
||||
group('buildExternalMapUri', () {
|
||||
test('builds platform-appropriate map URIs', () {
|
||||
expect(
|
||||
buildExternalMapUri(
|
||||
37.7749,
|
||||
-122.4194,
|
||||
platform: TargetPlatform.android,
|
||||
isWeb: false,
|
||||
).toString(),
|
||||
'geo:37.7749,-122.4194?q=37.7749,-122.4194',
|
||||
);
|
||||
expect(
|
||||
buildExternalMapUri(
|
||||
37.7749,
|
||||
-122.4194,
|
||||
platform: TargetPlatform.iOS,
|
||||
isWeb: false,
|
||||
).toString(),
|
||||
'https://maps.apple.com/?ll=37.7749,-122.4194&q=37.7749,-122.4194',
|
||||
);
|
||||
expect(
|
||||
buildExternalMapUri(
|
||||
37.7749,
|
||||
-122.4194,
|
||||
platform: TargetPlatform.macOS,
|
||||
isWeb: false,
|
||||
).toString(),
|
||||
'https://maps.apple.com/?ll=37.7749,-122.4194&q=37.7749,-122.4194',
|
||||
);
|
||||
expect(
|
||||
buildExternalMapUri(
|
||||
37.7749,
|
||||
-122.4194,
|
||||
platform: TargetPlatform.linux,
|
||||
isWeb: false,
|
||||
).toString(),
|
||||
'https://www.openstreetmap.org/?mlat=37.7749&mlon=-122.4194#map=16/37.7749/-122.4194',
|
||||
);
|
||||
expect(
|
||||
buildExternalMapUri(
|
||||
37.7749,
|
||||
-122.4194,
|
||||
platform: TargetPlatform.windows,
|
||||
isWeb: false,
|
||||
).toString(),
|
||||
'https://www.openstreetmap.org/?mlat=37.7749&mlon=-122.4194#map=16/37.7749/-122.4194',
|
||||
);
|
||||
expect(
|
||||
buildExternalMapUri(-0.0, -0.0, isWeb: true).toString(),
|
||||
'https://www.openstreetmap.org/?mlat=0.0&mlon=0.0#map=16/0.0/0.0',
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,484 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:provider/single_child_widget.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'package:meshcore_open/connector/meshcore_connector.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations.dart';
|
||||
import 'package:meshcore_open/models/custom_theme.dart';
|
||||
import 'package:meshcore_open/review_mode/review_frames.dart';
|
||||
import 'package:meshcore_open/review_mode/review_radio.dart';
|
||||
import 'package:meshcore_open/screens/channel_chat_screen.dart';
|
||||
import 'package:meshcore_open/screens/chat_screen.dart';
|
||||
import 'package:meshcore_open/screens/coordinate_map_screen.dart';
|
||||
import 'package:meshcore_open/services/app_debug_log_service.dart';
|
||||
import 'package:meshcore_open/services/app_settings_service.dart';
|
||||
import 'package:meshcore_open/services/ble_debug_log_service.dart';
|
||||
import 'package:meshcore_open/services/chat_text_scale_service.dart';
|
||||
import 'package:meshcore_open/services/image_codec_service.dart';
|
||||
import 'package:meshcore_open/services/map_tile_cache_service.dart';
|
||||
import 'package:meshcore_open/services/message_retry_service.dart';
|
||||
import 'package:meshcore_open/services/path_history_service.dart';
|
||||
import 'package:meshcore_open/services/received_image_store.dart';
|
||||
import 'package:meshcore_open/services/storage_service.dart';
|
||||
import 'package:meshcore_open/services/timeout_prediction_service.dart';
|
||||
import 'package:meshcore_open/services/translation_service.dart';
|
||||
import 'package:meshcore_open/services/ui_view_state_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';
|
||||
|
||||
final _l10n = lookupAppLocalizations(const Locale('en'));
|
||||
|
||||
(MeshCoreConnector, List<SingleChildWidget>)? _shared;
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
final tempDir = Directory.systemTemp.createTempSync(
|
||||
'coordinate_messages_test',
|
||||
);
|
||||
String? copiedText;
|
||||
final launchedUrls = <String>[];
|
||||
|
||||
setUpAll(() async {
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(
|
||||
const MethodChannel('plugins.flutter.io/path_provider'),
|
||||
(call) async => tempDir.path,
|
||||
);
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(SystemChannels.platform, (call) async {
|
||||
if (call.method == 'Clipboard.setData') {
|
||||
copiedText =
|
||||
(call.arguments as Map<dynamic, dynamic>)['text'] as String?;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(
|
||||
const MethodChannel('plugins.flutter.io/url_launcher'),
|
||||
(call) async {
|
||||
if (call.method == 'launch' || call.method == 'launchUrl') {
|
||||
final args = call.arguments as Map<dynamic, dynamic>;
|
||||
launchedUrls.add(args['url'] as String);
|
||||
return true;
|
||||
}
|
||||
if (call.method == 'canLaunch') {
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
);
|
||||
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
});
|
||||
|
||||
tearDownAll(() {
|
||||
_shared = null;
|
||||
if (tempDir.existsSync()) {
|
||||
tempDir.deleteSync(recursive: true);
|
||||
}
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
copiedText = null;
|
||||
launchedUrls.clear();
|
||||
});
|
||||
|
||||
/// Finds the global center of the first substring matching [target] inside
|
||||
/// a [RenderParagraph] (mobile) or [RenderEditable] (desktop).
|
||||
Offset findSpanOffset(WidgetTester tester, String target) {
|
||||
for (final render in tester.renderObjectList<RenderParagraph>(
|
||||
find.byType(RichText),
|
||||
)) {
|
||||
final plain = render.text.toPlainText();
|
||||
final idx = plain.indexOf(target);
|
||||
if (idx < 0) continue;
|
||||
final boxes = render.getBoxesForSelection(
|
||||
TextSelection(baseOffset: idx, extentOffset: idx + target.length),
|
||||
);
|
||||
if (boxes.isNotEmpty) {
|
||||
final localCenter = boxes.first.toRect().center;
|
||||
return render.localToGlobal(localCenter);
|
||||
}
|
||||
}
|
||||
for (final state in tester.stateList<EditableTextState>(
|
||||
find.byType(EditableText),
|
||||
)) {
|
||||
final editable = state.renderEditable;
|
||||
final plain = editable.plainText;
|
||||
final idx = plain.indexOf(target);
|
||||
if (idx < 0) continue;
|
||||
final boxes = editable.getEndpointsForSelection(
|
||||
TextSelection(baseOffset: idx, extentOffset: idx + target.length),
|
||||
);
|
||||
if (boxes.isNotEmpty) {
|
||||
final first = boxes.first.point;
|
||||
final last = boxes.last.point;
|
||||
final localCenter = Offset((first.dx + last.dx) / 2, first.dy - 6);
|
||||
return editable.localToGlobal(localCenter);
|
||||
}
|
||||
}
|
||||
fail('Could not locate rendered span for "$target"');
|
||||
}
|
||||
|
||||
for (final isDesktop in [false, true]) {
|
||||
final platformLabel = isDesktop ? 'desktop' : 'phone';
|
||||
|
||||
testWidgets(
|
||||
'DM message with URL and coordinate: tap opens map, Back preserves scroll, '
|
||||
'long-press shows menu, non-coordinate gestures preserved ($platformLabel)',
|
||||
(tester) async {
|
||||
PlatformInfo.debugIsDesktopOverride = isDesktop;
|
||||
addTearDown(() => PlatformInfo.debugIsDesktopOverride = null);
|
||||
|
||||
final (connector, providers) = _shared ??= await _connect(tester);
|
||||
final contact = connector.contacts.firstWhere(
|
||||
(c) => c.type == advTypeChat,
|
||||
);
|
||||
final nowSecs = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
|
||||
const coord = '37.7749, -122.4194';
|
||||
const url = 'https://example.com/trail';
|
||||
const messageText = 'Check $url and meet at $coord soon';
|
||||
|
||||
await tester.runAsync(() async {
|
||||
connector.handleFrameForTest(
|
||||
ReviewFrames.contactMessage(
|
||||
senderPublicKey: contact.publicKey,
|
||||
text: messageText,
|
||||
timestampSecs: nowSecs - 60,
|
||||
),
|
||||
);
|
||||
for (var i = 0; i < 18; i++) {
|
||||
connector.handleFrameForTest(
|
||||
ReviewFrames.contactMessage(
|
||||
senderPublicKey: contact.publicKey,
|
||||
text: 'Filler message #$i',
|
||||
timestampSecs: nowSecs - 30 + i,
|
||||
),
|
||||
);
|
||||
}
|
||||
connector.handleFrameForTest(
|
||||
ReviewFrames.contactMessage(
|
||||
senderPublicKey: contact.publicKey,
|
||||
text: 'Reply sent at 45.5152, -122.6784',
|
||||
timestampSecs: nowSecs,
|
||||
),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 50));
|
||||
});
|
||||
|
||||
await _pumpApp(tester, providers, ChatScreen(contact: contact));
|
||||
|
||||
final scrollable = tester.widget<Scrollable>(
|
||||
find.byType(Scrollable).first,
|
||||
);
|
||||
final controller = scrollable.controller!;
|
||||
final targetFinder = find.textContaining(coord, findRichText: true);
|
||||
if (targetFinder.evaluate().isEmpty) {
|
||||
await tester.dragUntilVisible(
|
||||
targetFinder,
|
||||
find.byType(Scrollable).first,
|
||||
const Offset(0, 200),
|
||||
maxIteration: 30,
|
||||
);
|
||||
}
|
||||
await Scrollable.ensureVisible(
|
||||
tester.element(targetFinder.first),
|
||||
alignment: 0.5,
|
||||
);
|
||||
await _settle(tester);
|
||||
final savedOffset = controller.offset;
|
||||
expect(savedOffset, greaterThan(0));
|
||||
|
||||
// 1. Tap coordinate -> opens CoordinateMapScreen with pin + label + centered AppBar.
|
||||
final coordCenter = findSpanOffset(tester, coord);
|
||||
await tester.tapAt(coordCenter);
|
||||
await _settle(tester);
|
||||
|
||||
expect(find.byType(CoordinateMapScreen), findsOneWidget);
|
||||
expect(find.text(coord), findsOneWidget);
|
||||
final appBar = tester.widget<AppBar>(find.byType(AppBar));
|
||||
expect(appBar.centerTitle, isTrue);
|
||||
final flutterMap = tester.widget<FlutterMap>(find.byType(FlutterMap));
|
||||
expect(
|
||||
flutterMap.options.initialCenter.latitude,
|
||||
closeTo(37.7749, 1e-6),
|
||||
);
|
||||
expect(
|
||||
flutterMap.options.initialCenter.longitude,
|
||||
closeTo(-122.4194, 1e-6),
|
||||
);
|
||||
expect(find.byIcon(Icons.location_on), findsOneWidget);
|
||||
|
||||
// 2. Pressing Back returns to exact same chat and scroll position.
|
||||
await tester.pageBack();
|
||||
await _settle(tester);
|
||||
expect(find.byType(CoordinateMapScreen), findsNothing);
|
||||
expect(find.byType(ChatScreen), findsOneWidget);
|
||||
expect(controller.offset, closeTo(savedOffset, 1.0));
|
||||
|
||||
// 3. Long-press coordinate -> opens Copy & Open in maps sheet (NOT bubble menu).
|
||||
await tester.longPressAt(findSpanOffset(tester, coord));
|
||||
await _settle(tester);
|
||||
expect(find.text(_l10n.coordinate_copyLocation), findsOneWidget);
|
||||
expect(find.text(_l10n.coordinate_openInMaps), findsOneWidget);
|
||||
expect(find.text(_l10n.chat_reply), findsNothing);
|
||||
|
||||
// Copy location writes to clipboard and shows snackbar.
|
||||
await tester.tap(find.text(_l10n.coordinate_copyLocation));
|
||||
await _settle(tester);
|
||||
expect(copiedText, coord);
|
||||
expect(find.text(_l10n.coordinate_copied), findsOneWidget);
|
||||
|
||||
// Re-open long-press sheet and tap Open in maps.
|
||||
await tester.longPressAt(findSpanOffset(tester, coord));
|
||||
await _settle(tester);
|
||||
await tester.tap(find.text(_l10n.coordinate_openInMaps));
|
||||
await _settle(tester);
|
||||
expect(launchedUrls, isNotEmpty);
|
||||
expect(launchedUrls.last, contains('37.7749'));
|
||||
expect(launchedUrls.last, contains('-122.4194'));
|
||||
|
||||
// 4. Tapping URL still opens existing URL confirmation dialog.
|
||||
await tester.tapAt(findSpanOffset(tester, url));
|
||||
await _settle(tester);
|
||||
expect(find.text(_l10n.chat_openLink), findsOneWidget);
|
||||
await tester.tap(find.text(_l10n.common_cancel));
|
||||
await _settle(tester);
|
||||
|
||||
// 5. Long-pressing non-coordinate text in same message opens bubble actions.
|
||||
if (!isDesktop) {
|
||||
await tester.longPressAt(findSpanOffset(tester, 'Check'));
|
||||
await _settle(tester);
|
||||
expect(find.text(_l10n.chat_addReaction), findsOneWidget);
|
||||
Navigator.of(tester.element(find.text(_l10n.chat_addReaction))).pop();
|
||||
await _settle(tester);
|
||||
}
|
||||
|
||||
await _tearDown(tester);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets(
|
||||
'Channel messages render coordinates, support tap/long-press, and fit 320dp @ 1.3x & 2.0x',
|
||||
(tester) async {
|
||||
PlatformInfo.debugIsDesktopOverride = false;
|
||||
addTearDown(() => PlatformInfo.debugIsDesktopOverride = null);
|
||||
|
||||
final (connector, providers) = _shared ??= await _connect(tester);
|
||||
final channel = connector.channels.first;
|
||||
final nowSecs = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
|
||||
const channelCoord = '-33.86882, 151.20929';
|
||||
const channelMsg =
|
||||
'#wardriving https://meshcore.co.uk at ($channelCoord) SNR 7.5dB';
|
||||
|
||||
await tester.runAsync(() async {
|
||||
connector.handleFrameForTest(
|
||||
ReviewFrames.channelMessage(
|
||||
channelIndex: channel.index,
|
||||
sender: 'Maya',
|
||||
message: channelMsg,
|
||||
timestampSecs: nowSecs - 30,
|
||||
),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 50));
|
||||
});
|
||||
|
||||
final customTheme = MeshTheme.fromCustom(
|
||||
CustomTheme(
|
||||
id: 'test-forest',
|
||||
name: 'Forest',
|
||||
background: const Color(0xFF0E1A13),
|
||||
accent: const Color(0xFF4ADE80),
|
||||
),
|
||||
);
|
||||
|
||||
for (final scale in [1.3, 2.0]) {
|
||||
tester.view.physicalSize = const Size(320, 568);
|
||||
tester.view.devicePixelRatio = 1.0;
|
||||
addTearDown(tester.view.reset);
|
||||
|
||||
final overflows = <String>[];
|
||||
final previousError = FlutterError.onError;
|
||||
FlutterError.onError = (details) {
|
||||
final text = details.exceptionAsString();
|
||||
if (text.contains('overflowed')) {
|
||||
overflows.add(text.split('\n').first);
|
||||
} else {
|
||||
previousError?.call(details);
|
||||
}
|
||||
};
|
||||
addTearDown(() => FlutterError.onError = previousError);
|
||||
|
||||
await _pumpApp(
|
||||
tester,
|
||||
providers,
|
||||
ChannelChatScreen(channel: channel),
|
||||
theme: customTheme,
|
||||
textScaler: TextScaler.linear(scale),
|
||||
);
|
||||
|
||||
final coordFinder = find.textContaining(
|
||||
channelCoord,
|
||||
findRichText: true,
|
||||
);
|
||||
if (coordFinder.evaluate().isEmpty) {
|
||||
await tester.dragUntilVisible(
|
||||
coordFinder,
|
||||
find.byType(Scrollable).first,
|
||||
const Offset(0, 200),
|
||||
maxIteration: 30,
|
||||
);
|
||||
}
|
||||
await Scrollable.ensureVisible(
|
||||
tester.element(coordFinder.first),
|
||||
alignment: 0.5,
|
||||
);
|
||||
await _settle(tester);
|
||||
|
||||
// Long-press coordinate in channel message -> bottom sheet fits 320dp without overflow.
|
||||
await tester.longPressAt(findSpanOffset(tester, channelCoord));
|
||||
await _settle(tester);
|
||||
expect(find.text(_l10n.coordinate_copyLocation), findsOneWidget);
|
||||
expect(find.text(_l10n.coordinate_openInMaps), findsOneWidget);
|
||||
Navigator.of(
|
||||
tester.element(find.text(_l10n.coordinate_copyLocation)),
|
||||
).pop();
|
||||
await _settle(tester);
|
||||
|
||||
// Tap coordinate -> CoordinateMapScreen fits 320dp without overflow.
|
||||
await tester.tapAt(findSpanOffset(tester, channelCoord));
|
||||
await _settle(tester);
|
||||
expect(find.byType(CoordinateMapScreen), findsOneWidget);
|
||||
|
||||
// Verify 48dp touch targets on CoordinateMapScreen controls & AppBar buttons.
|
||||
for (final iconButton in tester.widgetList<IconButton>(
|
||||
find.byType(IconButton),
|
||||
)) {
|
||||
final box = tester.renderObject<RenderBox>(find.byWidget(iconButton));
|
||||
expect(box.size.width, greaterThanOrEqualTo(48));
|
||||
expect(box.size.height, greaterThanOrEqualTo(48));
|
||||
}
|
||||
|
||||
await tester.pageBack();
|
||||
await _settle(tester);
|
||||
FlutterError.onError = previousError;
|
||||
expect(overflows, isEmpty);
|
||||
}
|
||||
|
||||
await _tearDown(tester);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _pumpApp(
|
||||
WidgetTester tester,
|
||||
List<SingleChildWidget> providers,
|
||||
Widget home, {
|
||||
ThemeData? theme,
|
||||
TextScaler textScaler = TextScaler.noScaling,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
MultiProvider(
|
||||
providers: providers,
|
||||
child: MaterialApp(
|
||||
theme: theme ?? MeshTheme.light(),
|
||||
locale: const Locale('en'),
|
||||
localizationsDelegates: const [
|
||||
AppLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
builder: (context, child) => MediaQuery(
|
||||
data: MediaQuery.of(context).copyWith(textScaler: textScaler),
|
||||
child: child!,
|
||||
),
|
||||
home: home,
|
||||
),
|
||||
),
|
||||
);
|
||||
await _settle(tester);
|
||||
}
|
||||
|
||||
Future<void> _settle(WidgetTester tester) async {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _tearDown(WidgetTester tester) async {
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
await tester.pump(const Duration(minutes: 1));
|
||||
}
|
||||
|
||||
Future<(MeshCoreConnector, List<SingleChildWidget>)> _connect(
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
final storage = StorageService();
|
||||
final connector = MeshCoreConnector();
|
||||
final retry = MessageRetryService();
|
||||
final settings = AppSettingsService();
|
||||
final translation = TranslationService(settings);
|
||||
|
||||
await tester.runAsync(() async {
|
||||
await settings.loadSettings();
|
||||
connector.initialize(
|
||||
retryService: retry,
|
||||
pathHistoryService: PathHistoryService(storage),
|
||||
appSettingsService: settings,
|
||||
translationService: translation,
|
||||
);
|
||||
connector.reviewRadioFactoryForTest = () => ReviewRadio(
|
||||
scheduledMessageInterval: const Duration(hours: 1),
|
||||
ackDelay: const Duration(hours: 1),
|
||||
replyDelay: const Duration(hours: 1),
|
||||
);
|
||||
await connector.connectReview();
|
||||
final end = DateTime.now().add(const Duration(seconds: 8));
|
||||
while ((connector.contacts.isEmpty ||
|
||||
connector.channels.isEmpty ||
|
||||
!connector.channelMapConfirmed) &&
|
||||
DateTime.now().isBefore(end)) {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 25));
|
||||
}
|
||||
});
|
||||
|
||||
return (
|
||||
connector,
|
||||
<SingleChildWidget>[
|
||||
ChangeNotifierProvider.value(value: connector),
|
||||
ChangeNotifierProvider.value(value: retry),
|
||||
ChangeNotifierProvider.value(value: PathHistoryService(storage)),
|
||||
ChangeNotifierProvider.value(value: settings),
|
||||
ChangeNotifierProvider.value(value: BleDebugLogService()),
|
||||
ChangeNotifierProvider.value(value: AppDebugLogService()),
|
||||
ChangeNotifierProvider.value(value: ChatTextScaleService()),
|
||||
ChangeNotifierProvider.value(value: translation),
|
||||
ChangeNotifierProvider.value(value: UiViewStateService()),
|
||||
Provider.value(value: storage),
|
||||
ChangeNotifierProvider.value(
|
||||
value: MapTileCacheService(appSettingsService: settings),
|
||||
),
|
||||
ChangeNotifierProvider.value(value: TimeoutPredictionService(storage)),
|
||||
ChangeNotifierProvider.value(value: ImageCodecService(settings)),
|
||||
ChangeNotifierProvider.value(value: ReceivedImageStore()),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import 'package:meshcore_open/screens/chat_screen.dart';
|
||||
import 'package:meshcore_open/screens/chrome_required_screen.dart';
|
||||
import 'package:meshcore_open/screens/companion_radio_stats_screen.dart';
|
||||
import 'package:meshcore_open/screens/contacts_screen.dart';
|
||||
import 'package:meshcore_open/screens/coordinate_map_screen.dart';
|
||||
import 'package:meshcore_open/screens/discovery_screen.dart';
|
||||
import 'package:meshcore_open/screens/line_of_sight_map_screen.dart';
|
||||
import 'package:meshcore_open/screens/map_cache_screen.dart';
|
||||
@@ -223,6 +224,11 @@ final Map<String, _ScreenBuilder> _screens = {
|
||||
LineOfSightEndpoint(label: 'B', point: LatLng(45.6, -122.7)),
|
||||
],
|
||||
),
|
||||
'CoordinateMap': (_) => const CoordinateMapScreen(
|
||||
latitude: 45.515232,
|
||||
longitude: -122.678385,
|
||||
label: '45.515232, -122.678385',
|
||||
),
|
||||
};
|
||||
|
||||
void main() {
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_linkify/flutter_linkify.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/helpers/link_handler.dart';
|
||||
import 'package:meshcore_open/models/custom_theme.dart';
|
||||
import 'package:meshcore_open/theme/mesh_theme.dart';
|
||||
import 'package:meshcore_open/widgets/mesh_ui.dart';
|
||||
import 'package:meshcore_open/widgets/message_bubble_frame.dart';
|
||||
import 'package:meshcore_open/widgets/translated_message_content.dart';
|
||||
|
||||
double _renderedScaleFactor(WidgetTester tester) {
|
||||
final linkify = find.byType(Linkify);
|
||||
if (linkify.evaluate().isNotEmpty) {
|
||||
return tester.widget<Linkify>(linkify.first).textScaleFactor;
|
||||
}
|
||||
List<double> _renderedScaleFactors(WidgetTester tester) {
|
||||
final editables = tester
|
||||
.stateList<EditableTextState>(find.byType(EditableText))
|
||||
.map((s) => s.renderEditable.textScaler.scale(16) / 16)
|
||||
.toList();
|
||||
if (editables.isNotEmpty) return editables;
|
||||
return tester
|
||||
.widget<SelectableLinkify>(find.byType(SelectableLinkify).first)
|
||||
.textScaleFactor;
|
||||
.renderObjectList<RenderParagraph>(find.byType(RichText))
|
||||
.map((p) => p.textScaler.scale(16) / 16)
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> _pump(WidgetTester tester, TextScaler scaler) {
|
||||
@@ -20,7 +27,7 @@ Future<void> _pump(WidgetTester tester, TextScaler scaler) {
|
||||
data: MediaQueryData(textScaler: scaler),
|
||||
child: const Scaffold(
|
||||
body: TranslatedMessageContent(
|
||||
displayText: 'hello https://example.com',
|
||||
displayText: 'hello https://example.com at 37.7749, -122.4194',
|
||||
originalText: 'bonjour',
|
||||
style: TextStyle(fontSize: 16),
|
||||
),
|
||||
@@ -33,23 +40,62 @@ Future<void> _pump(WidgetTester tester, TextScaler scaler) {
|
||||
void main() {
|
||||
testWidgets('message body follows the system text scale', (tester) async {
|
||||
await _pump(tester, TextScaler.noScaling);
|
||||
expect(_renderedScaleFactor(tester), 1.0);
|
||||
expect(_renderedScaleFactors(tester), everyElement(1.0));
|
||||
|
||||
await _pump(tester, const TextScaler.linear(2));
|
||||
expect(_renderedScaleFactor(tester), 2.0);
|
||||
expect(_renderedScaleFactors(tester), everyElement(2.0));
|
||||
});
|
||||
|
||||
testWidgets('original and translated text both scale', (tester) async {
|
||||
await _pump(tester, const TextScaler.linear(1.5));
|
||||
final factors = [
|
||||
...tester
|
||||
.widgetList<Linkify>(find.byType(Linkify))
|
||||
.map((w) => w.textScaleFactor),
|
||||
...tester
|
||||
.widgetList<SelectableLinkify>(find.byType(SelectableLinkify))
|
||||
.map((w) => w.textScaleFactor),
|
||||
];
|
||||
final factors = _renderedScaleFactors(tester);
|
||||
expect(factors, hasLength(2));
|
||||
expect(factors, everyElement(1.5));
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'coordinate style meets WCAG AA contrast across light, dark, and custom themes',
|
||||
(tester) async {
|
||||
final customForest = CustomTheme(
|
||||
id: 'forest',
|
||||
name: 'Forest',
|
||||
background: const Color(0xFF0E1A13),
|
||||
accent: const Color(0xFF4ADE80),
|
||||
);
|
||||
final themes = [
|
||||
MeshTheme.light(),
|
||||
MeshTheme.dark(),
|
||||
MeshTheme.fromCustom(customForest),
|
||||
];
|
||||
|
||||
for (final theme in themes) {
|
||||
for (final isOutgoing in [false, true]) {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: theme,
|
||||
home: Builder(
|
||||
builder: (context) {
|
||||
final colors = MessageBubbleColors.of(
|
||||
context,
|
||||
isOutgoing: isOutgoing,
|
||||
);
|
||||
final coordStyle = LinkHandler.defaultCoordinateStyle(
|
||||
context,
|
||||
TextStyle(color: colors.text, fontSize: 14),
|
||||
backgroundColor: colors.background,
|
||||
);
|
||||
expect(coordStyle.decoration, TextDecoration.underline);
|
||||
expect(
|
||||
meshContrastRatio(coordStyle.color!, colors.background),
|
||||
greaterThanOrEqualTo(4.5),
|
||||
);
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user