Enable dependency injection of Window instead of using static property (#27389)
This commit is contained in:
@@ -62,3 +62,4 @@ export 'src/test_pointer.dart';
|
||||
export 'src/test_text_input.dart';
|
||||
export 'src/test_vsync.dart';
|
||||
export 'src/widget_tester.dart';
|
||||
export 'src/window.dart';
|
||||
|
||||
@@ -108,11 +108,11 @@ class MinimumTapTargetGuideline extends AccessibilityGuideline {
|
||||
const double delta = 0.001;
|
||||
if (paintBounds.left <= delta
|
||||
|| paintBounds.top <= delta
|
||||
|| (paintBounds.bottom - ui.window.physicalSize.height).abs() <= delta
|
||||
|| (paintBounds.right - ui.window.physicalSize.width).abs() <= delta)
|
||||
|| (paintBounds.bottom - tester.binding.window.physicalSize.height).abs() <= delta
|
||||
|| (paintBounds.right - tester.binding.window.physicalSize.width).abs() <= delta)
|
||||
return result;
|
||||
// shrink by device pixel ratio.
|
||||
final Size candidateSize = paintBounds.size / ui.window.devicePixelRatio;
|
||||
final Size candidateSize = paintBounds.size / tester.binding.window.devicePixelRatio;
|
||||
if (candidateSize.width < size.width || candidateSize.height < size.height)
|
||||
result += Evaluation.fail(
|
||||
'$node: expected tap target size of at least $size, but found $candidateSize\n'
|
||||
|
||||
@@ -13,6 +13,7 @@ import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart' show TestWindow;
|
||||
import 'package:quiver/testing/async.dart';
|
||||
import 'package:quiver/time.dart';
|
||||
import 'package:test_api/test_api.dart' as test_package;
|
||||
@@ -96,12 +97,16 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
///
|
||||
/// This constructor overrides the [debugPrint] global hook to point to
|
||||
/// [debugPrintOverride], which can be overridden by subclasses.
|
||||
TestWidgetsFlutterBinding() {
|
||||
TestWidgetsFlutterBinding() : _window = TestWindow(window: ui.window) {
|
||||
debugPrint = debugPrintOverride;
|
||||
debugDisableShadows = disableShadows;
|
||||
debugCheckIntrinsicSizes = checkIntrinsicSizes;
|
||||
}
|
||||
|
||||
@override
|
||||
TestWindow get window => _window;
|
||||
final TestWindow _window;
|
||||
|
||||
/// The value to set [debugPrint] to while tests are running.
|
||||
///
|
||||
/// This can be used to redirect console output from the framework, or to
|
||||
@@ -265,8 +270,8 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
|
||||
|
||||
@override
|
||||
ViewConfiguration createViewConfiguration() {
|
||||
final double devicePixelRatio = ui.window.devicePixelRatio;
|
||||
final Size size = _surfaceSize ?? ui.window.physicalSize / devicePixelRatio;
|
||||
final double devicePixelRatio = window.devicePixelRatio;
|
||||
final Size size = _surfaceSize ?? window.physicalSize / devicePixelRatio;
|
||||
return ViewConfiguration(
|
||||
size: size,
|
||||
devicePixelRatio: devicePixelRatio,
|
||||
@@ -665,8 +670,8 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
||||
@override
|
||||
void initInstances() {
|
||||
super.initInstances();
|
||||
ui.window.onBeginFrame = null;
|
||||
ui.window.onDrawFrame = null;
|
||||
window.onBeginFrame = null;
|
||||
window.onDrawFrame = null;
|
||||
}
|
||||
|
||||
FakeAsync _currentFakeAsync; // set in runTest; cleared in postTest
|
||||
@@ -1145,7 +1150,7 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
||||
_pendingFrame = null;
|
||||
_expectingFrame = false;
|
||||
} else if (framePolicy != LiveTestWidgetsFlutterBindingFramePolicy.benchmark) {
|
||||
ui.window.scheduleFrame();
|
||||
window.scheduleFrame();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1155,6 +1160,7 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
||||
renderView = _LiveTestRenderView(
|
||||
configuration: createViewConfiguration(),
|
||||
onNeedPaint: _handleViewNeedsPaint,
|
||||
window: window,
|
||||
);
|
||||
renderView.scheduleInitialFrame();
|
||||
}
|
||||
@@ -1286,7 +1292,10 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
||||
|
||||
@override
|
||||
ViewConfiguration createViewConfiguration() {
|
||||
return TestViewConfiguration(size: _surfaceSize ?? _kDefaultTestViewportSize);
|
||||
return TestViewConfiguration(
|
||||
size: _surfaceSize ?? _kDefaultTestViewportSize,
|
||||
window: window,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -1310,15 +1319,18 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
||||
/// size onto the actual display using the [BoxFit.contain] algorithm.
|
||||
class TestViewConfiguration extends ViewConfiguration {
|
||||
/// Creates a [TestViewConfiguration] with the given size. Defaults to 800x600.
|
||||
TestViewConfiguration({ Size size = _kDefaultTestViewportSize })
|
||||
: _paintMatrix = _getMatrix(size, ui.window.devicePixelRatio),
|
||||
_hitTestMatrix = _getMatrix(size, 1.0),
|
||||
TestViewConfiguration({
|
||||
Size size = _kDefaultTestViewportSize,
|
||||
ui.Window window,
|
||||
})
|
||||
: _paintMatrix = _getMatrix(size, window.devicePixelRatio, window),
|
||||
_hitTestMatrix = _getMatrix(size, 1.0, window),
|
||||
super(size: size);
|
||||
|
||||
static Matrix4 _getMatrix(Size size, double devicePixelRatio) {
|
||||
final double inverseRatio = devicePixelRatio / ui.window.devicePixelRatio;
|
||||
final double actualWidth = ui.window.physicalSize.width * inverseRatio;
|
||||
final double actualHeight = ui.window.physicalSize.height * inverseRatio;
|
||||
static Matrix4 _getMatrix(Size size, double devicePixelRatio, ui.Window window) {
|
||||
final double inverseRatio = devicePixelRatio / window.devicePixelRatio;
|
||||
final double actualWidth = window.physicalSize.width * inverseRatio;
|
||||
final double actualHeight = window.physicalSize.height * inverseRatio;
|
||||
final double desiredWidth = size.width;
|
||||
final double desiredHeight = size.height;
|
||||
double scale, shiftX, shiftY;
|
||||
@@ -1377,7 +1389,8 @@ class _LiveTestRenderView extends RenderView {
|
||||
_LiveTestRenderView({
|
||||
ViewConfiguration configuration,
|
||||
this.onNeedPaint,
|
||||
}) : super(configuration: configuration);
|
||||
@required ui.Window window,
|
||||
}) : super(configuration: configuration, window: window);
|
||||
|
||||
@override
|
||||
TestViewConfiguration get configuration => super.configuration;
|
||||
@@ -1714,4 +1727,4 @@ class _MockHttpHeaders extends HttpHeaders {
|
||||
|
||||
@override
|
||||
String value(String name) => null;
|
||||
}
|
||||
}
|
||||
303
packages/flutter_test/lib/src/window.dart
Normal file
303
packages/flutter_test/lib/src/window.dart
Normal file
@@ -0,0 +1,303 @@
|
||||
// Copyright 2018 The Chromium 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:typed_data' show ByteData;
|
||||
import 'dart:ui' hide window;
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// [Window] that wraps another [Window] and allows faking of some properties
|
||||
/// for testing purposes.
|
||||
///
|
||||
/// Tests for certain widgets, e.g., [MaterialApp], might require faking certain
|
||||
/// properties of a [Window]. [TestWindow] facilitates the faking of these properties
|
||||
/// by overidding the properties of a real [Window] with desired fake values. The
|
||||
/// binding used within tests, [TestWidgetsFlutterBinding], contains a [TestWindow]
|
||||
/// that is used by all tests.
|
||||
///
|
||||
/// ## Sample Code
|
||||
///
|
||||
/// A test can utilize a [TestWindow] in the following way:
|
||||
///
|
||||
/// ```dart
|
||||
/// testWidgets('your test name here', (WidgetTester tester) async {
|
||||
/// // Retrieve the TestWidgetsFlutterBinding.
|
||||
/// final TestWidgetsFlutterBinding testBinding = tester.binding;
|
||||
///
|
||||
/// // Fake the desired properties of the TestWindow. All code running
|
||||
/// // within this test will perceive the following fake text scale
|
||||
/// // factor as the real text scale factor of the window.
|
||||
/// testBinding.window.textScaleFactorFakeValue = 2.5;
|
||||
///
|
||||
/// // Test code that depends on text scale factor here.
|
||||
/// });
|
||||
/// ```
|
||||
///
|
||||
/// The [TestWidgetsFlutterBinding] is recreated for each test and
|
||||
/// therefore any fake values defined in one test will not persist
|
||||
/// to the next.
|
||||
///
|
||||
/// If a test needs to override a real [Window] property and then later
|
||||
/// return to using the real [Window] property, [TestWindow] provides
|
||||
/// methods to clear each individual test value, e.g., [clearLocaleTestValue()].
|
||||
///
|
||||
/// To clear all fake test values in a [TestWindow], consider using [clearAllTestValues()].
|
||||
class TestWindow implements Window {
|
||||
/// Constructs a [TestWindow] that defers all behavior to the given [window] unless
|
||||
/// explicitly overidden for test purposes.
|
||||
TestWindow({
|
||||
@required Window window,
|
||||
}) : _window = window;
|
||||
|
||||
/// The [Window] that is wrapped by this [TestWindow].
|
||||
final Window _window;
|
||||
|
||||
@override
|
||||
double get devicePixelRatio => _devicePixelRatio ?? _window.devicePixelRatio;
|
||||
double _devicePixelRatio;
|
||||
/// Hides the real device pixel ratio and reports the given [devicePixelRatio] instead.
|
||||
set devicePixelRatioTestValue(double devicePixelRatio) {
|
||||
_devicePixelRatio = devicePixelRatio;
|
||||
}
|
||||
/// Deletes any existing test device pixel ratio and returns to using the real device pixel ratio.
|
||||
void clearDevicePixelRatioTestValue() {
|
||||
_devicePixelRatio = null;
|
||||
}
|
||||
|
||||
@override
|
||||
Size get physicalSize => _physicalSizeTestValue ?? _window.physicalSize;
|
||||
Size _physicalSizeTestValue;
|
||||
/// Hides the real physical size and reports the given [physicalSizeTestValue] instead.
|
||||
set physicalSizeTestValue (Size physicalSizeTestValue) {
|
||||
_physicalSizeTestValue = physicalSizeTestValue;
|
||||
}
|
||||
/// Deletes any existing test physical size and returns to using the real physical size.
|
||||
void clearPhysicalSizeTestValue() {
|
||||
_physicalSizeTestValue = null;
|
||||
}
|
||||
|
||||
@override
|
||||
WindowPadding get viewInsets => _viewInsetsTestValue ?? _window.viewInsets;
|
||||
WindowPadding _viewInsetsTestValue;
|
||||
/// Hides the real view insets and reports the given [viewInsetsTestValue] instead.
|
||||
set viewInsetsTestValue(WindowPadding viewInsetsTestValue) {
|
||||
_viewInsetsTestValue = viewInsetsTestValue;
|
||||
}
|
||||
/// Deletes any existing test view insets and returns to using the real view insets.
|
||||
void clearViewInsetsTestValue() {
|
||||
_viewInsetsTestValue = null;
|
||||
}
|
||||
|
||||
@override
|
||||
WindowPadding get padding => _paddingTestValue ?? _window.padding;
|
||||
WindowPadding _paddingTestValue;
|
||||
/// Hides the real padding and reports the given [paddingTestValue] instead.
|
||||
set paddingTestValue(WindowPadding paddingTestValue) {
|
||||
_paddingTestValue = paddingTestValue;
|
||||
}
|
||||
/// Deletes any existing test padding and returns to using the real padding.
|
||||
void clearPaddingTestValue() {
|
||||
_paddingTestValue = null;
|
||||
}
|
||||
|
||||
@override
|
||||
VoidCallback get onMetricsChanged => _window.onMetricsChanged;
|
||||
@override
|
||||
set onMetricsChanged(VoidCallback callback) {
|
||||
_window.onMetricsChanged = callback;
|
||||
}
|
||||
|
||||
@override
|
||||
Locale get locale => _localeTestValue ?? _window.locale;
|
||||
Locale _localeTestValue;
|
||||
/// Hides the real locale and reports the given [localeTestValue] instead.
|
||||
set localeTestValue(Locale localeTestValue) {
|
||||
_localeTestValue = localeTestValue;
|
||||
}
|
||||
/// Deletes any existing test locale and returns to using the real locale.
|
||||
void clearLocaleTestValue() {
|
||||
_localeTestValue = null;
|
||||
}
|
||||
|
||||
@override
|
||||
List<Locale> get locales => _localesTestValue ?? _window.locales;
|
||||
List<Locale> _localesTestValue;
|
||||
/// Hides the real locales and reports the given [localesTestValue] instead.
|
||||
set localesTestValue(List<Locale> localesTestValue) {
|
||||
_localesTestValue = localesTestValue;
|
||||
}
|
||||
/// Deletes any existing test locales and returns to using the real locales.
|
||||
void clearLocalesTestValue() {
|
||||
_localesTestValue = null;
|
||||
}
|
||||
|
||||
@override
|
||||
VoidCallback get onLocaleChanged => _window.onLocaleChanged;
|
||||
@override
|
||||
set onLocaleChanged(VoidCallback callback) {
|
||||
_window.onLocaleChanged = callback;
|
||||
}
|
||||
|
||||
@override
|
||||
double get textScaleFactor => _textScaleFactorTestValue ?? _window.textScaleFactor;
|
||||
double _textScaleFactorTestValue;
|
||||
/// Hides the real text scale factor and reports the given [textScaleFactorTestValue] instead.
|
||||
set textScaleFactorTestValue(double textScaleFactorTestValue) {
|
||||
_textScaleFactorTestValue = textScaleFactorTestValue;
|
||||
}
|
||||
/// Deletes any existing test text scale factor and returns to using the real text scale factor.
|
||||
void clearTextScaleFactorTestValue() {
|
||||
_textScaleFactorTestValue = null;
|
||||
}
|
||||
|
||||
@override
|
||||
bool get alwaysUse24HourFormat => _alwaysUse24HourFormatTestValue ?? _window.alwaysUse24HourFormat;
|
||||
bool _alwaysUse24HourFormatTestValue;
|
||||
/// Hides the real clock format and reports the given [alwaysUse24HourFormatTestValue] instead.
|
||||
set alwaysUse24HourFormatTestValue(bool alwaysUse24HourFormatTestValue) {
|
||||
_alwaysUse24HourFormatTestValue = alwaysUse24HourFormatTestValue;
|
||||
}
|
||||
/// Deletes any existing test clock format and returns to using the real clock format.
|
||||
void clearAlwaysUse24HourTestValue() {
|
||||
_alwaysUse24HourFormatTestValue = null;
|
||||
}
|
||||
|
||||
@override
|
||||
VoidCallback get onTextScaleFactorChanged => _window.onTextScaleFactorChanged;
|
||||
@override
|
||||
set onTextScaleFactorChanged(VoidCallback callback) {
|
||||
_window.onTextScaleFactorChanged = callback;
|
||||
}
|
||||
|
||||
@override
|
||||
FrameCallback get onBeginFrame => _window.onBeginFrame;
|
||||
@override
|
||||
set onBeginFrame(FrameCallback callback) {
|
||||
_window.onBeginFrame = callback;
|
||||
}
|
||||
|
||||
@override
|
||||
VoidCallback get onDrawFrame => _window.onDrawFrame;
|
||||
@override
|
||||
set onDrawFrame(VoidCallback callback) {
|
||||
_window.onDrawFrame = callback;
|
||||
}
|
||||
|
||||
@override
|
||||
PointerDataPacketCallback get onPointerDataPacket => _window.onPointerDataPacket;
|
||||
@override
|
||||
set onPointerDataPacket(PointerDataPacketCallback callback) {
|
||||
_window.onPointerDataPacket = callback;
|
||||
}
|
||||
|
||||
@override
|
||||
String get defaultRouteName => _defaultRouteNameTestValue ?? _window.defaultRouteName;
|
||||
String _defaultRouteNameTestValue;
|
||||
/// Hides the real default route name and reports the given [defaultRouteNameTestValue] instead.
|
||||
set defaultRouteNameTestValue(String defaultRouteNameTestValue) {
|
||||
_defaultRouteNameTestValue = defaultRouteNameTestValue;
|
||||
}
|
||||
/// Deletes any existing test default route name and returns to using the real default route name.
|
||||
void clearDefaultRouteNameTestValue() {
|
||||
_defaultRouteNameTestValue = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void scheduleFrame() {
|
||||
_window.scheduleFrame();
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Scene scene) {
|
||||
_window.render(scene);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get semanticsEnabled => _semanticsEnabledTestValue ?? _window.semanticsEnabled;
|
||||
bool _semanticsEnabledTestValue;
|
||||
/// Hides the real semantics enabled and reports the given [semanticsEnabledTestValue] instead.
|
||||
set semanticsEnabledTestValue(bool semanticsEnabledTestValue) {
|
||||
_semanticsEnabledTestValue = semanticsEnabledTestValue;
|
||||
}
|
||||
/// Deletes any existing test semantics enabled and returns to using the real semantics enabled.
|
||||
void clearSemanticsEnabledTestValue() {
|
||||
_semanticsEnabledTestValue = null;
|
||||
}
|
||||
|
||||
@override
|
||||
VoidCallback get onSemanticsEnabledChanged => _window.onSemanticsEnabledChanged;
|
||||
@override
|
||||
set onSemanticsEnabledChanged(VoidCallback callback) {
|
||||
_window.onSemanticsEnabledChanged = callback;
|
||||
}
|
||||
|
||||
@override
|
||||
SemanticsActionCallback get onSemanticsAction => _window.onSemanticsAction;
|
||||
@override
|
||||
set onSemanticsAction(SemanticsActionCallback callback) {
|
||||
_window.onSemanticsAction = callback;
|
||||
}
|
||||
|
||||
@override
|
||||
AccessibilityFeatures get accessibilityFeatures => _accessibilityFeaturesTestValue ?? _window.accessibilityFeatures;
|
||||
AccessibilityFeatures _accessibilityFeaturesTestValue;
|
||||
/// Hides the real accessibility features and reports the given [accessibilityFeaturesTestValue] instead.
|
||||
set accessibilityFeaturesTestValue(AccessibilityFeatures accessibilityFeaturesTestValue) {
|
||||
_accessibilityFeaturesTestValue = accessibilityFeaturesTestValue;
|
||||
}
|
||||
/// Deletes any existing test accessibility features and returns to using the real accessibility features.
|
||||
void clearAccessibilityFeaturesTestValue() {
|
||||
_accessibilityFeaturesTestValue = null;
|
||||
}
|
||||
|
||||
@override
|
||||
VoidCallback get onAccessibilityFeaturesChanged => _window.onAccessibilityFeaturesChanged;
|
||||
@override
|
||||
set onAccessibilityFeaturesChanged(VoidCallback callback) {
|
||||
_window.onAccessibilityFeaturesChanged = callback;
|
||||
}
|
||||
|
||||
@override
|
||||
void updateSemantics(SemanticsUpdate update) {
|
||||
_window.updateSemantics(update);
|
||||
}
|
||||
|
||||
@override
|
||||
void setIsolateDebugName(String name) {
|
||||
_window.setIsolateDebugName(name);
|
||||
}
|
||||
|
||||
@override
|
||||
void sendPlatformMessage(String name,
|
||||
ByteData data,
|
||||
PlatformMessageResponseCallback callback) {
|
||||
_window.sendPlatformMessage(name, data, callback);
|
||||
}
|
||||
|
||||
@override
|
||||
PlatformMessageCallback get onPlatformMessage => _window.onPlatformMessage;
|
||||
@override
|
||||
set onPlatformMessage(PlatformMessageCallback callback) {
|
||||
_window.onPlatformMessage = callback;
|
||||
}
|
||||
|
||||
/// Delete any test value properties that have been set on this [TestWindow]
|
||||
/// and return to reporting the real [Window] values for all [Window] properties.
|
||||
///
|
||||
/// If desired, clearing of properties can be done on an individual basis, e.g.,
|
||||
/// [clearLocaleTestValue()].
|
||||
void clearAllTestValues() {
|
||||
clearAccessibilityFeaturesTestValue();
|
||||
clearAlwaysUse24HourTestValue();
|
||||
clearDefaultRouteNameTestValue();
|
||||
clearDevicePixelRatioTestValue();
|
||||
clearLocaleTestValue();
|
||||
clearLocalesTestValue();
|
||||
clearPaddingTestValue();
|
||||
clearPhysicalSizeTestValue();
|
||||
clearSemanticsEnabledTestValue();
|
||||
clearTextScaleFactorTestValue();
|
||||
clearViewInsetsTestValue();
|
||||
}
|
||||
}
|
||||
256
packages/flutter_test/test/window_test.dart
Normal file
256
packages/flutter_test/test/window_test.dart
Normal file
@@ -0,0 +1,256 @@
|
||||
// Copyright 2018 The Chromium 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 Size, Locale, WindowPadding, AccessibilityFeatures;
|
||||
|
||||
import 'package:flutter/widgets.dart' show WidgetsBinding;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
void main() {
|
||||
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<WindowPadding>(
|
||||
tester: tester,
|
||||
realValue: ui.window.viewInsets,
|
||||
fakeValue: const FakeWindowPadding(),
|
||||
propertyRetriever: () {
|
||||
return WidgetsBinding.instance.window.viewInsets;
|
||||
},
|
||||
propertyFaker: (TestWidgetsFlutterBinding binding, WindowPadding fakeValue) {
|
||||
binding.window.viewInsetsTestValue = fakeValue;
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('TestWindow can fake padding', (WidgetTester tester) async {
|
||||
verifyThatTestWindowCanFakeProperty<WindowPadding>(
|
||||
tester: tester,
|
||||
realValue: ui.window.padding,
|
||||
fakeValue: const FakeWindowPadding(),
|
||||
propertyRetriever: () {
|
||||
return WidgetsBinding.instance.window.padding;
|
||||
},
|
||||
propertyFaker: (TestWidgetsFlutterBinding binding, WindowPadding 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 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 semantics enabled', (WidgetTester tester) async {
|
||||
verifyThatTestWindowCanFakeProperty<bool>(
|
||||
tester: tester,
|
||||
realValue: ui.window.semanticsEnabled,
|
||||
fakeValue: !ui.window.semanticsEnabled,
|
||||
propertyRetriever: () {
|
||||
return WidgetsBinding.instance.window.semanticsEnabled;
|
||||
},
|
||||
propertyFaker: (TestWidgetsFlutterBinding binding, bool fakeValue) {
|
||||
binding.window.semanticsEnabledTestValue = 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 clear out fake properties all at once', (WidgetTester tester) {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
return testBinding;
|
||||
}
|
||||
|
||||
class FakeWindowPadding implements WindowPadding {
|
||||
const FakeWindowPadding({
|
||||
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 FakeAccessibilityFeatures implements AccessibilityFeatures {
|
||||
const FakeAccessibilityFeatures({
|
||||
this.accessibleNavigation = false,
|
||||
this.invertColors = false,
|
||||
this.disableAnimations = false,
|
||||
this.boldText = false,
|
||||
this.reduceMotion = false,
|
||||
});
|
||||
|
||||
@override
|
||||
final bool accessibleNavigation;
|
||||
|
||||
@override
|
||||
final bool invertColors;
|
||||
|
||||
@override
|
||||
final bool disableAnimations;
|
||||
|
||||
@override
|
||||
final bool boldText;
|
||||
|
||||
@override
|
||||
final bool reduceMotion;
|
||||
}
|
||||
Reference in New Issue
Block a user