@@ -6,6 +6,7 @@ import 'package:flutter_test/flutter_test.dart';
6
6
import 'package:zulip/log.dart' ;
7
7
import 'package:zulip/model/actions.dart' ;
8
8
import 'package:zulip/model/database.dart' ;
9
+ import 'package:zulip/model/settings.dart' ;
9
10
import 'package:zulip/widgets/app.dart' ;
10
11
import 'package:zulip/widgets/home.dart' ;
11
12
import 'package:zulip/widgets/page.dart' ;
@@ -43,18 +44,31 @@ void main() {
43
44
]);
44
45
});
45
46
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);
52
53
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
+ });
58
72
});
59
73
});
60
74
@@ -82,6 +96,8 @@ void main() {
82
96
83
97
testWidgets ('push route when removing last route on stack' , (tester) async {
84
98
await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
99
+ await testBinding.globalStore.settings
100
+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
85
101
await prepare (tester);
86
102
// The navigator stack should contain only a home page route.
87
103
@@ -99,6 +115,8 @@ void main() {
99
115
testWidgets ('push route when popping last route on stack' , (tester) async {
100
116
// Set up the loading of per-account data to fail.
101
117
await testBinding.globalStore.insertAccount (eg.selfAccount.toCompanion (false ));
118
+ await testBinding.globalStore.settings
119
+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
102
120
testBinding.globalStore.loadPerAccountDuration = Duration .zero;
103
121
testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized ();
104
122
await prepare (tester);
@@ -133,6 +151,8 @@ void main() {
133
151
const loadPerAccountDuration = Duration (seconds: 30 );
134
152
assert (loadPerAccountDuration > kTryAnotherAccountWaitPeriod);
135
153
await testBinding.globalStore.insertAccount (eg.selfAccount.toCompanion (false ));
154
+ await testBinding.globalStore.settings
155
+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
136
156
testBinding.globalStore.loadPerAccountDuration = loadPerAccountDuration;
137
157
testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized ();
138
158
await prepare (tester);
@@ -282,6 +302,8 @@ void main() {
282
302
addTearDown (testBinding.reset);
283
303
await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
284
304
await testBinding.globalStore.add (eg.otherAccount, eg.initialSnapshot ());
305
+ await testBinding.globalStore.settings
306
+ .setInt (IntGlobalSetting .lastVisitedAccountId, eg.selfAccount.id);
285
307
286
308
final pushedRoutes = < Route <void >> [];
287
309
final poppedRoutes = < Route <void >> [];
@@ -318,6 +340,47 @@ void main() {
318
340
..page.isA <HomePage >();
319
341
});
320
342
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
+
321
384
group ('log out' , () {
322
385
Future <(Widget , Widget )> prepare (WidgetTester tester, {
323
386
required List <Account > accounts,
@@ -360,6 +423,35 @@ void main() {
360
423
await tester.pumpAndSettle ();
361
424
check (testBinding.globalStore).accounts.deepEquals ([eg.selfAccount]);
362
425
});
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
+ });
363
455
});
364
456
});
365
457
0 commit comments