tap_region_test.dart 7.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2014 The Flutter 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 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('TapRegionSurface detects outside taps', (WidgetTester tester) async {
12
    final Set<String> tappedOutside = <String>{};
13 14 15 16 17 18 19 20 21 22 23 24
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Column(
          children: <Widget>[
            const Text('Outside Surface'),
            TapRegionSurface(
              child: Row(
                children: <Widget>[
                  const Text('Outside'),
                  TapRegion(
                    onTapOutside: (PointerEvent event) {
25
                      tappedOutside.add('No Group');
26 27 28 29 30 31
                    },
                    child: const Text('No Group'),
                  ),
                  TapRegion(
                    groupId: 1,
                    onTapOutside: (PointerEvent event) {
32
                      tappedOutside.add('Group 1 A');
33 34 35 36 37 38
                    },
                    child: const Text('Group 1 A'),
                  ),
                  TapRegion(
                    groupId: 1,
                    onTapOutside: (PointerEvent event) {
39
                      tappedOutside.add('Group 1 B');
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
                    },
                    child: const Text('Group 1 B'),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );

    await tester.pump();

    Future<void> click(Finder finder) async {
      final TestGesture gesture = await tester.startGesture(
        tester.getCenter(finder),
        kind: PointerDeviceKind.mouse,
      );
      await gesture.up();
      await gesture.removePointer();
    }

62
    expect(tappedOutside, isEmpty);
63 64 65

    await click(find.text('No Group'));
    expect(
66
        tappedOutside,
67 68 69 70
        unorderedEquals(<String>{
          'Group 1 A',
          'Group 1 B',
        }));
71
    tappedOutside.clear();
72 73 74

    await click(find.text('Group 1 A'));
    expect(
75
        tappedOutside,
76 77 78
        equals(<String>{
          'No Group',
        }));
79
    tappedOutside.clear();
80 81 82

    await click(find.text('Group 1 B'));
    expect(
83
        tappedOutside,
84 85 86
        equals(<String>{
          'No Group',
        }));
87
    tappedOutside.clear();
88 89 90

    await click(find.text('Outside'));
    expect(
91
        tappedOutside,
92 93 94 95 96
        unorderedEquals(<String>{
          'No Group',
          'Group 1 A',
          'Group 1 B',
        }));
97
    tappedOutside.clear();
98 99

    await click(find.text('Outside Surface'));
100
    expect(tappedOutside, isEmpty);
101 102
  });
  testWidgets('TapRegionSurface detects inside taps', (WidgetTester tester) async {
103
    final Set<String> tappedInside = <String>{};
104 105 106 107 108 109 110 111 112 113 114 115
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Column(
          children: <Widget>[
            const Text('Outside Surface'),
            TapRegionSurface(
              child: Row(
                children: <Widget>[
                  const Text('Outside'),
                  TapRegion(
                    onTapInside: (PointerEvent event) {
116
                      tappedInside.add('No Group');
117 118 119 120 121 122
                    },
                    child: const Text('No Group'),
                  ),
                  TapRegion(
                    groupId: 1,
                    onTapInside: (PointerEvent event) {
123
                      tappedInside.add('Group 1 A');
124 125 126 127 128 129
                    },
                    child: const Text('Group 1 A'),
                  ),
                  TapRegion(
                    groupId: 1,
                    onTapInside: (PointerEvent event) {
130
                      tappedInside.add('Group 1 B');
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
                    },
                    child: const Text('Group 1 B'),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );

    await tester.pump();

    Future<void> click(Finder finder) async {
      final TestGesture gesture = await tester.startGesture(
        tester.getCenter(finder),
        kind: PointerDeviceKind.mouse,
      );
      await gesture.up();
      await gesture.removePointer();
    }

153
    expect(tappedInside, isEmpty);
154 155 156

    await click(find.text('No Group'));
    expect(
157
        tappedInside,
158 159 160
        unorderedEquals(<String>{
          'No Group',
        }));
161
    tappedInside.clear();
162 163 164

    await click(find.text('Group 1 A'));
    expect(
165
        tappedInside,
166 167 168 169
        equals(<String>{
          'Group 1 A',
          'Group 1 B',
        }));
170
    tappedInside.clear();
171 172 173

    await click(find.text('Group 1 B'));
    expect(
174
        tappedInside,
175 176 177 178
        equals(<String>{
          'Group 1 A',
          'Group 1 B',
        }));
179
    tappedInside.clear();
180 181

    await click(find.text('Outside'));
182 183
    expect(tappedInside, isEmpty);
    tappedInside.clear();
184 185

    await click(find.text('Outside Surface'));
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    expect(tappedInside, isEmpty);
  });
  testWidgets('Setting the group updates the registration', (WidgetTester tester) async {
    final Set<String> tappedOutside = <String>{};
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: TapRegionSurface(
          child: Row(
            children: <Widget>[
              const Text('Outside'),
              TapRegion(
                groupId: 1,
                onTapOutside: (PointerEvent event) {
                  tappedOutside.add('Group 1 A');
                },
                child: const Text('Group 1 A'),
              ),
              TapRegion(
                groupId: 1,
                onTapOutside: (PointerEvent event) {
                  tappedOutside.add('Group 1 B');
                },
                child: const Text('Group 1 B'),
              ),
            ],
          ),
        ),
      ),
    );

    await tester.pump();

    Future<void> click(Finder finder) async {
      final TestGesture gesture = await tester.startGesture(
        tester.getCenter(finder),
        kind: PointerDeviceKind.mouse,
      );
      await gesture.up();
      await gesture.removePointer();
    }

    expect(tappedOutside, isEmpty);

    await click(find.text('Group 1 A'));
    expect(tappedOutside, isEmpty);
    await click(find.text('Group 1 B'));
    expect(tappedOutside, isEmpty);
    await click(find.text('Outside'));
    expect(
        tappedOutside,
        equals(<String>[
          'Group 1 A',
          'Group 1 B',
        ]));
    tappedOutside.clear();

    // Now change out the groups.
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: TapRegionSurface(
          child: Row(
            children: <Widget>[
              const Text('Outside'),
              TapRegion(
                groupId: 1,
                onTapOutside: (PointerEvent event) {
                  tappedOutside.add('Group 1 A');
                },
                child: const Text('Group 1 A'),
              ),
              TapRegion(
                groupId: 2,
                onTapOutside: (PointerEvent event) {
                  tappedOutside.add('Group 2 A');
                },
                child: const Text('Group 2 A'),
              ),
            ],
          ),
        ),
      ),
    );

    await click(find.text('Group 1 A'));
    expect(tappedOutside, equals(<String>['Group 2 A']));
    tappedOutside.clear();

    await click(find.text('Group 2 A'));
    expect(tappedOutside, equals(<String>['Group 1 A']));
    tappedOutside.clear();

    await click(find.text('Outside'));
    expect(
        tappedOutside,
        equals(<String>[
          'Group 1 A',
          'Group 2 A',
        ]));
    tappedOutside.clear();
287 288
  });
}