find.dart 7.41 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:meta/meta.dart';

7 8 9 10 11
import 'error.dart';
import 'message.dart';

const List<Type> _supportedKeyValueTypes = const <Type>[String, int];

12 13 14 15
DriverError _createInvalidKeyValueTypeError(String invalidType) {
  return new DriverError('Unsupported key value type $invalidType. Flutter Driver only supports ${_supportedKeyValueTypes.join(", ")}');
}

16 17 18 19 20 21
/// A command aimed at an object to be located by [finder].
///
/// Implementations must provide a concrete [kind]. If additional data is
/// required beyond the [finder] the implementation may override [serialize]
/// and add more keys to the returned map.
abstract class CommandWithTarget extends Command {
22
  /// Constructs this command given a [finder].
23
  CommandWithTarget(this.finder, {Duration timeout}) : super(timeout: timeout) {
24 25 26 27
    if (finder == null)
      throw new DriverError('${this.runtimeType} target cannot be null');
  }

28 29 30 31 32
  /// Deserializes the command from JSON generated by [serialize].
  CommandWithTarget.deserialize(Map<String, String> json)
      : finder = SerializableFinder.deserialize(json),
        super.deserialize(json);

33 34 35 36 37 38 39 40 41 42 43
  /// Locates the object or objects targeted by this command.
  final SerializableFinder finder;

  /// This method is meant to be overridden if data in addition to [finder]
  /// is serialized to JSON.
  ///
  /// Example:
  ///
  ///     Map<String, String> toJson() => super.toJson()..addAll({
  ///       'foo': this.foo,
  ///     });
44
  @override
45 46
  Map<String, String> serialize() =>
      super.serialize()..addAll(finder.serialize());
47 48
}

49 50
/// Waits until [finder] can locate the target.
class WaitFor extends CommandWithTarget {
51
  @override
52
  final String kind = 'waitFor';
53

54 55 56 57
  /// Creates a command that waits for the widget identified by [finder] to
  /// appear within the [timeout] amount of time.
  ///
  /// If [timeout] is not specified the command times out after 5 seconds.
58 59
  WaitFor(SerializableFinder finder, {Duration timeout})
      : super(finder, timeout: timeout);
60

61
  /// Deserializes the command from JSON generated by [serialize].
62
  WaitFor.deserialize(Map<String, String> json) : super.deserialize(json);
63
}
64

65 66 67 68 69 70 71 72 73 74 75 76
/// Waits until there are no more transient callbacks in the queue.
class WaitUntilNoTransientCallbacks extends Command {
  @override
  final String kind = 'waitUntilNoTransientCallbacks';

  WaitUntilNoTransientCallbacks({Duration timeout}) : super(timeout: timeout);

  /// Deserializes the command from JSON generated by [serialize].
  WaitUntilNoTransientCallbacks.deserialize(Map<String, String> json)
      : super.deserialize(json);
}

77
/// The result of a [WaitFor] command.
78
class WaitForResult extends Result {
79
  /// Deserializes the result from JSON.
80 81 82
  static WaitForResult fromJson(Map<String, dynamic> json) {
    return new WaitForResult();
  }
83 84

  @override
85
  Map<String, dynamic> toJson() => <String, dynamic>{};
86 87 88
}

/// Describes how to the driver should search for elements.
89
abstract class SerializableFinder {
90
  /// Identifies the type of finder to be used by the driver extension.
91
  String get finderType;
92

93
  /// Deserializes a finder from JSON generated by [serialize].
94
  static SerializableFinder deserialize(Map<String, String> json) {
95
    final String finderType = json['finderType'];
96
    switch(finderType) {
97 98 99
      case 'ByValueKey': return ByValueKey.deserialize(json);
      case 'ByTooltipMessage': return ByTooltipMessage.deserialize(json);
      case 'ByText': return ByText.deserialize(json);
100
    }
101
    throw new DriverError('Unsupported search specification type $finderType');
102 103
  }

104 105 106 107 108
  /// Serializes common fields to JSON.
  ///
  /// Methods that override [serialize] are expected to call `super.serialize`
  /// and add more fields to the returned [Map].
  @mustCallSuper
pq's avatar
pq committed
109
  Map<String, String> serialize() => <String, String>{
110
    'finderType': finderType,
111 112 113
  };
}

