extension.dart 24 KB
Newer Older
1 2 3 4 5
// 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 'dart:async';
6

7
import 'package:flutter/semantics.dart';
8 9
import 'package:meta/meta.dart';

10
import 'package:flutter/cupertino.dart';
11
import 'package:flutter/foundation.dart';
12
import 'package:flutter/gestures.dart';
13
import 'package:flutter/material.dart';
14
import 'package:flutter/rendering.dart' show RendererBinding, SemanticsHandle;
15
import 'package:flutter/scheduler.dart';
16
import 'package:flutter/services.dart';
17
import 'package:flutter/widgets.dart';
18
import 'package:flutter_test/flutter_test.dart';
19

20
import '../common/diagnostics_tree.dart';
21 22 23
import '../common/error.dart';
import '../common/find.dart';
import '../common/frame_sync.dart';
24
import '../common/geometry.dart';
25 26 27 28 29 30
import '../common/gesture.dart';
import '../common/health.dart';
import '../common/message.dart';
import '../common/render_tree.dart';
import '../common/request_data.dart';
import '../common/semantics.dart';
31
import '../common/text.dart';
32 33
import '../common/wait.dart';
import 'wait_conditions.dart';
34

35 36
const String _extensionMethodName = 'driver';
const String _extensionMethod = 'ext.flutter.$_extensionMethodName';
37

38 39 40 41
/// 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.
42
typedef DataHandler = Future<String> Function(String message);
43

44
class _DriverBinding extends BindingBase with ServicesBinding, SchedulerBinding, GestureBinding, PaintingBinding, SemanticsBinding, RendererBinding, WidgetsBinding {
45
  _DriverBinding(this._handler, this._silenceErrors);
46 47

  final DataHandler _handler;
48
  final bool _silenceErrors;
49

50 51 52
  @override
  void initServiceExtensions() {
    super.initServiceExtensions();
53
    final FlutterDriverExtension extension = FlutterDriverExtension(_handler, _silenceErrors);
54 55
    registerServiceExtension(
      name: _extensionMethodName,
56
      callback: extension.call,
57 58 59
    );
  }
}
60 61 62 63 64 65 66 67

/// Enables Flutter Driver VM service extension.
///
/// This extension is required for tests that use `package:flutter_driver` to
/// drive applications from a separate process.
///
/// Call this function prior to running your application, e.g. before you call
/// `runApp`.
68 69 70
///
/// Optionally you can pass a [DataHandler] callback. It will be called if the
/// test calls [FlutterDriver.requestData].
71
///
Kent Boogaart's avatar
Kent Boogaart committed
72
/// `silenceErrors` will prevent exceptions from being logged. This is useful
73 74 75 76
/// 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.
void enableFlutterDriverExtension({ DataHandler handler, bool silenceErrors = false }) {
77
  assert(WidgetsBinding.instance == null);
78
  _DriverBinding(handler, silenceErrors);
79
  assert(WidgetsBinding.instance is _DriverBinding);
80 81
}

82
/// Signature for functions that handle a command and return a result.
83
typedef CommandHandlerCallback = Future<Result> Function(Command c);
84

85
/// Signature for functions that deserialize a JSON map to a command object.
86
typedef CommandDeserializerCallback = Command Function(Map<String, String> params);
87

88 89
/// Signature for functions that run the given finder and return the [Element]
/// found, if any, or null otherwise.
90
typedef FinderConstructor = Finder Function(SerializableFinder finder);
91

