"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/packages/flutter_driver/lib/src/common/wait.dart" (13 Nov 2020, 11998 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 import 'dart:convert';
    6 
    7 import 'message.dart';
    8 
    9 /// A Flutter Driver command that waits until a given [condition] is satisfied.
   10 class WaitForCondition extends Command {
   11   /// Creates a command that waits for the given [condition] is met.
   12   ///
   13   /// The [condition] argument must not be null.
   14   const WaitForCondition(this.condition, {Duration timeout})
   15       : assert(condition != null),
   16         super(timeout: timeout);
   17 
   18   /// Deserializes this command from the value generated by [serialize].
   19   ///
   20   /// The [json] argument cannot be null.
   21   WaitForCondition.deserialize(Map<String, String> json)
   22       : assert(json != null),
   23         condition = _deserialize(json),
   24         super.deserialize(json);
   25 
   26   /// The condition that this command shall wait for.
   27   final SerializableWaitCondition condition;
   28 
   29   @override
   30   Map<String, String> serialize() => super.serialize()..addAll(condition.serialize());
   31 
   32   @override
   33   String get kind => 'waitForCondition';
   34 
   35   @override
   36   bool get requiresRootWidgetAttached => condition.requiresRootWidgetAttached;
   37 }
   38 
   39 /// A Flutter Driver command that waits until there are no more transient callbacks in the queue.
   40 ///
   41 /// This command has been deprecated in favor of [WaitForCondition]. Construct
   42 /// a command that waits until no transient callbacks as follows:
   43 ///
   44 /// ```dart
   45 /// WaitForCondition noTransientCallbacks = WaitForCondition(NoTransientCallbacks());
   46 /// ```
   47 @Deprecated(
   48   'This command has been deprecated in favor of WaitForCondition. '
   49   'Use WaitForCondition command with NoTransientCallbacks. '
   50   'This feature was deprecated after v1.9.3.'
   51 )
   52 class WaitUntilNoTransientCallbacks extends Command {
   53   /// Creates a command that waits for there to be no transient callbacks.
   54   const WaitUntilNoTransientCallbacks({ Duration timeout }) : super(timeout: timeout);
   55 
   56   /// Deserializes this command from the value generated by [serialize].
   57   WaitUntilNoTransientCallbacks.deserialize(Map<String, String> json)
   58       : super.deserialize(json);
   59 
   60   @override
   61   String get kind => 'waitUntilNoTransientCallbacks';
   62 }
   63 
   64 /// A Flutter Driver command that waits until the frame is synced.
   65 ///
   66 /// This command has been deprecated in favor of [WaitForCondition]. Construct
   67 /// a command that waits until no pending frame as follows:
   68 ///
   69 /// ```dart
   70 /// WaitForCondition noPendingFrame = WaitForCondition(NoPendingFrame());
   71 /// ```
   72 @Deprecated(
   73   'This command has been deprecated in favor of WaitForCondition. '
   74   'Use WaitForCondition command with NoPendingFrame. '
   75   'This feature was deprecated after v1.9.3.'
   76 )
   77 class WaitUntilNoPendingFrame extends Command {
   78   /// Creates a command that waits until there's no pending frame scheduled.
   79   const WaitUntilNoPendingFrame({ Duration timeout }) : super(timeout: timeout);
   80 
   81   /// Deserializes this command from the value generated by [serialize].
   82   WaitUntilNoPendingFrame.deserialize(Map<String, String> json)
   83       : super.deserialize(json);
   84 
   85   @override
   86   String get kind => 'waitUntilNoPendingFrame';
   87 }
   88 
   89 /// A Flutter Driver command that waits until the Flutter engine rasterizes the
   90 /// first frame.
   91 ///
   92 /// {@template flutter.frame_rasterized_vs_presented}
   93 /// Usually, the time that a frame is rasterized is very close to the time that
   94 /// it gets presented on the display. Specifically, rasterization is the last
   95 /// expensive phase of a frame that's still in Flutter's control.
   96 /// {@endtemplate}
   97 ///
   98 /// This command has been deprecated in favor of [WaitForCondition]. Construct
   99 /// a command that waits until no pending frame as follows:
  100 ///
  101 /// ```dart
  102 /// WaitForCondition firstFrameRasterized = WaitForCondition(FirstFrameRasterized());
  103 /// ```
  104 @Deprecated(
  105   'This command has been deprecated in favor of WaitForCondition. '
  106   'Use WaitForCondition command with FirstFrameRasterized. '
  107   'This feature was deprecated after v1.9.3.'
  108 )
  109 class WaitUntilFirstFrameRasterized extends Command {
  110   /// Creates this command.
  111   const WaitUntilFirstFrameRasterized({ Duration timeout }) : super(timeout: timeout);
  112 
  113   /// Deserializes this command from the value generated by [serialize].
  114   WaitUntilFirstFrameRasterized.deserialize(Map<String, String> json)
  115       : super.deserialize(json);
  116 
  117   @override
  118   String get kind => 'waitUntilFirstFrameRasterized';
  119 }
  120 
  121 /// Thrown to indicate a serialization error.
  122 class SerializationException implements Exception {
  123   /// Creates a [SerializationException] with an optional error message.
  124   const SerializationException([this.message]);
  125 
  126   /// The error message, possibly null.
  127   final String message;
  128 
  129   @override
  130   String toString() => 'SerializationException($message)';
  131 }
  132 
  133 /// Base class for Flutter Driver wait conditions, objects that describe conditions
  134 /// the driver can wait for.
  135 ///
  136 /// This class is sent from the driver script running on the host to the driver
  137 /// extension on device to perform waiting on a given condition. In the extension,
  138 /// it will be converted to a `WaitCondition` that actually defines the wait logic.
  139 ///
  140 /// If you subclass this, you also need to implement a `WaitCondition` in the extension.
  141 abstract class SerializableWaitCondition {
  142   /// A const constructor to allow subclasses to be const.
  143   const SerializableWaitCondition();
  144 
  145   /// Identifies the name of the wait condition.
  146   String get conditionName;
  147 
  148   /// Serializes the object to JSON.
  149   Map<String, String> serialize() {
  150     return <String, String>{
  151       'conditionName': conditionName
  152     };
  153   }
  154 
  155   /// Whether this command requires the widget tree to be initialized before
  156   /// the command may be run.
  157   ///
  158   /// This defaults to true to force the application under test to call [runApp]
  159   /// before attempting to remotely drive the application. Subclasses may
  160   /// override this to return false if they allow invocation before the
  161   /// application has started.
  162   ///
  163   /// See also:
  164   ///
  165   ///  * [WidgetsBinding.isRootWidgetAttached], which indicates whether the
  166   ///    widget tree has been initialized.
  167   bool get requiresRootWidgetAttached => true;
  168 }
  169 
  170 /// A condition that waits until no transient callbacks are scheduled.
  171 class NoTransientCallbacks extends SerializableWaitCondition {
  172   /// Creates a [NoTransientCallbacks] condition.
  173   const NoTransientCallbacks();
  174 
  175   /// Factory constructor to parse a [NoTransientCallbacks] instance from the
  176   /// given JSON map.
  177   ///
  178   /// The [json] argument must not be null.
  179   factory NoTransientCallbacks.deserialize(Map<String, String> json) {
  180     assert(json != null);
  181     if (json['conditionName'] != 'NoTransientCallbacksCondition')
  182       throw SerializationException('Error occurred during deserializing the NoTransientCallbacksCondition JSON string: $json');
  183     return const NoTransientCallbacks();
  184   }
  185 
  186   @override
  187   String get conditionName => 'NoTransientCallbacksCondition';
  188 }
  189 
  190 /// A condition that waits until no pending frame is scheduled.
  191 class NoPendingFrame extends SerializableWaitCondition {
  192   /// Creates a [NoPendingFrame] condition.
  193   const NoPendingFrame();
  194 
  195   /// Factory constructor to parse a [NoPendingFrame] instance from the given
  196   /// JSON map.
  197   ///
  198   /// The [json] argument must not be null.
  199   factory NoPendingFrame.deserialize(Map<String, String> json) {
  200     assert(json != null);
  201     if (json['conditionName'] != 'NoPendingFrameCondition')
  202       throw SerializationException('Error occurred during deserializing the NoPendingFrameCondition JSON string: $json');
  203     return const NoPendingFrame();
  204   }
  205 
  206   @override
  207   String get conditionName => 'NoPendingFrameCondition';
  208 }
  209 
  210 /// A condition that waits until the Flutter engine has rasterized the first frame.
  211 class FirstFrameRasterized extends SerializableWaitCondition {
  212   /// Creates a [FirstFrameRasterized] condition.
  213   const FirstFrameRasterized();
  214 
  215   /// Factory constructor to parse a [FirstFrameRasterized] instance from the
  216   /// given JSON map.
  217   ///
  218   /// The [json] argument must not be null.
  219   factory FirstFrameRasterized.deserialize(Map<String, String> json) {
  220     assert(json != null);
  221     if (json['conditionName'] != 'FirstFrameRasterizedCondition')
  222       throw SerializationException('Error occurred during deserializing the FirstFrameRasterizedCondition JSON string: $json');
  223     return const FirstFrameRasterized();
  224   }
  225 
  226   @override
  227   String get conditionName => 'FirstFrameRasterizedCondition';
  228 
  229   @override
  230   bool get requiresRootWidgetAttached => false;
  231 }
  232 
  233 /// A condition that waits until there are no pending platform messages.
  234 class NoPendingPlatformMessages extends SerializableWaitCondition {
  235   /// Creates a [NoPendingPlatformMessages] condition.
  236   const NoPendingPlatformMessages();
  237 
  238   /// Factory constructor to parse a [NoPendingPlatformMessages] instance from the
  239   /// given JSON map.
  240   ///
  241   /// The [json] argument must not be null.
  242   factory NoPendingPlatformMessages.deserialize(Map<String, String> json) {
  243     assert(json != null);
  244     if (json['conditionName'] != 'NoPendingPlatformMessagesCondition')
  245       throw SerializationException('Error occurred during deserializing the NoPendingPlatformMessagesCondition JSON string: $json');
  246     return const NoPendingPlatformMessages();
  247   }
  248 
  249   @override
  250   String get conditionName => 'NoPendingPlatformMessagesCondition';
  251 }
  252 
  253 /// A combined condition that waits until all the given [conditions] are met.
  254 class CombinedCondition extends SerializableWaitCondition {
  255   /// Creates a [CombinedCondition] condition.
  256   ///
  257   /// The [conditions] argument must not be null.
  258   const CombinedCondition(this.conditions)
  259       : assert(conditions != null);
  260 
  261   /// Factory constructor to parse a [CombinedCondition] instance from the
  262   /// given JSON map.
  263   ///
  264   /// The [jsonMap] argument must not be null.
  265   factory CombinedCondition.deserialize(Map<String, String> jsonMap) {
  266     assert(jsonMap != null);
  267     if (jsonMap['conditionName'] != 'CombinedCondition')
  268       throw SerializationException('Error occurred during deserializing the CombinedCondition JSON string: $jsonMap');
  269     if (jsonMap['conditions'] == null) {
  270       return const CombinedCondition(<SerializableWaitCondition>[]);
  271     }
  272 
  273     final List<SerializableWaitCondition> conditions = <SerializableWaitCondition>[];
  274     for (final Map<String, dynamic> condition in (json.decode(jsonMap['conditions']) as List<dynamic>).cast<Map<String, dynamic>>()) {
  275       conditions.add(_deserialize(condition.cast<String, String>()));
  276     }
  277     return CombinedCondition(conditions);
  278   }
  279 
  280   /// A list of conditions it waits for.
  281   final List<SerializableWaitCondition> conditions;
  282 
  283   @override
  284   String get conditionName => 'CombinedCondition';
  285 
  286   @override
  287   Map<String, String> serialize() {
  288     final Map<String, String> jsonMap = super.serialize();
  289     final List<Map<String, String>> jsonConditions = conditions.map(
  290       (SerializableWaitCondition condition) {
  291         assert(condition != null);
  292         return condition.serialize();
  293       }).toList();
  294     jsonMap['conditions'] = json.encode(jsonConditions);
  295     return jsonMap;
  296   }
  297 }
  298 
  299 /// Parses a [SerializableWaitCondition] or its subclass from the given [json] map.
  300 ///
  301 /// The [json] argument must not be null.
  302 SerializableWaitCondition _deserialize(Map<String, String> json) {
  303   assert(json != null);
  304   final String conditionName = json['conditionName'];
  305   switch (conditionName) {
  306     case 'NoTransientCallbacksCondition':
  307       return NoTransientCallbacks.deserialize(json);
  308     case 'NoPendingFrameCondition':
  309       return NoPendingFrame.deserialize(json);
  310     case 'FirstFrameRasterizedCondition':
  311       return FirstFrameRasterized.deserialize(json);
  312     case 'NoPendingPlatformMessagesCondition':
  313       return NoPendingPlatformMessages.deserialize(json);
  314     case 'CombinedCondition':
  315       return CombinedCondition.deserialize(json);
  316   }
  317   throw SerializationException(
  318       'Unsupported wait condition $conditionName in the JSON string $json');
  319 }