fix: initialize reminder channel and clarify notification permission diagnostics

This commit is contained in:
Codex
2026-10-27 17:37:03 +08:00
parent db953b8b3d
commit fbcb7fb637
9 changed files with 165 additions and 10 deletions
@@ -7,6 +7,54 @@ import 'package:hepan_everyday/services/local_notification_service.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('fresh notification initialization creates the course category before checks', () async {
debugDefaultTargetPlatformOverride = TargetPlatform.android;
AndroidFlutterLocalNotificationsPlugin.registerWith();
const channel = MethodChannel('dexterous.com/flutter/local_notifications');
const system = MethodChannel('com.tpschedule.app/reminders');
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
final categories = <Map<Object?, Object?>>[];
messenger.setMockMethodCallHandler(channel, (call) async {
switch (call.method) {
case 'initialize':
return true;
case 'createNotificationChannel':
final data = call.arguments as Map;
categories.add({
...Map<Object?, Object?>.from(data),
// Android returns a numeric color even when no LED was set.
'ledColor': data['ledColor'] ?? 0,
});
return null;
case 'getNotificationAppLaunchDetails':
return {'notificationLaunchedApp': false};
case 'areNotificationsEnabled':
case 'canScheduleExactNotifications':
return true;
case 'getNotificationChannels':
return categories;
case 'pendingNotificationRequests':
return [];
}
return null;
});
messenger.setMockMethodCallHandler(
system,
(_) async => <Object?, Object?>{},
);
addTearDown(() {
debugDefaultTargetPlatformOverride = null;
messenger.setMockMethodCallHandler(channel, null);
messenger.setMockMethodCallHandler(system, null);
});
final gateway = PluginReminderGateway();
final diagnosis = await gateway.diagnose();
expect(diagnosis['channelImportance'], Importance.high.value);
expect(diagnosis['appNotificationsEnabled'], isTrue);
expect(diagnosis['channelExists'], isTrue);
expect(categories.single['id'], 'hepan_lessons');
});
test(
'querying future reminders does not depend on active notification access',
() async {
@@ -70,6 +70,42 @@ Future<void> _open(WidgetTester tester, _Notifications notifications) async {
}
void main() {
testWidgets(
'Android 7 does not ask the user to create an unsupported category',
(tester) async {
final notifications = _Notifications()
..diagnosis = Completer<Map<String, Object?>>();
notifications.diagnosis!.complete({
'notifications': true,
'appNotificationsEnabled': true,
'channelExists': false,
'system': {'sdk': 24},
});
await _open(tester, notifications);
await tester.tap(find.text('检查提醒'));
await tester.pumpAndSettle();
expect(find.textContaining('类别尚未建立'), findsNothing);
expect(find.text('系统通知总开关:已允许'), findsOneWidget);
},
);
testWidgets(
'diagnosis separates the application switch from the reminder category',
(tester) async {
final notifications = _Notifications()
..diagnosis = Completer<Map<String, Object?>>();
notifications.diagnosis!.complete({
'notifications': false,
'appNotificationsEnabled': false,
'channelExists': true,
'courseChannelEnabled': true,
});
await _open(tester, notifications);
await tester.tap(find.text('检查提醒'));
await tester.pumpAndSettle();
expect(find.text('系统通知总开关:未允许'), findsOneWidget);
expect(find.text('课前提醒类别:已允许'), findsOneWidget);
},
);
testWidgets('test controls distinguish immediate and background reminders', (
tester,
) async {