Files
pipelines/app/test/features/attention/ui/notification_permission_tile_test.dart
T

139 lines
4.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/notify/notification_models.dart';
import 'package:pipelines_app/features/attention/ui/notification_permission_tile.dart';
import 'needs_you_test_support.dart';
/// The tile inside a normal page, so both native hosts can draw it. The page
/// scrolls like the Settings list does, so the tile is never given a fixed
/// height to overflow.
Widget _page({
required NotificationPermissionState state,
bool cupertino = false,
bool asked = false,
VoidCallback? onRequest,
}) => attentionApp(
Scaffold(
body: SingleChildScrollView(
child: NotificationPermissionTile(
state: state,
asked: asked,
onRequest: onRequest,
),
),
),
cupertino: cupertino,
);
void _noop() {}
void main() {
for (final native in [false, true]) {
final host = native ? 'Cupertino' : 'Material';
testWidgets('$host shows the state\'s own honest copy', (tester) async {
final cases = <NotificationPermissionState, String>{
NotificationPermissionState.unsupported: notificationUnsupportedCopy,
NotificationPermissionState.needsSecureContext:
notificationInsecureCopy,
NotificationPermissionState.granted: notificationGrantedCopy,
NotificationPermissionState.denied: notificationDeniedCopy,
NotificationPermissionState.notRequested: notificationDefaultCopy,
};
for (final entry in cases.entries) {
await tester.pumpWidget(
_page(state: entry.key, cupertino: native, onRequest: _noop),
);
// The Settings section owns the heading; the tile is its body.
expect(find.text(notificationSectionTitle), findsNothing);
expect(find.text(notificationRationale), findsOneWidget);
expect(find.text(entry.value), findsOneWidget, reason: '${entry.key}');
expect(
find.byKey(notificationEnableKey),
entry.key == NotificationPermissionState.notRequested
? findsOneWidget
: findsNothing,
reason: '${entry.key}',
);
}
});
testWidgets('$host shows the asked wording without an action', (
tester,
) async {
await tester.pumpWidget(
_page(
state: NotificationPermissionState.notRequested,
asked: true,
cupertino: native,
),
);
expect(find.text(notificationDefaultAskedCopy), findsOneWidget);
expect(find.text(notificationDefaultCopy), findsNothing);
});
testWidgets('$host Enable runs the explicit request', (tester) async {
var requested = 0;
await tester.pumpWidget(
_page(
state: NotificationPermissionState.notRequested,
cupertino: native,
onRequest: () => requested++,
),
);
await tester.tap(find.byKey(notificationEnableKey));
await tester.pump();
expect(requested, 1);
});
testWidgets('$host tile wraps at 320 logical px and 2x in dark', (
tester,
) async {
tester.view.devicePixelRatio = 2;
tester.view.physicalSize = const Size(640, 1280);
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
// The undecided state is the hardest: it carries the Enable action as well
// as the longest copy, so the section title must still have room to read.
await tester.pumpWidget(
attentionApp(
Scaffold(
body: SingleChildScrollView(
child: NotificationPermissionTile(
state: NotificationPermissionState.notRequested,
onRequest: _noop,
),
),
),
cupertino: native,
dark: true,
textScale: 2,
),
);
expect(tester.takeException(), isNull);
final rationale = tester.renderObject<RenderParagraph>(
find.text(notificationRationale),
);
expect(
rationale.size.width,
greaterThan(100),
reason: 'the action must not squeeze the section copy',
);
final body = tester.renderObject<RenderParagraph>(
find.text(notificationDefaultCopy),
);
expect(body.didExceedMaxLines, isFalse);
expect(find.byKey(notificationEnableKey), findsOneWidget);
final rect = tester.getRect(find.byKey(notificationStateKey));
expect(rect.left, greaterThanOrEqualTo(0));
expect(rect.right, lessThanOrEqualTo(320));
expect(tester.takeException(), isNull);
});
}
}