Skip to content

Commit 5d1358b

Browse files
gnpricechrisbobbe
authored andcommitted
push_device [nfc]: Auto-register from PushDeviceManager, vs UpdateMachine
1 parent 7c29bec commit 5d1358b

File tree

6 files changed

+32
-29
lines changed

6 files changed

+32
-29
lines changed

lib/model/push_device.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import 'store.dart';
55
/// and tracking the server's responses on the status of push devices.
66
// TODO(#1764) do that tracking of responses
77
class PushDeviceManager extends PerAccountStoreBase {
8-
PushDeviceManager({required super.core});
8+
PushDeviceManager({required super.core}) {
9+
if (!debugAutoRegisterToken) {
10+
return;
11+
}
12+
registerToken();
13+
}
914

1015
bool _disposed = false;
1116

@@ -27,9 +32,6 @@ class PushDeviceManager extends PerAccountStoreBase {
2732
// TODO(#322) save acked token, to dedupe updating it on the server
2833
// TODO(#323) track the addFcmToken/etc request, warn if not succeeding
2934
Future<void> registerToken() async {
30-
if (!debugEnableRegisterToken) {
31-
return;
32-
}
3335
NotificationService.instance.token.addListener(_registerToken);
3436
await _registerToken();
3537
}
@@ -40,22 +42,22 @@ class PushDeviceManager extends PerAccountStoreBase {
4042
await NotificationService.instance.registerToken(connection);
4143
}
4244

43-
/// In debug mode, controls whether [registerToken] should
44-
/// have its normal effect.
45+
/// In debug mode, controls whether [registerToken] should be called
46+
/// immediately in the constructor.
4547
///
4648
/// Outside of debug mode, this is always true and the setter has no effect.
47-
static bool get debugEnableRegisterToken {
49+
static bool get debugAutoRegisterToken {
4850
bool result = true;
4951
assert(() {
50-
result = _debugEnableRegisterToken;
52+
result = _debugAutoRegisterToken;
5153
return true;
5254
}());
5355
return result;
5456
}
55-
static bool _debugEnableRegisterToken = true;
56-
static set debugEnableRegisterToken(bool value) {
57+
static bool _debugAutoRegisterToken = true;
58+
static set debugAutoRegisterToken(bool value) {
5759
assert(() {
58-
_debugEnableRegisterToken = value;
60+
_debugAutoRegisterToken = value;
5961
return true;
6062
}());
6163
}

lib/model/store.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,6 @@ class UpdateMachine {
11231123
// serverEmojiDataUrl are already unsupported at time of writing.)
11241124
unawaited(updateMachine.fetchEmojiData(initialSnapshot.serverEmojiDataUrl!));
11251125
}
1126-
unawaited(store.pushDevices.registerToken());
11271126
store.presence.start();
11281127
return updateMachine;
11291128
}

test/model/actions_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter_test/flutter_test.dart';
77
import 'package:http/http.dart' as http;
88
import 'package:http/testing.dart' as http_testing;
99
import 'package:zulip/model/actions.dart';
10+
import 'package:zulip/model/push_device.dart';
1011
import 'package:zulip/model/store.dart';
1112
import 'package:zulip/notifications/receive.dart';
1213

@@ -147,6 +148,8 @@ void main() {
147148
}));
148149

149150
test('notifications are removed after logout', () => awaitFakeAsync((async) async {
151+
PushDeviceManager.debugAutoRegisterToken = false;
152+
addTearDown(() => PushDeviceManager.debugAutoRegisterToken = true);
150153
await prepare();
151154
testBinding.firebaseMessagingInitialToken = '123';
152155
addTearDown(NotificationService.debugReset);

test/model/push_device_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ void main() {
1616
TestZulipBinding.ensureInitialized();
1717

1818
group('registerToken', () {
19+
// TODO test registerToken gets called on constructing an instance
20+
1921
late PushDeviceManager model;
2022
late FakeApiConnection connection;
2123

2224
void prepareStore() {
25+
PushDeviceManager.debugAutoRegisterToken = false;
26+
addTearDown(() => PushDeviceManager.debugAutoRegisterToken = true);
2327
final store = eg.store();
2428
model = store.pushDevices;
2529
connection = store.connection as FakeApiConnection;

test/model/store_test.dart

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ import 'package:zulip/api/route/realm.dart';
1717
import 'package:zulip/log.dart';
1818
import 'package:zulip/model/actions.dart';
1919
import 'package:zulip/model/presence.dart';
20-
import 'package:zulip/model/push_device.dart';
2120
import 'package:zulip/model/server_support.dart';
2221
import 'package:zulip/model/store.dart';
23-
import 'package:zulip/notifications/receive.dart';
2422

2523
import '../api/fake_api.dart';
2624
import '../api/model/model_checks.dart';
@@ -137,17 +135,14 @@ void main() {
137135
}));
138136

139137
test('GlobalStore.perAccount loading succeeds', () => awaitFakeAsync((async) async {
140-
NotificationService.instance.token = ValueNotifier('asdf');
141-
addTearDown(NotificationService.debugReset);
142-
143138
final globalStore = UpdateMachineTestGlobalStore(accounts: [eg.selfAccount]);
144139
final connection = globalStore.apiConnectionFromAccount(eg.selfAccount) as FakeApiConnection;
145140
final future = globalStore.perAccount(eg.selfAccount.id);
146141
check(connection.takeRequests()).length.equals(1); // register request
147142

148143
await future;
149-
// poll, server-emoji-data, register-token requests
150-
check(connection.takeRequests()).length.equals(3);
144+
// poll and server-emoji-data requests
145+
check(connection.takeRequests()).length.equals(2);
151146
check(connection).isOpen.isTrue();
152147
}));
153148

@@ -163,7 +158,7 @@ void main() {
163158

164159
await check(future).throws<AccountNotFoundException>();
165160
check(globalStore.takeDoRemoveAccountCalls()).single.equals(eg.selfAccount.id);
166-
// no poll, server-emoji-data, or register-token requests
161+
// no poll or other follow-up requests
167162
check(connection.takeRequests()).isEmpty();
168163
check(connection).isOpen.isFalse();
169164
}));
@@ -182,7 +177,7 @@ void main() {
182177

183178
await check(future).throws<AccountNotFoundException>();
184179
check(globalStore.takeDoRemoveAccountCalls()).single.equals(eg.selfAccount.id);
185-
// no poll, server-emoji-data, or register-token requests
180+
// no poll or other follow-up requests
186181
check(connection.takeRequests()).isEmpty();
187182
check(connection).isOpen.isFalse();
188183
}));
@@ -203,7 +198,7 @@ void main() {
203198

204199
await check(future).throws<AccountNotFoundException>();
205200
check(globalStore.takeDoRemoveAccountCalls()).isEmpty();
206-
// no poll, server-emoji-data, or register-token requests
201+
// no poll or other follow-up requests
207202
check(connection.takeRequests()).isEmpty();
208203
check(connection).isOpen.isFalse();
209204
}));
@@ -224,7 +219,7 @@ void main() {
224219

225220
await check(future).throws<AccountNotFoundException>();
226221
check(globalStore.takeDoRemoveAccountCalls()).isEmpty();
227-
// no poll, server-emoji-data, or register-token requests
222+
// no poll or other follow-up requests
228223
check(connection.takeRequests()).isEmpty();
229224
check(connection).isOpen.isFalse();
230225
}));
@@ -246,7 +241,7 @@ void main() {
246241

247242
await check(future).throws<AccountNotFoundException>();
248243
check(globalStore.takeDoRemoveAccountCalls()).isEmpty();
249-
// no poll, server-emoji-data, or register-token requests
244+
// no poll or other follow-up requests
250245
check(connection.takeRequests()).isEmpty();
251246
check(connection).isOpen.isFalse();
252247
}));
@@ -270,7 +265,7 @@ void main() {
270265

271266
await check(future).throws<AccountNotFoundException>();
272267
check(globalStore.takeDoRemoveAccountCalls()).isEmpty();
273-
// no poll, server-emoji-data, or register-token requests
268+
// no poll or other follow-up requests
274269
check(connection.takeRequests()).isEmpty();
275270
check(connection).isOpen.isFalse();
276271
}));
@@ -294,7 +289,7 @@ void main() {
294289

295290
await check(future).throws<AccountNotFoundException>();
296291
check(globalStore.takeDoRemoveAccountCalls()).isEmpty();
297-
// no retry-register, poll, server-emoji-data, or register-token requests
292+
// no retry-register, poll, or other follow-up requests
298293
check(connection.takeRequests()).isEmpty();
299294
check(connection).isOpen.isFalse();
300295
}));
@@ -484,8 +479,6 @@ void main() {
484479
as FakeApiConnection);
485480
UpdateMachine.debugEnableFetchEmojiData = false;
486481
addTearDown(() => UpdateMachine.debugEnableFetchEmojiData = true);
487-
PushDeviceManager.debugEnableRegisterToken = false;
488-
addTearDown(() => PushDeviceManager.debugEnableRegisterToken = true);
489482
}
490483

