-
Notifications
You must be signed in to change notification settings - Fork 27.2k
/
find.dart
348 lines (287 loc) · 11.3 KB
/
find.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// 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.
import 'package:meta/meta.dart';
import 'error.dart';
import 'message.dart';
const List<Type> _supportedKeyValueTypes = <Type>[String, int];
DriverError _createInvalidKeyValueTypeError(String invalidType) {
return DriverError('Unsupported key value type $invalidType. Flutter Driver only supports ${_supportedKeyValueTypes.join(", ")}');
}
/// A Flutter Driver 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 {
/// Constructs this command given a [finder].
CommandWithTarget(this.finder, {Duration timeout}) : super(timeout: timeout) {
if (finder == null)
throw DriverError('$runtimeType target cannot be null');
}
/// Deserializes this command from the value generated by [serialize].
CommandWithTarget.deserialize(Map<String, String> json)
: finder = SerializableFinder.deserialize(json),
super.deserialize(json);
/// 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,
/// });
@override
Map<String, String> serialize() =>
super.serialize()..addAll(finder.serialize());
}
/// A Flutter Driver command that waits until [finder] can locate the target.
class WaitFor extends CommandWithTarget {
/// 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.
WaitFor(SerializableFinder finder, {Duration timeout})
: super(finder, timeout: timeout);
/// Deserializes this command from the value generated by [serialize].
WaitFor.deserialize(Map<String, String> json) : super.deserialize(json);
@override
final String kind = 'waitFor';
}
/// The result of a [WaitFor] command.
class WaitForResult extends Result {
/// Deserializes the result from JSON.
static WaitForResult fromJson(Map<String, dynamic> json) {
return WaitForResult();
}
@override
Map<String, dynamic> toJson() => <String, dynamic>{};
}
/// 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
/// disappear within the [timeout] amount of time.
///
/// If [timeout] is not specified the command times out after 5 seconds.
WaitForAbsent(SerializableFinder finder, {Duration timeout})
: super(finder, timeout: timeout);
/// Deserializes this command from the value generated by [serialize].
WaitForAbsent.deserialize(Map<String, String> json) : super.deserialize(json);
@override
final String kind = 'waitForAbsent';
}
/// The result of a [WaitForAbsent] command.
class WaitForAbsentResult extends Result {
/// Deserializes the result from JSON.
static WaitForAbsentResult fromJson(Map<String, dynamic> json) {
return WaitForAbsentResult();
}
@override
Map<String, dynamic> toJson() => <String, dynamic>{};
}
/// A Flutter Driver command that waits until there are no more transient callbacks in the queue.
class WaitUntilNoTransientCallbacks extends Command {
/// Creates a command that waits for there to be no transient callbacks.
WaitUntilNoTransientCallbacks({ Duration timeout }) : super(timeout: timeout);
/// Deserializes this command from the value generated by [serialize].
WaitUntilNoTransientCallbacks.deserialize(Map<String, String> json)
: super.deserialize(json);
@override
final String kind = 'waitUntilNoTransientCallbacks';
}
/// Base class for Flutter Driver finders, objects that describe how the driver
/// should search for elements.
abstract class SerializableFinder {
/// Identifies the type of finder to be used by the driver extension.
String get finderType;
/// Serializes common fields to JSON.
///
/// Methods that override [serialize] are expected to call `super.serialize`
/// and add more fields to the returned [Map].
@mustCallSuper
Map<String, String> serialize() => <String, String>{
'finderType': finderType,
};
/// Deserializes a finder from JSON generated by [serialize].
static SerializableFinder deserialize(Map<String, String> json) {
final String finderType = json['finderType'];
switch (finderType) {
case 'ByType': return ByType.deserialize(json);
case 'ByValueKey': return ByValueKey.deserialize(json);
case 'ByTooltipMessage': return ByTooltipMessage.deserialize(json);
case 'BySemanticsLabel': return BySemanticsLabel.deserialize(json);
case 'ByText': return ByText.deserialize(json);
case 'PageBack': return PageBack();
}
throw DriverError('Unsupported search specification type $finderType');
}
}
/// A Flutter Driver finder that finds widgets by tooltip text.
class ByTooltipMessage extends SerializableFinder {
/// Creates a tooltip finder given the tooltip's message [text].
ByTooltipMessage(this.text);
/// Tooltip message text.
final String text;
@override
final String finderType = 'ByTooltipMessage';
@override
Map<String, String> serialize() => super.serialize()..addAll(<String, String>{
'text': text,
});
/// Deserializes the finder from JSON generated by [serialize].
static ByTooltipMessage deserialize(Map<String, String> json) {
return ByTooltipMessage(json['text']);
}
}
/// A Flutter Driver finder that finds widgets by semantic label.
///
/// If the [label] property is a [String], the finder will try to find an exact
/// match. If it is a [RegExp], it will return true for [RegExp.hasMatch].
class BySemanticsLabel extends SerializableFinder {
/// Creates a semantic label finder given the [label].
BySemanticsLabel(this.label);
/// A [Pattern] matching the [Semantics.properties.label].
///
/// If this is a [String], it will be treated as an exact match.
final Pattern label;
@override
final String finderType = 'BySemanticsLabel';
@override
Map<String, String> serialize() {
if (label is RegExp) {
final RegExp regExp = label;
return super.serialize()..addAll(<String, String>{
'label': regExp.pattern,
'isRegExp': 'true',
});
} else {
return super.serialize()..addAll(<String, String>{
'label': label,
});
}
}
/// Deserializes the finder from JSON generated by [serialize].
static BySemanticsLabel deserialize(Map<String, String> json) {
final bool isRegExp = json['isRegExp'] == 'true';
return BySemanticsLabel(isRegExp ? RegExp(json['label']) : json['label']);
}
}
/// A Flutter Driver finder that finds widgets by [text] inside a [Text] or
/// [EditableText] widget.
class ByText extends SerializableFinder {
/// Creates a text finder given the text.
ByText(this.text);
/// The text that appears inside the [Text] or [EditableText] widget.
final String text;
@override
final String finderType = 'ByText';
@override
Map<String, String> serialize() => super.serialize()..addAll(<String, String>{
'text': text,
});
/// Deserializes the finder from JSON generated by [serialize].
static ByText deserialize(Map<String, String> json) {
return ByText(json['text']);
}
}
/// A Flutter Driver finder that finds widgets by `ValueKey`.
class ByValueKey extends SerializableFinder {
/// Creates a finder given the key value.
ByValueKey(this.keyValue)
: keyValueString = '$keyValue',
keyValueType = '${keyValue.runtimeType}' {
if (!_supportedKeyValueTypes.contains(keyValue.runtimeType))
throw _createInvalidKeyValueTypeError('$keyValue.runtimeType');
}
/// 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;
@override
final String finderType = 'ByValueKey';
@override
Map<String, String> serialize() => super.serialize()..addAll(<String, String>{
'keyValueString': keyValueString,
'keyValueType': keyValueType,
});
/// Deserializes the finder from JSON generated by [serialize].
static ByValueKey deserialize(Map<String, String> json) {
final String keyValueString = json['keyValueString'];
final String keyValueType = json['keyValueType'];
switch (keyValueType) {
case 'int':
return ByValueKey(int.parse(keyValueString));
case 'String':
return ByValueKey(keyValueString);
default:
throw _createInvalidKeyValueTypeError(keyValueType);
}
}
}
/// A Flutter Driver finder that finds widgets by their [runtimeType].
class ByType extends SerializableFinder {
/// Creates a finder that given the runtime type in string form.
ByType(this.type);
/// The widget's [runtimeType], in string form.
final String type;
@override
final String finderType = 'ByType';
@override
Map<String, String> serialize() => super.serialize()..addAll(<String, String>{
'type': type,
});
/// Deserializes the finder from JSON generated by [serialize].
static ByType deserialize(Map<String, String> json) {
return ByType(json['type']);
}
}
/// A Flutter Driver finder that finds the back button on the page's Material
/// or Cupertino scaffold.
///
/// See also:
///
/// * [WidgetTester.pageBack], for a similar functionality in widget tests.
class PageBack extends SerializableFinder {
@override
String get finderType => 'PageBack';
}
/// A Flutter driver command that retrieves a semantics id using a specified finder.
///
/// This command requires assertions to be enabled on the device.
///
/// If the object returned by the finder does not have its own semantics node,
/// then the semantics node of the first ancestor is returned instead.
///
/// Throws an error if a finder returns multiple objects or if there are no
/// semantics nodes.
///
/// Semantics must be enabled to use this method, either using a platform
/// specific shell command or [FlutterDriver.setSemantics].
class GetSemanticsId extends CommandWithTarget {
/// Creates a command which finds a Widget and then looks up the semantic id.
GetSemanticsId(SerializableFinder finder, {Duration timeout}) : super(finder, timeout: timeout);
/// Creates a command from a json map.
GetSemanticsId.deserialize(Map<String, String> json)
: super.deserialize(json);
@override
String get kind => 'get_semantics_id';
}
/// The result of a [GetSemanticsId] command.
class GetSemanticsIdResult extends Result {
/// Creates a new [GetSemanticsId] result.
GetSemanticsIdResult(this.id);
/// The semantics id of the node;
final int id;
/// Deserializes this result from JSON.
static GetSemanticsIdResult fromJson(Map<String, dynamic> json) {
return GetSemanticsIdResult(json['id']);
}
@override
Map<String, dynamic> toJson() => <String, dynamic>{'id': id};
}