From 147bb4db3f01a24c7c53191dc7ce7f391d670b6d Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Wed, 3 Feb 2021 15:26:03 -0800 Subject: [PATCH] More null-safety fixes for flutter_driver (#75343) --- .../flutter_driver/lib/src/common/find.dart | 28 ----------- .../lib/src/common/frame_sync.dart | 14 ------ .../lib/src/common/gesture.dart | 30 ----------- .../lib/src/common/handler_factory.dart | 50 +++++++++---------- .../lib/src/common/message.dart | 13 +++++ .../flutter_driver/lib/src/common/text.dart | 28 ----------- .../lib/src/extension/extension.dart | 2 +- .../test/src/real_tests/extension_test.dart | 34 ++++++------- 8 files changed, 56 insertions(+), 143 deletions(-) diff --git a/packages/flutter_driver/lib/src/common/find.dart b/packages/flutter_driver/lib/src/common/find.dart index 01bd838e6c..b3cd5f6593 100644 --- a/packages/flutter_driver/lib/src/common/find.dart +++ b/packages/flutter_driver/lib/src/common/find.dart @@ -64,20 +64,6 @@ class WaitFor extends CommandWithTarget { String get kind => 'waitFor'; } -/// The result of a [WaitFor] command. -class WaitForResult extends Result { - /// Creates a [WaitForResult]. - const WaitForResult(); - - /// Deserializes the result from JSON. - static WaitForResult fromJson(Map json) { - return const WaitForResult(); - } - - @override - Map toJson() => {}; -} - /// A Flutter Driver command that waits until [finder] can no longer locate the target. class WaitForAbsent extends CommandWithTarget { /// Creates a command that waits for the widget identified by [finder] to @@ -94,20 +80,6 @@ class WaitForAbsent extends CommandWithTarget { String get kind => 'waitForAbsent'; } -/// The result of a [WaitForAbsent] command. -class WaitForAbsentResult extends Result { - /// Creates a [WaitForAbsentResult]. - const WaitForAbsentResult(); - - /// Deserializes the result from JSON. - static WaitForAbsentResult fromJson(Map json) { - return const WaitForAbsentResult(); - } - - @override - Map toJson() => {}; -} - /// Base class for Flutter Driver finders, objects that describe how the driver /// should search for elements. abstract class SerializableFinder { diff --git a/packages/flutter_driver/lib/src/common/frame_sync.dart b/packages/flutter_driver/lib/src/common/frame_sync.dart index a9fd347125..e9bd0596fc 100644 --- a/packages/flutter_driver/lib/src/common/frame_sync.dart +++ b/packages/flutter_driver/lib/src/common/frame_sync.dart @@ -25,17 +25,3 @@ class SetFrameSync extends Command { 'enabled': '$enabled', }); } - -/// The result of a [SetFrameSync] command. -class SetFrameSyncResult extends Result { - /// Creates a [SetFrameSyncResult]. - const SetFrameSyncResult(); - - /// Deserializes this result from JSON. - static SetFrameSyncResult fromJson(Map json) { - return const SetFrameSyncResult(); - } - - @override - Map toJson() => {}; -} diff --git a/packages/flutter_driver/lib/src/common/gesture.dart b/packages/flutter_driver/lib/src/common/gesture.dart index edd87c84b1..25ea8545c1 100644 --- a/packages/flutter_driver/lib/src/common/gesture.dart +++ b/packages/flutter_driver/lib/src/common/gesture.dart @@ -4,7 +4,6 @@ import 'deserialization_factory.dart'; import 'find.dart'; -import 'message.dart'; /// A Flutter Driver command that taps on a target widget located by [finder]. class Tap extends CommandWithTarget { @@ -18,21 +17,6 @@ class Tap extends CommandWithTarget { String get kind => 'tap'; } -/// The result of a [Tap] command. -class TapResult extends Result { - /// Creates a [TapResult]. - const TapResult(); - - /// Deserializes this result from JSON. - static TapResult fromJson(Map json) { - return const TapResult(); - } - - @override - Map toJson() => {}; -} - - /// A Flutter Driver command that commands the driver to perform a scrolling action. class Scroll extends CommandWithTarget { /// Creates a scroll command that will attempt to scroll a scrollable view by @@ -78,20 +62,6 @@ class Scroll extends CommandWithTarget { }); } -/// The result of a [Scroll] command. -class ScrollResult extends Result { - /// Creates a [ScrollResult]. - const ScrollResult(); - - /// Deserializes this result from JSON. - static ScrollResult fromJson(Map json) { - return const ScrollResult(); - } - - @override - Map toJson() => {}; -} - /// A Flutter Driver command that commands the driver to ensure that the element /// represented by [finder] has been scrolled completely into view. class ScrollIntoView extends CommandWithTarget { diff --git a/packages/flutter_driver/lib/src/common/handler_factory.dart b/packages/flutter_driver/lib/src/common/handler_factory.dart index 22eb52f3d9..28d1b17da1 100644 --- a/packages/flutter_driver/lib/src/common/handler_factory.dart +++ b/packages/flutter_driver/lib/src/common/handler_factory.dart @@ -154,7 +154,7 @@ mixin CommandHandlerFactory { final TestTextInput _testTextInput = TestTextInput(); /// Deserializes the finder from JSON generated by [Command.serialize] or [CommandWithTarget.serialize]. - Future handleCommand(Command command, WidgetController prober, CreateFinderFactory finderFactory) { + Future handleCommand(Command command, WidgetController prober, CreateFinderFactory finderFactory) { switch(command.kind) { case 'get_health': return _getHealth(command); case 'get_layer_tree': return _getLayerTree(command); @@ -192,14 +192,14 @@ mixin CommandHandlerFactory { return RenderTree(RendererBinding.instance?.renderView.toStringDeep()); } - Future _enterText(Command command) async { + Future _enterText(Command command) async { if (!_testTextInput.isRegistered) { throw 'Unable to fulfill `FlutterDriver.enterText`. Text emulation is ' 'disabled. You can enable it using `FlutterDriver.setTextEntryEmulation`.'; } final EnterText enterTextCommand = command as EnterText; _testTextInput.enterText(enterTextCommand.text); - return const EnterTextResult(); + return Result.empty; } Future _requestData(Command command) async { @@ -210,49 +210,49 @@ mixin CommandHandlerFactory { : await dataHandler(requestDataCommand.message)); } - Future _setFrameSync(Command command) async { + Future _setFrameSync(Command command) async { final SetFrameSync setFrameSyncCommand = command as SetFrameSync; _frameSync = setFrameSyncCommand.enabled; - return const SetFrameSyncResult(); + return Result.empty; } - Future _tap(Command command, WidgetController prober, CreateFinderFactory finderFactory) async { + Future _tap(Command command, WidgetController prober, CreateFinderFactory finderFactory) async { final Tap tapCommand = command as Tap; final Finder computedFinder = await waitForElement( finderFactory.createFinder(tapCommand.finder).hitTestable(), ); await prober.tap(computedFinder); - return const TapResult(); + return Result.empty; } - Future _waitFor(Command command, CreateFinderFactory finderFactory) async { + Future _waitFor(Command command, CreateFinderFactory finderFactory) async { final WaitFor waitForCommand = command as WaitFor; await waitForElement(finderFactory.createFinder(waitForCommand.finder)); - return const WaitForResult(); + return Result.empty; } - Future _waitForAbsent(Command command, CreateFinderFactory finderFactory) async { + Future _waitForAbsent(Command command, CreateFinderFactory finderFactory) async { final WaitForAbsent waitForAbsentCommand = command as WaitForAbsent; await waitForAbsentElement(finderFactory.createFinder(waitForAbsentCommand.finder)); - return const WaitForAbsentResult(); + return Result.empty; } - Future _waitForCondition(Command command) async { + Future _waitForCondition(Command command) async { assert(command != null); final WaitForCondition waitForConditionCommand = command as WaitForCondition; final WaitCondition condition = deserializeCondition(waitForConditionCommand.condition); await condition.wait(); - return null; + return Result.empty; } @Deprecated( 'This method has been deprecated in favor of _waitForCondition. ' 'This feature was deprecated after v1.9.3.' ) - Future _waitUntilNoTransientCallbacks(Command command) async { + Future _waitUntilNoTransientCallbacks(Command command) async { if (SchedulerBinding.instance!.transientCallbackCount != 0) await _waitUntilFrame(() => SchedulerBinding.instance!.transientCallbackCount == 0); - return null; + return Result.empty; } /// Returns a future that waits until no pending frame is scheduled (frame is synced). @@ -278,12 +278,12 @@ mixin CommandHandlerFactory { 'This method has been deprecated in favor of _waitForCondition. ' 'This feature was deprecated after v1.9.3.' ) - Future _waitUntilNoPendingFrame(Command command) async { + Future _waitUntilNoPendingFrame(Command command) async { await _waitUntilFrame(() { return SchedulerBinding.instance!.transientCallbackCount == 0 && !SchedulerBinding.instance!.hasScheduledFrame; }); - return null; + return Result.empty; } Future _getSemanticsId(Command command, CreateFinderFactory finderFactory) async { @@ -351,7 +351,7 @@ mixin CommandHandlerFactory { ))); } - Future _scroll(Command command, WidgetController _prober, CreateFinderFactory finderFactory) async { + Future _scroll(Command command, WidgetController _prober, CreateFinderFactory finderFactory) async { final Scroll scrollCommand = command as Scroll; final Finder target = await waitForElement(finderFactory.createFinder(scrollCommand.finder)); final int totalMoves = scrollCommand.duration.inMicroseconds * scrollCommand.frequency ~/ Duration.microsecondsPerSecond; @@ -369,14 +369,14 @@ mixin CommandHandlerFactory { } _prober.binding.handlePointerEvent(pointer.up()); - return const ScrollResult(); + return Result.empty; } - Future _scrollIntoView(Command command, CreateFinderFactory finderFactory) async { + Future _scrollIntoView(Command command, CreateFinderFactory finderFactory) async { final ScrollIntoView scrollIntoViewCommand = command as ScrollIntoView; final Finder target = await waitForElement(finderFactory.createFinder(scrollIntoViewCommand.finder)); await Scrollable.ensureVisible(target.evaluate().single, duration: const Duration(milliseconds: 100), alignment: scrollIntoViewCommand.alignment); - return const ScrollResult(); + return Result.empty; } Future _getText(Command command, CreateFinderFactory finderFactory) async { @@ -408,14 +408,14 @@ mixin CommandHandlerFactory { return GetTextResult(text); } - Future _setTextEntryEmulation(Command command) async { + Future _setTextEntryEmulation(Command command) async { final SetTextEntryEmulation setTextEntryEmulationCommand = command as SetTextEntryEmulation; if (setTextEntryEmulationCommand.enabled) { _testTextInput.register(); } else { _testTextInput.unregister(); } - return const SetTextEntryEmulationResult(); + return Result.empty; } SemanticsHandle? _semantics; @@ -446,9 +446,9 @@ mixin CommandHandlerFactory { 'This method has been deprecated in favor of _waitForCondition. ' 'This feature was deprecated after v1.9.3.' ) - Future _waitUntilFirstFrameRasterized(Command command) async { + Future _waitUntilFirstFrameRasterized(Command command) async { await WidgetsBinding.instance!.waitUntilFirstFrameRasterized; - return null; + return Result.empty; } /// Runs `finder` repeatedly until it finds one or more [Element]s. diff --git a/packages/flutter_driver/lib/src/common/message.dart b/packages/flutter_driver/lib/src/common/message.dart index 893af074eb..ee0829e325 100644 --- a/packages/flutter_driver/lib/src/common/message.dart +++ b/packages/flutter_driver/lib/src/common/message.dart @@ -64,6 +64,19 @@ abstract class Result { /// A const constructor to allow subclasses to be const. const Result(); + /// An empty responds that does not include any result data. + /// + /// Consider using this object as a result for [Command]s that do not return + /// any data. + static const Result empty = _EmptyResult(); + /// Serializes this message to a JSON map. Map toJson(); } + +class _EmptyResult extends Result { + const _EmptyResult(); + + @override + Map toJson() => {}; +} diff --git a/packages/flutter_driver/lib/src/common/text.dart b/packages/flutter_driver/lib/src/common/text.dart index a394fbe549..f3f1b4bd1c 100644 --- a/packages/flutter_driver/lib/src/common/text.dart +++ b/packages/flutter_driver/lib/src/common/text.dart @@ -59,20 +59,6 @@ class EnterText extends Command { }); } -/// The result of the [EnterText] command. -class EnterTextResult extends Result { - /// Creates a successful result of entering the text. - const EnterTextResult(); - - /// Deserializes the result from JSON. - static EnterTextResult fromJson(Map json) { - return const EnterTextResult(); - } - - @override - Map toJson() => const {}; -} - /// A Flutter Driver command that enables and disables text entry emulation. class SetTextEntryEmulation extends Command { /// Creates a command that enables and disables text entry emulation. @@ -94,17 +80,3 @@ class SetTextEntryEmulation extends Command { 'enabled': '$enabled', }); } - -/// The result of the [SetTextEntryEmulation] command. -class SetTextEntryEmulationResult extends Result { - /// Creates a successful result. - const SetTextEntryEmulationResult(); - - /// Deserializes the result from JSON. - static SetTextEntryEmulationResult fromJson(Map json) { - return const SetTextEntryEmulationResult(); - } - - @override - Map toJson() => const {}; -} diff --git a/packages/flutter_driver/lib/src/extension/extension.dart b/packages/flutter_driver/lib/src/extension/extension.dart index d0219174b3..657ce7c0ee 100644 --- a/packages/flutter_driver/lib/src/extension/extension.dart +++ b/packages/flutter_driver/lib/src/extension/extension.dart @@ -433,7 +433,7 @@ class FlutterDriverExtension with DeserializeFinderFactory, CreateFinderFactory, } @override - Future handleCommand(Command command, WidgetController prober, CreateFinderFactory finderFactory) { + Future handleCommand(Command command, WidgetController prober, CreateFinderFactory finderFactory) { final String kind = command.kind; if(_commandExtensions.containsKey(kind)) { return _commandExtensions[kind]!.call(command, prober, finderFactory, this); diff --git a/packages/flutter_driver/test/src/real_tests/extension_test.dart b/packages/flutter_driver/test/src/real_tests/extension_test.dart index 9542ac71a0..36c39c53e8 100644 --- a/packages/flutter_driver/test/src/real_tests/extension_test.dart +++ b/packages/flutter_driver/test/src/real_tests/extension_test.dart @@ -56,7 +56,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -81,7 +81,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -117,7 +117,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -142,7 +142,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -159,7 +159,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -182,7 +182,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -201,7 +201,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -230,7 +230,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -259,7 +259,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -277,7 +277,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -310,7 +310,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -359,7 +359,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -410,7 +410,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -460,7 +460,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -1145,7 +1145,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -1171,7 +1171,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); }); @@ -1195,7 +1195,7 @@ void main() { result, { 'isError': false, - 'response': null, + 'response': {}, }, ); });