Test the error dialog routing and the HTTP error dialog

This commit is contained in:
Roland Geider
2026-12-03 20:35:45 +01:00
parent 7cd6d54e16
commit 02b1974300
2 changed files with 194 additions and 8 deletions

View File

@@ -16,12 +16,18 @@
* 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 'package:wger/l10n/generated/app_localizations.dart';
import '../helpers/pump_app.dart';
@@ -49,6 +55,17 @@ class _SnackbarDuringBuild extends StatelessWidget {
}
}
/// 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 = [
@@ -127,21 +144,21 @@ void main() {
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(
MaterialApp(
testApp(
const Scaffold(body: _SnackbarDuringBuild()),
navigatorKey: navigatorKey,
scaffoldMessengerKey: scaffoldMessengerKey,
localizationsDelegates: const [
AppLocalizations.delegate,
...GlobalMaterialLocalizations.delegates,
],
supportedLocales: AppLocalizations.supportedLocales,
home: const Scaffold(body: _SnackbarDuringBuild()),
),
);
@@ -151,4 +168,171 @@ void main() {
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');
});
});
}

View File

@@ -41,6 +41,7 @@ Widget testApp(
Map<String, WidgetBuilder> routes = const {},
String? initialRoute,
GlobalKey<NavigatorState>? navigatorKey,
GlobalKey<ScaffoldMessengerState>? scaffoldMessengerKey,
}) {
assert(
container == null || overrides.isEmpty,
@@ -52,6 +53,7 @@ Widget testApp(
localizationsDelegates: appLocalizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
navigatorKey: navigatorKey,
scaffoldMessengerKey: scaffoldMessengerKey,
initialRoute: initialRoute,
routes: routes,
home: home,