Files
pipelines/app/test/features/attention/notify/notification_permission_test.dart
T

104 lines
3.5 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/notify/notification_controller.dart';
import 'package:pipelines_app/features/attention/notify/notification_models.dart';
import 'package:pipelines_app/features/attention/notify/notification_store.dart';
import 'notification_test_support.dart';
void main() {
group('permission is only ever the platform\'s answer', () {
test('an unsupported platform is never asked and never posts', () async {
final bridge = FakeNotificationBridge(supported: false);
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
expect(notify.permission, NotificationPermissionState.unsupported);
expect(
await notify.requestPermission(),
NotificationPermissionState.unsupported,
);
expect(bridge.requests, 0);
expect(
notify.publishTransitions([transitionFor(1)], qualifying: {'1:Failed'}),
0,
);
expect(bridge.shown, isEmpty);
});
test('an insecure page reports needsSecureContext', () async {
final bridge = FakeNotificationBridge(secureContext: false);
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
expect(notify.permission, NotificationPermissionState.needsSecureContext);
await notify.requestPermission();
expect(bridge.requests, 0);
});
test('a default page is asked once and follows the real answer', () async {
final store = MemoryNotificationStore();
final bridge = FakeNotificationBridge(answer: 'granted');
final notify = NotificationController(store: store, bridge: bridge);
addTearDown(notify.dispose);
await notify.load();
expect(notify.permission, NotificationPermissionState.notRequested);
expect(notify.canNotify, isFalse);
final state = await notify.requestPermission();
expect(bridge.requests, 1);
expect(state, NotificationPermissionState.granted);
expect(notify.canNotify, isTrue);
expect(notify.asked, isTrue);
await settle();
expect(store.stored, contains('"asked":true'));
});
test('a denied answer is never re-prompted', () async {
final bridge = FakeNotificationBridge(permission: 'denied');
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
expect(notify.permission, NotificationPermissionState.denied);
expect(
await notify.requestPermission(),
NotificationPermissionState.denied,
);
expect(bridge.requests, 0);
expect(
notify.publishTransitions([transitionFor(1)], qualifying: {'1:Failed'}),
0,
);
expect(bridge.shown, isEmpty);
});
test(
'a prompt that grants nothing keeps the honest default wording',
() async {
final bridge = FakeNotificationBridge(answer: 'default');
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
final state = await notify.requestPermission();
expect(state, NotificationPermissionState.notRequested);
expect(notify.asked, isTrue);
expect(notify.canNotify, isFalse);
},
);
});
}