mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-05 23:44:02 +01:00
339 lines
12 KiB
Dart
339 lines
12 KiB
Dart
/*
|
|
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
|
* Copyright (c) 2026 wger Team
|
|
*
|
|
* wger Workout Manager is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:material_ui/material_ui.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
|
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
|
|
import 'package:wger/core/error_dialogs.dart';
|
|
import 'package:wger/core/errors.dart';
|
|
import 'package:wger/core/exceptions/http_exception.dart';
|
|
import 'package:wger/core/keys.dart';
|
|
|
|
import '../helpers/pump_app.dart';
|
|
|
|
/// Requests an error dialog from *inside* its own build, the situation a
|
|
/// FlutterError.onError handler is in when a widget fails to build.
|
|
class _ErrorDuringBuild extends StatelessWidget {
|
|
const _ErrorDuringBuild();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
showGeneralErrorDialog('boom during build', StackTrace.current, context: context);
|
|
return const SizedBox.shrink();
|
|
}
|
|
}
|
|
|
|
/// Requests the transient-error snackbar from inside a build, where the
|
|
/// messenger's setState would otherwise assert.
|
|
class _SnackbarDuringBuild extends StatelessWidget {
|
|
const _SnackbarDuringBuild();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
showTransientErrorSnackbar();
|
|
return const SizedBox.shrink();
|
|
}
|
|
}
|
|
|
|
/// Captures the URL "Report issue" hands to the browser
|
|
class _FakeUrlLauncher extends Fake with MockPlatformInterfaceMixin implements UrlLauncherPlatform {
|
|
String? launchedUrl;
|
|
|
|
@override
|
|
Future<bool> launchUrl(String url, LaunchOptions options) async {
|
|
launchedUrl = url;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
group('formatApiErrors', () {
|
|
final errors = [
|
|
ApiError(key: 'username', errorMessages: ['too short', 'already taken']),
|
|
];
|
|
|
|
test('leaves the text color unset so it inherits the theme (dark-mode readable)', () {
|
|
// Hardcoding a color (the old Colors.black default) made the API error
|
|
// dialog unreadable on a dark background. A null color inherits instead.
|
|
final texts = formatApiErrors(errors).whereType<Text>().toList();
|
|
|
|
expect(texts, isNotEmpty);
|
|
expect(texts.every((t) => t.style?.color == null), isTrue);
|
|
});
|
|
|
|
test('applies an explicit color when one is given', () {
|
|
final texts = formatApiErrors(errors, color: Colors.red).whereType<Text>().toList();
|
|
|
|
expect(texts, isNotEmpty);
|
|
expect(texts.every((t) => t.style?.color == Colors.red), isTrue);
|
|
});
|
|
});
|
|
|
|
group('htmlErrorTitle', () {
|
|
test('extracts and unescapes the title of a bot-wall page', () {
|
|
const html =
|
|
'<!doctype html><html lang="en"><head>'
|
|
'<title>Making sure you're not a bot!</title>'
|
|
'</head><body></body></html>';
|
|
|
|
expect(htmlErrorTitle(html), "Making sure you're not a bot!");
|
|
});
|
|
|
|
test('matches case-insensitively and ignores attributes on the title tag', () {
|
|
expect(
|
|
htmlErrorTitle('<HEAD><TITLE data-x="1">Just a moment...</TITLE></HEAD>'),
|
|
'Just a moment...',
|
|
);
|
|
});
|
|
|
|
test('returns null when there is no usable title', () {
|
|
expect(htmlErrorTitle('<html><body>no title here</body></html>'), isNull);
|
|
expect(htmlErrorTitle('<html><head><title> </title></head></html>'), isNull);
|
|
expect(htmlErrorTitle(''), isNull);
|
|
});
|
|
});
|
|
|
|
group('showGeneralErrorDialog during build', () {
|
|
Widget app(Widget home) => testApp(home);
|
|
|
|
testWidgets('defers the dialog to the next frame instead of asserting', (tester) async {
|
|
// Regression: a mid-build route push asserted, and the secondary
|
|
// exception wedged the one-dialog guard shut until app restart.
|
|
await tester.pumpWidget(app(const _ErrorDuringBuild()));
|
|
|
|
// The build itself must not throw...
|
|
expect(tester.takeException(), isNull);
|
|
|
|
// ...and the dialog appears on the following frame.
|
|
await tester.pump();
|
|
expect(find.byType(AlertDialog), findsOneWidget);
|
|
|
|
// Dismissing releases the guard: a second dialog can be shown.
|
|
await tester.tap(find.text('OK'));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byType(AlertDialog), findsNothing);
|
|
|
|
final context = tester.element(find.byType(SizedBox));
|
|
showGeneralErrorDialog('second error', StackTrace.current, context: context);
|
|
// One frame runs the post-frame callback (pushing the route), the next
|
|
// builds the dialog.
|
|
await tester.pump();
|
|
await tester.pump();
|
|
expect(
|
|
find.byType(AlertDialog),
|
|
findsOneWidget,
|
|
reason: 'the guard flag must not stay wedged after the first dialog',
|
|
);
|
|
|
|
// The guard is global: a dialog left open would suppress the dialogs of
|
|
// the tests after this one
|
|
await tester.tap(find.text('OK'));
|
|
await tester.pumpAndSettle();
|
|
});
|
|
|
|
testWidgets('snackbar requested during build appears without asserting', (tester) async {
|
|
// Same bug class as the dialogs: showSnackBar sets state on the
|
|
// messenger, which asserts during build.
|
|
await tester.pumpWidget(
|
|
testApp(
|
|
const Scaffold(body: _SnackbarDuringBuild()),
|
|
navigatorKey: navigatorKey,
|
|
scaffoldMessengerKey: scaffoldMessengerKey,
|
|
),
|
|
);
|
|
|
|
expect(tester.takeException(), isNull);
|
|
|
|
await tester.pump();
|
|
expect(find.byType(SnackBar), findsOneWidget);
|
|
});
|
|
});
|
|
|
|
group('showHttpExceptionErrorDialog', () {
|
|
Future<BuildContext> pumpHome(WidgetTester tester) async {
|
|
await tester.pumpWidget(testApp(const Scaffold(body: SizedBox())));
|
|
return tester.element(find.byType(SizedBox));
|
|
}
|
|
|
|
Future<void> show(
|
|
WidgetTester tester,
|
|
WgerHttpException exception,
|
|
BuildContext context,
|
|
) async {
|
|
showHttpExceptionErrorDialog(exception, context: context);
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
testWidgets('lists the field errors of the server', (tester) async {
|
|
final context = await pumpHome(tester);
|
|
|
|
await show(
|
|
tester,
|
|
WgerHttpException.fromMap({
|
|
'name': ['This field may not be blank.'],
|
|
}),
|
|
context,
|
|
);
|
|
|
|
expect(find.text('An Error Occurred!'), findsOneWidget);
|
|
expect(find.textContaining('This field may not be blank.'), findsOneWidget);
|
|
// The form-validation fallback, not the diagnostic dialog
|
|
expect(find.text('Report issue'), findsNothing);
|
|
|
|
await tester.tap(find.text('Close'));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byType(AlertDialog), findsNothing);
|
|
});
|
|
|
|
testWidgets('renders an HTML answer instead of listing it as a field error', (tester) async {
|
|
final context = await pumpHome(tester);
|
|
|
|
await show(
|
|
tester,
|
|
WgerHttpException(
|
|
http.Response(
|
|
'<html><body><h1>Bad Gateway</h1></body></html>',
|
|
502,
|
|
headers: {HttpHeaders.contentTypeHeader: 'text/html'},
|
|
),
|
|
),
|
|
context,
|
|
);
|
|
|
|
expect(find.byType(ServerHtmlError), findsOneWidget);
|
|
await tester.tap(find.text('Close'));
|
|
await tester.pumpAndSettle();
|
|
});
|
|
|
|
testWidgets('one dialog at a time, the next one after it is closed', (tester) async {
|
|
final context = await pumpHome(tester);
|
|
final first = WgerHttpException.fromMap({'name': 'first'});
|
|
final second = WgerHttpException.fromMap({'name': 'second'});
|
|
|
|
await show(tester, first, context);
|
|
await show(tester, second, context);
|
|
expect(find.byType(AlertDialog), findsOneWidget);
|
|
expect(find.textContaining('second'), findsNothing);
|
|
|
|
await tester.tap(find.text('Close'));
|
|
await tester.pumpAndSettle();
|
|
await show(tester, second, context);
|
|
expect(find.textContaining('second'), findsOneWidget);
|
|
|
|
await tester.tap(find.text('Close'));
|
|
await tester.pumpAndSettle();
|
|
});
|
|
});
|
|
|
|
group('handleError', () {
|
|
Future<void> pumpApp(WidgetTester tester) => tester.pumpWidget(
|
|
testApp(
|
|
const Scaffold(body: SizedBox()),
|
|
navigatorKey: navigatorKey,
|
|
scaffoldMessengerKey: scaffoldMessengerKey,
|
|
),
|
|
);
|
|
|
|
Future<void> handle(WidgetTester tester, Object error) async {
|
|
handleError(error, StackTrace.current);
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
Future<void> close(WidgetTester tester, String button) async {
|
|
await tester.tap(find.text(button));
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
testWidgets('a cosmetic error shows nothing', (tester) async {
|
|
await pumpApp(tester);
|
|
|
|
await handle(tester, StateError('Failed to load image'));
|
|
|
|
expect(find.byType(AlertDialog), findsNothing);
|
|
expect(find.byType(SnackBar), findsNothing);
|
|
});
|
|
|
|
testWidgets('a network error only shows a snackbar', (tester) async {
|
|
await pumpApp(tester);
|
|
|
|
await handle(tester, const SocketException('unreachable'));
|
|
|
|
expect(find.byType(SnackBar), findsOneWidget);
|
|
expect(find.byType(AlertDialog), findsNothing);
|
|
});
|
|
|
|
testWidgets('a rejected form shows the field errors, no bug report', (tester) async {
|
|
await pumpApp(tester);
|
|
|
|
await handle(tester, WgerHttpException.fromMap({'name': 'too long'}));
|
|
|
|
expect(find.textContaining('too long'), findsOneWidget);
|
|
expect(find.text('Report issue'), findsNothing);
|
|
await close(tester, 'Close');
|
|
});
|
|
|
|
testWidgets('a rejected sync upload offers a bug report naming it', (tester) async {
|
|
final launcher = _FakeUrlLauncher();
|
|
final original = UrlLauncherPlatform.instance;
|
|
UrlLauncherPlatform.instance = launcher;
|
|
addTearDown(() => UrlLauncherPlatform.instance = original);
|
|
// The report names the app version
|
|
PackageInfo.setMockInitialValues(
|
|
appName: 'wger',
|
|
packageName: 'de.wger.flutter',
|
|
version: '1.2.3',
|
|
buildNumber: '45',
|
|
buildSignature: '',
|
|
);
|
|
await pumpApp(tester);
|
|
|
|
await handle(
|
|
tester,
|
|
WgerHttpException.fromMap(
|
|
{'date': 'Enter a valid date.'},
|
|
source: ExceptionSource.powersync,
|
|
statusCode: 400,
|
|
),
|
|
);
|
|
await tester.tap(find.text('Report issue'));
|
|
await tester.pumpAndSettle();
|
|
|
|
final report = Uri.parse(launcher.launchedUrl!);
|
|
expect(report.host, 'github.com');
|
|
expect(report.queryParameters['title'], 'Sync upload rejected (HTTP 400)');
|
|
expect(report.queryParameters['app-version'], startsWith('1.2.3+45'));
|
|
expect(report.queryParameters['description'], contains('Enter a valid date.'));
|
|
await close(tester, 'OK');
|
|
});
|
|
|
|
testWidgets('any other error gets the diagnostic dialog', (tester) async {
|
|
await pumpApp(tester);
|
|
|
|
await handle(tester, StateError('something broke'));
|
|
|
|
expect(find.text('Report issue'), findsOneWidget);
|
|
await close(tester, 'OK');
|
|
});
|
|
});
|
|
}
|