TestWindow class

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 overriding 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:

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().

Implemented types

Constructors

TestWindow({@required Window window })
Constructs a TestWindow that defers all behavior to the given Window unless explicitly overridden for test purposes.

Properties

accessibilityFeatures AccessibilityFeatures
Additional accessibility features that may be enabled by the platform.
read-only, override
accessibilityFeaturesTestValue AccessibilityFeatures
Hides the real accessibility features and reports the given accessibilityFeaturesTestValue instead.
write-only
alwaysUse24HourFormat bool
The setting indicating whether time should always be shown in the 24-hour format. [...]
read-only, override
alwaysUse24HourFormatTestValue bool
Hides the real clock format and reports the given alwaysUse24HourFormatTestValue instead.
write-only
defaultRouteName String
The route or path that the embedder requested when the application was launched. [...]
read-only, override
defaultRouteNameTestValue String
Hides the real default route name and reports the given defaultRouteNameTestValue instead.
write-only
devicePixelRatio double
The number of device pixels for each logical pixel. This number might not be a power of two. Indeed, it might not even be an integer. For example, the Nexus 6 has a device pixel ratio of 3.5. [...]
read-only, override
devicePixelRatioTestValue double
Hides the real device pixel ratio and reports the given devicePixelRatio instead.
write-only
initialLifecycleState String
The lifecycle state immediately after dart isolate initialization. [...]
read-only, override
initialLifecycleStateTestValue String
Sets a faked initialLifecycleState for testing.
write-only
locale Locale
The system-reported default locale of the device. [...]
read-only, override
locales List<Locale>
The full system-reported supported locales of the device. [...]
read-only, override
localesTestValue List<Locale>
Hides the real locales and reports the given localesTestValue instead.
write-only
localeTestValue Locale
Hides the real locale and reports the given localeTestValue instead.
write-only
onAccessibilityFeaturesChanged VoidCallback
A callback that is invoked when the value of accessibilityFeatures changes. [...]
read / write, override
onBeginFrame FrameCallback
A callback that is invoked to notify the application that it is an appropriate time to provide a scene using the SceneBuilder API and the render method. When possible, this is driven by the hardware VSync signal. This is only called if scheduleFrame has been called since the last time this callback was invoked. [...]
read / write, override
onDrawFrame VoidCallback
A callback that is invoked for each frame after onBeginFrame has completed and after the microtask queue has been drained. This can be used to implement a second phase of frame rendering that happens after any deferred work queued by the onBeginFrame phase. [...]
read / write, override
onLocaleChanged VoidCallback
A callback that is invoked whenever locale changes value. [...]
read / write, override
onMetricsChanged VoidCallback
A callback that is invoked whenever the devicePixelRatio, physicalSize, padding, viewInsets, or systemGestureInsets values change, for example when the device is rotated or when the application is resized (e.g. when showing applications side-by-side on Android). [...]
read / write, override
onPlatformBrightnessChanged VoidCallback
A callback that is invoked whenever platformBrightness changes value. [...]
read / write, override
onPlatformMessage PlatformMessageCallback
Called whenever this window receives a message from a platform-specific plugin. [...]
read / write, override
onPointerDataPacket PointerDataPacketCallback
A callback that is invoked when pointer data is available. [...]
read / write, override
onReportTimings TimingsCallback
A callback that is invoked to report the FrameTiming of recently rasterized frames. [...]
read / write, override
onSemanticsAction SemanticsActionCallback
A callback that is invoked whenever the user requests an action to be performed. [...]
read / write, override
onSemanticsEnabledChanged VoidCallback
A callback that is invoked when the value of semanticsEnabled changes. [...]
read / write, override
onTextScaleFactorChanged VoidCallback
A callback that is invoked whenever textScaleFactor changes value. [...]
read / write, override
padding WindowPadding
The number of physical pixels on each side of the display rectangle into which the application can render, but which may be partially obscured by system UI (such as the system notification area), or or physical intrusions in the display (e.g. overscan regions on television screens or phone sensor housings). [...]
read-only, override
paddingTestValue WindowPadding
Hides the real padding and reports the given paddingTestValue instead.
write-only
physicalDepth double
The physical depth is the maximum elevation that the Window allows. [...]
read-only, override
physicalDepthTestValue double
Hides the real physical depth and reports the given physicalDepthTestValue instead.
write-only
physicalSize Size
The dimensions of the rectangle into which the application will be drawn, in physical pixels. [...]
read-only, override
physicalSizeTestValue Size
Hides the real physical size and reports the given physicalSizeTestValue instead.
write-only
platformBrightness Brightness
The setting indicating the current brightness mode of the host platform. If the platform has no preference, platformBrightness defaults to Brightness.light.
read-only, override
platformBrightnessTestValue Brightness
Hides the real text scale factor and reports the given platformBrightnessTestValue instead.
write-only
semanticsEnabled bool
Whether the user has requested that updateSemantics be called when the semantic contents of window changes. [...]
read-only, override
semanticsEnabledTestValue bool
Hides the real semantics enabled and reports the given semanticsEnabledTestValue instead.
write-only
textScaleFactor double
The system-reported text scale. [...]
read-only, override
textScaleFactorTestValue double
Hides the real text scale factor and reports the given textScaleFactorTestValue instead.
write-only
viewInsets WindowPadding
The number of physical pixels on each side of the display rectangle into which the application can render, but over which the operating system will likely place system UI, such as the keyboard, that fully obscures any content. [...]
read-only, override
viewInsetsTestValue WindowPadding
Hides the real view insets and reports the given viewInsetsTestValue instead.
write-only
viewPadding WindowPadding
The number of physical pixels on each side of the display rectangle into which the application can render, but which may be partially obscured by system UI (such as the system notification area), or or physical intrusions in the display (e.g. overscan regions on television screens or phone sensor housings). [...]
read-only, override
viewPaddingTestValue WindowPadding
Hides the real view padding and reports the given paddingTestValue instead.
write-only
hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
systemGestureInsets WindowPadding
The number of physical pixels on each side of the display rectangle into which the application can render, but where the operating system will consume input gestures for the sake of system navigation. [...]
read-only, inherited

