@@ -6,6 +6,7 @@ import 'package:flutter_test/flutter_test.dart';
66import 'package:zulip/log.dart' ;
77import 'package:zulip/model/actions.dart' ;
88import 'package:zulip/model/database.dart' ;
9+ import 'package:zulip/model/settings.dart' ;
910import 'package:zulip/widgets/app.dart' ;
1011import 'package:zulip/widgets/home.dart' ;
1112import 'package:zulip/widgets/page.dart' ;
@@ -43,18 +44,43 @@ void main() {
4344 ]);
4445 });
4546
46- testWidgets ('when have accounts, go to home page for first account' , (tester) async {
47- // We'll need per-account data for the account that a page will be opened
48- // for, but not for the other account.
49- await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
50- await testBinding.globalStore.insertAccount (eg.otherAccount.toCompanion (false ));
51- await prepare (tester);
47+ group ('when have accounts' , () {
48+ testWidgets ('with last account visited, go to home page for last account' , (tester) async {
49+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
50+ await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot ());
51+ await testBinding.globalStore.settings
52+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.otherAccount.id);
53+ await prepare (tester);
54+
55+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
56+ (it) => it.isA <MaterialAccountWidgetRoute >()
57+ ..accountId.equals (eg.otherAccount.id)
58+ ..page.isA <HomePage >(),
59+ ]);
60+ });
5261
53- check (pushedRoutes).deepEquals (< Condition <Object ?>> [
54- (it) => it.isA <MaterialAccountWidgetRoute >()
55- ..accountId.equals (eg.selfAccount.id)
56- ..page.isA <HomePage >(),
57- ]);
62+ testWidgets ('with no last account visited, go to choose account' , (tester) async {
63+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
64+ check (testBinding.globalStore.settings)
65+ .getInt (IntGlobalSetting .lastVisitedAccountId).isNull ();
66+ await prepare (tester);
67+
68+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
69+ (it) => it.isA <WidgetRoute >().page.isA <ChooseAccountPage >(),
70+ ]);
71+ });
72+
73+ testWidgets ('with last account id matching none of the accounts, go to choose account' , (tester) async {
74+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
75+ await testBinding.globalStore.settings
76+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.otherAccount.id);
77+ check (testBinding.globalStore).getAccount (eg.otherAccount.id).isNull ();
78+ await prepare (tester);
79+
80+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
81+ (it) => it.isA <WidgetRoute >().page.isA <ChooseAccountPage >(),
82+ ]);
83+ });
5884 });
5985 });
6086
@@ -82,6 +108,8 @@ void main() {
82108
83109 testWidgets ('push route when removing last route on stack' , (tester) async {
84110 await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
111+ await testBinding.globalStore.settings
112+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
85113 await prepare (tester);
86114 // The navigator stack should contain only a home page route.
87115
@@ -99,6 +127,8 @@ void main() {
99127 testWidgets ('push route when popping last route on stack' , (tester) async {
100128 // Set up the loading of per-account data to fail.
101129 await testBinding.globalStore.insertAccount (eg.selfAccount.toCompanion (false ));
130+ await testBinding.globalStore.settings
131+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
102132 testBinding.globalStore.loadPerAccountDuration = Duration .zero;
103133 testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized ();
104134 await prepare (tester);
@@ -133,6 +163,8 @@ void main() {
133163 const loadPerAccountDuration = Duration (seconds: 30 );
134164 assert (loadPerAccountDuration > kTryAnotherAccountWaitPeriod);
135165 await testBinding.globalStore.insertAccount (eg.selfAccount.toCompanion (false ));
166+ await testBinding.globalStore.settings
167+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
136168 testBinding.globalStore.loadPerAccountDuration = loadPerAccountDuration;
137169 testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized ();
138170 await prepare (tester);
@@ -282,6 +314,8 @@ void main() {
282314 addTearDown (testBinding.reset);
283315 await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
284316 await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot ());
317+ await testBinding.globalStore.settings
318+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
285319
286320 final pushedRoutes = < Route <void >> [];
287321 final poppedRoutes = < Route <void >> [];
@@ -318,6 +352,47 @@ void main() {
318352 ..page.isA <HomePage >();
319353 });
320354
355+ group ('choosing an account changes the last visited account' , () {
356+ testWidgets ('first null, then changes to the chosen account' , (tester) async {
357+ addTearDown (testBinding.reset);
358+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
359+
360+ await tester.pumpWidget (ZulipApp ());
361+ await tester.pump ();
362+
363+ check (testBinding.globalStore.settings)
364+ .getInt (IntGlobalSetting .lastVisitedAccountId).isNull ();
365+ await tester.tap (find.text (eg.selfAccount.email));
366+ await tester.pump ();
367+ check (testBinding.globalStore.settings)
368+ .getInt (IntGlobalSetting .lastVisitedAccountId).equals (eg.selfAccount.id);
369+ });
370+
371+ testWidgets ('first non-null, then changes to the chosen account' , (tester) async {
372+ addTearDown (testBinding.reset);
373+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
374+ await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot ());
375+ await testBinding.globalStore.settings
376+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
377+
378+ await tester.pumpWidget (ZulipApp ());
379+ await tester.pump ();
380+
381+ final navigator = await ZulipApp .navigator;
382+ unawaited (navigator.push (
383+ MaterialWidgetRoute (page: const ChooseAccountPage ())));
384+ await tester.pump ();
385+ await tester.pump ();
386+
387+ check (testBinding.globalStore.settings)
388+ .getInt (IntGlobalSetting .lastVisitedAccountId).equals (eg.selfAccount.id);
389+ await tester.tap (find.text (eg.otherAccount.email));
390+ await tester.pump ();
391+ check (testBinding.globalStore.settings)
392+ .getInt (IntGlobalSetting .lastVisitedAccountId).equals (eg.otherAccount.id);
393+ });
394+ });
395+
321396 group ('log out' , () {
322397 Future <(Widget , Widget )> prepare (WidgetTester tester, {required Account account}) async {
323398 await setupChooseAccountPage (tester, accounts: [account]);
0 commit comments