test_text_input.dart 2.73 KB
Newer Older
1 2 3 4 5
// Copyright 2015 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
import 'dart:typed_data';
7 8 9

import 'package:flutter/services.dart';

10 11
import 'widget_tester.dart';

12 13
const String _kTextInputClientChannel = 'flutter/textinputclient';

14 15 16 17 18 19 20 21 22 23
/// A testing stub for the system's onscreen keyboard.
///
/// Typical app tests will not need to use this class directly.
///
/// See also:
///
/// * [WidgetTester.enterText], which uses this class to simulate keyboard input.
/// * [WidgetTester.showKeyboard], which uses this class to simulate showing the
///   popup keyboard and initializing its text.
class TestTextInput {
24
  /// Installs this object as a mock handler for [SystemChannels.textInput].
25
  void register() {
26
    SystemChannels.textInput.setMockMethodCallHandler(_handleTextInputCall);
27 28
  }

29
  int _client = 0;
30 31 32 33 34 35 36

  /// The last set of arguments that [TextInputConnection.setEditingState] sent
  /// to the embedder.
  ///
  /// This is a map representation of a [TextEditingValue] object. For example,
  /// it will have a `text` entry whose value matches the most recent
  /// [TextEditingValue.text] that was sent to the embedder.
37 38
  Map<String, dynamic> editingState;

39
  Future<dynamic> _handleTextInputCall(MethodCall methodCall) async {
40
    switch (methodCall.method) {
41
      case 'TextInput.setClient':
42
        _client = methodCall.arguments[0];
43
        break;
44 45 46 47
      case 'TextInput.clearClient':
        _client = 0;
        _isVisible = false;
        break;
48
      case 'TextInput.setEditingState':
49
        editingState = methodCall.arguments;
50
        break;
51 52 53 54 55 56
      case 'TextInput.show':
        _isVisible = true;
        break;
      case 'TextInput.hide':
        _isVisible = false;
        break;
57 58 59
    }
  }

60 61 62 63 64
  /// Whether the onscreen keyboard is visible to the user.
  bool get isVisible => _isVisible;
  bool _isVisible = false;

  /// Simulates the user changing the [TextEditingValue] to the given value.
65
  void updateEditingValue(TextEditingValue value) {
66
    expect(_client, isNonZero);
67
    BinaryMessages.handlePlatformMessage(
68 69 70 71
      SystemChannels.textInput.name,
      SystemChannels.textInput.codec.encodeMethodCall(
        new MethodCall(
          'TextInputClient.updateEditingState',
72
          <dynamic>[_client, value.toJSON()],
73 74
        ),
      ),
75
      (ByteData data) { /* response from framework is discarded */ },
76
    );
77 78
  }

79
  /// Simulates the user typing the given text.
80
  void enterText(String text) {
81
    updateEditingValue(new TextEditingValue(
82
      text: text,
83
      composing: new TextRange(start: 0, end: text.length),
84 85
    ));
  }
86 87 88 89 90

  /// Simulates the user hiding the onscreen keyboard.
  void hide() {
    _isVisible = false;
  }
91
}