Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite logOutAccount actions test to use realistic routes #1257

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/widgets/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import '../notifications/receive.dart';
import 'dialog.dart';
import 'store.dart';

Future<void> logOutAccount(BuildContext context, int accountId) async {
final globalStore = GlobalStoreWidget.of(context);

Future<void> logOutAccount(GlobalStore globalStore, int accountId) async {
final account = globalStore.getAccount(accountId);
if (account == null) return; // TODO(log)

Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class ChooseAccountPage extends StatelessWidget {
actionButtonText: zulipLocalizations.logOutConfirmationDialogConfirmButton,
onActionButtonPress: () {
// TODO error handling if db write fails?
logOutAccount(context, accountId);
logOutAccount(GlobalStoreWidget.of(context), accountId);
});
},
child: Text(zulipLocalizations.chooseAccountPageLogOutButton)),
Expand Down
59 changes: 33 additions & 26 deletions lib/widgets/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ const kTryAnotherAccountWaitPeriod = Duration(seconds: 5);
class _LoadingPlaceholderPage extends StatefulWidget {
const _LoadingPlaceholderPage({required this.accountId});

/// The relevant account for this page.
///
/// The account is not guaranteed to exist in the global store. This can
/// happen briefly when the account is removed from the database for logout,
/// but before [PerAccountStoreWidget.routeToRemoveOnLogout] is processed.
final int accountId;

@override
Expand Down Expand Up @@ -180,35 +185,37 @@ class _LoadingPlaceholderPageState extends State<_LoadingPlaceholderPage> {
@override
Widget build(BuildContext context) {
final zulipLocalizations = ZulipLocalizations.of(context);
final realmUrl = GlobalStoreWidget.of(context)
// TODO(#1219) `!` is incorrect
.getAccount(widget.accountId)!.realmUrl;
final account = GlobalStoreWidget.of(context).getAccount(widget.accountId);

return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(),
Visibility(
visible: showTryAnotherAccount,
maintainSize: true,
maintainAnimation: true,
maintainState: true,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Column(
children: [
const SizedBox(height: 16),
Text(zulipLocalizations.tryAnotherAccountMessage(realmUrl.toString())),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () => Navigator.push(context,
MaterialWidgetRoute(page: const ChooseAccountPage())),
child: Text(zulipLocalizations.tryAnotherAccountButton)),
]))),
])));
body: (account == null)
// We should only reach this state very briefly.
// See [_LoadingPlaceholderPage.accountId].
? const SizedBox.shrink()

: Center(child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(),
Visibility(
visible: showTryAnotherAccount,
maintainSize: true,
maintainAnimation: true,
maintainState: true,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Column(
children: [
const SizedBox(height: 16),
Text(zulipLocalizations.tryAnotherAccountMessage(account.realmUrl.toString())),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () => Navigator.push(context,
MaterialWidgetRoute(page: const ChooseAccountPage())),
child: Text(zulipLocalizations.tryAnotherAccountButton)),
]))),
])));
}
}

Expand Down
71 changes: 28 additions & 43 deletions test/widgets/actions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import 'package:zulip/model/store.dart';
import 'package:zulip/notifications/receive.dart';
import 'package:zulip/widgets/actions.dart';
import 'package:zulip/widgets/app.dart';
import 'package:zulip/widgets/inbox.dart';
import 'package:zulip/widgets/home.dart';
import 'package:zulip/widgets/message_list.dart';
import 'package:zulip/widgets/page.dart';
import 'package:zulip/widgets/store.dart';

import '../api/fake_api.dart';
import '../example_data.dart' as eg;
Expand All @@ -30,6 +32,7 @@ import '../model/unreads_checks.dart';
import '../stdlib_checks.dart';
import '../test_navigation.dart';
import 'dialog_checks.dart';
import 'page_checks.dart';
import 'test_app.dart';

