@@ -43,18 +43,50 @@ void main() {
4343 ]);
4444 });
4545
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);
46+ group ('when have accounts' , () {
47+ testWidgets ('with account(s) visited, go to home page for the last visited account' , (tester) async {
48+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
49+ await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot (
50+ realmUsers: [eg.otherUser]));
51+ await prepare (tester);
52+
53+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
54+ (it) => it.isA <MaterialAccountWidgetRoute >()
55+ ..accountId.equals (eg.otherAccount.id)
56+ ..page.isA <HomePage >(),
57+ ]);
58+ });
5259
53- check (pushedRoutes).deepEquals (< Condition <Object ?>> [
54- (it) => it.isA <MaterialAccountWidgetRoute >()
55- ..accountId.equals (eg.selfAccount.id)
56- ..page.isA <HomePage >(),
57- ]);
60+ testWidgets ('with no account visited, go to home page for the first account' , (tester) async {
61+ await testBinding.globalStore.add (
62+ eg.selfAccount, eg.initialSnapshot (),
63+ markLastVisited: false );
64+ await testBinding.globalStore.add (
65+ eg.otherAccount, eg.initialSnapshot (realmUsers: [eg.otherUser]),
66+ markLastVisited: false );
67+ // We assume this is set by the database migration step from v9 to v10.
68+ // That migration step integrity is excercised in its own test case
69+ // in test/model/database_test.dart.
70+ await testBinding.globalStore.setLastVisitedAccount (eg.selfAccount.id);
71+ await prepare (tester);
72+
73+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
74+ (it) => it.isA <MaterialAccountWidgetRoute >()
75+ ..accountId.equals (eg.selfAccount.id)
76+ ..page.isA <HomePage >(),
77+ ]);
78+ });
79+
80+ testWidgets ('with last visited account id matching none of the accounts, go to choose account' , (tester) async {
81+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
82+ await testBinding.globalStore.setLastVisitedAccount (eg.otherAccount.id);
83+ check (testBinding.globalStore).getAccount (eg.otherAccount.id).isNull ();
84+ await prepare (tester);
85+
86+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
87+ (it) => it.isA <WidgetRoute >().page.isA <ChooseAccountPage >(),
88+ ]);
89+ });
5890 });
5991 });
6092
@@ -99,6 +131,7 @@ void main() {
99131 testWidgets ('push route when popping last route on stack' , (tester) async {
100132 // Set up the loading of per-account data to fail.
101133 await testBinding.globalStore.insertAccount (eg.selfAccount.toCompanion (false ));
134+ await testBinding.globalStore.setLastVisitedAccount (eg.selfAccount.id);
102135 testBinding.globalStore.loadPerAccountDuration = Duration .zero;
103136 testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized ();
104137 await prepare (tester);
@@ -133,6 +166,7 @@ void main() {
133166 const loadPerAccountDuration = Duration (seconds: 30 );
134167 assert (loadPerAccountDuration > kTryAnotherAccountWaitPeriod);
135168 await testBinding.globalStore.insertAccount (eg.selfAccount.toCompanion (false ));
169+ await testBinding.globalStore.setLastVisitedAccount (eg.selfAccount.id);
136170 testBinding.globalStore.loadPerAccountDuration = loadPerAccountDuration;
137171 testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized ();
138172 await prepare (tester);
@@ -281,8 +315,9 @@ void main() {
281315 testWidgets ('choosing an account clears the navigator stack' , (tester) async {
282316 addTearDown (testBinding.reset);
283317 await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
284- await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot (
285- realmUsers: [eg.otherUser]));
318+ await testBinding.globalStore.add (
319+ eg.otherAccount, eg.initialSnapshot (realmUsers: [eg.otherUser]),
320+ markLastVisited: false );
286321
287322 final pushedRoutes = < Route <void >> [];
288323 final poppedRoutes = < Route <void >> [];
@@ -319,6 +354,27 @@ void main() {
319354 ..page.isA <HomePage >();
320355 });
321356
357+ testWidgets ('choosing an account changes the last visited account' , (tester) async {
358+ addTearDown (testBinding.reset);
359+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
360+ await testBinding.globalStore.add (
361+ eg.otherAccount, eg.initialSnapshot (realmUsers: [eg.otherUser]),
362+ markLastVisited: false );
363+
364+ await tester.pumpWidget (ZulipApp ());
365+ await tester.pump ();
366+
367+ final navigator = await ZulipApp .navigator;
368+ unawaited (navigator.push (MaterialWidgetRoute (page: const ChooseAccountPage ())));
369+ await tester.pump ();
370+ await tester.pump ();
371+
372+ check (testBinding.globalStore).lastVisitedAccount.equals (eg.selfAccount);
373+ await tester.tap (find.text (eg.otherAccount.email));
374+ await tester.pump ();
375+ check (testBinding.globalStore).lastVisitedAccount.equals (eg.otherAccount);
376+ });
377+
322378 group ('log out' , () {
323379 Future <(Widget , Widget )> prepare (WidgetTester tester, {required Account account}) async {
324380 await setupChooseAccountPage (tester, accounts: [account]);
0 commit comments