flutter/packages/flutter_test/test/window_test.dart
Casey Hillers 1f42612323
Revert PRs relating to single window assumption (#122369)
* Revert "Remove references to BindingBase.window (#122119)"

This reverts commit c7681f00cf.

* Revert "Remove another reference to BindingBase.window (#122341)"

This reverts commit 6ec4445063.

* Revert "Reland (2): Removes single window assumptions from `flutter_test` (#122233)"

This reverts commit eb3d317ea0.

* Revert "Remove single view assumption from TestViewConfiguration (#122352)"

This reverts commit 927289fb4e.

* Revert "Updates `flutter/test/cupertino` to no longer use `TestWindow` (#122325)"

This reverts commit 67e17e45f0.

* Revert "Updates `flutter/test/gestures` to no longer reference `TestWindow` (#122327)"

This reverts commit c2a5111cc0.

* Revert "Updates `flutter/test/rendering` to no longer use `TestWindow` (#122347)"

This reverts commit 28b65e089b.

* Revert "Updates `flutter_localizations/test` to stop using `TestWindow` (#122321)"

This reverts commit 01367d52d7.
2023-03-09 22:53:38 -08:00

290 lines
10 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' as ui show window;
import 'dart:ui' show AccessibilityFeatures, Brightness, Locale, PlatformDispatcher, SemanticsUpdate, SingletonFlutterWindow, Size, ViewPadding;
import 'package:flutter/semantics.dart' show SemanticsUpdateBuilder;
import 'package:flutter/widgets.dart' show WidgetsBinding, WidgetsBindingObserver;
import 'package:flutter_test/flutter_test.dart';
void main() {
test('TestWindow can handle new methods without breaking', () {
final dynamic testWindow = TestWindow(window: ui.window);
// ignore: avoid_dynamic_calls
expect(testWindow.someNewProperty, null);
});
testWidgets('TestWindow can fake device pixel ratio', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<double>(
tester: tester,
realValue: ui.window.devicePixelRatio,
fakeValue: 2.5,
propertyRetriever: () {
return WidgetsBinding.instance.window.devicePixelRatio;
},
propertyFaker: (TestWidgetsFlutterBinding binding, double fakeValue) {
binding.window.devicePixelRatioTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake physical size', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<Size>(
tester: tester,
realValue: ui.window.physicalSize,
fakeValue: const Size(50, 50),
propertyRetriever: () {
return WidgetsBinding.instance.window.physicalSize;
},
propertyFaker: (TestWidgetsFlutterBinding binding, Size fakeValue) {
binding.window.physicalSizeTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake view insets', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<ViewPadding>(
tester: tester,
realValue: ui.window.viewInsets,
fakeValue: const FakeViewPadding(),
propertyRetriever: () {
return WidgetsBinding.instance.window.viewInsets;
},
propertyFaker: (TestWidgetsFlutterBinding binding, ViewPadding fakeValue) {
binding.window.viewInsetsTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake padding', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<ViewPadding>(
tester: tester,
realValue: ui.window.padding,
fakeValue: const FakeViewPadding(),
propertyRetriever: () {
return WidgetsBinding.instance.window.padding;
},
propertyFaker: (TestWidgetsFlutterBinding binding, ViewPadding fakeValue) {
binding.window.paddingTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake locale', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<Locale>(
tester: tester,
realValue: ui.window.locale,
fakeValue: const Locale('fake_language_code'),
propertyRetriever: () {
return WidgetsBinding.instance.window.locale;
},
propertyFaker: (TestWidgetsFlutterBinding binding, Locale fakeValue) {
binding.window.localeTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake locales', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<List<Locale>>(
tester: tester,
realValue: ui.window.locales,
fakeValue: <Locale>[const Locale('fake_language_code')],
propertyRetriever: () {
return WidgetsBinding.instance.window.locales;
},
propertyFaker: (TestWidgetsFlutterBinding binding, List<Locale> fakeValue) {
binding.window.localesTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake text scale factor', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<double>(
tester: tester,
realValue: ui.window.textScaleFactor,
fakeValue: 2.5,
propertyRetriever: () {
return WidgetsBinding.instance.window.textScaleFactor;
},
propertyFaker: (TestWidgetsFlutterBinding binding, double fakeValue) {
binding.window.textScaleFactorTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake clock format', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<bool>(
tester: tester,
realValue: ui.window.alwaysUse24HourFormat,
fakeValue: !ui.window.alwaysUse24HourFormat,
propertyRetriever: () {
return WidgetsBinding.instance.window.alwaysUse24HourFormat;
},
propertyFaker: (TestWidgetsFlutterBinding binding, bool fakeValue) {
binding.window.alwaysUse24HourFormatTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake brieflyShowPassword', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<bool>(
tester: tester,
realValue: ui.window.brieflyShowPassword,
fakeValue: !ui.window.brieflyShowPassword,
propertyRetriever: () => WidgetsBinding.instance.window.brieflyShowPassword,
propertyFaker: (TestWidgetsFlutterBinding binding, bool fakeValue) {
binding.window.brieflyShowPasswordTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake default route name', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<String>(
tester: tester,
realValue: ui.window.defaultRouteName,
fakeValue: 'fake_route',
propertyRetriever: () {
return WidgetsBinding.instance.window.defaultRouteName;
},
propertyFaker: (TestWidgetsFlutterBinding binding, String fakeValue) {
binding.window.defaultRouteNameTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake accessibility features', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<AccessibilityFeatures>(
tester: tester,
realValue: ui.window.accessibilityFeatures,
fakeValue: const FakeAccessibilityFeatures(),
propertyRetriever: () {
return WidgetsBinding.instance.window.accessibilityFeatures;
},
propertyFaker: (TestWidgetsFlutterBinding binding, AccessibilityFeatures fakeValue) {
binding.window.accessibilityFeaturesTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can fake platform brightness', (WidgetTester tester) async {
verifyThatTestWindowCanFakeProperty<Brightness>(
tester: tester,
realValue: Brightness.light,
fakeValue: Brightness.dark,
propertyRetriever: () {
return WidgetsBinding.instance.window.platformBrightness;
},
propertyFaker: (TestWidgetsFlutterBinding binding, Brightness fakeValue) {
binding.window.platformBrightnessTestValue = fakeValue;
},
);
});
testWidgets('TestWindow can clear out fake properties all at once', (WidgetTester tester) async {
final double originalDevicePixelRatio = ui.window.devicePixelRatio;
final double originalTextScaleFactor = ui.window.textScaleFactor;
final TestWindow testWindow = retrieveTestBinding(tester).window;
// Set fake values for window properties.
testWindow.devicePixelRatioTestValue = 2.5;
testWindow.textScaleFactorTestValue = 3.0;
// Erase fake window property values.
testWindow.clearAllTestValues();
// Verify that the window once again reports real property values.
expect(WidgetsBinding.instance.window.devicePixelRatio, originalDevicePixelRatio);
expect(WidgetsBinding.instance.window.textScaleFactor, originalTextScaleFactor);
});
testWidgets('TestWindow sends fake locales when WidgetsBindingObserver notifiers are called', (WidgetTester tester) async {
final List<Locale> defaultLocales = WidgetsBinding.instance.window.locales;
final TestObserver observer = TestObserver();
retrieveTestBinding(tester).addObserver(observer);
final List<Locale> expectedValue = <Locale>[const Locale('fake_language_code')];
retrieveTestBinding(tester).window.localesTestValue = expectedValue;
expect(observer.locales, equals(expectedValue));
retrieveTestBinding(tester).window.localesTestValue = defaultLocales;
});
test('Window test', () {
final FakeSingletonWindow fakeWindow = FakeSingletonWindow();
final TestWindow testWindow = TestWindow(window: fakeWindow);
final SemanticsUpdate update = SemanticsUpdateBuilder().build();
testWindow.updateSemantics(update);
expect(fakeWindow.lastUpdate, update);
});
}
void verifyThatTestWindowCanFakeProperty<WindowPropertyType>({
required WidgetTester tester,
required WindowPropertyType? realValue,
required WindowPropertyType fakeValue,
required WindowPropertyType? Function() propertyRetriever,
required Function(TestWidgetsFlutterBinding, WindowPropertyType fakeValue) propertyFaker,
}) {
WindowPropertyType? propertyBeforeFaking;
WindowPropertyType? propertyAfterFaking;
propertyBeforeFaking = propertyRetriever();
propertyFaker(retrieveTestBinding(tester), fakeValue);
propertyAfterFaking = propertyRetriever();
expect(propertyBeforeFaking, realValue);
expect(propertyAfterFaking, fakeValue);
}
TestWidgetsFlutterBinding retrieveTestBinding(WidgetTester tester) {
final WidgetsBinding binding = tester.binding;
assert(binding is TestWidgetsFlutterBinding);
final TestWidgetsFlutterBinding testBinding = binding as TestWidgetsFlutterBinding;
return testBinding;
}
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;
}
class TestObserver with WidgetsBindingObserver {
List<Locale>? locales;
Locale? locale;
@override
void didChangeLocales(List<Locale>? locales) {
this.locales = locales;
}
}
class FakeSingletonWindow extends Fake implements SingletonFlutterWindow {
SemanticsUpdate? lastUpdate;
@override
PlatformDispatcher get platformDispatcher => PlatformDispatcher.instance;
@override
void updateSemantics(SemanticsUpdate update) {
lastUpdate = update;
}
}