import "dart:async"; import "package:flutter/foundation.dart"; import "package:flutter/widgets.dart"; import "package:flutter_test/flutter_test.dart"; import "package:garage_ui/garage_ui.dart" show LinearProgressIndicator; import "package:garage_updates/garage_updates.dart"; // GarageUpdater.launch against a driver that does whatever the test says class FakeDriver implements UpdateDriver { FakeDriver({this.installed = true}); @override bool installed; @override String? get notInstalledReason => installed ? null : "this isn't a release build"; @override FutureOr Function()? onBeforeRestart; final calls = []; /// what check() answers. a Completer to answer late, an error to throw Object? answer; bool startsKeepFailing = false; Object? downloadError; Object? installError; Completer? downloadGate; @override Future noteStart() async { calls.add("noteStart"); return startsKeepFailing; } @override Future check() async { calls.add("check"); final a = answer; if (a is Completer) return a.future; if (a is UpdateInfo) return a; throw a ?? StateError("no answer set"); } @override Future download(UpdateRelease release, {DownloadProgress? onProgress}) async { calls.add("download ${release.build}"); onProgress?.call(50, 100); await downloadGate?.future; final e = downloadError; if (e != null) throw e; onProgress?.call(100, 100); } @override Future install(UpdateRelease release) async { calls.add("install ${release.build}"); final e = installError; if (e != null) throw e; } @override Future restart({List args = const []}) async { await onBeforeRestart?.call(); calls.add("restart ${args.join(" ")}".trim()); } @override Future markHealthy() async => calls.add("markHealthy"); } const _release = UpdateRelease( label: "v13", build: 13, notes: "Fixed the thing", asset: UpdateAsset(url: "http://x/app.zip", filename: "app.zip", format: "zip", size: 100, sha256: "aa"), ); const _current = UpdateInfo(status: UpdateStatus.current); const _available = UpdateInfo(status: UpdateStatus.available, latest: _release); const _required = UpdateInfo(status: UpdateStatus.required, minimumBuild: 13, latest: _release); // the updater leaves a 6 hourly timer running, as it should. flutter_test // counts timers the moment the body returns, before any tearDown, so the // tidying has to happen in here void updaterTest( String name, Future Function(WidgetTester t) body, { TargetPlatform platform = TargetPlatform.macOS, }) { testWidgets(name, (t) async { GarageUpdater.reset(); // a flutter test says its android unless told otherwise debugDefaultTargetPlatformOverride = platform; try { await body(t); } finally { // take the splash down first, that schedules the markHealthy timer await t.pumpWidget(const SizedBox()); await t.pump(); GarageUpdater.reset(); debugDefaultTargetPlatformOverride = null; } }); } void main() { // starts the updater the way launch() does, with the splash going to the // tester instead of runApp ({Future done, bool Function() finished, Widget? Function() splash}) start( FakeDriver driver, { UpdateMode mode = UpdateMode.silent, List args = const ["--route=/x"], }) { Widget? shown; var finished = false; final done = GarageUpdater.instance .start( args: args, project: "proj_1", build: 12, label: "v12", branding: const UpdaterBranding(name: "Garage Stash"), mode: mode, driver: driver, show: (w) => shown = w, ) .then((_) => finished = true); return (done: done, finished: () => finished, splash: () => shown); } updaterTest("up to date: the splash shows, then the app carries on", (t) async { final d = FakeDriver()..answer = _current; final run = start(d); await t.pump(); expect(run.splash(), isA()); await t.pumpWidget(run.splash()!); await t.pump(); expect(run.finished(), isTrue); expect(find.text("Starting"), findsOneWidget); expect(d.calls, ["noteStart", "check"]); expect(GarageUpdater.instance.ready.value, isNull); expect(GarageUpdater.instance.label, "v12"); }); updaterTest("no answer in 2 seconds: the app starts, and the late answer is dealt with behind it", (t) async { final late = Completer(); final d = FakeDriver()..answer = late; final run = start(d); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(const Duration(milliseconds: 1900)); expect(run.finished(), isFalse); expect(find.text("Checking for updates"), findsOneWidget); await t.pump(const Duration(milliseconds: 200)); expect(run.finished(), isTrue); expect(d.calls, ["noteStart", "check"]); final heard = []; GarageUpdater.instance.readyStream.listen(heard.add); // the server finally answers, the app is long since up late.complete(_available); await t.pump(); await t.pump(); expect(d.calls, ["noteStart", "check", "download 13"]); expect(GarageUpdater.instance.ready.value, same(_available)); expect(heard, [_available]); expect(GarageUpdater.instance.notes, "Fixed the thing"); // downloaded, not installed. thats the apps call now expect(d.calls.where((c) => c.startsWith("install")), isEmpty); }); updaterTest("a check that fails lets the app start", (t) async { final d = FakeDriver()..answer = UpdateError("network", "Couldn't reach the update server"); final run = start(d); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); expect(run.finished(), isTrue); expect(find.text("Retry"), findsNothing); }); updaterTest("silent + available: downloaded, installed and restarted behind the splash", (t) async { final gate = Completer(); final d = FakeDriver() ..answer = _available ..downloadGate = gate; final stopped = []; GarageUpdater.instance.onBeforeRestart = () => stopped.add("agent stopped"); final run = start(d); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); await t.pump(); expect(find.text("Downloading update"), findsOneWidget); expect(t.widget(find.byType(LinearProgressIndicator)).value, 0.5); expect(run.finished(), isFalse); gate.complete(); await t.pump(); await t.pump(); expect(d.calls, ["noteStart", "check", "download 13", "install 13", "restart --route=/x"]); expect(stopped, ["agent stopped"]); }); updaterTest("silent + available that fails: the old build starts anyway", (t) async { final d = FakeDriver() ..answer = _available ..installError = UpdateError("install_failed", "The update could not be installed (3)"); final run = start(d); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); await t.pump(); expect(run.finished(), isTrue); expect(find.text("Retry"), findsNothing); expect(d.calls.where((c) => c.startsWith("restart")), isEmpty); }); updaterTest("prompt + available: nothing is installed, ready fires, restartToUpdate applies it", (t) async { final d = FakeDriver()..answer = _available; final run = start(d, mode: UpdateMode.prompt); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); await t.pump(); expect(run.finished(), isTrue); expect(d.calls, ["noteStart", "check", "download 13"]); expect(GarageUpdater.instance.ready.value?.latest?.build, 13); await GarageUpdater.instance.restartToUpdate(); expect(d.calls, ["noteStart", "check", "download 13", "install 13", "restart --route=/x"]); }); updaterTest("android never installs at launch unless it has to, its installer always asks", (t) async { final d = FakeDriver()..answer = _available; final run = start(d); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); await t.pump(); expect(run.finished(), isTrue); expect(d.calls, ["noteStart", "check", "download 13"]); expect(GarageUpdater.instance.ready.value?.latest?.build, 13); }, platform: TargetPlatform.android); updaterTest("required: the app is kept out, a failure offers Retry, and Retry goes again", (t) async { final d = FakeDriver() ..answer = _required ..downloadError = UpdateError("checksum_mismatch", "The download didn't match its checksum"); final run = start(d); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); await t.pump(); expect(run.finished(), isFalse); expect(find.text("The download didn't match its checksum"), findsOneWidget); expect(find.text("Retry"), findsOneWidget); // still out, however long its left await t.pump(const Duration(minutes: 10)); expect(run.finished(), isFalse); expect(d.calls.where((c) => c.startsWith("install")), isEmpty); d.downloadError = null; await t.tap(find.text("Retry")); await t.pump(); await t.pump(); await t.pump(); expect(d.calls, [ "noteStart", "check", "download 13", "check", "download 13", "install 13", "restart --route=/x", ]); // a real restart ends the process. the fake comes back, which is androids // case: the system installer is up and the app still must not start expect(run.finished(), isFalse); expect(find.text("Install the update to continue."), findsOneWidget); }); updaterTest("required is required in prompt mode too", (t) async { final d = FakeDriver()..answer = _required; final run = start(d, mode: UpdateMode.prompt); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); await t.pump(); expect(d.calls, contains("install 13")); expect(run.finished(), isFalse); }); updaterTest("required with nothing installable says so and keeps the app out", (t) async { final d = FakeDriver()..answer = const UpdateInfo(status: UpdateStatus.required, minimumBuild: 13); final run = start(d); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); expect(run.finished(), isFalse); expect(find.text("Retry"), findsOneWidget); expect(d.calls.where((c) => c.startsWith("download")), isEmpty); }); updaterTest("not installed in the layout: no splash, no check, nothing", (t) async { final d = FakeDriver(installed: false); final run = start(d); await t.pump(); expect(run.finished(), isTrue); expect(run.splash(), isNull); expect(d.calls, isEmpty); // and the rest of the surface is safe to call await GarageUpdater.instance.markHealthy(); await GarageUpdater.instance.restartToUpdate(); expect(await GarageUpdater.instance.checkNow(), isNull); expect(d.calls, isEmpty); expect(GarageUpdater.instance.ready.value, isNull); expect(GarageUpdater.instance.notes, isNull); }); updaterTest("a start that put the old build back restarts before anything else", (t) async { final d = FakeDriver() ..startsKeepFailing = true ..answer = _current; start(d); await t.pump(); expect(d.calls.take(2), ["noteStart", "restart --route=/x"]); }); updaterTest("markHealthy is called a few seconds after the host apps first frame", (t) async { final d = FakeDriver()..answer = _current; final run = start(d); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); expect(run.finished(), isTrue); // sat on the splash, the app hasnt drawn anything of its own yet await t.pump(const Duration(seconds: 10)); expect(d.calls, isNot(contains("markHealthy"))); await t.pumpWidget(const SizedBox(key: Key("the host app"))); await t.pump(const Duration(seconds: 2)); expect(d.calls, isNot(contains("markHealthy"))); await t.pump(const Duration(seconds: 2)); expect(d.calls, contains("markHealthy")); }); updaterTest("checks again every 6 hours while the app runs", (t) async { final d = FakeDriver()..answer = _current; final run = start(d); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); await t.pumpWidget(const SizedBox()); expect(d.calls.where((c) => c == "check").length, 1); await t.pump(const Duration(hours: 5, minutes: 59)); expect(d.calls.where((c) => c == "check").length, 1); d.answer = _available; await t.pump(const Duration(minutes: 2)); await t.pump(); expect(d.calls.where((c) => c == "check").length, 2); expect(d.calls, contains("download 13")); expect(GarageUpdater.instance.ready.value?.latest?.build, 13); // the same build again in another 6 hours isnt fetched twice await t.pump(const Duration(hours: 6, minutes: 1)); await t.pump(); expect(d.calls.where((c) => c == "download 13").length, 1); }); updaterTest("launch twice is ignored the second time", (t) async { final d = FakeDriver()..answer = _current; final run = start(d); await t.pump(); await t.pumpWidget(run.splash()!); await t.pump(); final again = start(FakeDriver()..answer = _required); await t.pump(); expect(again.finished(), isTrue); expect(again.splash(), isNull); }); }