92 93 94 95 96
/// 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].
97 98
@visibleForTesting
class FlutterDriverExtension {
99
  /// Creates an object to manage a Flutter Driver connection.
100
  FlutterDriverExtension(this._requestDataHandler, this._silenceErrors) {
101 102
    _testTextInput.register();

103
    _commandHandlers.addAll(<String, CommandHandlerCallback>{
104
      'get_health': _getHealth,
105
      'get_render_tree': _getRenderTree,
106
      'enter_text': _enterText,
107
      'get_text': _getText,
108
      'request_data': _requestData,
109 110
      'scroll': _scroll,
      'scrollIntoView': _scrollIntoView,
111 112
      'set_frame_sync': _setFrameSync,
      'set_semantics': _setSemantics,
113
      'set_text_entry_emulation': _setTextEntryEmulation,
114
      'tap': _tap,
115
      'waitFor': _waitFor,
116
      'waitForAbsent': _waitForAbsent,
117 118 119 120
      'waitForCondition': _waitForCondition,
      'waitUntilNoTransientCallbacks': _waitUntilNoTransientCallbacks, // ignore: deprecated_member_use_from_same_package
      'waitUntilNoPendingFrame': _waitUntilNoPendingFrame, // ignore: deprecated_member_use_from_same_package
      'waitUntilFirstFrameRasterized': _waitUntilFirstFrameRasterized, // ignore: deprecated_member_use_from_same_package
121
      'get_semantics_id': _getSemanticsId,
122
      'get_offset': _getOffset,
123
      'get_diagnostics_tree': _getDiagnosticsTree,
124
    });
125

126
    _commandDeserializers.addAll(<String, CommandDeserializerCallback>{
127 128 129 130 131 132 133 134 135 136 137 138 139
      'get_health': (Map<String, String> params) => GetHealth.deserialize(params),
      'get_render_tree': (Map<String, String> params) => GetRenderTree.deserialize(params),
      'enter_text': (Map<String, String> params) => EnterText.deserialize(params),
      'get_text': (Map<String, String> params) => GetText.deserialize(params),
      'request_data': (Map<String, String> params) => RequestData.deserialize(params),
      'scroll': (Map<String, String> params) => Scroll.deserialize(params),
      'scrollIntoView': (Map<String, String> params) => ScrollIntoView.deserialize(params),
      'set_frame_sync': (Map<String, String> params) => SetFrameSync.deserialize(params),
      'set_semantics': (Map<String, String> params) => SetSemantics.deserialize(params),
      'set_text_entry_emulation': (Map<String, String> params) => SetTextEntryEmulation.deserialize(params),
      'tap': (Map<String, String> params) => Tap.deserialize(params),
      'waitFor': (Map<String, String> params) => WaitFor.deserialize(params),
      'waitForAbsent': (Map<String, String> params) => WaitForAbsent.deserialize(params),
140 141 142 143
      'waitForCondition': (Map<String, String> params) => WaitForCondition.deserialize(params),
      'waitUntilNoTransientCallbacks': (Map<String, String> params) => WaitUntilNoTransientCallbacks.deserialize(params), // ignore: deprecated_member_use_from_same_package
      'waitUntilNoPendingFrame': (Map<String, String> params) => WaitUntilNoPendingFrame.deserialize(params), // ignore: deprecated_member_use_from_same_package
      'waitUntilFirstFrameRasterized': (Map<String, String> params) => WaitUntilFirstFrameRasterized.deserialize(params), // ignore: deprecated_member_use_from_same_package
144
      'get_semantics_id': (Map<String, String> params) => GetSemanticsId.deserialize(params),
145
      'get_offset': (Map<String, String> params) => GetOffset.deserialize(params),
146
      'get_diagnostics_tree': (Map<String, String> params) => GetDiagnosticsTree.deserialize(params),
147
    });
148

149
    _finders.addAll(<String, FinderConstructor>{
150 151
      'ByText': (SerializableFinder finder) => _createByTextFinder(finder),
      'ByTooltipMessage': (SerializableFinder finder) => _createByTooltipMessageFinder(finder),
152
      'BySemanticsLabel': (SerializableFinder finder) => _createBySemanticsLabelFinder(finder),
153 154
      'ByValueKey': (SerializableFinder finder) => _createByValueKeyFinder(finder),
      'ByType': (SerializableFinder finder) => _createByTypeFinder(finder),
155
      'PageBack': (SerializableFinder finder) => _createPageBackFinder(),
156 157
      'Ancestor': (SerializableFinder finder) => _createAncestorFinder(finder),
      'Descendant': (SerializableFinder finder) => _createDescendantFinder(finder),
158
    });
159 160
  }

161 162
  final TestTextInput _testTextInput = TestTextInput();

163
  final DataHandler _requestDataHandler;
164
  final bool _silenceErrors;
165

166
  static final Logger _log = Logger('FlutterDriverExtension');
167

168
  final WidgetController _prober = LiveWidgetController(WidgetsBinding.instance);
169 170
  final Map<String, CommandHandlerCallback> _commandHandlers = <String, CommandHandlerCallback>{};
  final Map<String, CommandDeserializerCallback> _commandDeserializers = <String, CommandDeserializerCallback>{};
171
  final Map<String, FinderConstructor> _finders = <String, FinderConstructor>{};
172

173 174 175 176
  /// With [_frameSync] enabled, Flutter Driver will wait to perform an action
  /// until there are no pending frames in the app under test.
  bool _frameSync = true;

177 178 179 180 181 182 183 184 185 186
  /// 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.
187
  @visibleForTesting
188
  Future<Map<String, dynamic>> call(Map<String, String> params) async {
189
    final String commandKind = params['command'];
190
    try {
191 192
      final CommandHandlerCallback commandHandler = _commandHandlers[commandKind];
      final CommandDeserializerCallback commandDeserializer =
193
          _commandDeserializers[commandKind];
194 195
      if (commandHandler == null || commandDeserializer == null)
        throw 'Extension $_extensionMethod does not support command $commandKind';
196
      final Command command = commandDeserializer(params);
197 198
      assert(WidgetsBinding.instance.isRootWidgetAttached || !command.requiresRootWidgetAttached,
          'No root widget is attached; have you remembered to call runApp()?');
199 200 201 202
      Future<Result> responseFuture = commandHandler(command);
      if (command.timeout != null)
        responseFuture = responseFuture.timeout(command.timeout);
      final Result response = await responseFuture;
203
      return _makeResponse(response?.toJson());
204
    } on TimeoutException catch (error, stackTrace) {
205
      final String msg = 'Timeout while executing $commandKind: $error\n$stackTrace';
206
      _log.error(msg);
207
      return _makeResponse(msg, isError: true);
208
    } catch (error, stackTrace) {
209
      final String msg = 'Uncaught extension error while executing $commandKind: $error\n$stackTrace';
210 211
      if (!_silenceErrors)
        _log.error(msg);
212
      return _makeResponse(msg, isError: true);
213 214 215
    }
  }

216
  Map<String, dynamic> _makeResponse(dynamic response, { bool isError = false }) {
217 218 219 220 221 222
    return <String, dynamic>{
      'isError': isError,
      'response': response,
    };
  }

223
  Future<Health> _getHealth(Command command) async => const Health(HealthStatus.ok);
224

225
  Future<RenderTree> _getRenderTree(Command command) async {
226
    return RenderTree(RendererBinding.instance?.renderView?.toStringDeep());
227 228
  }

229
  // This can be used to wait for the first frame being rasterized during app launch.
230
  @Deprecated('This method has been deprecated in favor of _waitForCondition.')
231 232 233 234 235
  Future<Result> _waitUntilFirstFrameRasterized(Command command) async {
    await WidgetsBinding.instance.waitUntilFirstFrameRasterized;
    return null;
  }

236
  // Waits until at the end of a frame the provided [condition] is [true].
237
  Future<void> _waitUntilFrame(bool condition(), [ Completer<void> completer ]) {
238
    completer ??= Completer<void>();
239 240 241 242 243 244 245 246 247 248
    if (!condition()) {
      SchedulerBinding.instance.addPostFrameCallback((Duration timestamp) {
        _waitUntilFrame(condition, completer);
      });
    } else {
      completer.complete();
    }
    return completer.future;
  }

249
  /// Runs `finder` repeatedly until it finds one or more [Element]s.
250 251 252
  Future<Finder> _waitForElement(Finder finder) async {
    if (_frameSync)
      await _waitUntilFrame(() => SchedulerBinding.instance.transientCallbackCount == 0);
253

254
    await _waitUntilFrame(() => finder.evaluate().isNotEmpty);
255 256 257 258 259

    if (_frameSync)
      await _waitUntilFrame(() => SchedulerBinding.instance.transientCallbackCount == 0);

    return finder;
260 261
  }

262 263 264 265 266
  /// Runs `finder` repeatedly until it finds zero [Element]s.
  Future<Finder> _waitForAbsentElement(Finder finder) async {
    if (_frameSync)
      await _waitUntilFrame(() => SchedulerBinding.instance.transientCallbackCount == 0);

267
    await _waitUntilFrame(() => finder.evaluate().isEmpty);
268 269 270 271 272 273 274

    if (_frameSync)
      await _waitUntilFrame(() => SchedulerBinding.instance.transientCallbackCount == 0);

    return finder;
  }

275 276
  Finder _createByTextFinder(ByText arguments) {
    return find.text(arguments.text);
277 278
  }

279 280
  Finder _createByTooltipMessageFinder(ByTooltipMessage arguments) {
    return find.byElementPredicate((Element element) {
281
      final Widget widget = element.widget;
282 283 284
      if (widget is Tooltip)
        return widget.message == arguments.text;
      return false;
285
    }, description: 'widget with text tooltip "${arguments.text}"');
286 287
  }

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  Finder _createBySemanticsLabelFinder(BySemanticsLabel arguments) {
    return find.byElementPredicate((Element element) {
      if (element is! RenderObjectElement) {
        return false;
      }
      final String semanticsLabel = element.renderObject?.debugSemantics?.label;
      if (semanticsLabel == null) {
        return false;
      }
      final Pattern label = arguments.label;
      return label is RegExp
          ? label.hasMatch(semanticsLabel)
          : label == semanticsLabel;
    }, description: 'widget with semantic label "${arguments.label}"');
  }

304
  Finder _createByValueKeyFinder(ByValueKey arguments) {
305 306
    switch (arguments.keyValueType) {
      case 'int':
307
        return find.byKey(ValueKey<int>(arguments.keyValue));
308
      case 'String':
309
        return find.byKey(ValueKey<String>(arguments.keyValue));
310 311 312
      default:
        throw 'Unsupported ByValueKey type: ${arguments.keyValueType}';
    }
313 314
  }

315 316 317 318 319 320
  Finder _createByTypeFinder(ByType arguments) {
    return find.byElementPredicate((Element element) {
      return element.widget.runtimeType.toString() == arguments.type;
    }, description: 'widget with runtimeType "${arguments.type}"');
  }

321 322 323 324 325 326 327 328 329 330 331
  Finder _createPageBackFinder() {
    return find.byElementPredicate((Element element) {
      final Widget widget = element.widget;
      if (widget is Tooltip)
        return widget.message == 'Back';
      if (widget is CupertinoNavigationBarBackButton)
        return true;
      return false;
    }, description: 'Material or Cupertino back button');
  }

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  Finder _createAncestorFinder(Ancestor arguments) {
    return find.ancestor(
      of: _createFinder(arguments.of),
      matching: _createFinder(arguments.matching),
      matchRoot: arguments.matchRoot,
    );
  }