void main() {
Expand Down Expand Up @@ -106,7 +109,7 @@ void main() {
final newConnection = separateConnection()
..prepare(delay: unregisterDelay, json: {'msg': '', 'result': 'success'});

final future = logOutAccount(context, eg.selfAccount.id);
final future = logOutAccount(GlobalStoreWidget.of(context), eg.selfAccount.id);
// Unregister-token request and account removal dispatched together
checkSingleUnregisterRequest(newConnection);
check(testBinding.globalStore.takeDoRemoveAccountCalls())
Expand Down Expand Up @@ -138,7 +141,7 @@ void main() {
final newConnection = separateConnection()
..prepare(delay: unregisterDelay, exception: exception);

final future = logOutAccount(context, eg.selfAccount.id);
final future = logOutAccount(GlobalStoreWidget.of(context), eg.selfAccount.id);
// Unregister-token request and account removal dispatched together
checkSingleUnregisterRequest(newConnection);
check(testBinding.globalStore.takeDoRemoveAccountCalls())
Expand All @@ -156,16 +159,6 @@ void main() {
});

testWidgets("logged-out account's routes removed from nav; other accounts' remain", (tester) async {
Future<void> makeUnreadTopicInInbox(int accountId, String topic) async {
final stream = eg.stream();
final message = eg.streamMessage(stream: stream, topic: topic);
final store = await testBinding.globalStore.perAccount(accountId);
await store.addStream(stream);
await store.addSubscription(eg.subscription(stream));
await store.addMessage(message);
await tester.pump();
}

addTearDown(testBinding.reset);

final account1 = eg.account(id: 1, user: eg.user());
Expand All @@ -174,50 +167,42 @@ void main() {
await testBinding.globalStore.add(account2, eg.initialSnapshot());

final testNavObserver = TestNavigatorObserver();
final pushedRoutes = <Route<void>>[];
testNavObserver.onPushed = (route, prevRoute) => pushedRoutes.add(route);

await tester.pumpWidget(ZulipApp(navigatorObservers: [testNavObserver]));
await tester.pump();
final account1Route = pushedRoutes.single;
check(account1Route).isA<WidgetRoute>().page.isA<HomePage>();

final account2Connection =
(await testBinding.globalStore.perAccount(account2.id)).connection as FakeApiConnection;
account2Connection.prepare(json: eg.newestGetMessagesResult(
foundOldest: true, messages: []).toJson());
final account2Route = MessageListPage.buildRoute(
accountId: account2.id, narrow: const CombinedFeedNarrow());
final navigator = await ZulipApp.navigator;
navigator.popUntil((_) => false); // clear starting routes
await tester.pumpAndSettle();

final pushedRoutes = <Route<dynamic>>[];
testNavObserver.onPushed = (route, prevRoute) => pushedRoutes.add(route);
// TODO(#737): switch to a realistic setup:
// https://github.com/zulip/zulip-flutter/pull/1076#discussion_r1874124363
final account1Route = MaterialAccountWidgetRoute(
accountId: account1.id, page: const InboxPageBody());
final account2Route = MaterialAccountWidgetRoute(
accountId: account2.id, page: const InboxPageBody());
unawaited(navigator.push(account1Route));
unawaited(navigator.push(account2Route));
await tester.pumpAndSettle();
check(pushedRoutes).deepEquals([account1Route, account2Route]);

await makeUnreadTopicInInbox(account1.id, 'topic in account1');
final findAccount1PageContent = find.text('topic in account1', skipOffstage: false);

await makeUnreadTopicInInbox(account2.id, 'topic in account2');
final findAccount2PageContent = find.text('topic in account2', skipOffstage: false);
await tester.pump();

final findLoadingPage = find.byType(LoadingPlaceholderPage, skipOffstage: false);
final findAccount1PageContent = find.byType(HomePage, skipOffstage: false);
final findAccount2PageContent = find.byType(MessageListPage, skipOffstage: false);

check(pushedRoutes).deepEquals([account1Route, account2Route]);
check(findAccount1PageContent).findsOne();
check(findLoadingPage).findsNothing();
check(findAccount2PageContent).findsOne();

final removedRoutes = <Route<dynamic>>[];
final removedRoutes = <Route<void>>[];
testNavObserver.onRemoved = (route, prevRoute) => removedRoutes.add(route);

final context = tester.element(find.byType(MaterialApp));
final future = logOutAccount(context, account1.id);
final future = logOutAccount(testBinding.globalStore, account1.id);
await tester.pump(TestGlobalStore.removeAccountDuration);
await future;
await tester.pumpAndSettle(); // wait for animations, if any
check(removedRoutes).single.identicalTo(account1Route);
check(testBinding.globalStore.takeDoRemoveAccountCalls())
.single.equals(account1.id);
check(findAccount1PageContent).findsNothing();
check(findLoadingPage).findsOne();

await tester.pump();
check(findAccount1PageContent).findsNothing();
check(findLoadingPage).findsNothing();
check(findAccount2PageContent).findsOne();
});
});
Expand Down
20 changes: 20 additions & 0 deletions test/widgets/home_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:zulip/api/model/events.dart';
import 'package:zulip/model/narrow.dart';
import 'package:zulip/model/store.dart';
import 'package:zulip/widgets/about_zulip.dart';
import 'package:zulip/widgets/actions.dart';
import 'package:zulip/widgets/app.dart';
import 'package:zulip/widgets/app_bar.dart';
import 'package:zulip/widgets/home.dart';
Expand All @@ -14,13 +15,15 @@ import 'package:zulip/widgets/inbox.dart';
import 'package:zulip/widgets/message_list.dart';
import 'package:zulip/widgets/page.dart';
import 'package:zulip/widgets/profile.dart';
import 'package:zulip/widgets/store.dart';
import 'package:zulip/widgets/subscription_list.dart';
import 'package:zulip/widgets/theme.dart';

import '../api/fake_api.dart';
import '../example_data.dart' as eg;
import '../flutter_checks.dart';
import '../model/binding.dart';
import '../model/store_checks.dart';
import '../model/test_store.dart';
import '../test_navigation.dart';
import 'message_list_checks.dart';
Expand Down Expand Up @@ -441,5 +444,22 @@ void main () {
// No additional wait for loadPerAccount.
checkOnHomePage(tester, expectedAccount: eg.selfAccount);
});

testWidgets('logging out', (tester) async {
// Regression test for: https://github.com/zulip/zulip-flutter/issues/1219
addTearDown(testBinding.reset);
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
await tester.pumpWidget(const ZulipApp());
await tester.pump(Duration.zero); // wait for the loading page
await tester.pump(loadPerAccountDuration);

final element = tester.element(find.byType(HomePage));
final future = logOutAccount(GlobalStoreWidget.of(element), eg.selfAccount.id);
await tester.pump(TestGlobalStore.removeAccountDuration);
await future;
// No error expected from [_LoadingPlaceholderPage] briefly not having
// access to the account being logged out.
check(testBinding.globalStore).accountIds.isEmpty();
});
});
}