@@ -3,13 +3,15 @@ import 'dart:async';
33import 'package:checks/checks.dart' ;
44import 'package:flutter/material.dart' ;
55import 'package:flutter_test/flutter_test.dart' ;
6+ import 'package:zulip/basic.dart' ;
67import 'package:zulip/log.dart' ;
78import 'package:zulip/model/actions.dart' ;
89import 'package:zulip/model/database.dart' ;
910import 'package:zulip/widgets/app.dart' ;
1011import 'package:zulip/widgets/home.dart' ;
1112import 'package:zulip/widgets/page.dart' ;
1213
14+ import '../basic_checks.dart' ;
1315import '../example_data.dart' as eg;
1416import '../flutter_checks.dart' ;
1517import '../model/binding.dart' ;
@@ -43,18 +45,47 @@ void main() {
4345 ]);
4446 });
4547
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);
48+ group ('when have accounts' , () {
49+ testWidgets ('with account(s) visited, go to home page for the last visited account' , (tester) async {
50+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
51+ await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot (
52+ realmUsers: [eg.otherUser]));
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 account visited, go to home page for the first account' , (tester) async {
63+ await testBinding.globalStore.add (
64+ eg.selfAccount, eg.initialSnapshot (),
65+ markLastVisited: false );
66+ await testBinding.globalStore.add (
67+ eg.otherAccount, eg.initialSnapshot (realmUsers: [eg.otherUser]),
68+ markLastVisited: false );
69+ check (testBinding.globalStore).lastVisitedAccount.isA <OptionNone <Account ?>>();
70+ await prepare (tester);
71+
72+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
73+ (it) => it.isA <MaterialAccountWidgetRoute >()
74+ ..accountId.equals (eg.selfAccount.id)
75+ ..page.isA <HomePage >(),
76+ ]);
77+ });
78+
79+ testWidgets ('with last visited account id matching none of the accounts, go to choose account' , (tester) async {
80+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
81+ await testBinding.globalStore.setLastVisitedAccount (eg.otherAccount.id);
82+ check (testBinding.globalStore).getAccount (eg.otherAccount.id).isNull ();
83+ await prepare (tester);
84+
85+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
86+ (it) => it.isA <WidgetRoute >().page.isA <ChooseAccountPage >(),
87+ ]);
88+ });
5889 });
5990 });
6091
@@ -99,6 +130,7 @@ void main() {
99130 testWidgets ('push route when popping last route on stack' , (tester) async {
100131 // Set up the loading of per-account data to fail.
101132 await testBinding.globalStore.insertAccount (eg.selfAccount.toCompanion (false ));
133+ await testBinding.globalStore.setLastVisitedAccount (eg.selfAccount.id);
102134 testBinding.globalStore.loadPerAccountDuration = Duration .zero;
103135 testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized ();
104136 await prepare (tester);
@@ -133,6 +165,7 @@ void main() {
133165 const loadPerAccountDuration = Duration (seconds: 30 );
134166 assert (loadPerAccountDuration > kTryAnotherAccountWaitPeriod);
135167 await testBinding.globalStore.insertAccount (eg.selfAccount.toCompanion (false ));
168+ await testBinding.globalStore.setLastVisitedAccount (eg.selfAccount.id);
136169 testBinding.globalStore.loadPerAccountDuration = loadPerAccountDuration;
137170 testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized ();
138171 await prepare (tester);
@@ -281,8 +314,9 @@ void main() {
281314 testWidgets ('choosing an account clears the navigator stack' , (tester) async {
282315 addTearDown (testBinding.reset);
283316 await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
284- await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot (
285- realmUsers: [eg.otherUser]));
317+ await testBinding.globalStore.add (
318+ eg.otherAccount, eg.initialSnapshot (realmUsers: [eg.otherUser]),
319+ markLastVisited: false );
286320
287321 final pushedRoutes = < Route <void >> [];
288322 final poppedRoutes = < Route <void >> [];
@@ -319,6 +353,29 @@ void main() {
319353 ..page.isA <HomePage >();
320354 });
321355
356+ testWidgets ('choosing an account changes the last visited account' , (tester) async {
357+ addTearDown (testBinding.reset);
358+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
359+ await testBinding.globalStore.add (
360+ eg.otherAccount, eg.initialSnapshot (realmUsers: [eg.otherUser]),
361+ markLastVisited: false );
362+
363+ await tester.pumpWidget (ZulipApp ());
364+ await tester.pump ();
365+
366+ final navigator = await ZulipApp .navigator;
367+ unawaited (navigator.push (MaterialWidgetRoute (page: const ChooseAccountPage ())));
368+ await tester.pump ();
369+ await tester.pump ();
370+
371+ check (testBinding.globalStore).lastVisitedAccount
372+ .isA <OptionSome <Account ?>>().value.equals (eg.selfAccount);
373+ await tester.tap (find.text (eg.otherAccount.email));
374+ await tester.pump ();
375+ check (testBinding.globalStore).lastVisitedAccount
376+ .isA <OptionSome <Account ?>>().value.equals (eg.otherAccount);
377+ });
378+
322379 group ('log out' , () {
323380 Future <(Widget , Widget )> prepare (WidgetTester tester, {required Account account}) async {
324381 await setupChooseAccountPage (tester, accounts: [account]);
0 commit comments