Unverified Commit 7ba1d5ec authored by pdblasi-google's avatar pdblasi-google Committed by GitHub

Updates `flutter/test/widgets` to no longer reference `TestWindow` (#122354)

Updates `flutter/test/widgets` to no longer reference `TestWindow`
parent 47c0723f
...@@ -146,7 +146,7 @@ Future<void> _expectColors(WidgetTester tester, Finder finder, Set<Color> allowe ...@@ -146,7 +146,7 @@ Future<void> _expectColors(WidgetTester tester, Finder finder, Set<Color> allowe
assert(position.dx.round() < image.width); assert(position.dx.round() < image.width);
assert(position.dy.round() >= 0); assert(position.dy.round() >= 0);
assert(position.dy.round() < image.height); assert(position.dy.round() < image.height);
final Offset precisePosition = position * binding.window.devicePixelRatio; final Offset precisePosition = position * tester.view.devicePixelRatio;
final Color actual = _getPixel(bytes, precisePosition.dx.round(), precisePosition.dy.round(), image.width); final Color actual = _getPixel(bytes, precisePosition.dx.round(), precisePosition.dy.round(), image.width);
expect(actual, expected, reason: 'Pixel at $position is $actual but expected $expected.'); expect(actual, expected, reason: 'Pixel at $position is $actual but expected $expected.');
}); });
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:convert' show jsonDecode; import 'dart:convert' show jsonDecode;
import 'dart:ui' as ui;
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
...@@ -44,26 +43,6 @@ class _MatchesMethodCall extends Matcher { ...@@ -44,26 +43,6 @@ class _MatchesMethodCall extends Matcher {
} }
} }
// Used to set window.viewInsets since the real ui.WindowPadding has only a
// private constructor.
class _TestViewPadding implements ui.ViewPadding {
const _TestViewPadding({
required this.bottom,
});
@override
final double bottom;
@override
double get top => 0.0;
@override
double get left => 0.0;
@override
double get right => 0.0;
}
late TextEditingController controller; late TextEditingController controller;
final FocusNode focusNode = FocusNode(debugLabel: 'EditableText Node'); final FocusNode focusNode = FocusNode(debugLabel: 'EditableText Node');
final FocusScopeNode focusScopeNode = FocusScopeNode(debugLabel: 'EditableText Scope Node'); final FocusScopeNode focusScopeNode = FocusScopeNode(debugLabel: 'EditableText Scope Node');
...@@ -134,6 +113,8 @@ void main() { ...@@ -134,6 +113,8 @@ void main() {
testWidgets('Text with selection can be shown on the screen when the keyboard shown', (WidgetTester tester) async { testWidgets('Text with selection can be shown on the screen when the keyboard shown', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/119628 // Regression test for https://github.com/flutter/flutter/issues/119628
addTearDown(tester.view.reset);
final ScrollController scrollController = ScrollController(); final ScrollController scrollController = ScrollController();
final TextEditingController textController = TextEditingController.fromValue( final TextEditingController textController = TextEditingController.fromValue(
const TextEditingValue(text: 'I love flutter'), const TextEditingValue(text: 'I love flutter'),
...@@ -164,8 +145,7 @@ void main() { ...@@ -164,8 +145,7 @@ void main() {
await tester.pumpWidget(widget); await tester.pumpWidget(widget);
await tester.showKeyboard(find.byType(EditableText)); await tester.showKeyboard(find.byType(EditableText));
TestWidgetsFlutterBinding.instance.window.viewInsetsTestValue = const _TestViewPadding(bottom: 500); tester.view.viewInsets = const FakeViewPadding(bottom: 500);
addTearDown(TestWidgetsFlutterBinding.instance.window.clearViewInsetsTestValue);
textController.selection = TextSelection( textController.selection = TextSelection(
baseOffset: 0, baseOffset: 0,
extentOffset: textController.text.length, extentOffset: textController.text.length,
...@@ -173,13 +153,15 @@ void main() { ...@@ -173,13 +153,15 @@ void main() {
await tester.pump(); await tester.pump();
// The offset of the scrollController should change immediately after window changes its metrics. // The offset of the scrollController should change immediately after view changes its metrics.
final double offsetAfter = scrollController.offset; final double offsetAfter = scrollController.offset;
expect(offsetAfter, isNot(0.0)); expect(offsetAfter, isNot(0.0));
}); });
// Related issue: https://github.com/flutter/flutter/issues/98115 // Related issue: https://github.com/flutter/flutter/issues/98115
testWidgets('ScheduleShowCaretOnScreen with no animation when the window changes metrics', (WidgetTester tester) async { testWidgets('ScheduleShowCaretOnScreen with no animation when the view changes metrics', (WidgetTester tester) async {
addTearDown(tester.view.reset);
final ScrollController scrollController = ScrollController(); final ScrollController scrollController = ScrollController();
final Widget widget = MaterialApp( final Widget widget = MaterialApp(
home: Scaffold( home: Scaffold(
...@@ -215,11 +197,10 @@ void main() { ...@@ -215,11 +197,10 @@ void main() {
); );
await tester.pumpWidget(widget); await tester.pumpWidget(widget);
await tester.showKeyboard(find.byType(EditableText)); await tester.showKeyboard(find.byType(EditableText));
TestWidgetsFlutterBinding.instance.window.viewInsetsTestValue = const _TestViewPadding(bottom: 500); tester.view.viewInsets = const FakeViewPadding(bottom: 500);
addTearDown(TestWidgetsFlutterBinding.instance.window.clearViewInsetsTestValue);
await tester.pump(); await tester.pump();
// The offset of the scrollController should change immediately after window changes its metrics. // The offset of the scrollController should change immediately after view changes its metrics.
final double offsetAfter = scrollController.offset; final double offsetAfter = scrollController.offset;
expect(offsetAfter, isNot(0.0)); expect(offsetAfter, isNot(0.0));
}); });
...@@ -1841,7 +1822,7 @@ void main() { ...@@ -1841,7 +1822,7 @@ void main() {
}); });
testWidgets('toolbar hidden on mobile when orientation changes', (WidgetTester tester) async { testWidgets('toolbar hidden on mobile when orientation changes', (WidgetTester tester) async {
addTearDown(tester.binding.window.clearPhysicalSizeTestValue); addTearDown(tester.view.reset);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
...@@ -1871,7 +1852,7 @@ void main() { ...@@ -1871,7 +1852,7 @@ void main() {
expect(find.text('Paste'), findsOneWidget); expect(find.text('Paste'), findsOneWidget);
// Hide the menu by changing orientation. // Hide the menu by changing orientation.
tester.binding.window.physicalSizeTestValue = const Size(1800.0, 2400.0); tester.view.physicalSize = const Size(1800.0, 2400.0);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(find.text('Paste'), findsNothing); expect(find.text('Paste'), findsNothing);
...@@ -5176,9 +5157,9 @@ void main() { ...@@ -5176,9 +5157,9 @@ void main() {
}); });
testWidgets('selection rects are sent when they change', (WidgetTester tester) async { testWidgets('selection rects are sent when they change', (WidgetTester tester) async {
addTearDown(tester.binding.window.clearPhysicalSizeTestValue); addTearDown(tester.view.reset);
// Ensure selection rects are sent on iPhone (using SE 3rd gen size) // Ensure selection rects are sent on iPhone (using SE 3rd gen size)
tester.binding.window.physicalSizeTestValue = const Size(750.0, 1334.0); tester.view.physicalSize = const Size(750.0, 1334.0);
final List<List<SelectionRect>> log = <List<SelectionRect>>[]; final List<List<SelectionRect>> log = <List<SelectionRect>>[];
SystemChannels.textInput.setMockMethodCallHandler((MethodCall methodCall) async { SystemChannels.textInput.setMockMethodCallHandler((MethodCall methodCall) async {
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:ui' as ui; import 'dart:ui' as ui;
import 'dart:ui' show ViewPadding;
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
...@@ -184,25 +183,6 @@ class MyStatefulWidgetState extends State<MyStatefulWidget> { ...@@ -184,25 +183,6 @@ class MyStatefulWidgetState extends State<MyStatefulWidget> {
Widget build(BuildContext context) => Text(widget.value); Widget build(BuildContext context) => Text(widget.value);
} }
class FakeViewPadding implements ViewPadding {
const FakeViewPadding({
this.left = 0.0,
this.top = 0.0,
this.right = 0.0,
this.bottom = 0.0,
});
@override
final double left;
@override
final double top;
@override
final double right;
@override
final double bottom;
}
Future<void> main() async { Future<void> main() async {
final ui.Image testImage = await createTestImage(); final ui.Image testImage = await createTestImage();
...@@ -3090,14 +3070,16 @@ Future<void> main() async { ...@@ -3090,14 +3070,16 @@ Future<void> main() async {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(tester.takeException(), isNull); expect(tester.takeException(), isNull);
}); });
testWidgets('smooth transition between different incoming data', (WidgetTester tester) async { testWidgets('smooth transition between different incoming data', (WidgetTester tester) async {
addTearDown(tester.view.reset);
final GlobalKey<NavigatorState> navigatorKey = GlobalKey(); final GlobalKey<NavigatorState> navigatorKey = GlobalKey();
const Key imageKey1 = Key('image1'); const Key imageKey1 = Key('image1');
const Key imageKey2 = Key('image2'); const Key imageKey2 = Key('image2');
final TestImageProvider imageProvider = TestImageProvider(testImage); final TestImageProvider imageProvider = TestImageProvider(testImage);
final TestWidgetsFlutterBinding testBinding = tester.binding;
testBinding.window.paddingTestValue = const FakeViewPadding(top: 50); tester.view.padding = const FakeViewPadding(top: 50);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
...@@ -3152,10 +3134,7 @@ Future<void> main() async { ...@@ -3152,10 +3134,7 @@ Future<void> main() async {
expect(tester.getTopLeft(find.byType(Image)).dy, moreOrLessEquals(forwardRest, epsilon: 0.1)); expect(tester.getTopLeft(find.byType(Image)).dy, moreOrLessEquals(forwardRest, epsilon: 0.1));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(tester.getTopLeft(find.byType(Image)).dy, moreOrLessEquals(forwardRest, epsilon: 0.1)); expect(tester.getTopLeft(find.byType(Image)).dy, moreOrLessEquals(forwardRest, epsilon: 0.1));
});
testBinding.window.clearAllTestValues();
},
);
} }
class TestDependencies extends StatelessWidget { class TestDependencies extends StatelessWidget {
......
...@@ -1190,6 +1190,8 @@ void main() { ...@@ -1190,6 +1190,8 @@ void main() {
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS, TargetPlatform.linux, TargetPlatform.windows })); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS, TargetPlatform.linux, TargetPlatform.windows }));
testWidgets('viewport changes size', (WidgetTester tester) async { testWidgets('viewport changes size', (WidgetTester tester) async {
addTearDown(tester.view.reset);
final TransformationController transformationController = TransformationController(); final TransformationController transformationController = TransformationController();
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
...@@ -1222,8 +1224,7 @@ void main() { ...@@ -1222,8 +1224,7 @@ void main() {
expect(transformationController.value, equals(Matrix4.identity())); expect(transformationController.value, equals(Matrix4.identity()));
// Shrink the size of the screen. // Shrink the size of the screen.
tester.binding.window.physicalSizeTestValue = const Size(100.0, 100.0); tester.view.physicalSize = const Size(100.0, 100.0);
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
await tester.pump(); await tester.pump();
// Attempting to drag to pan still doesn't work, because the image has // Attempting to drag to pan still doesn't work, because the image has
......
...@@ -142,7 +142,7 @@ void main() { ...@@ -142,7 +142,7 @@ void main() {
// Set the starting viewportDimension to 0.0 // Set the starting viewportDimension to 0.0
await tester.binding.setSurfaceSize(Size.zero); await tester.binding.setSurfaceSize(Size.zero);
final MediaQueryData mediaQueryData = MediaQueryData.fromView(tester.binding.window); final MediaQueryData mediaQueryData = MediaQueryData.fromView(tester.view);
Widget build(Size size) { Widget build(Size size) {
return MediaQuery( return MediaQuery(
...@@ -1048,7 +1048,7 @@ void main() { ...@@ -1048,7 +1048,7 @@ void main() {
viewportDimension: 25.0, viewportDimension: 25.0,
axisDirection: AxisDirection.right, axisDirection: AxisDirection.right,
viewportFraction: 1.0, viewportFraction: 1.0,
devicePixelRatio: tester.binding.window.devicePixelRatio, devicePixelRatio: tester.view.devicePixelRatio,
); );
expect(page.page, 6); expect(page.page, 6);
final PageMetrics page2 = page.copyWith( final PageMetrics page2 = page.copyWith(
......
...@@ -2599,6 +2599,8 @@ void main() { ...@@ -2599,6 +2599,8 @@ void main() {
); );
testWidgets('PlatformViewLink includes offset in create call when using texture layer', (WidgetTester tester) async { testWidgets('PlatformViewLink includes offset in create call when using texture layer', (WidgetTester tester) async {
addTearDown(tester.view.reset);
late FakeAndroidViewController controller; late FakeAndroidViewController controller;
final PlatformViewLink platformViewLink = PlatformViewLink( final PlatformViewLink platformViewLink = PlatformViewLink(
...@@ -2622,8 +2624,8 @@ void main() { ...@@ -2622,8 +2624,8 @@ void main() {
}, },
); );
TestWidgetsFlutterBinding.instance.window.physicalSizeTestValue = const Size(400, 200); tester.view.physicalSize= const Size(400, 200);
TestWidgetsFlutterBinding.instance.window.devicePixelRatioTestValue = 1.0; tester.view.devicePixelRatio = 1.0;
await tester.pumpWidget( await tester.pumpWidget(
Container( Container(
...@@ -2638,9 +2640,6 @@ void main() { ...@@ -2638,9 +2640,6 @@ void main() {
); );
expect(controller.createPosition, const Offset(150, 75)); expect(controller.createPosition, const Offset(150, 75));
TestWidgetsFlutterBinding.instance.window.clearPhysicalSizeTestValue();
TestWidgetsFlutterBinding.instance.window.clearDevicePixelRatioTestValue();
}); });
testWidgets( testWidgets(
......
...@@ -2038,7 +2038,9 @@ void main() { ...@@ -2038,7 +2038,9 @@ void main() {
expect(find.byType(RawScrollbar), isNot(paints..rect())); // Hide the bar. expect(find.byType(RawScrollbar), isNot(paints..rect())); // Hide the bar.
}); });
testWidgets('The bar can show or hide when the window size change', (WidgetTester tester) async { testWidgets('The bar can show or hide when the view size change', (WidgetTester tester) async {
addTearDown(tester.view.reset);
final ScrollController scrollController = ScrollController(); final ScrollController scrollController = ScrollController();
Widget buildFrame() { Widget buildFrame() {
return Directionality( return Directionality(
...@@ -2061,21 +2063,19 @@ void main() { ...@@ -2061,21 +2063,19 @@ void main() {
), ),
); );
} }
tester.binding.window.physicalSizeTestValue = const Size(800.0, 600.0); tester.view.physicalSize = const Size(800.0, 600.0);
tester.binding.window.devicePixelRatioTestValue = 1; tester.view.devicePixelRatio = 1;
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
addTearDown(tester.binding.window.clearDevicePixelRatioTestValue);
await tester.pumpWidget(buildFrame()); await tester.pumpWidget(buildFrame());
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(scrollController.offset, 0.0); expect(scrollController.offset, 0.0);
expect(find.byType(RawScrollbar), isNot(paints..rect())); // Not shown. expect(find.byType(RawScrollbar), isNot(paints..rect())); // Not shown.
tester.binding.window.physicalSizeTestValue = const Size(800.0, 599.0); tester.view.physicalSize = const Size(800.0, 599.0);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(find.byType(RawScrollbar), paints..rect()..rect()); // Show the bar. expect(find.byType(RawScrollbar), paints..rect()..rect()); // Show the bar.
tester.binding.window.physicalSizeTestValue = const Size(800.0, 600.0); tester.view.physicalSize = const Size(800.0, 600.0);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(find.byType(RawScrollbar), isNot(paints..rect())); // Not shown. expect(find.byType(RawScrollbar), isNot(paints..rect())); // Not shown.
}); });
......
...@@ -1668,6 +1668,8 @@ void main() { ...@@ -1668,6 +1668,8 @@ void main() {
}); });
testWidgets('toolbar is hidden on mobile when orientation changes', (WidgetTester tester) async { testWidgets('toolbar is hidden on mobile when orientation changes', (WidgetTester tester) async {
addTearDown(tester.view.reset);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
home: SelectableRegion( home: SelectableRegion(
...@@ -1677,7 +1679,6 @@ void main() { ...@@ -1677,7 +1679,6 @@ void main() {
), ),
), ),
); );
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
final RenderParagraph paragraph1 = tester.renderObject<RenderParagraph>(find.descendant(of: find.text('How are you?'), matching: find.byType(RichText))); final RenderParagraph paragraph1 = tester.renderObject<RenderParagraph>(find.descendant(of: find.text('How are you?'), matching: find.byType(RichText)));
final TestGesture gesture = await tester.startGesture(textOffsetToPosition(paragraph1, 6)); // at the 'r' final TestGesture gesture = await tester.startGesture(textOffsetToPosition(paragraph1, 6)); // at the 'r'
...@@ -1690,7 +1691,7 @@ void main() { ...@@ -1690,7 +1691,7 @@ void main() {
expect(find.text('Copy'), findsOneWidget); expect(find.text('Copy'), findsOneWidget);
// Hide the toolbar by changing orientation. // Hide the toolbar by changing orientation.
tester.binding.window.physicalSizeTestValue = const Size(1800.0, 2400.0); tester.view.physicalSize = const Size(1800.0, 2400.0);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(find.text('Copy'), findsNothing); expect(find.text('Copy'), findsNothing);
......
...@@ -264,12 +264,11 @@ void main() { ...@@ -264,12 +264,11 @@ void main() {
}, skip: kIsWeb); // TODO(jonahwilliams): https://github.com/flutter/flutter/issues/106689 }, skip: kIsWeb); // TODO(jonahwilliams): https://github.com/flutter/flutter/issues/106689
testWidgets('SnapshotWidget should have same result when enabled', (WidgetTester tester) async { testWidgets('SnapshotWidget should have same result when enabled', (WidgetTester tester) async {
tester.binding.window addTearDown(tester.view.reset);
..physicalSizeTestValue = const Size(10, 10)
..devicePixelRatioTestValue = 1; tester.view
addTearDown(() => tester.binding.window ..physicalSize = const Size(10, 10)
..clearPhysicalSizeTestValue() ..devicePixelRatio = 1;
..clearDevicePixelRatioTestValue());
const ValueKey<String> repaintBoundaryKey = ValueKey<String>('boundary'); const ValueKey<String> repaintBoundaryKey = ValueKey<String>('boundary');
final SnapshotController controller = SnapshotController(); final SnapshotController controller = SnapshotController();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment