Revert usages of the binding's platformDispatcher to use window instead (#70319)

This reverts usages of the binding's platformDispatcher to use window again temporarily, because there isn't a TestPlatformDispatcher yet, and so some tests were failing because they mocked the TestWindow to return certain things (locales) that were returning the real values instead of the test values.

Once I've created a TestPlatformDispatcher to allow fake data to be passed to it, we can go back to using the platformDispatcher in all of these places
This commit is contained in:
Greg Spencer 2020-11-12 11:17:00 -08:00 committed by GitHub
parent c6d4a6ef5c
commit 86f9ab511e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 49 additions and 31 deletions

View File

@ -236,12 +236,12 @@ abstract class BindingBase {
} }
_postExtensionStateChangedEvent( _postExtensionStateChangedEvent(
brightnessOverrideExtensionName, brightnessOverrideExtensionName,
(debugBrightnessOverride ?? platformDispatcher.platformBrightness).toString(), (debugBrightnessOverride ?? window.platformBrightness).toString(),
); );
await reassembleApplication(); await reassembleApplication();
} }
return <String, dynamic>{ return <String, dynamic>{
'value': (debugBrightnessOverride ?? platformDispatcher.platformBrightness).toString(), 'value': (debugBrightnessOverride ?? window.platformBrightness).toString(),
}; };
}, },
); );

View File

@ -198,7 +198,7 @@ mixin GestureBinding on BindingBase implements HitTestable, HitTestDispatcher, H
void initInstances() { void initInstances() {
super.initInstances(); super.initInstances();
_instance = this; _instance = this;
platformDispatcher.onPointerDataPacket = _handlePointerDataPacket; window.onPointerDataPacket = _handlePointerDataPacket;
} }
@override @override

View File

@ -33,7 +33,7 @@ mixin RendererBinding on BindingBase, ServicesBinding, SchedulerBinding, Gesture
onSemanticsOwnerCreated: _handleSemanticsOwnerCreated, onSemanticsOwnerCreated: _handleSemanticsOwnerCreated,
onSemanticsOwnerDisposed: _handleSemanticsOwnerDisposed, onSemanticsOwnerDisposed: _handleSemanticsOwnerDisposed,
); );
platformDispatcher window
..onMetricsChanged = handleMetricsChanged ..onMetricsChanged = handleMetricsChanged
..onTextScaleFactorChanged = handleTextScaleFactorChanged ..onTextScaleFactorChanged = handleTextScaleFactorChanged
..onPlatformBrightnessChanged = handlePlatformBrightnessChanged ..onPlatformBrightnessChanged = handlePlatformBrightnessChanged
@ -280,7 +280,7 @@ mixin RendererBinding on BindingBase, ServicesBinding, SchedulerBinding, Gesture
} }
void _handleSemanticsEnabledChanged() { void _handleSemanticsEnabledChanged() {
setSemanticsEnabled(platformDispatcher.semanticsEnabled); setSemanticsEnabled(window.semanticsEnabled);
} }
/// Whether the render tree associated with this binding should produce a tree /// Whether the render tree associated with this binding should produce a tree

View File

@ -265,10 +265,10 @@ mixin SchedulerBinding on BindingBase {
void addTimingsCallback(TimingsCallback callback) { void addTimingsCallback(TimingsCallback callback) {
_timingsCallbacks.add(callback); _timingsCallbacks.add(callback);
if (_timingsCallbacks.length == 1) { if (_timingsCallbacks.length == 1) {
assert(platformDispatcher.onReportTimings == null); assert(window.onReportTimings == null);
platformDispatcher.onReportTimings = _executeTimingsCallbacks; window.onReportTimings = _executeTimingsCallbacks;
} }
assert(platformDispatcher.onReportTimings == _executeTimingsCallbacks); assert(window.onReportTimings == _executeTimingsCallbacks);
} }
/// Removes a callback that was earlier added by [addTimingsCallback]. /// Removes a callback that was earlier added by [addTimingsCallback].
@ -276,7 +276,7 @@ mixin SchedulerBinding on BindingBase {
assert(_timingsCallbacks.contains(callback)); assert(_timingsCallbacks.contains(callback));
_timingsCallbacks.remove(callback); _timingsCallbacks.remove(callback);
if (_timingsCallbacks.isEmpty) { if (_timingsCallbacks.isEmpty) {
platformDispatcher.onReportTimings = null; window.onReportTimings = null;
} }
} }
@ -724,8 +724,8 @@ mixin SchedulerBinding on BindingBase {
/// [PlatformDispatcher.onDrawFrame] are registered. /// [PlatformDispatcher.onDrawFrame] are registered.
@protected @protected
void ensureFrameCallbacksRegistered() { void ensureFrameCallbacksRegistered() {
platformDispatcher.onBeginFrame ??= _handleBeginFrame; window.onBeginFrame ??= _handleBeginFrame;
platformDispatcher.onDrawFrame ??= _handleDrawFrame; window.onDrawFrame ??= _handleDrawFrame;
} }
/// Schedules a new frame using [scheduleFrame] if this object is not /// Schedules a new frame using [scheduleFrame] if this object is not
@ -790,7 +790,7 @@ mixin SchedulerBinding on BindingBase {
return true; return true;
}()); }());
ensureFrameCallbacksRegistered(); ensureFrameCallbacksRegistered();
platformDispatcher.scheduleFrame(); window.scheduleFrame();
_hasScheduledFrame = true; _hasScheduledFrame = true;
} }
@ -827,7 +827,7 @@ mixin SchedulerBinding on BindingBase {
debugPrintStack(label: 'scheduleForcedFrame() called. Current phase is $schedulerPhase.'); debugPrintStack(label: 'scheduleForcedFrame() called. Current phase is $schedulerPhase.');
return true; return true;
}()); }());
platformDispatcher.scheduleFrame(); window.scheduleFrame();
_hasScheduledFrame = true; _hasScheduledFrame = true;
} }

