feedback_test.dart 7.01 KB
Newer Older
1 2 3 4
// Copyright 2017 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.

5 6
import 'dart:ui';

7
import 'package:flutter/material.dart';
8
import 'package:flutter/services.dart';
9 10
import 'package:flutter_test/flutter_test.dart';

11
import '../widgets/semantics_tester.dart';
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import 'feedback_tester.dart';

void main () {
  const Duration kWaitDuration = const Duration(seconds: 1);

  FeedbackTester feedback;

  setUp(() {
    feedback = new FeedbackTester();
  });

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

  group('Feedback on Android', () {
28 29 30 31 32 33 34 35 36 37 38 39 40
    List<Map<String, Object>> semanticEvents;

    setUp(() {
      semanticEvents = <Map<String, Object>>[];
      SystemChannels.accessibility.setMockMessageHandler((dynamic message) {
        final Map<dynamic, dynamic> typedMessage = message;
        semanticEvents.add(typedMessage.cast<String, Object>());
      });
    });

    tearDown(() {
      SystemChannels.accessibility.setMockMessageHandler(null);
    });
41 42

    testWidgets('forTap', (WidgetTester tester) async {
43 44
      final SemanticsTester semanticsTester = new SemanticsTester(tester);

45 46 47 48 49 50 51 52
      await tester.pumpWidget(new TestWidget(
        tapHandler: (BuildContext context) {
          return () => Feedback.forTap(context);
        },
      ));
      await tester.pumpAndSettle(kWaitDuration);
      expect(feedback.hapticCount, 0);
      expect(feedback.clickSoundCount, 0);
53
      expect(semanticEvents, isEmpty);
54 55 56

      await tester.tap(find.text('X'));
      await tester.pumpAndSettle(kWaitDuration);
57 58
      final RenderObject object = tester.firstRenderObject(find.byType(GestureDetector));

59 60
      expect(feedback.hapticCount, 0);
      expect(feedback.clickSoundCount, 1);
61 62 63 64 65 66 67 68
      expect(semanticEvents.single, <String, dynamic>{
        'type': 'tap',
        'nodeId': object.debugSemantics.id,
        'data': <String, dynamic>{},
      });
      expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.tap), true);

      semanticsTester.dispose();
69 70 71
    });

    testWidgets('forTap Wrapper', (WidgetTester tester) async {
72 73
      final SemanticsTester semanticsTester = new SemanticsTester(tester);

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
      int callbackCount = 0;
      final VoidCallback callback = () {
        callbackCount++;
      };

      await tester.pumpWidget(new TestWidget(
        tapHandler: (BuildContext context) {
          return Feedback.wrapForTap(callback, context);
        },
      ));
      await tester.pumpAndSettle(kWaitDuration);
      expect(feedback.hapticCount, 0);
      expect(feedback.clickSoundCount, 0);
      expect(callbackCount, 0);

      await tester.tap(find.text('X'));
      await tester.pumpAndSettle(kWaitDuration);
91 92
      final RenderObject object = tester.firstRenderObject(find.byType(GestureDetector));

93 94 95
      expect(feedback.hapticCount, 0);
      expect(feedback.clickSoundCount, 1);
      expect(callbackCount, 1);
96 97 98 99 100 101 102 103
      expect(semanticEvents.single, <String, dynamic>{
        'type': 'tap',
        'nodeId': object.debugSemantics.id,
        'data': <String, dynamic>{},
      });
      expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.tap), true);

      semanticsTester.dispose();
104 105 106
    });

    testWidgets('forLongPress', (WidgetTester tester) async {
107 108
      final SemanticsTester semanticsTester = new SemanticsTester(tester);

109 110 111 112 113 114 115 116 117 118 119
      await tester.pumpWidget(new TestWidget(
        longPressHandler: (BuildContext context) {
          return () => Feedback.forLongPress(context);
        },
      ));
      await tester.pumpAndSettle(kWaitDuration);
      expect(feedback.hapticCount, 0);
      expect(feedback.clickSoundCount, 0);

      await tester.longPress(find.text('X'));
      await tester.pumpAndSettle(kWaitDuration);
120 121
      final RenderObject object = tester.firstRenderObject(find.byType(GestureDetector));

122 123
      expect(feedback.hapticCount, 1);
      expect(feedback.clickSoundCount, 0);
124 125 126 127 128 129 130 131
      expect(semanticEvents.single, <String, dynamic>{
        'type': 'longPress',
        'nodeId': object.debugSemantics.id,
        'data': <String, dynamic>{},
      });
      expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.longPress), true);

      semanticsTester.dispose();
