absorb_pointer_test.dart 2.78 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/material.dart';
6
import 'package:flutter_test/flutter_test.dart';
7

8 9
import 'semantics_tester.dart';

10 11 12 13
void main() {
  testWidgets('AbsorbPointers do not block siblings', (WidgetTester tester) async {
    bool tapped = false;
    await tester.pumpWidget(
14
      Column(
15
        children: <Widget>[
16 17
          Expanded(
            child: GestureDetector(
18 19 20 21
              onTap: () => tapped = true,
            ),
          ),
          const Expanded(
22
            child: AbsorbPointer(),
23 24 25 26 27 28 29
          ),
        ],
      ),
    );
    await tester.tap(find.byType(GestureDetector));
    expect(tapped, true);
  });
30

31 32 33 34 35 36 37 38 39 40 41 42 43
  group('AbsorbPointer semantics', () {
    testWidgets('does not change semantics when not absorbing', (WidgetTester tester) async {
      final UniqueKey key = UniqueKey();
      await tester.pumpWidget(
        MaterialApp(
          home: AbsorbPointer(
            absorbing: false,
            child: ElevatedButton(
              key: key,
              onPressed: () { },
              child: const Text('button'),
            ),
          ),
44
        ),
45 46 47 48 49 50 51 52 53 54 55 56 57
      );
      expect(
        tester.getSemantics(find.byKey(key)),
        matchesSemantics(
          label: 'button',
          hasTapAction: true,
          isButton: true,
          isFocusable: true,
          hasEnabledState: true,
          isEnabled: true,
        ),
      );
    });
58

59 60 61 62 63 64 65 66 67 68 69 70 71
    testWidgets('drops semantics when its ignoreSemantics is true', (WidgetTester tester) async {
      final SemanticsTester semantics = SemanticsTester(tester);
      final UniqueKey key = UniqueKey();
      await tester.pumpWidget(
        MaterialApp(
          home: AbsorbPointer(
            ignoringSemantics: true,
            child: ElevatedButton(
              key: key,
              onPressed: () { },
              child: const Text('button'),
            ),
          ),
72
        ),
73 74 75 76
      );
      expect(semantics, isNot(includesNodeWith(label: 'button')));
      semantics.dispose();
    });
77

78 79 80 81 82 83 84 85 86 87
    testWidgets('ignores user interactions', (WidgetTester tester) async {
      final UniqueKey key = UniqueKey();
      await tester.pumpWidget(
        MaterialApp(
          home: AbsorbPointer(
            child: ElevatedButton(
              key: key,
              onPressed: () { },
              child: const Text('button'),
            ),
88
          ),
89 90 91 92 93 94 95 96 97 98 99 100 101 102
        ),
      );
      expect(
        tester.getSemantics(find.byKey(key)),
        // Tap action is blocked.
        matchesSemantics(
          label: 'button',
          isButton: true,
          isFocusable: true,
          hasEnabledState: true,
          isEnabled: true,
        ),
      );
    });
103 104
  });
}