input_test.dart 4.45 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// 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.

Adam Barth's avatar
Adam Barth committed
5 6
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
7
import 'package:flutter/rendering.dart';
8
import 'package:sky_services/editing/editing.mojom.dart' as mojom;
9 10
import 'package:test/test.dart';

11 12
class MockKeyboard implements mojom.Keyboard {
  mojom.KeyboardClient client;
13

14
  void setClient(mojom.KeyboardClientStub client, mojom.KeyboardConfiguration configuraiton) {
15 16 17
    this.client = client.impl;
  }

18
  void show() {}
19 20

  void hide() {}
21

22
  void setEditingState(mojom.EditingState state) {}
23 24 25
}

void main() {
26
  WidgetFlutterBinding.ensureInitialized(); // for serviceMocker
27
  MockKeyboard mockKeyboard = new MockKeyboard();
28
  serviceMocker.registerMockService(mojom.Keyboard.serviceName, mockKeyboard);
29

30
  test('Editable text has consistent size', () {
31 32 33 34 35 36
    testWidgets((WidgetTester tester) {
      GlobalKey inputKey = new GlobalKey();
      String inputValue;

      Widget builder() {
        return new Center(
37 38 39
          child: new Material(
            child: new Input(
              key: inputKey,
40
              hintText: 'Placeholder',
41 42
              onChanged: (String value) { inputValue = value; }
            )
43 44 45
          )
        );
      }
46

47
      tester.pumpWidget(builder());
48

49 50
      Element input = tester.findElementByKey(inputKey);
      Size emptyInputSize = (input.renderObject as RenderBox).size;
51

52 53 54
      void enterText(String testValue) {
        // Simulate entry of text through the keyboard.
        expect(mockKeyboard.client, isNotNull);
55 56 57 58
        mockKeyboard.client.updateEditingState(new mojom.EditingState()
          ..text = testValue
          ..composingBase = 0
          ..composingExtent = testValue.length);
59

60 61
        // Check that the onChanged event handler fired.
        expect(inputValue, equals(testValue));
62

63 64 65 66 67
        tester.pumpWidget(builder());
      }

      enterText(' ');
      expect((input.renderObject as RenderBox).size, equals(emptyInputSize));
68

69
      enterText('Test');
70 71
      expect((input.renderObject as RenderBox).size, equals(emptyInputSize));
    });
72
  });
73 74

  test('Cursor blinks', () {
75 76 77 78 79
    testWidgets((WidgetTester tester) {
      GlobalKey inputKey = new GlobalKey();

      Widget builder() {
        return new Center(
80 81 82
          child: new Material(
            child: new Input(
              key: inputKey,
83
              hintText: 'Placeholder'
84
            )
85 86 87
          )
        );
      }
88

89
      tester.pumpWidget(builder());
90

91
      RawEditableTextState editableText = tester.findStateOfType(RawEditableTextState);
92 93 94

      // Check that the cursor visibility toggles after each blink interval.
      void checkCursorToggle() {
Hixie's avatar
Hixie committed
95 96 97 98 99 100 101 102 103 104 105
        bool initialShowCursor = editableText.cursorCurrentlyVisible;
        tester.async.elapse(editableText.cursorBlinkInterval);
        expect(editableText.cursorCurrentlyVisible, equals(!initialShowCursor));
        tester.async.elapse(editableText.cursorBlinkInterval);
        expect(editableText.cursorCurrentlyVisible, equals(initialShowCursor));
        tester.async.elapse(editableText.cursorBlinkInterval ~/ 10);
        expect(editableText.cursorCurrentlyVisible, equals(initialShowCursor));
        tester.async.elapse(editableText.cursorBlinkInterval);
        expect(editableText.cursorCurrentlyVisible, equals(!initialShowCursor));
        tester.async.elapse(editableText.cursorBlinkInterval);
        expect(editableText.cursorCurrentlyVisible, equals(initialShowCursor));
106 107 108 109 110
      }

      checkCursorToggle();

      // Try the test again with a nonempty EditableText.
111 112 113 114
      mockKeyboard.client.updateEditingState(new mojom.EditingState()
        ..text = 'X'
        ..selectionBase = 1
        ..selectionExtent = 1);
115 116 117
      checkCursorToggle();
    });
  });
118

Adam Barth's avatar
Adam Barth committed
119 120 121 122 123 124
  test('hideText control test', () {
    testWidgets((WidgetTester tester) {
      GlobalKey inputKey = new GlobalKey();

      Widget builder() {
        return new Center(
125 126 127 128
          child: new Material(
            child: new Input(
              key: inputKey,
              hideText: true,
129
              hintText: 'Placeholder'
130
            )
Adam Barth's avatar
Adam Barth committed
131 132 133 134 135 136 137
          )
        );
      }

      tester.pumpWidget(builder());

      const String testValue = 'ABC';
138 139 140 141
      mockKeyboard.client.updateEditingState(new mojom.EditingState()
        ..text = testValue
        ..selectionBase = testValue.length
        ..selectionExtent = testValue.length);
Adam Barth's avatar
Adam Barth committed
142 143 144 145

      tester.pump();
    });
  });
146
}