132 133 134
    });

    testWidgets('forLongPress Wrapper', (WidgetTester tester) async {
135
      final SemanticsTester semanticsTester = new SemanticsTester(tester);
136 137 138 139 140 141 142 143 144 145 146
      int callbackCount = 0;
      final VoidCallback callback = () {
        callbackCount++;
      };

      await tester.pumpWidget(new TestWidget(
        longPressHandler: (BuildContext context) {
          return Feedback.wrapForLongPress(callback, context);
        },
      ));
      await tester.pumpAndSettle(kWaitDuration);
147 148
      final RenderObject object = tester.firstRenderObject(find.byType(GestureDetector));

149 150 151 152 153 154 155 156 157
      expect(feedback.hapticCount, 0);
      expect(feedback.clickSoundCount, 0);
      expect(callbackCount, 0);

      await tester.longPress(find.text('X'));
      await tester.pumpAndSettle(kWaitDuration);
      expect(feedback.hapticCount, 1);
      expect(feedback.clickSoundCount, 0);
      expect(callbackCount, 1);
158 159 160 161 162 163 164 165
      expect(semanticEvents.single, <String, dynamic>{
        'type': 'longPress',
        'nodeId': object.debugSemantics.id,
        'data': <String, dynamic>{},
      });
      expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.longPress), true);

      semanticsTester.dispose();
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 204 205 206
    });

  });

  group('Feedback on iOS', () {
    testWidgets('forTap', (WidgetTester tester) async {
      await tester.pumpWidget(new Theme(
        data: new ThemeData(platform: TargetPlatform.iOS),
        child: new TestWidget(
          tapHandler: (BuildContext context) {
            return () => Feedback.forTap(context);
          },
        ),
      ));

      await tester.tap(find.text('X'));
      await tester.pumpAndSettle(kWaitDuration);
      expect(feedback.hapticCount, 0);
      expect(feedback.clickSoundCount, 0);
    });

    testWidgets('forLongPress', (WidgetTester tester) async {
      await tester.pumpWidget(new Theme(
        data: new ThemeData(platform: TargetPlatform.iOS),
        child: new TestWidget(
          longPressHandler: (BuildContext context) {
            return () => Feedback.forLongPress(context);
          },
        ),
      ));

      await tester.longPress(find.text('X'));
      await tester.pumpAndSettle(kWaitDuration);
      expect(feedback.hapticCount, 0);
      expect(feedback.clickSoundCount, 0);
    });
  });
}

class TestWidget extends StatelessWidget {

207
  const TestWidget({
208 209
    this.tapHandler = nullHandler,
    this.longPressHandler = nullHandler,
210 211 212 213 214 215 216 217 218 219 220 221
  });

  final HandlerCreator tapHandler;
  final HandlerCreator longPressHandler;

  static VoidCallback nullHandler(BuildContext context) => null;

  @override
  Widget build(BuildContext context) {
    return new GestureDetector(
        onTap: tapHandler(context),
        onLongPress: longPressHandler(context),
Ian Hickson's avatar
Ian Hickson committed
222
        child: const Text('X', textDirection: TextDirection.ltr),
223 224 225 226
    );
  }
}

227
typedef VoidCallback HandlerCreator(BuildContext context);