ink_well_test.dart 9.7 KB
Newer Older
1 2 3 4 5
// 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/material.dart';
6
import 'package:flutter/rendering.dart';
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:flutter/gestures.dart';
9

10
import '../rendering/mock_canvas.dart';
11
import '../widgets/semantics_tester.dart';
12 13
import 'feedback_tester.dart';

14 15
void main() {
  testWidgets('InkWell gestures control test', (WidgetTester tester) async {
16
    final List<String> log = <String>[];
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: Material(
        child: Center(
          child: InkWell(
            onTap: () {
              log.add('tap');
            },
            onDoubleTap: () {
              log.add('double-tap');
            },
            onLongPress: () {
              log.add('long-press');
            },
            onTapDown: (TapDownDetails details) {
              log.add('tap-down');
            },
            onTapCancel: () {
              log.add('tap-cancel');
            },
38
          ),
39
        ),
40 41
      ),
    ));
42 43 44 45 46 47 48

    await tester.tap(find.byType(InkWell), pointer: 1);

    expect(log, isEmpty);

    await tester.pump(const Duration(seconds: 1));

49
    expect(log, equals(<String>['tap-down', 'tap']));
50 51 52
    log.clear();

    await tester.tap(find.byType(InkWell), pointer: 2);
53
    await tester.pump(const Duration(milliseconds: 100));
54 55
    await tester.tap(find.byType(InkWell), pointer: 3);

56
    expect(log, equals(<String>['double-tap']));
57 58 59 60
    log.clear();

    await tester.longPress(find.byType(InkWell), pointer: 4);

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    expect(log, equals(<String>['tap-down', 'tap-cancel', 'long-press']));

    log.clear();
    TestGesture gesture = await tester.startGesture(tester.getRect(find.byType(InkWell)).center);
    await tester.pump(const Duration(milliseconds: 100));
    expect(log, equals(<String>['tap-down']));
    await gesture.up();
    await tester.pump(const Duration(seconds: 1));

    log.clear();
    gesture = await tester.startGesture(tester.getRect(find.byType(InkWell)).center);
    await tester.pump(const Duration(milliseconds: 100));
    await gesture.moveBy(const Offset(0.0, 200.0));
    await gesture.cancel();
    expect(log, equals(<String>['tap-down', 'tap-cancel']));
76
  });
77 78 79

  testWidgets('long-press and tap on disabled should not throw', (WidgetTester tester) async {
    await tester.pumpWidget(const Material(
80 81 82 83 84
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: InkWell(),
        ),
85
      ),
86 87 88 89 90 91 92
    ));
    await tester.tap(find.byType(InkWell), pointer: 1);
    await tester.pump(const Duration(seconds: 1));
    await tester.longPress(find.byType(InkWell), pointer: 1);
    await tester.pump(const Duration(seconds: 1));
  });

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  testWidgets('ink well changes color on hover', (WidgetTester tester) async {
    await tester.pumpWidget(Material(
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: Container(
            width: 100,
            height: 100,
            child: InkWell(
              hoverColor: const Color(0xff00ff00),
              splashColor: const Color(0xffff0000),
              focusColor: const Color(0xff0000ff),
              highlightColor: const Color(0xf00fffff),
              onTap: () {},
              onLongPress: () {},
              onHover: (bool hover) {}
            ),
          ),
        ),
      ),
    ));
    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await gesture.addPointer();
    await gesture.moveTo(tester.getCenter(find.byType(Container)));
    await tester.pumpAndSettle();
    final RenderObject inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
    expect(inkFeatures, paints..rect(rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0), color: const Color(0xff00ff00)));

    await gesture.removePointer();
  });

  testWidgets('ink response changes color on focus', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode(debugLabel: 'Ink Focus');
    await tester.pumpWidget(Material(
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: Focus(
            focusNode: focusNode,
            child: Container(
              width: 100,
              height: 100,
              child: InkWell(
                hoverColor: const Color(0xff00ff00),
                splashColor: const Color(0xffff0000),
                focusColor: const Color(0xff0000ff),
                highlightColor: const Color(0xf00fffff),
                onTap: () {},
                onLongPress: () {},
                onHover: (bool hover) {}
              ),
            ),
          ),
        ),
      ),
    ));
    await tester.pumpAndSettle();
    final RenderObject inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
    expect(inkFeatures, paintsExactlyCountTimes(#rect, 0));
    focusNode.requestFocus();
    await tester.pumpAndSettle();
    expect(inkFeatures, paints
      ..rect(rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0), color: const Color(0xff0000ff)));
  });

