ink_well_test.dart 11.1 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
  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: () {},
108
              onHover: (bool hover) {},
109 110 111 112 113 114 115
            ),
          ),
        ),
      ),
    ));
    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await gesture.addPointer();
116
    addTearDown(gesture.removePointer);
117 118 119 120 121 122 123
    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)));
  });

  testWidgets('ink response changes color on focus', (WidgetTester tester) async {
124
    WidgetsBinding.instance.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    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: () {},
142
                onHover: (bool hover) {},
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
              ),
            ),
          ),
        ),
      ),
    ));
    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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  testWidgets("ink response doesn't change color on focus when on touch device", (WidgetTester tester) async {
    WidgetsBinding.instance.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTouch;
    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: () {},
177
                  onHover: (bool hover) {},
178 179 180 181 182 183 184 185 186 187 188 189 190 191
              ),
            ),
          ),
        ),
      ),
    ));
    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, paintsExactlyCountTimes(#rect, 0));
  });

192 193 194 195
  group('feedback', () {
    FeedbackTester feedback;

    setUp(() {
196
      feedback = FeedbackTester();
197 198 199 200 201 202 203
    });

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

    testWidgets('enabled (default)', (WidgetTester tester) async {
204 205
      await tester.pumpWidget(Material(
        child: Directionality(
206
          textDirection: TextDirection.ltr,
207 208
          child: Center(
            child: InkWell(
209 210
              onTap: () {},
              onLongPress: () {},
211
            ),
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
          ),
        ),
      ));
      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 {
232 233
      await tester.pumpWidget(Material(
        child: Directionality(
234
          textDirection: TextDirection.ltr,
235 236
          child: Center(
            child: InkWell(
237 238
              onTap: () {},
              onLongPress: () {},
239 240
              enableFeedback: false,
            ),
241
          ),
242
        ),
243 244 245 246 247 248 249 250 251 252 253 254
      ));
      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);
    });
  });
255 256

  testWidgets('splashing survives scrolling when keep-alive is enabled', (WidgetTester tester) async {
257
    Future<void> runTest(bool keepAlive) async {
258
      await tester.pumpWidget(
259
        Directionality(
260
          textDirection: TextDirection.ltr,
261
          child: Material(
262 263
            child: CompositedTransformFollower(
              // forces a layer, which makes the paints easier to separate out
264 265
              link: LayerLink(),
              child: ListView(
266
                addAutomaticKeepAlives: keepAlive,
267
                dragStartBehavior: DragStartBehavior.down,
268
                children: <Widget>[
269
                  Container(height: 500.0, child: InkWell(onTap: () {}, child: const Placeholder())),
270 271
                  Container(height: 500.0),
                  Container(height: 500.0),
272 273 274
                ],
              ),
            ),
275 276
          ),
        ),
277
      );
278
      expect(tester.renderObject<RenderProxyBox>(find.byType(PhysicalModel)).child, isNot(paints..circle()));
279 280 281
      await tester.tap(find.byType(InkWell));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 10));
282
      expect(tester.renderObject<RenderProxyBox>(find.byType(PhysicalModel)).child, paints..circle());
283 284 285 286 287
      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(
288
        tester.renderObject<RenderProxyBox>(find.byType(PhysicalModel)).child,
289
        keepAlive ? (paints..circle()) : isNot(paints..circle()),
290 291
      );
    }
292

293 294 295
    await runTest(true);
    await runTest(false);
  });
296 297

  testWidgets('excludeFromSemantics', (WidgetTester tester) async {
298
    final SemanticsTester semantics = SemanticsTester(tester);
299

300
    await tester.pumpWidget(Directionality(
301
      textDirection: TextDirection.ltr,
302 303
      child: Material(
        child: InkWell(
304
          onTap: () {},
305 306 307 308 309 310
          child: const Text('Button'),
        ),
      ),
    ));
    expect(semantics, includesNodeWith(label: 'Button', actions: <SemanticsAction>[SemanticsAction.tap]));

311
    await tester.pumpWidget(Directionality(
312
      textDirection: TextDirection.ltr,
313 314
      child: Material(
        child: InkWell(
315
          onTap: () {},
316 317 318 319 320 321 322 323 324
          child: const Text('Button'),
          excludeFromSemantics: true,
        ),
      ),
    ));
    expect(semantics, isNot(includesNodeWith(label: 'Button', actions: <SemanticsAction>[SemanticsAction.tap])));

    semantics.dispose();
  });
325
}