@@ -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,31 @@ 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 visited, go to home page for last account' , (tester) async {
60+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
61+ await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot ());
62+ await testBinding.globalStore.settings
63+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.otherAccount.id);
64+ await prepare (tester);
65+
66+ check (pushedRoutes).deepEquals (< Condition <Object ?>> [
67+ (it) => it.isA <MaterialAccountWidgetRoute >()
68+ ..accountId.equals (eg.otherAccount.id)
69+ ..page.isA <HomePage >(),
70+ ]);
71+ });
5872 });
5973 });
6074
@@ -82,6 +96,8 @@ void main() {
8296
8397 testWidgets ('push route when removing last route on stack' , (tester) async {
8498 await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
99+ await testBinding.globalStore.settings
100+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
85101 await prepare (tester);
86102 // The navigator stack should contain only a home page route.
87103
@@ -99,6 +115,8 @@ void main() {
99115 testWidgets ('push route when popping last route on stack' , (tester) async {
100116 // Set up the loading of per-account data to fail.
101117 await testBinding.globalStore.insertAccount (eg.selfAccount.toCompanion (false ));
118+ await testBinding.globalStore.settings
119+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
102120 testBinding.globalStore.loadPerAccountDuration = Duration .zero;
103121 testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized ();
104122 await prepare (tester);
@@ -133,6 +151,8 @@ void main() {
133151 const loadPerAccountDuration = Duration (seconds: 30 );
134152 assert (loadPerAccountDuration > kTryAnotherAccountWaitPeriod);
135153 await testBinding.globalStore.insertAccount (eg.selfAccount.toCompanion (false ));
154+ await testBinding.globalStore.settings
155+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
136156 testBinding.globalStore.loadPerAccountDuration = loadPerAccountDuration;
137157 testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized ();
138158 await prepare (tester);
@@ -282,6 +302,8 @@ void main() {
282302 addTearDown (testBinding.reset);
283303 await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
284304 await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot ());
305+ await testBinding.globalStore.settings
306+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
285307
286308 final pushedRoutes = < Route <void >> [];
287309 final poppedRoutes = < Route <void >> [];
@@ -318,6 +340,47 @@ void main() {
318340 ..page.isA <HomePage >();
319341 });
320342
343+ group ('choosing an account changes the last visited account' , () {
344+ testWidgets ('first null, then changes to the chosen account' , (tester) async {
345+ addTearDown (testBinding.reset);
346+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
347+
348+ await tester.pumpWidget (ZulipApp ());
349+ await tester.pump ();
350+
351+ check (testBinding.globalStore.settings)
352+ .getInt (IntGlobalSetting .lastVisitedAccountId).isNull ();
353+ await tester.tap (find.text (eg.selfAccount.email));
354+ await tester.pump ();
355+ check (testBinding.globalStore.settings)
356+ .getInt (IntGlobalSetting .lastVisitedAccountId).equals (eg.selfAccount.id);
357+ });
358+
359+ testWidgets ('first non-null, then changes to the chosen account' , (tester) async {
360+ addTearDown (testBinding.reset);
361+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
362+ await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot ());
363+ await testBinding.globalStore.settings
364+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
365+
366+ await tester.pumpWidget (ZulipApp ());
367+ await tester.pump ();
368+
369+ final navigator = await ZulipApp .navigator;
370+ unawaited (navigator.push (
371+ MaterialWidgetRoute (page: const ChooseAccountPage ())));
372+ await tester.pump ();
373+ await tester.pump ();
374+
375+ check (testBinding.globalStore.settings)
376+ .getInt (IntGlobalSetting .lastVisitedAccountId).equals (eg.selfAccount.id);
377+ await tester.tap (find.text (eg.otherAccount.email));
378+ await tester.pump ();
379+ check (testBinding.globalStore.settings)
380+ .getInt (IntGlobalSetting .lastVisitedAccountId).equals (eg.otherAccount.id);
381+ });
382+ });
383+
321384 group ('log out' , () {
322385 Future <(Widget , Widget )> prepare (WidgetTester tester, {
323386 required List <Account > accounts,
@@ -360,6 +423,35 @@ void main() {
360423 await tester.pumpAndSettle ();
361424 check (testBinding.globalStore).accounts.deepEquals ([eg.selfAccount]);
362425 });
426+
427+ group ('last visited account' , () {
428+ testWidgets ('is the logged out one -> last account set to null' , (tester) async {
429+ await testBinding.globalStore.settings
430+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
431+
432+ final (actionButton, _) = await prepare (tester,
433+ accounts: [eg.selfAccount, eg.otherAccount], logoutAccount: eg.selfAccount);
434+ await tester.tap (find.byWidget (actionButton));
435+ await tester.pump (TestGlobalStore .removeAccountDuration);
436+ check (testBinding.globalStore)
437+ ..accounts.deepEquals ([eg.otherAccount])
438+ ..settings.getInt (IntGlobalSetting .lastVisitedAccountId).isNull ();
439+ });
440+
441+ testWidgets ('is not the logged out one -> last account not changed' , (tester) async {
442+ await testBinding.globalStore.settings
443+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.otherAccount.id);
444+
445+ final (actionButton, _) = await prepare (tester,
446+ accounts: [eg.selfAccount, eg.otherAccount], logoutAccount: eg.selfAccount);
447+ await tester.tap (find.byWidget (actionButton));
448+ await tester.pump (TestGlobalStore .removeAccountDuration);
449+ check (testBinding.globalStore)
450+ ..accounts.deepEquals ([eg.otherAccount])
451+ ..settings.getInt (IntGlobalSetting .lastVisitedAccountId)
452+ .equals (eg.otherAccount.id);
453+ });
454+ });
363455 });
364456 });
365457
0 commit comments