491484
void checkLastRequest() {
@@ -572,7 +565,6 @@ void main() {
572565
}));
573566

574567
// TODO test UpdateMachine.load starts polling loop
575-
// TODO test UpdateMachine.load calls [PushDeviceManager.registerToken]
576568
});
577569

578570
group('UpdateMachine.fetchEmojiData', () {

test/notifications/open_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:zulip/host/notifications.dart';
1010
import 'package:zulip/model/database.dart';
1111
import 'package:zulip/model/localizations.dart';
1212
import 'package:zulip/model/narrow.dart';
13+
import 'package:zulip/model/push_device.dart';
1314
import 'package:zulip/notifications/open.dart';
1415
import 'package:zulip/notifications/receive.dart';
1516
import 'package:zulip/widgets/app.dart';
@@ -80,6 +81,8 @@ void main() {
8081
testBinding.firebaseMessagingInitialToken = '012abc';
8182
addTearDown(NotificationService.debugReset);
8283
NotificationService.debugBackgroundIsolateIsLive = false;
84+
PushDeviceManager.debugAutoRegisterToken = false;
85+
addTearDown(() => PushDeviceManager.debugAutoRegisterToken = true);
8386
await NotificationService.instance.start();
8487
}
8588

0 commit comments

Comments
 (0)