text_field_splash_test.dart 5.49 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
import 'package:flutter/gestures.dart' show kPressTimeout;
6 7 8
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

9 10
bool confirmCalled = false;
bool cancelCalled = false;
11 12 13

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

  @override
  void confirm() {
41
    confirmCalled = true;
42 43 44 45 46
    super.confirm();
  }

  @override
  void cancel() {
47
    cancelCalled = true;
48 49 50 51 52 53 54 55 56
    super.cancel();
  }
}

class TestInkSplashFactory extends InteractiveInkFeatureFactory {
  const TestInkSplashFactory();

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

void main() {
86 87 88 89 90 91
  setUp(() {
    confirmCalled = false;
    cancelCalled = false;
  });

  testWidgets('Tapping should never cause a splash', (WidgetTester tester) async {
92 93
    final Key textField1 = UniqueKey();
    final Key textField2 = UniqueKey();
94 95

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

    await tester.tap(find.byKey(textField1));
125
    await tester.pumpAndSettle();
126 127
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
128 129

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

    await tester.tap(find.byKey(textField2));
135
    await tester.pumpAndSettle();
136 137
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
138 139

    await tester.tapAt(tester.getTopLeft(find.byKey(textField1)));
140
    await tester.pumpAndSettle();
141 142
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
143 144

    await tester.tap(find.byKey(textField2));
145
    await tester.pumpAndSettle();
146 147
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
148 149
  });

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

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

    await tester.pump(kPressTimeout);

184 185
    await gesture1.moveTo(const Offset(400.0, 300.0));
    await gesture1.up();
186 187
    expect(confirmCalled, isFalse);
    expect(cancelCalled, isFalse);
188

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