import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/core/transport/connection_state.dart'; import 'package:pipelines_app/features/templates/ui/template_create_wording.dart'; import 'template_create_test_support.dart'; /// A 360×640 phone: the short viewport a large text scale runs out of room on. void _shortPhone(WidgetTester tester) { tester.view.devicePixelRatio = 1; tester.view.physicalSize = const Size(360, 640); addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); } /// The Material scrollable region between the title and the pinned actions. Rect _contentViewport(WidgetTester tester) => tester.getRect( find .descendant( of: find.byType(AlertDialog), matching: find.byType(SingleChildScrollView), ) .first, ); void _expectInside(Rect viewport, Rect child, String what) { expect( child.top, greaterThanOrEqualTo(viewport.top - 0.5), reason: '$what is scrolled above the visible content', ); expect( child.bottom, lessThanOrEqualTo(viewport.bottom + 0.5), reason: '$what is below the visible content', ); } void main() { testWidgets( 'at 2x on a short phone the full label and a refusal are visible without ' 'scrolling', (tester) async { _shortPhone(tester); const refusal = 'invalid definition: fixture stage "añadir" has no type'; final gateway = FakeTemplateCreateGateway( (type, fields) async => throw const ProtocolError(refusal), ); await tester.pumpWidget( templateCreateApp( TemplateCreateHost(gateway: gateway, onCreated: (_) {}), textScale: 2, ), ); await tester.tap(find.byKey(templateCreateOpenKey)); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); final label = find.text(templateCreateNameLabel); expect(label, findsOneWidget); final paragraph = tester.renderObject(label); expect(paragraph.text.toPlainText(), templateCreateNameLabel); expect( paragraph.didExceedMaxLines, isFalse, reason: 'the required suffix must not be ellipsized', ); final unscaled = TextPainter( text: paragraph.text, textDirection: paragraph.textDirection, textScaler: paragraph.textScaler, )..layout(); expect( unscaled.width, greaterThan(paragraph.size.width), reason: 'at 2x the label wraps instead of shrinking to one line', ); unscaled.dispose(); await tester.enterText( find.byKey(templateCreateNameKey), 'Narrow attempt', ); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); expect(find.text(refusal), findsOneWidget); _expectInside( _contentViewport(tester), tester.getRect(find.byKey(templateCreateErrorKey)), 'the validation refusal', ); expect(gateway.calls.map((call) => call.type), ['validate']); }, ); testWidgets('at 2x on a short phone the accepted line is visible without ' 'scrolling', (tester) async { _shortPhone(tester); final gateway = FakeTemplateCreateGateway(acceptedTemplateCreate); final created = []; await tester.pumpWidget( templateCreateApp( TemplateCreateHost(gateway: gateway, onCreated: created.add), textScale: 2, ), ); await tester.tap(find.byKey(templateCreateOpenKey)); await tester.pumpAndSettle(); await tester.enterText(find.byKey(templateCreateNameKey), 'Probe release'); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect( find.byKey(templateCreateSuccessKey), findsOneWidget, reason: 'the success line is shown, not merely off-screen', ); _expectInside( _contentViewport(tester), tester.getRect(find.byKey(templateCreateSuccessKey)), 'the accepted create line', ); expect(created.single, {'id': 1, 'name': 'Server name'}); }); testWidgets('a refusal is revealed after the content was scrolled away', ( tester, ) async { tester.view.devicePixelRatio = 1; tester.view.physicalSize = const Size(320, 480); addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); const refusal = 'invalid stage'; final gateway = FakeTemplateCreateGateway( (type, fields) async => throw const ProtocolError(refusal), ); await tester.pumpWidget( templateCreateApp( TemplateCreateHost(gateway: gateway, onCreated: (_) {}), textScale: 3, ), ); await tester.tap(find.byKey(templateCreateOpenKey)); await tester.pumpAndSettle(); await tester.enterText(find.byKey(templateCreateNameKey), 'Scrolled away'); await tester.pump(); // Scroll the prompt content to its end, as a reader would. final position = tester .state( find .descendant( of: find.byType(SingleChildScrollView), matching: find.byType(Scrollable), ) .first, ) .position; expect( position.maxScrollExtent, greaterThan(0), reason: 'the 3x content must overflow for this check to mean anything', ); position.jumpTo(position.maxScrollExtent); await tester.pumpAndSettle(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); expect(find.text(refusal), findsOneWidget); final viewport = _contentViewport(tester); final feedback = tester.getRect(find.byKey(templateCreateErrorKey)); // At 3x the message can be taller than the viewport; its start must still // be brought back into view after the content was scrolled away. expect( feedback.top, greaterThanOrEqualTo(viewport.top - 0.5), reason: 'the refusal is scrolled above the visible content', ); expect( feedback.top, lessThan(viewport.bottom), reason: 'the refusal start is below the visible content', ); }); testWidgets('the name field is labelled for screen readers on both hosts', ( tester, ) async { final handle = tester.ensureSemantics(); for (final cupertino in [false, true]) { await tester.pumpWidget( templateCreateApp( TemplateCreateHost( gateway: FakeTemplateCreateGateway(acceptedTemplateCreate), onCreated: (_) {}, cupertino: cupertino, ), cupertino: cupertino, textScale: 2, ), ); await tester.tap(find.byKey(templateCreateOpenKey)); await tester.pumpAndSettle(); await tester.enterText(find.byKey(templateCreateNameKey), 'Scaled'); await tester.pump(); final data = tester .getSemantics(find.byKey(templateCreateNameKey)) .getSemanticsData(); expect(data.label, templateCreateNameLabel); expect(data.value, 'Scaled'); expect(data.flagsCollection.isTextField, isTrue); } handle.dispose(); }); }