158 159 160 161
  group('feedback', () {
    FeedbackTester feedback;

    setUp(() {
162
      feedback = FeedbackTester();
163 164 165 166 167 168 169
    });

    tearDown(() {
      feedback?.dispose();
    });

    testWidgets('enabled (default)', (WidgetTester tester) async {
170 171
      await tester.pumpWidget(Material(
        child: Directionality(
172
          textDirection: TextDirection.ltr,
173 174
          child: Center(
            child: InkWell(
175 176
              onTap: () {},
              onLongPress: () {},
177
            ),
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
          ),
        ),
      ));
      await tester.tap(find.byType(InkWell), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 1);
      expect(feedback.hapticCount, 0);

      await tester.tap(find.byType(InkWell), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 2);
      expect(feedback.hapticCount, 0);

      await tester.longPress(find.byType(InkWell), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 2);
      expect(feedback.hapticCount, 1);
    });

    testWidgets('disabled', (WidgetTester tester) async {
198 199
      await tester.pumpWidget(Material(
        child: Directionality(
200
          textDirection: TextDirection.ltr,
201 202
          child: Center(
            child: InkWell(
203 204
              onTap: () {},
              onLongPress: () {},
205 206
              enableFeedback: false,
            ),
207
          ),
208
        ),
209 210 211 212 213 214 215 216 217 218 219 220
      ));
      await tester.tap(find.byType(InkWell), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 0);
      expect(feedback.hapticCount, 0);

      await tester.longPress(find.byType(InkWell), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 0);
      expect(feedback.hapticCount, 0);
    });
  });
221 222

  testWidgets('splashing survives scrolling when keep-alive is enabled', (WidgetTester tester) async {
223
    Future<void> runTest(bool keepAlive) async {
224
      await tester.pumpWidget(
225
        Directionality(
226
          textDirection: TextDirection.ltr,
227
          child: Material(
228 229
            child: CompositedTransformFollower(
              // forces a layer, which makes the paints easier to separate out
230 231
              link: LayerLink(),
              child: ListView(
232
                addAutomaticKeepAlives: keepAlive,
233
                dragStartBehavior: DragStartBehavior.down,
234
                children: <Widget>[
235
                  Container(height: 500.0, child: InkWell(onTap: () {}, child: const Placeholder())),
236 237
                  Container(height: 500.0),
                  Container(height: 500.0),
238 239 240
                ],
              ),
            ),
241 242
          ),
        ),
243
      );
244
      expect(tester.renderObject<RenderProxyBox>(find.byType(PhysicalModel)).child, isNot(paints..circle()));
245 246 247
      await tester.tap(find.byType(InkWell));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 10));
248
      expect(tester.renderObject<RenderProxyBox>(find.byType(PhysicalModel)).child, paints..circle());
249 250 251 252 253
      await tester.drag(find.byType(ListView), const Offset(0.0, -1000.0));
      await tester.pump(const Duration(milliseconds: 10));
      await tester.drag(find.byType(ListView), const Offset(0.0, 1000.0));
      await tester.pump(const Duration(milliseconds: 10));
      expect(
254
        tester.renderObject<RenderProxyBox>(find.byType(PhysicalModel)).child,
255
        keepAlive ? (paints..circle()) : isNot(paints..circle()),
256 257
      );
    }
258

259 260 261
    await runTest(true);
    await runTest(false);
  });
262 263

  testWidgets('excludeFromSemantics', (WidgetTester tester) async {
264
    final SemanticsTester semantics = SemanticsTester(tester);
265

266
    await tester.pumpWidget(Directionality(
267
      textDirection: TextDirection.ltr,
268 269
      child: Material(
        child: InkWell(
270
          onTap: () {},
271 272 273 274 275 276
          child: const Text('Button'),
        ),
      ),
    ));
    expect(semantics, includesNodeWith(label: 'Button', actions: <SemanticsAction>[SemanticsAction.tap]));

277
    await tester.pumpWidget(Directionality(
278
      textDirection: TextDirection.ltr,
279 280
      child: Material(
        child: InkWell(
281
          onTap: () {},
282 283 284 285 286 287 288 289 290
          child: const Text('Button'),
          excludeFromSemantics: true,
        ),
      ),
    ));
    expect(semantics, isNot(includesNodeWith(label: 'Button', actions: <SemanticsAction>[SemanticsAction.tap])));

    semantics.dispose();
  });
291
}