View File

@ -21,7 +21,7 @@ mixin SemanticsBinding on BindingBase {
void initInstances() { void initInstances() {
super.initInstances(); super.initInstances();
_instance = this; _instance = this;
_accessibilityFeatures = platformDispatcher.accessibilityFeatures; _accessibilityFeatures = window.accessibilityFeatures;
} }
/// Called when the platform accessibility features change. /// Called when the platform accessibility features change.
@ -29,7 +29,7 @@ mixin SemanticsBinding on BindingBase {
/// See [dart:ui.PlatformDispatcher.onAccessibilityFeaturesChanged]. /// See [dart:ui.PlatformDispatcher.onAccessibilityFeaturesChanged].
@protected @protected
void handleAccessibilityFeaturesChanged() { void handleAccessibilityFeaturesChanged() {
_accessibilityFeatures = platformDispatcher.accessibilityFeatures; _accessibilityFeatures = window.accessibilityFeatures;
} }
/// Creates an empty semantics update builder. /// Creates an empty semantics update builder.

View File

@ -2658,7 +2658,7 @@ class SemanticsOwner extends ChangeNotifier {
final CustomSemanticsAction action = CustomSemanticsAction.getAction(actionId)!; final CustomSemanticsAction action = CustomSemanticsAction.getAction(actionId)!;
builder.updateCustomAction(id: actionId, label: action.label, hint: action.hint, overrideId: action.action?.index ?? -1); builder.updateCustomAction(id: actionId, label: action.label, hint: action.hint, overrideId: action.action?.index ?? -1);
} }
SemanticsBinding.instance!.platformDispatcher.updateSemantics(builder.build()); SemanticsBinding.instance!.window.updateSemantics(builder.build());
notifyListeners(); notifyListeners();
} }

View File

@ -28,7 +28,7 @@ mixin ServicesBinding on BindingBase, SchedulerBinding {
_instance = this; _instance = this;
_defaultBinaryMessenger = createBinaryMessenger(); _defaultBinaryMessenger = createBinaryMessenger();
_restorationManager = createRestorationManager(); _restorationManager = createRestorationManager();
platformDispatcher.onPlatformMessage = defaultBinaryMessenger.handlePlatformMessage; window.onPlatformMessage = defaultBinaryMessenger.handlePlatformMessage;
initLicenses(); initLicenses();
SystemChannels.system.setMessageHandler((dynamic message) => handleSystemMessage(message as Object)); SystemChannels.system.setMessageHandler((dynamic message) => handleSystemMessage(message as Object));
SystemChannels.lifecycle.setMessageHandler(_handleLifecycleMessage); SystemChannels.lifecycle.setMessageHandler(_handleLifecycleMessage);

View File

@ -1113,15 +1113,15 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
// If window.defaultRouteName isn't '/', we should assume it was set // If window.defaultRouteName isn't '/', we should assume it was set
// intentionally via `setInitialRoute`, and should override whatever is in // intentionally via `setInitialRoute`, and should override whatever is in
// [widget.initialRoute]. // [widget.initialRoute].
String get _initialRouteName => WidgetsBinding.instance!.platformDispatcher.defaultRouteName != Navigator.defaultRouteName String get _initialRouteName => WidgetsBinding.instance!.window.defaultRouteName != Navigator.defaultRouteName
? WidgetsBinding.instance!.platformDispatcher.defaultRouteName ? WidgetsBinding.instance!.window.defaultRouteName
: widget.initialRoute ?? WidgetsBinding.instance!.platformDispatcher.defaultRouteName; : widget.initialRoute ?? WidgetsBinding.instance!.window.defaultRouteName;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_updateRouting(); _updateRouting();
_locale = _resolveLocales(WidgetsBinding.instance!.platformDispatcher.locales, widget.supportedLocales); _locale = _resolveLocales(WidgetsBinding.instance!.window.locales, widget.supportedLocales);
WidgetsBinding.instance!.addObserver(this); WidgetsBinding.instance!.addObserver(this);
} }

View File

