Files
pipelines/app/test/features/settings/settings_device_delivery_section_test.dart
T
alan 826e420423 Deliver server notifications from the Android app itself
A native foreground service keeps an authenticated websocket to the
connected server (handed over by the app on every connect), subscribes with
a replay cursor, shows each notification with kind-based channels and
grouping, opens pipelines:// links on tap, and clears taps, dismissals and
cross-device clears. It starts at boot and app launch, backs off and follows
network changes. Settings gains the delivery switch, status, notification
and battery-exemption actions, and the server's optional ntfy_enabled
toggle.
2026-11-01 02:21:22 +00:00

155 lines
5.2 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/notification_service/notification_service_bridge.dart';
import 'package:pipelines_app/features/settings/settings_device_delivery_section.dart';
import '../notification_service/fake_notification_service.dart';
Widget _host(
NotificationServiceBridge bridge, {
required bool cupertino,
Future<void> Function()? requestPermission,
}) => MaterialApp(
theme: ThemeData(
platform: cupertino ? TargetPlatform.iOS : TargetPlatform.android,
),
home: Scaffold(
body: SingleChildScrollView(
child: SettingsDeviceDeliverySection(
bridge: bridge,
requestNotificationPermission: requestPermission,
),
),
),
);
bool _switchValue(WidgetTester tester, {required bool cupertino}) {
final tile = find.byKey(DeviceDeliveryKeys.toggle);
if (cupertino) {
return tester
.widget<CupertinoSwitch>(
find.descendant(of: tile, matching: find.byType(CupertinoSwitch)),
)
.value;
}
return tester.widget<SwitchListTile>(tile).value;
}
void main() {
for (final cupertino in [false, true]) {
final style = cupertino ? 'Cupertino' : 'Material';
testWidgets('$style shows the switch, status and ntfy note', (
tester,
) async {
final service = FakeNotificationService();
await tester.pumpWidget(_host(service, cupertino: cupertino));
await tester.pumpAndSettle();
expect(find.text(deviceDeliverySwitchLabel), findsOneWidget);
expect(_switchValue(tester, cupertino: cupertino), isTrue);
expect(find.text('Connected'), findsOneWidget);
expect(find.text(deviceDeliveryNote), findsOneWidget);
expect(find.text(deviceDeliveryBatteryLabel), findsOneWidget);
expect(find.byKey(DeviceDeliveryKeys.permission), findsNothing);
});
testWidgets('$style switching off stops delivery', (tester) async {
final service = FakeNotificationService();
await tester.pumpWidget(_host(service, cupertino: cupertino));
await tester.pumpAndSettle();
await tester.tap(find.byKey(DeviceDeliveryKeys.toggle));
await tester.pumpAndSettle();
expect(service.calls.last.$1, 'setEnabled');
expect(service.calls.last.$2, {'enabled': false});
expect(_switchValue(tester, cupertino: cupertino), isFalse);
expect(find.text('Off'), findsOneWidget);
// The grants only matter while delivery is on.
expect(find.text(deviceDeliveryBatteryLabel), findsNothing);
});
}
testWidgets('live status pushed by the service is shown', (tester) async {
final service = FakeNotificationService();
await tester.pumpWidget(_host(service, cupertino: false));
await tester.pumpAndSettle();
service.push(service.copy(state: DeliveryState.reconnecting));
await tester.pumpAndSettle();
expect(find.text('Reconnecting…'), findsOneWidget);
});
testWidgets('allow background delivery asks for the exemption', (
tester,
) async {
final service = FakeNotificationService();
await tester.pumpWidget(_host(service, cupertino: false));
await tester.pumpAndSettle();
await tester.tap(find.text(deviceDeliveryBatteryLabel));
await tester.pumpAndSettle();
expect(
service.calls.map((call) => call.$1),
contains('requestBatteryExemption'),
);
expect(
find.text('Allowed: battery optimisation will not pause delivery'),
findsOneWidget,
);
});
testWidgets('blocked notifications offer the permission request', (
tester,
) async {
var asked = 0;
final service = FakeNotificationService();
service.current = service.copy(notificationsAllowed: false);
await tester.pumpWidget(
_host(
service,
cupertino: false,
requestPermission: () async {
asked++;
service.current = service.copy(notificationsAllowed: true);
},
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(DeviceDeliveryKeys.permission));
await tester.pumpAndSettle();
expect(asked, 1);
expect(find.byKey(DeviceDeliveryKeys.permission), findsNothing);
});
testWidgets('switching on asks for notifications when blocked', (
tester,
) async {
var asked = 0;
final service = FakeNotificationService();
service.current = service.copy(
enabled: false,
state: DeliveryState.off,
notificationsAllowed: false,
);
await tester.pumpWidget(
_host(service, cupertino: false, requestPermission: () async => asked++),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(DeviceDeliveryKeys.toggle));
await tester.pumpAndSettle();
expect(asked, 1);
expect(service.calls.last.$1, 'setEnabled');
expect(service.calls.last.$2, {'enabled': true});
});
testWidgets('a failing platform is reported, not hidden', (tester) async {
await tester.pumpWidget(_host(_FailingService(), cupertino: false));
await tester.pumpAndSettle();
expect(find.byKey(DeviceDeliveryKeys.error), findsOneWidget);
});
}
class _FailingService extends FakeNotificationService {
@override
Future<NotificationServiceStatus> status() async =>
throw StateError('no service');
}