form_test.dart 5.49 KB
Newer Older
1 2 3 4 5 6
// 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:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
7
import 'package:flutter_services/editing.dart' as mojom;
8

9 10 11
class MockKeyboard extends mojom.KeyboardProxy {
  MockKeyboard() : super.unbound();

12
  mojom.KeyboardClient client;
13
  mojom.EditingState currentState;
14 15 16 17 18 19 20 21 22 23 24 25 26

  @override
  void setClient(mojom.KeyboardClientStub client, mojom.KeyboardConfiguration configuraiton) {
    this.client = client.impl;
  }

  @override
  void show() {}

  @override
  void hide() {}

  @override
27 28 29 30
  void setEditingState(mojom.EditingState state) {
    currentState = state;
  }

31 32 33 34
}

void main() {
  MockKeyboard mockKeyboard = new MockKeyboard();
35 36

  setUpAll(() {
37
    serviceMocker.registerMockService(mockKeyboard);
38
  });
39 40 41 42 43 44 45 46 47 48

  void enterText(String testValue) {
    // Simulate entry of text through the keyboard.
    expect(mockKeyboard.client, isNotNull);
    mockKeyboard.client.updateEditingState(new mojom.EditingState()
      ..text = testValue
      ..composingBase = 0
      ..composingExtent = testValue.length);
  }

49
  testWidgets('Setter callback is called', (WidgetTester tester) async {
50 51 52 53 54 55 56 57
    String fieldValue;

    Widget builder() {
      return new Center(
        child: new Material(
          child: new Form(
            child: new Input(
              formField: new FormField<String>(
58
                setter: (String value) { fieldValue = value; }
59 60 61
              )
            )
          )
62 63 64
        )
      );
    }
65

66
    await tester.pumpWidget(builder());
67

68
    expect(fieldValue, isNull);
69

70 71 72
    Future<Null> checkText(String testValue) async {
      enterText(testValue);
      // pump'ing is unnecessary because callback happens regardless of frames
73 74
      expect(fieldValue, equals(testValue));
    }
75

76 77
    await checkText('Test');
    await checkText('');
78 79
  });

80
  testWidgets('Validator sets the error text', (WidgetTester tester) async {
81 82 83 84 85 86 87 88 89 90 91
    GlobalKey inputKey = new GlobalKey();
    String errorText(String input) => input + '/error';

    Widget builder() {
      return new Center(
        child: new Material(
          child: new Form(
            child: new Input(
              key: inputKey,
              formField: new FormField<String>(
                validator: errorText
92 93 94
              )
            )
          )
95 96 97
        )
      );
    }
98

99
    await tester.pumpWidget(builder());
100

101
    Future<Null> checkErrorText(String testValue) async {
102
      enterText(testValue);
103
      await tester.pump();
104 105 106
      // Check for a new Text widget with our error text.
      expect(find.text(errorText(testValue)), findsOneWidget);
    }
107

108 109
    await checkErrorText('Test');
    await checkErrorText('');
110 111
  });

112
  testWidgets('Multiple Inputs communicate', (WidgetTester tester) async {
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    GlobalKey inputKey = new GlobalKey();
    GlobalKey focusKey = new GlobalKey();
    // Input 1's text value.
    String fieldValue;
    // Input 2's validator depends on a input 1's value.
    String errorText(String input) => fieldValue.toString() + '/error';

    Widget builder() {
      return new Center(
        child: new Material(
          child: new Form(
            child: new Focus(
              key: focusKey,
              child: new Block(
                children: <Widget>[
                  new Input(
                    key: inputKey,
                    formField: new FormField<String>(
131
                      setter: (String value) { fieldValue = value; }
132 133 134 135 136
                    )
                  ),
                  new Input(
                    formField: new FormField<String>(
                      validator: errorText
137
                    )
138 139
                  )
                ]
140 141 142
              )
            )
          )
143 144 145
        )
      );
    }
146

147
    await tester.pumpWidget(builder());
148
    Focus.moveTo(inputKey);
149
    await tester.pump();
150

151
    Future<Null> checkErrorText(String testValue) async {
152
      enterText(testValue);
153
      await tester.pump();
154

155
      expect(fieldValue, equals(testValue));
156

157 158
      // Check for a new Text widget with our error text.
      expect(find.text(errorText(testValue)), findsOneWidget);
159
      return null;
160
    }
161

162 163
    await checkErrorText('Test');
    await checkErrorText('');
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

  testWidgets('Provide initial value to input', (WidgetTester tester) async {
    String initialValue = 'hello';
    String currentValue;

    Widget builder() {
      return new Center(
          child: new Material(
              child: new Form(
                  child: new Input(
                      value: new InputValue(text: initialValue),
                      formField: new FormField<String>(
                          setter: (String value) { currentValue = value; }
                      )
                  )
              )
          )
      );
    }

    await tester.pumpWidget(builder());

    // initial value should be loaded into keyboard editing state
    expect(mockKeyboard.currentState, isNotNull);
    expect(mockKeyboard.currentState.text, equals(initialValue));

    // initial value should also be visible in the raw input line
    RawInputLineState editableText = tester.state(find.byType(RawInputLine));
    expect(editableText.config.value.text, equals(initialValue));

    // sanity check, make sure we can still edit the text and everything updates
    expect(currentValue, isNull);
    enterText('world');
    expect(currentValue, equals('world'));
    await tester.pump();
    expect(editableText.config.value.text, equals('world'));

  });
203
}