text_field_splash_test.dart 5.47 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'package:flutter/gestures.dart' show kPressTimeout;
8 9 10 11
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';

12 13
bool confirmCalled = false;
bool cancelCalled = false;
14 15 16 17 18 19 20

class TestInkSplash extends InkSplash {
  TestInkSplash({
    MaterialInkController controller,
    RenderBox referenceBox,
    Offset position,
    Color color,
21
    bool containedInkWell = false,
22 23
    RectCallback rectCallback,
    BorderRadius borderRadius,
24
    ShapeBorder customBorder,
25 26
    double radius,
    VoidCallback onRemoved,
27
    TextDirection textDirection,
28 29 30 31 32 33 34 35
  }) : super(
    controller: controller,
    referenceBox: referenceBox,
    position: position,
    color: color,
    containedInkWell: containedInkWell,
    rectCallback: rectCallback,
    borderRadius: borderRadius,
36
    customBorder: customBorder,
37 38
    radius: radius,
    onRemoved: onRemoved,
39
    textDirection: textDirection,
40 41 42 43
  );

  @override
  void confirm() {
44
    confirmCalled = true;
45 46 47 48 49
    super.confirm();
  }

  @override
  void cancel() {
50
    cancelCalled = true;
51 52 53 54 55 56 57 58 59 60 61 62 63
    super.cancel();
  }
}

class TestInkSplashFactory extends InteractiveInkFeatureFactory {
  const TestInkSplashFactory();

  @override
  InteractiveInkFeature create({
    MaterialInkController controller,
    RenderBox referenceBox,
    Offset position,
    Color color,
64
    bool containedInkWell = false,
65 66
    RectCallback rectCallback,
    BorderRadius borderRadius,
67
    ShapeBorder customBorder,
68 69
    double radius,
    VoidCallback onRemoved,
70
    TextDirection textDirection,
71
  }) {
72
    return TestInkSplash(
73 74 75 76 77 78 79
      controller: controller,
      referenceBox: referenceBox,
      position: position,
      color: color,
      containedInkWell: containedInkWell,
      rectCallback: rectCallback,
      borderRadius: borderRadius,
80
      customBorder: customBorder,
81 82
      radius: radius,
      onRemoved: onRemoved,
83
      textDirection: textDirection,
84 85 86 87 88
    );
  }
}

void main() {
89 90 91 92 93 94
  setUp(() {
    confirmCalled = false;
    cancelCalled = false;
  });

  testWidgets('Tapping should never cause a splash', (WidgetTester tester) async {
95 96
    final Key textField1 = UniqueKey();
    final Key textField2 = UniqueKey();
97 98

    await tester.pumpWidget(
99 100 101 102 103
      MaterialApp(
        home: Theme(
          data: ThemeData.light().copyWith(splashFactory: const TestInkSplashFactory()),
          child: Material(
            child: Container(
104
              alignment: Alignment.topLeft,
105
              child: Column(
106
                children: <Widget>[
107
                  TextField(
108 109 110 111 112
                    key: textField1,
                    decoration: const InputDecoration(
                      labelText: 'label',
                    ),
                  ),
113
                  TextField(
114 115 116 117 118 119 120 121 122 123
                    key: textField2,
                    decoration: const InputDecoration(
                      labelText: 'label',
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
124
      ),
125 126 127
    );

    await tester.tap(find.byKey(textField1));
128
    await tester.pumpAndSettle();
129 130
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
131 132

    await tester.tap(find.byKey(textField1));
133
    await tester.pumpAndSettle();
134 135
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
136 137

    await tester.tap(find.byKey(textField2));
138
    await tester.pumpAndSettle();
139 140
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
141 142

    await tester.tapAt(tester.getTopLeft(find.byKey(textField1)));
143
    await tester.pumpAndSettle();
144 145
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
146 147

    await tester.tap(find.byKey(textField2));
148
    await tester.pumpAndSettle();
149 150
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
151 152
  });

153
  testWidgets('Splash should never be created or canceled', (WidgetTester tester) async {
154
    await tester.pumpWidget(
155 156 157 158 159
      MaterialApp(
        home: Theme(
          data: ThemeData.light().copyWith(splashFactory: const TestInkSplashFactory()),
          child: Material(
            child: ListView(
160 161
              children: <Widget>[
                const TextField(
162
                  decoration: InputDecoration(
163 164 165 166
                    labelText: 'label1',
                  ),
                ),
                const TextField(
167
                  decoration: InputDecoration(
168 169 170
                    labelText: 'label2',
                  ),
                ),
171
                Container(
172 173 174 175 176 177 178
                  height: 1000.0,
                  color: const Color(0xFF00FF00),
                ),
              ],
            ),
          ),
        ),
179
      ),
180 181
    );

182
    // If there were a splash, this would cancel the splash.
183
    final TestGesture gesture1 = await tester.startGesture(tester.getCenter(find.text('label1')));
184 185 186

    await tester.pump(kPressTimeout);

187 188
    await gesture1.moveTo(const Offset(400.0, 300.0));
    await gesture1.up();
189 190
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
191

192
    // Pointer is dragged upwards causing a scroll, splash would be canceled.
193
    final TestGesture gesture2 = await tester.startGesture(tester.getCenter(find.text('label2')));
194 195
    await tester.pump(kPressTimeout);
    await gesture2.moveBy(const Offset(0.0, -200.0));
196
    await gesture2.up();
197 198
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
199 200
  });
}