@ -245,7 +245,7 @@ abstract class WidgetsBindingObserver {
/// ///
/// This method exposes notifications from /// This method exposes notifications from
/// [dart:ui.PlatformDispatcher.onLocaleChanged]. /// [dart:ui.PlatformDispatcher.onLocaleChanged].
void didChangeLocales(List<Locale>? locale) { } void didChangeLocales(List<Locale>? locales) { }
/// Called when the system puts the app in the background or returns /// Called when the system puts the app in the background or returns
/// the app to the foreground. /// the app to the foreground.
@ -287,8 +287,8 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB
// properly setup the [defaultBinaryMessenger] instance. // properly setup the [defaultBinaryMessenger] instance.
_buildOwner = BuildOwner(); _buildOwner = BuildOwner();
buildOwner!.onBuildScheduled = _handleBuildScheduled; buildOwner!.onBuildScheduled = _handleBuildScheduled;
platformDispatcher.onLocaleChanged = handleLocaleChanged; window.onLocaleChanged = handleLocaleChanged;
platformDispatcher.onAccessibilityFeaturesChanged = handleAccessibilityFeaturesChanged; window.onAccessibilityFeaturesChanged = handleAccessibilityFeaturesChanged;
SystemChannels.navigation.setMethodCallHandler(_handleNavigationInvocation); SystemChannels.navigation.setMethodCallHandler(_handleNavigationInvocation);
FlutterErrorDetails.propertiesTransformers.add(transformDebugCreator); FlutterErrorDetails.propertiesTransformers.add(transformDebugCreator);
} }
@ -586,7 +586,7 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB
@protected @protected
@mustCallSuper @mustCallSuper
void handleLocaleChanged() { void handleLocaleChanged() {
dispatchLocalesChanged(platformDispatcher.locales); dispatchLocalesChanged(window.locales);
} }
/// Notify all the observers that the locale has changed (using /// Notify all the observers that the locale has changed (using
@ -1016,7 +1016,7 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB
/// method again with the matched locale of the first call omitted from /// method again with the matched locale of the first call omitted from
/// `supportedLocales`. /// `supportedLocales`.
Locale? computePlatformResolvedLocale(List<Locale> supportedLocales) { Locale? computePlatformResolvedLocale(List<Locale> supportedLocales) {
return platformDispatcher.computePlatformResolvedLocale(supportedLocales); return window.computePlatformResolvedLocale(supportedLocales);
} }
} }

View File

@ -19,14 +19,14 @@ void main() {
// Simulates the engine completing a frame render to trigger the // Simulates the engine completing a frame render to trigger the
// appropriate callback setting [WidgetBinding.firstFrameRasterized]. // appropriate callback setting [WidgetBinding.firstFrameRasterized].
binding.platformDispatcher.onReportTimings!(<FrameTiming>[]); binding.window.onReportTimings!(<FrameTiming>[]);
expect(binding.firstFrameRasterized, isFalse); expect(binding.firstFrameRasterized, isFalse);
binding.allowFirstFrame(); binding.allowFirstFrame();
fakeAsync.flushTimers(); fakeAsync.flushTimers();
// Simulates the engine again. // Simulates the engine again.
binding.platformDispatcher.onReportTimings!(<FrameTiming>[]); binding.window.onReportTimings!(<FrameTiming>[]);
expect(binding.firstFrameRasterized, isTrue); expect(binding.firstFrameRasterized, isTrue);
}); });
}); });

View File

@ -1039,8 +1039,8 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
@override @override
void ensureFrameCallbacksRegistered() { void ensureFrameCallbacksRegistered() {
// Leave PlatformDispatcher alone, do nothing. // Leave PlatformDispatcher alone, do nothing.
assert(platformDispatcher.onDrawFrame == null); assert(window.onDrawFrame == null);
assert(platformDispatcher.onBeginFrame == null); assert(window.onBeginFrame == null);
} }
@override @override

View File

@ -5,7 +5,7 @@
import 'dart:ui' as ui show window; import 'dart:ui' as ui show window;
import 'dart:ui' show Size, Locale, WindowPadding, AccessibilityFeatures, Brightness; import 'dart:ui' show Size, Locale, WindowPadding, AccessibilityFeatures, Brightness;
import 'package:flutter/widgets.dart' show WidgetsBinding; import 'package:flutter/widgets.dart' show WidgetsBinding, WidgetsBindingObserver;
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
void main() { void main() {
@ -184,6 +184,14 @@ void main() {
expect(WidgetsBinding.instance!.window.devicePixelRatio, originalDevicePixelRatio); expect(WidgetsBinding.instance!.window.devicePixelRatio, originalDevicePixelRatio);
expect(WidgetsBinding.instance!.window.textScaleFactor, originalTextScaleFactor); expect(WidgetsBinding.instance!.window.textScaleFactor, originalTextScaleFactor);
}); });
testWidgets('TestWindow sends fake locales when WidgetsBindingObserver notifiers are called', (WidgetTester tester) async {
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));
});
} }
void verifyThatTestWindowCanFakeProperty<WindowPropertyType>({ void verifyThatTestWindowCanFakeProperty<WindowPropertyType>({
@ -273,3 +281,13 @@ class FakeAccessibilityFeatures implements AccessibilityFeatures {
return null; return null;
} }
} }
class TestObserver with WidgetsBindingObserver {
List<Locale>? locales;
Locale? locale;
@override
void didChangeLocales(List<Locale>? locales) {
this.locales = locales;
}
}