1// Copyright 2016 The Chromium 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 5import 'package:meta/meta.dart'; 6 7/// An object sent from the Flutter Driver to a Flutter application to instruct 8/// the application to perform a task. 9abstract class Command { 10 /// Abstract const constructor. This constructor enables subclasses to provide 11 /// const constructors so that they can be used in const expressions. 12 const Command({ this.timeout }); 13 14 /// Deserializes this command from the value generated by [serialize]. 15 Command.deserialize(Map<String, String> json) 16 : timeout = _parseTimeout(json); 17 18 static Duration _parseTimeout(Map<String, String> json) { 19 final String timeout = json['timeout']; 20 if (timeout == null) 21 return null; 22 return Duration(milliseconds: int.parse(timeout)); 23 } 24 25 /// The maximum amount of time to wait for the command to complete. 26 /// 27 /// Defaults to no timeout, because it is common for operations to take oddly 28 /// long in test environments (e.g. because the test host is overloaded), and 29 /// having timeouts essentially means having race conditions. 30 final Duration timeout; 31 32 /// Identifies the type of the command object and of the handler. 33 String get kind; 34 35 /// Whether this command requires the widget tree to be initialized before 36 /// the command may be run. 37 /// 38 /// This defaults to true to force the application under test to call [runApp] 39 /// before attempting to remotely drive the application. Subclasses may 40 /// override this to return false if they allow invocation before the 41 /// application has started. 42 /// 43 /// See also: 44 /// 45 /// * [WidgetsBinding.isRootWidgetAttached], which indicates whether the 46 /// widget tree has been initialized. 47 bool get requiresRootWidgetAttached => true; 48 49 /// Serializes this command to parameter name/value pairs. 50 @mustCallSuper 51 Map<String, String> serialize() { 52 final Map<String, String> result = <String, String>{ 53 'command': kind, 54 }; 55 if (timeout != null) 56 result['timeout'] = '${timeout.inMilliseconds}'; 57 return result; 58 } 59} 60 61/// An object sent from a Flutter application back to the Flutter Driver in 62/// response to a command. 63abstract class Result { 64 /// A const constructor to allow subclasses to be const. 65 const Result(); 66 67 /// Serializes this message to a JSON map. 68 Map<String, dynamic> toJson(); 69} 70