import "package:flutter/widgets.dart"; import "package:flutter_test/flutter_test.dart"; import "package:garage_ui/garage_ui.dart"; import "package:garage_updates/garage_updates.dart"; void main() { const branding = UpdaterBranding( name: "Garage Stash", logo: SizedBox(key: Key("logo")), ); Future pumpSplash(WidgetTester t, {UpdaterBranding b = branding, VoidCallback? onGone}) async { final c = SplashController(); await t.pumpWidget(UpdateSplashApp(branding: b, controller: c, onGone: onGone)); return c; } double? bar(WidgetTester t) => t.widget(find.byType(LinearProgressIndicator)).value; testWidgets("the logo, the name, one status line and a bar", (t) async { await pumpSplash(t); expect(find.byKey(const Key("logo")), findsOneWidget); expect(find.text("Garage Stash"), findsOneWidget); expect(find.text("Checking for updates"), findsOneWidget); expect(find.byType(LinearProgressIndicator), findsOneWidget); // nothing known yet, so the bar just sweeps expect(bar(t), isNull); expect(find.text("Retry"), findsNothing); expect(find.byType(Button), findsNothing); }); testWidgets("each stage has its line", (t) async { final c = await pumpSplash(t); c.show(SplashStage.downloading, progress: 0); await t.pump(); expect(find.text("Downloading update"), findsOneWidget); expect(find.text("Checking for updates"), findsNothing); expect(bar(t), 0); c.progress = 0.4; await t.pump(); expect(bar(t), 0.4); c.show(SplashStage.installing); await t.pump(); expect(find.text("Installing"), findsOneWidget); expect(bar(t), isNull); c.show(SplashStage.starting); await t.pump(); expect(find.text("Starting"), findsOneWidget); expect(find.text("Installing"), findsNothing); }); testWidgets("a failure shows why and a Retry, and Retry clears it", (t) async { final c = await pumpSplash(t); c.show(SplashStage.downloading, progress: 0.3); var retried = false; c.fail("The download didn't match its checksum").then((_) => retried = true); await t.pump(); expect(find.text("The download didn't match its checksum"), findsOneWidget); expect(find.text("Retry"), findsOneWidget); // the stage it failed at is still what the status line says expect(find.text("Downloading update"), findsOneWidget); expect(bar(t), 0.3); expect(retried, isFalse); await t.tap(find.text("Retry")); await t.pump(); expect(retried, isTrue); expect(find.text("Retry"), findsNothing); expect(find.text("The download didn't match its checksum"), findsNothing); }); testWidgets("a failure with nothing measured stops the bar sweeping", (t) async { final c = await pumpSplash(t); c.show(SplashStage.installing); c.fail("The update could not be installed."); await t.pump(); expect(bar(t), 0); }); testWidgets("no logo is fine", (t) async { await pumpSplash(t, b: const UpdaterBranding(name: "Plain")); expect(find.text("Plain"), findsOneWidget); expect(find.byType(LinearProgressIndicator), findsOneWidget); }); testWidgets("its drawn with the garage theme, carbon unless the developer says otherwise", (t) async { await pumpSplash(t); var theme = GarageTheme.of(t.element(find.byType(UpdateSplash))); expect(theme.colorScheme, GarageSchemes.carbon); final light = ThemeData(colorScheme: GarageSchemes.light); await t.pumpWidget(const SizedBox()); await pumpSplash(t, b: UpdaterBranding(name: "Light", theme: light)); theme = GarageTheme.of(t.element(find.byType(UpdateSplash))); expect(theme.colorScheme, GarageSchemes.light); }); testWidgets("the host app taking over is noticed", (t) async { var gone = 0; await pumpSplash(t, onGone: () => gone++); expect(gone, 0); // what the apps own runApp does to us await t.pumpWidget(const SizedBox()); expect(gone, 1); }); }