114 115
/// Finds widgets by tooltip text.
class ByTooltipMessage extends SerializableFinder {
116
  @override
117
  final String finderType = 'ByTooltipMessage';
118

119
  /// Creates a tooltip finder given the tooltip's message [text].
120 121 122 123 124
  ByTooltipMessage(this.text);

  /// Tooltip message text.
  final String text;

125
  @override
pq's avatar
pq committed
126
  Map<String, String> serialize() => super.serialize()..addAll(<String, String>{
127 128 129
    'text': text,
  });

130
  /// Deserializes the finder from JSON generated by [serialize].
131
  static ByTooltipMessage deserialize(Map<String, String> json) {
132 133 134 135
    return new ByTooltipMessage(json['text']);
  }
}

136 137
/// Finds widgets by [text] inside a `Text` widget.
class ByText extends SerializableFinder {
138
  @override
139
  final String finderType = 'ByText';
140

141
  /// Creates a text finder given the text.
142 143
  ByText(this.text);

144
  /// The text that appears inside the `Text` widget.
145 146
  final String text;

147
  @override
pq's avatar
pq committed
148
  Map<String, String> serialize() => super.serialize()..addAll(<String, String>{
149 150 151
    'text': text,
  });

152
  /// Deserializes the finder from JSON generated by [serialize].
153
  static ByText deserialize(Map<String, String> json) {
154 155 156 157
    return new ByText(json['text']);
  }
}

158 159
/// Finds widgets by `ValueKey`.
class ByValueKey extends SerializableFinder {
160
  @override
161
  final String finderType = 'ByValueKey';
162

163
  /// Creates a finder given the key value.
164 165
  ByValueKey(this.keyValue)
    : this.keyValueString = '$keyValue',
166 167
      this.keyValueType = '${keyValue.runtimeType}' {
    if (!_supportedKeyValueTypes.contains(keyValue.runtimeType))
168
      throw _createInvalidKeyValueTypeError('$keyValue.runtimeType');
169 170 171 172 173 174 175 176 177 178 179 180 181
  }

  /// The true value of the key.
  final dynamic keyValue;

  /// Stringified value of the key (we can only send strings to the VM service)
  final String keyValueString;

  /// The type name of the key.
  ///
  /// May be one of "String", "int". The list of supported types may change.
  final String keyValueType;

182
  @override
pq's avatar
pq committed
183
  Map<String, String> serialize() => super.serialize()..addAll(<String, String>{
184 185
    'keyValueString': keyValueString,
    'keyValueType': keyValueType,
186
  });
187

188
  /// Deserializes the finder from JSON generated by [serialize].
189
  static ByValueKey deserialize(Map<String, String> json) {
190 191
    final String keyValueString = json['keyValueString'];
    final String keyValueType = json['keyValueType'];
192 193
    switch(keyValueType) {
      case 'int':
194
        return new ByValueKey(int.parse(keyValueString));
195
      case 'String':
196
        return new ByValueKey(keyValueString);
197
      default:
198
        throw _createInvalidKeyValueTypeError(keyValueType);
199 200 201 202 203 204
    }
  }
}

/// Command to read the text from a given element.
class GetText extends CommandWithTarget {
205
  @override
206 207
  final String kind = 'get_text';

208
  /// [finder] looks for an element that contains a piece of text.
209
  GetText(SerializableFinder finder, { Duration timeout }) : super(finder, timeout: timeout);
210

211
  /// Deserializes the command from JSON generated by [serialize].
212
  GetText.deserialize(Map<String, dynamic> json) : super.deserialize(json);
213

214
  @override
215
  Map<String, String> serialize() => super.serialize();
216 217
}

218
/// The result of the [GetText] command.
219
class GetTextResult extends Result {
220
  /// Creates a result with the given [text].
221 222
  GetTextResult(this.text);

223
  /// The text extracted by the [GetText] command.
224 225
  final String text;

226
  /// Deserializes the result from JSON.
Ian Hickson's avatar
Ian Hickson committed
227 228 229 230
  static GetTextResult fromJson(Map<String, dynamic> json) {
    return new GetTextResult(json['text']);
  }

231
  @override
pq's avatar
pq committed
232
  Map<String, dynamic> toJson() => <String, String>{
233 234 235
    'text': text,
  };
}