Repository navigation
Expand file tree
/
Copy pathextension.dart
More file actions
495 lines (455 loc) · 17 KB
/
Copy pathextension.dart
File metadata and controls
495 lines (455 loc) · 17 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// @docImport 'package:flutter_driver/flutter_driver.dart';
library;
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart' show RendererBinding;
import 'package:flutter/scheduler.dart';
import 'package:flutter/semantics.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import '../common/deserialization_factory.dart';
import '../common/error.dart';
import '../common/find.dart';
import '../common/handler_factory.dart';
import '../common/message.dart';
import '_extension_io.dart' if (dart.library.js_interop) '_extension_web.dart';
const String _extensionMethodName = 'driver';
/// Signature for the handler passed to [enableFlutterDriverExtension].
///
/// Messages are described in string form and should return a [Future] which
/// eventually completes to a string response.
typedef DataHandler = Future<String> Function(String? message);
class _DriverBinding extends BindingBase
with
SchedulerBinding,
ServicesBinding,
GestureBinding,
PaintingBinding,
SemanticsBinding,
RendererBinding,
WidgetsBinding,
TestDefaultBinaryMessengerBinding {
_DriverBinding(
this._handler,
this._silenceErrors,
this._enableTextEntryEmulation,
this.finders,
this.commands,
);
final DataHandler? _handler;
final bool _silenceErrors;
final bool _enableTextEntryEmulation;
final List<FinderExtension>? finders;
final List<CommandExtension>? commands;
// Because you can't really control which zone a driver test uses,
// we override the test for zones here.
@override
bool debugCheckZone(String entryPoint) {
return true;
}
@override
void initServiceExtensions() {
super.initServiceExtensions();
final extension = FlutterDriverExtension(
_handler,
_silenceErrors,
_enableTextEntryEmulation,
finders: finders ?? const <FinderExtension>[],
commands: commands ?? const <CommandExtension>[],
);
registerServiceExtension(name: _extensionMethodName, callback: extension.call);
if (kIsWeb) {
registerWebServiceExtension(extension.call);
}
}
}
// Examples can assume:
// import 'package:flutter_driver/flutter_driver.dart';
// import 'package:flutter/widgets.dart';
// import 'package:flutter_driver/driver_extension.dart';
// import 'package:flutter_test/flutter_test.dart' hide find;
// import 'package:flutter_test/flutter_test.dart' as flutter_test;
// typedef MyHomeWidget = Placeholder;
// abstract class SomeWidget extends StatelessWidget { const SomeWidget({super.key, required this.title}); final String title; }
// late FlutterDriver driver;
// abstract class StubNestedCommand { int get times; SerializableFinder get finder; }
// class StubCommandResult extends Result { const StubCommandResult(this.arg); final String arg; @override Map<String, dynamic> toJson() => <String, dynamic>{}; }
// abstract class StubProberCommand { int get times; SerializableFinder get finder; }
/// Enables Flutter Driver VM service extension.
///
/// This extension is required for tests that use `package:flutter_driver` to
/// drive applications from a separate process. In order to allow the driver
/// to interact with the application, this method changes the behavior of the
/// framework in several ways - including keyboard interaction and text
/// editing. Applications intended for release should never include this
/// method.
///
/// Call this function prior to running your application, e.g. before you call
/// `runApp`.
///
/// Optionally you can pass a [DataHandler] callback. It will be called if the
/// test calls [FlutterDriver.requestData].
///
/// `silenceErrors` will prevent exceptions from being logged. This is useful
/// for tests where exceptions are expected. Defaults to false. Any errors
/// will still be returned in the `response` field of the result JSON along
/// with an `isError` boolean.
///
/// The `enableTextEntryEmulation` parameter controls whether the application interacts
/// with the system's text entry methods or a mocked out version used by Flutter Driver.
/// If it is set to false, [FlutterDriver.enterText] will fail,
/// but testing the application with real keyboard input is possible.
/// This value may be updated during a test by calling [FlutterDriver.setTextEntryEmulation].
///
/// The `finders` and `commands` parameters are optional and used to add custom
/// finders or commands, as in the following example.
///
/// ```dart
/// void main() {
/// enableFlutterDriverExtension(
/// finders: <FinderExtension>[ SomeFinderExtension() ],
/// commands: <CommandExtension>[ SomeCommandExtension() ],
/// );
///
/// runApp(const MyHomeWidget());
/// }
///
/// class SomeFinderExtension extends FinderExtension {
/// @override
/// String get finderType => 'SomeFinder';
///
/// @override
/// SerializableFinder deserialize(Map<String, String> params, DeserializeFinderFactory finderFactory, {String? path}) {
/// return SomeFinder(params['title']!);
/// }
///
/// @override
/// Finder createFinder(SerializableFinder finder, CreateFinderFactory finderFactory) {
/// final SomeFinder someFinder = finder as SomeFinder;
///
/// return flutter_test.find.byElementPredicate((Element element) {
/// final Widget widget = element.widget;
/// if (widget is SomeWidget) {
/// return widget.title == someFinder.title;
/// }
/// return false;
/// });
/// }
/// }
///
/// // Use this class in a test anywhere where a SerializableFinder is expected.
/// class SomeFinder extends SerializableFinder {
/// const SomeFinder(this.title);
///
/// final String title;
///
/// @override
/// String get finderType => 'SomeFinder';
///
/// @override
/// Map<String, String> serialize() => super.serialize()..addAll(<String, String>{
/// 'title': title,
/// });
/// }
///
/// class SomeCommandExtension extends CommandExtension {
/// @override
/// String get commandKind => 'SomeCommand';
///
/// @override
/// Future<Result> call(Command command, WidgetController prober, CreateFinderFactory finderFactory, CommandHandlerFactory handlerFactory) async {
/// final SomeCommand someCommand = command as SomeCommand;
///
/// // Deserialize [Finder]:
/// final Finder finder = finderFactory.createFinder(someCommand.finder);
///
/// // Wait for [Element]:
/// handlerFactory.waitForElement(finder);
///
/// // Alternatively, wait for [Element] absence:
/// handlerFactory.waitForAbsentElement(finder);
///
/// // Submit known [Command]s:
/// for (int i = 0; i < someCommand.times; i++) {
/// await handlerFactory.handleCommand(Tap(someCommand.finder), prober, finderFactory);
/// }
///
/// // Alternatively, use [WidgetController]:
/// for (int i = 0; i < someCommand.times; i++) {
/// await prober.tap(finder);
/// }
///
/// return const SomeCommandResult('foo bar');
/// }
///
/// @override
/// Command deserialize(Map<String, String> params, DeserializeFinderFactory finderFactory, DeserializeCommandFactory commandFactory) {
/// return SomeCommand.deserialize(params, finderFactory);
/// }
/// }
///
/// // Pass an instance of this class to `FlutterDriver.sendCommand` to invoke
/// // the custom command during a test.
/// class SomeCommand extends CommandWithTarget {
/// SomeCommand(super.finder, this.times, {super.timeout});
///
/// SomeCommand.deserialize(super.json, super.finderFactory)
/// : times = int.parse(json['times']!),
/// super.deserialize();
///
/// @override
/// Map<String, String> serialize() {
/// return super.serialize()..addAll(<String, String>{'times': '$times'});
/// }
///
/// @override
/// String get kind => 'SomeCommand';
///
/// final int times;
/// }
///
/// class SomeCommandResult extends Result {
/// const SomeCommandResult(this.resultParam);
///
/// final String resultParam;
///
/// @override
/// Map<String, dynamic> toJson() {
/// return <String, dynamic>{
/// 'resultParam': resultParam,
/// };
/// }
/// }
/// ```
void enableFlutterDriverExtension({
DataHandler? handler,
bool silenceErrors = false,
bool enableTextEntryEmulation = true,
List<FinderExtension>? finders,
List<CommandExtension>? commands,
}) {
_DriverBinding(
handler,
silenceErrors,
enableTextEntryEmulation,
finders ?? <FinderExtension>[],
commands ?? <CommandExtension>[],
);
assert(WidgetsBinding.instance is _DriverBinding);
}
/// Signature for functions that handle a command and return a result.
typedef CommandHandlerCallback = Future<Result?> Function(Command c);
/// Signature for functions that deserialize a JSON map to a command object.
typedef CommandDeserializerCallback = Command Function(Map<String, String> params);
/// Used to expand the new [Finder].
abstract class FinderExtension {
/// Identifies the type of finder to be used by the driver extension.
String get finderType;
/// Deserializes the finder from JSON generated by [SerializableFinder.serialize].
///
/// Use [finderFactory] to deserialize nested [Finder]s.
///
/// See also:
/// * [Ancestor], a finder that uses other [Finder]s as parameters.
SerializableFinder deserialize(
Map<String, String> params,
DeserializeFinderFactory finderFactory,
);
/// Signature for functions that run the given finder and return the [Element]
/// found, if any, or null otherwise.
///
/// Call [finderFactory] to create known, nested [Finder]s from [SerializableFinder]s.
Finder createFinder(SerializableFinder finder, CreateFinderFactory finderFactory);
}
/// Used to expand the new [Command].
///
/// See also:
/// * [CommandWithTarget], a base class for [Command]s with [Finder]s.
abstract class CommandExtension {
/// Identifies the type of command to be used by the driver extension.
String get commandKind;
/// Deserializes the command from JSON generated by [Command.serialize].
///
/// Use [finderFactory] to deserialize nested [Finder]s.
/// Usually used for [CommandWithTarget]s.
///
/// Call [commandFactory] to deserialize commands specified as parameters.
///
/// See also:
/// * [CommandWithTarget], a base class for commands with target finders.
/// * [Tap], a command that uses [Finder]s as parameter.
Command deserialize(
Map<String, String> params,
DeserializeFinderFactory finderFactory,
DeserializeCommandFactory commandFactory,
);
/// Calls action for given [command].
/// Returns action [Result].
/// Invoke [prober] functions to perform widget actions.
/// Use [finderFactory] to create [Finder]s from [SerializableFinder].
/// Call [handlerFactory] to invoke other [Command]s or [CommandWithTarget]s.
///
/// The following example shows invoking nested command with [handlerFactory].
///
/// ```dart
/// @override
/// Future<Result> call(Command command, WidgetController prober, CreateFinderFactory finderFactory, CommandHandlerFactory handlerFactory) async {
/// final StubNestedCommand stubCommand = command as StubNestedCommand;
/// for (int i = 0; i < stubCommand.times; i++) {
/// await handlerFactory.handleCommand(Tap(stubCommand.finder), prober, finderFactory);
/// }
/// return const StubCommandResult('stub response');
/// }
/// ```
///
/// Check the example below for direct [WidgetController] usage with [prober]:
///
/// ```dart
/// @override
/// Future<Result> call(Command command, WidgetController prober, CreateFinderFactory finderFactory, CommandHandlerFactory handlerFactory) async {
/// final StubProberCommand stubCommand = command as StubProberCommand;
/// for (int i = 0; i < stubCommand.times; i++) {
/// await prober.tap(finderFactory.createFinder(stubCommand.finder));
/// }
/// return const StubCommandResult('stub response');
/// }
/// ```
Future<Result> call(
Command command,
WidgetController prober,
CreateFinderFactory finderFactory,
CommandHandlerFactory handlerFactory,
);
}
/// The class that manages communication between a Flutter Driver test and the
/// application being remote-controlled, on the application side.
///
/// This is not normally used directly. It is instantiated automatically when
/// calling [enableFlutterDriverExtension].
@visibleForTesting
class FlutterDriverExtension
with
DeserializeFinderFactory,
CreateFinderFactory,
DeserializeCommandFactory,
CommandHandlerFactory {
/// Creates an object to manage a Flutter Driver connection.
FlutterDriverExtension(
this._requestDataHandler,
this._silenceErrors,
this._enableTextEntryEmulation, {
List<FinderExtension> finders = const <FinderExtension>[],
List<CommandExtension> commands = const <CommandExtension>[],
}) {
if (_enableTextEntryEmulation) {
registerTextInput();
}
for (final finder in finders) {
_finderExtensions[finder.finderType] = finder;
}
for (final command in commands) {
_commandExtensions[command.commandKind] = command;
}
}
final WidgetController _prober = LiveWidgetController(WidgetsBinding.instance);
final DataHandler? _requestDataHandler;
final bool _silenceErrors;
final bool _enableTextEntryEmulation;
void _log(String message) {
driverLog('FlutterDriverExtension', message);
}
final Map<String, FinderExtension> _finderExtensions = <String, FinderExtension>{};
final Map<String, CommandExtension> _commandExtensions = <String, CommandExtension>{};
/// Processes a driver command configured by [params] and returns a result
/// as an arbitrary JSON object.
///
/// [params] must contain key "command" whose value is a string that
/// identifies the kind of the command and its corresponding
/// [CommandDeserializerCallback]. Other keys and values are specific to the
/// concrete implementation of [Command] and [CommandDeserializerCallback].
///
/// The returned JSON is command specific. Generally the caller deserializes
/// the result into a subclass of [Result], but that's not strictly required.
@visibleForTesting
Future<Map<String, dynamic>> call(Map<String, String> params) async {
final String commandKind = params['command']!;
try {
final Command command = deserializeCommand(params, this);
assert(
WidgetsBinding.instance.isRootWidgetAttached || !command.requiresRootWidgetAttached,
'No root widget is attached; have you remembered to call runApp()?',
);
Future<Result> responseFuture = handleCommand(command, _prober, this);
if (command.timeout != null) {
responseFuture = responseFuture.timeout(command.timeout!);
}
final Result response = await responseFuture;
return _makeResponse(response.toJson());
} on TimeoutException catch (error, stackTrace) {
final message = 'Timeout while executing $commandKind: $error\n$stackTrace';
_log(message);
return _makeResponse(message, isError: true);
} catch (error, stackTrace) {
final message = 'Uncaught extension error while executing $commandKind: $error\n$stackTrace';
if (!_silenceErrors) {
_log(message);
}
return _makeResponse(message, isError: true);
}
}
Map<String, dynamic> _makeResponse(dynamic response, {bool isError = false}) {
return <String, dynamic>{'isError': isError, 'response': response};
}
@override
SerializableFinder deserializeFinder(Map<String, String> json, {String? path}) {
final String? finderType = json['finderType'];
if (_finderExtensions.containsKey(finderType)) {
return _finderExtensions[finderType]!.deserialize(json, this);
}
return super.deserializeFinder(json, path: path);
}
@override
Finder createFinder(SerializableFinder finder) {
final String finderType = finder.finderType;
if (_finderExtensions.containsKey(finderType)) {
return _finderExtensions[finderType]!.createFinder(finder, this);
}
return super.createFinder(finder);
}
@override
Command deserializeCommand(
Map<String, String> params,
DeserializeFinderFactory finderFactory, {
String? path,
}) {
final String? kind = params['command'];
if (_commandExtensions.containsKey(kind)) {
return _commandExtensions[kind]!.deserialize(params, finderFactory, this);
}
return super.deserializeCommand(params, finderFactory, path: path);
}
@override
@protected
DataHandler? getDataHandler() {
return _requestDataHandler;
}
@override
Future<Result> handleCommand(
Command command,
WidgetController prober,
CreateFinderFactory finderFactory,
) {
final String kind = command.kind;
if (_commandExtensions.containsKey(kind)) {
return _commandExtensions[kind]!.call(command, prober, finderFactory, this);
}
return super.handleCommand(command, prober, finderFactory);
}
}