@@ -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 no last account visited, go to choose account' , (tester) async {
49+ await testBinding.globalStore. add (eg.selfAccount, eg. initialSnapshot ());
50+ check ( testBinding.globalStore.settings)
51+ . getInt ( IntGlobalSetting .lastVisitedAccountId). isNull ( );
52+ await prepare (tester);
5253
53- check (pushedRoutes).deepEquals (< Condition <Object ?>> [
54- (it) => it.isA <MaterialAccountWidgetRoute >()
55- ..accountId.equals (eg.selfAccount.id)
56- ..page.isA <HomePage >(),
57- ]);
54+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
55+ (it) => it.isA <WidgetRoute >().page.isA <ChooseAccountPage >(),
56+ ]);
57+ });
58+
59+ testWidgets ('with last account id matching none of the accounts, go to choose account' , (tester) async {
60+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
61+ await testBinding.globalStore.settings
62+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.otherAccount.id);
63+ check (testBinding.globalStore).getAccount (eg.otherAccount.id).isNull ();
64+ await prepare (tester);
65+
66+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
67+ (it) => it.isA <WidgetRoute >().page.isA <ChooseAccountPage >(),
68+ ]);
69+ });
70+
71+ testWidgets ('with last account visited, go to home page for last account' , (tester) async {
72+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
73+ await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot ());
74+ await testBinding.globalStore.settings
75+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.otherAccount.id);
76+ await prepare (tester);
77+
78+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
79+ (it) => it.isA <MaterialAccountWidgetRoute >()
80+ ..accountId.equals (eg.otherAccount.id)
81+ ..page.isA <HomePage >(),
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, {
323398 List <Account >? accounts,
@@ -360,6 +435,35 @@ void main() {
360435 await tester.pumpAndSettle ();
361436 check (testBinding.globalStore).accounts.deepEquals ([eg.selfAccount]);
362437 });
438+
439+ group ('last visited account' , () {
440+ testWidgets ('is the logged out one -> last account set to null' , (tester) async {
441+ await testBinding.globalStore.settings
442+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
443+
444+ final (actionButton, _) = await prepare (tester,
445+ accounts: [eg.selfAccount, eg.otherAccount], logoutAccount: eg.selfAccount);
446+ await tester.tap (find.byWidget (actionButton));
447+ await tester.pump (TestGlobalStore .removeAccountDuration);
448+ check (testBinding.globalStore)
449+ ..accounts.deepEquals ([eg.otherAccount])
450+ ..settings.getInt (IntGlobalSetting .lastVisitedAccountId).isNull ();
451+ });
452+
453+ testWidgets ('is not the logged out one -> last account not changed' , (tester) async {
454+ await testBinding.globalStore.settings
455+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.otherAccount.id);
456+
457+ final (actionButton, _) = await prepare (tester,
458+ accounts: [eg.selfAccount, eg.otherAccount], logoutAccount: eg.selfAccount);
459+ await tester.tap (find.byWidget (actionButton));
460+ await tester.pump (TestGlobalStore .removeAccountDuration);
461+ check (testBinding.globalStore)
462+ ..accounts.deepEquals ([eg.otherAccount])
463+ ..settings.getInt (IntGlobalSetting .lastVisitedAccountId)
464+ .equals (eg.otherAccount.id);
465+ });
466+ });
363467 });
364468 });
365469
0 commit comments