  Finder _createDescendantFinder(Descendant arguments) {
    return find.descendant(
      of: _createFinder(arguments.of),
      matching: _createFinder(arguments.matching),
      matchRoot: arguments.matchRoot,
    );
  }

348
  Finder _createFinder(SerializableFinder finder) {
349
    final FinderConstructor constructor = _finders[finder.finderType];
350

351
    if (constructor == null)
352 353
      throw 'Unsupported finder type: ${finder.finderType}';

354
    return constructor(finder);
355 356
  }

357
  Future<TapResult> _tap(Command command) async {
358
    final Tap tapCommand = command;
359 360 361 362
    final Finder computedFinder = await _waitForElement(
      _createFinder(tapCommand.finder).hitTestable()
    );
    await _prober.tap(computedFinder);
363
    return const TapResult();
364 365
  }

366
  Future<WaitForResult> _waitFor(Command command) async {
367
    final WaitFor waitForCommand = command;
368
    await _waitForElement(_createFinder(waitForCommand.finder));
369
    return const WaitForResult();
370 371
  }

372 373 374
  Future<WaitForAbsentResult> _waitForAbsent(Command command) async {
    final WaitForAbsent waitForAbsentCommand = command;
    await _waitForAbsentElement(_createFinder(waitForAbsentCommand.finder));
375
    return const WaitForAbsentResult();
376 377
  }

378 379 380 381 382 383 384 385 386
  Future<Result> _waitForCondition(Command command) async {
    assert(command != null);
    final WaitForCondition waitForConditionCommand = command;
    final WaitCondition condition = deserializeCondition(waitForConditionCommand.condition);
    await condition.wait();
    return null;
  }

