form_test.dart 5.53 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;
Adam Barth's avatar
Adam Barth committed
8
import 'package:meta/meta.dart';
9

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

13
  mojom.KeyboardClient client;
14
  mojom.EditingState currentState;
15 16

  @override
Adam Barth's avatar
Adam Barth committed
17
  void setClient(@checked mojom.KeyboardClientStub client, mojom.KeyboardConfiguration configuraiton) {
18 19 20 21 22 23 24 25 26 27
    this.client = client.impl;
  }

  @override
  void show() {}

  @override
  void hide() {}

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

32 33 34 35
}

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

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

  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);
  }

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

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

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

69
    expect(fieldValue, isNull);
70

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

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

81
  testWidgets('Validator sets the error text', (WidgetTester tester) async {
82 83 84 85 86 87 88 89 90 91 92
    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
93 94 95
              )
            )
          )
96 97 98
        )
      );
    }
99

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

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

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

113
  testWidgets('Multiple Inputs communicate', (WidgetTester tester) async {
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    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>(
132
                      setter: (String value) { fieldValue = value; }
133 134 135 136 137
                    )
                  ),
                  new Input(
                    formField: new FormField<String>(
                      validator: errorText
138
                    )
139 140
                  )
                ]
141 142 143
              )
            )
          )
144 145 146
        )
      );
    }
147

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

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

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

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

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

  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'));

  });
204
}