Methods

clearAccessibilityFeaturesTestValue() → void
Deletes any existing test accessibility features and returns to using the real accessibility features.
clearAllTestValues() → void
Delete any test value properties that have been set on this TestWindow and return to reporting the real Window values for all Window properties. [...]
clearAlwaysUse24HourTestValue() → void
Deletes any existing test clock format and returns to using the real clock format.
clearDefaultRouteNameTestValue() → void
Deletes any existing test default route name and returns to using the real default route name.
clearDevicePixelRatioTestValue() → void
Deletes any existing test device pixel ratio and returns to using the real device pixel ratio.
clearLocalesTestValue() → void
Deletes any existing test locales and returns to using the real locales.
clearLocaleTestValue() → void
Deletes any existing test locale and returns to using the real locale.
clearPaddingTestValue() → void
Deletes any existing test padding and returns to using the real padding.
clearPhysicalDepthTestValue() → void
Deletes any existing test physical depth and returns to using the real physical depth.
clearPhysicalSizeTestValue() → void
Deletes any existing test physical size and returns to using the real physical size.
clearPlatformBrightnessTestValue() → void
Deletes any existing test platform brightness and returns to using the real platform brightness.
clearSemanticsEnabledTestValue() → void
Deletes any existing test semantics enabled and returns to using the real semantics enabled.
clearTextScaleFactorTestValue() → void
Deletes any existing test text scale factor and returns to using the real text scale factor.
clearViewInsetsTestValue() → void
Deletes any existing test view insets and returns to using the real view insets.
clearViewPaddingTestValue() → void
Deletes any existing test view padding and returns to using the real viewPadding.
noSuchMethod(Invocation invocation) → dynamic
This gives us some grace time when the dart:ui side adds something to Window, and makes things easier when we do rolls to give us time to catch up.
override
render(Scene scene) → void
Updates the application's rendering on the GPU with the newly provided Scene. This function must be called within the scope of the onBeginFrame or onDrawFrame callbacks being invoked. If this function is called a second time during a single onBeginFrame/onDrawFrame callback sequence or called outside the scope of those callbacks, the call will be ignored. [...]
override
scheduleFrame() → void
Requests that, at the next appropriate opportunity, the onBeginFrame and onDrawFrame callbacks be invoked. [...]
override
sendPlatformMessage(String name, ByteData data, PlatformMessageResponseCallback callback) → void
Sends a message to a platform-specific plugin. [...]
override
setIsolateDebugName(String name) → void
Set the debug name associated with this window's root isolate. [...]
override
updateSemantics(SemanticsUpdate update) → void
Change the retained semantics data about this window. [...]
override
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) bool
The equality operator. [...]
inherited