  @Deprecated('This method has been deprecated in favor of _waitForCondition.')
387
  Future<Result> _waitUntilNoTransientCallbacks(Command command) async {
388 389
    if (SchedulerBinding.instance.transientCallbackCount != 0)
      await _waitUntilFrame(() => SchedulerBinding.instance.transientCallbackCount == 0);
390
    return null;
391 392
  }

393
  /// Returns a future that waits until no pending frame is scheduled (frame is synced).
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  ///
  /// Specifically, it checks:
  /// * Whether the count of transient callbacks is zero.
  /// * Whether there's no pending request for scheduling a new frame.
  ///
  /// We consider the frame is synced when both conditions are met.
  ///
  /// This method relies on a Flutter Driver mechanism called "frame sync",
  /// which waits for transient animations to finish. Persistent animations will
  /// cause this to wait forever.
  ///
  /// If a test needs to interact with the app while animations are running, it
  /// should avoid this method and instead disable the frame sync using
  /// `set_frame_sync` method. See [FlutterDriver.runUnsynchronized] for more
  /// details on how to do this. Note, disabling frame sync will require the
  /// test author to use some other method to avoid flakiness.
410 411 412
  ///
  /// This method has been deprecated in favor of [_waitForCondition].
  @Deprecated('This method has been deprecated in favor of _waitForCondition.')
413
  Future<Result> _waitUntilNoPendingFrame(Command command) async {
414 415 416 417 418 419 420
    await _waitUntilFrame(() {
      return SchedulerBinding.instance.transientCallbackCount == 0
          && !SchedulerBinding.instance.hasScheduledFrame;
    });
    return null;
  }

421 422 423 424 425 426 427 428 429 430 431
  Future<GetSemanticsIdResult> _getSemanticsId(Command command) async {
    final GetSemanticsId semanticsCommand = command;
    final Finder target = await _waitForElement(_createFinder(semanticsCommand.finder));
    final Element element = target.evaluate().single;
    RenderObject renderObject = element.renderObject;
    SemanticsNode node;
    while (renderObject != null && node == null) {
      node = renderObject.debugSemantics;
      renderObject = renderObject.parent;
    }
    if (node == null)
432 433
      throw StateError('No semantics data found');
    return GetSemanticsIdResult(node.id);
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
  Future<GetOffsetResult> _getOffset(Command command) async {
    final GetOffset getOffsetCommand = command;
    final Finder finder = await _waitForElement(_createFinder(getOffsetCommand.finder));
    final Element element = finder.evaluate().single;
    final RenderBox box = element.renderObject;
    Offset localPoint;
    switch (getOffsetCommand.offsetType) {
      case OffsetType.topLeft:
        localPoint = Offset.zero;
        break;
      case OffsetType.topRight:
        localPoint = box.size.topRight(Offset.zero);
        break;
      case OffsetType.bottomLeft:
        localPoint = box.size.bottomLeft(Offset.zero);
        break;
      case OffsetType.bottomRight:
        localPoint = box.size.bottomRight(Offset.zero);
        break;
      case OffsetType.center:
        localPoint = box.size.center(Offset.zero);
        break;
    }
    final Offset globalPoint = box.localToGlobal(localPoint);
    return GetOffsetResult(dx: globalPoint.dx, dy: globalPoint.dy);
  }

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
  Future<DiagnosticsTreeResult> _getDiagnosticsTree(Command command) async {
    final GetDiagnosticsTree diagnosticsCommand = command;
    final Finder finder = await _waitForElement(_createFinder(diagnosticsCommand.finder));
    final Element element = finder.evaluate().single;
    DiagnosticsNode diagnosticsNode;
    switch (diagnosticsCommand.diagnosticsType) {
      case DiagnosticsType.renderObject:
        diagnosticsNode = element.renderObject.toDiagnosticsNode();
        break;
      case DiagnosticsType.widget:
        diagnosticsNode = element.toDiagnosticsNode();
        break;
    }
    return DiagnosticsTreeResult(diagnosticsNode.toJsonMap(DiagnosticsSerializationDelegate(
      subtreeDepth: diagnosticsCommand.subtreeDepth,
      includeProperties: diagnosticsCommand.includeProperties,
    )));
  }

482
  Future<ScrollResult> _scroll(Command command) async {
483 484
    final Scroll scrollCommand = command;
    final Finder target = await _waitForElement(_createFinder(scrollCommand.finder));
485
    final int totalMoves = scrollCommand.duration.inMicroseconds * scrollCommand.frequency ~/ Duration.microsecondsPerSecond;
486
    final Offset delta = Offset(scrollCommand.dx, scrollCommand.dy) / totalMoves.toDouble();
487
    final Duration pause = scrollCommand.duration ~/ totalMoves;
488 489
    final Offset startLocation = _prober.getCenter(target);
    Offset currentLocation = startLocation;
490 491
    final TestPointer pointer = TestPointer(1);
    final HitTestResult hitTest = HitTestResult();
492

493 494
    _prober.binding.hitTest(hitTest, startLocation);
    _prober.binding.dispatchEvent(pointer.down(startLocation), hitTest);
495
    await Future<void>.value(); // so that down and move don't happen in the same microtask
496
    for (int moves = 0; moves < totalMoves; moves += 1) {
497
      currentLocation = currentLocation + delta;
498
      _prober.binding.dispatchEvent(pointer.move(currentLocation), hitTest);
499
      await Future<void>.delayed(pause);
500
    }
501
    _prober.binding.dispatchEvent(pointer.up(), hitTest);
502

503
    return const ScrollResult();
504 505
  }

506
  Future<ScrollResult> _scrollIntoView(Command command) async {
507 508
    final ScrollIntoView scrollIntoViewCommand = command;
    final Finder target = await _waitForElement(_createFinder(scrollIntoViewCommand.finder));
Adam Barth's avatar
Adam Barth committed
509
    await Scrollable.ensureVisible(target.evaluate().single, duration: const Duration(milliseconds: 100), alignment: scrollIntoViewCommand.alignment ?? 0.0);
510
    return const ScrollResult();
511 512
  }

513
  Future<GetTextResult> _getText(Command command) async {
514 515
    final GetText getTextCommand = command;
    final Finder target = await _waitForElement(_createFinder(getTextCommand.finder));
516
    // TODO(yjbanov): support more ways to read text
517
    final Text text = target.evaluate().single.widget;
518
    return GetTextResult(text.data);
519
  }
520

521 522 523 524 525 526 527
  Future<SetTextEntryEmulationResult> _setTextEntryEmulation(Command command) async {
    final SetTextEntryEmulation setTextEntryEmulationCommand = command;
    if (setTextEntryEmulationCommand.enabled) {
      _testTextInput.register();
    } else {
      _testTextInput.unregister();
    }
528
    return const SetTextEntryEmulationResult();
529 530
  }

531
  Future<EnterTextResult> _enterText(Command command) async {
532 533 534 535
    if (!_testTextInput.isRegistered) {
      throw 'Unable to fulfill `FlutterDriver.enterText`. Text emulation is '
            'disabled. You can enable it using `FlutterDriver.setTextEntryEmulation`.';
    }
536 537
    final EnterText enterTextCommand = command;
    _testTextInput.enterText(enterTextCommand.text);
538
    return const EnterTextResult();
539 540
  }

541 542
  Future<RequestDataResult> _requestData(Command command) async {
    final RequestData requestDataCommand = command;
543
    return RequestDataResult(_requestDataHandler == null ? 'No requestData Extension registered' : await _requestDataHandler(requestDataCommand.message));
544 545
  }

546
  Future<SetFrameSyncResult> _setFrameSync(Command command) async {
547
    final SetFrameSync setFrameSyncCommand = command;
548
    _frameSync = setFrameSyncCommand.enabled;
549
    return const SetFrameSyncResult();
550
  }
551 552 553 554 555 556 557 558 559 560 561

  SemanticsHandle _semantics;
  bool get _semanticsIsEnabled => RendererBinding.instance.pipelineOwner.semanticsOwner != null;

  Future<SetSemanticsResult> _setSemantics(Command command) async {
    final SetSemantics setSemanticsCommand = command;
    final bool semanticsWasEnabled = _semanticsIsEnabled;
    if (setSemanticsCommand.enabled && _semantics == null) {
      _semantics = RendererBinding.instance.pipelineOwner.ensureSemantics();
      if (!semanticsWasEnabled) {
        // wait for the first frame where semantics is enabled.
562
        final Completer<void> completer = Completer<void>();
563 564 565 566 567 568 569 570 571
        SchedulerBinding.instance.addPostFrameCallback((Duration d) {
          completer.complete();
        });
        await completer.future;
      }
    } else if (!setSemanticsCommand.enabled && _semantics != null) {
      _semantics.dispose();
      _semantics = null;
    }
572
    return SetSemanticsResult(semanticsWasEnabled != _semanticsIsEnabled);
573
  }
574
}