text_selection_toolbar_button_test.dart 3.01 KB
Newer Older
1 2 3 4 5
// 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 'package:flutter/cupertino.dart';
6
import 'package:flutter_test/flutter_test.dart';
7
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8 9 10 11

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

12
  testWidgetsWithLeakTracking('can press', (WidgetTester tester) async {
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    bool pressed = false;
    await tester.pumpWidget(
      CupertinoApp(
        home: Center(
          child: CupertinoTextSelectionToolbarButton(
            child: const Text('Tap me'),
            onPressed: () {
              pressed = true;
            },
          ),
        ),
      ),
    );

    expect(pressed, false);

    await tester.tap(find.byType(CupertinoTextSelectionToolbarButton));
    expect(pressed, true);
  });

33
  testWidgetsWithLeakTracking('background darkens when pressed', (WidgetTester tester) async {
34 35 36 37 38 39 40 41 42 43 44
    await tester.pumpWidget(
      CupertinoApp(
        home: Center(
          child: CupertinoTextSelectionToolbarButton(
            child: const Text('Tap me'),
            onPressed: () { },
          ),
        ),
      ),
    );

45 46 47 48
    // Original with transparent background.
    DecoratedBox decoratedBox = tester.widget(find.descendant(
      of: find.byType(CupertinoButton),
      matching: find.byType(DecoratedBox),
49
    ));
50 51
    BoxDecoration boxDecoration = decoratedBox.decoration as BoxDecoration;
    expect(boxDecoration.color, const Color(0x00000000));
52 53 54 55 56 57

    // Make a "down" gesture on the button.
    final Offset center = tester.getCenter(find.byType(CupertinoTextSelectionToolbarButton));
    final TestGesture gesture = await tester.startGesture(center);
    await tester.pumpAndSettle();

58 59
    // When pressed, the background darkens.
    decoratedBox = tester.widget(find.descendant(
60
      of: find.byType(CupertinoTextSelectionToolbarButton),
61
      matching: find.byType(DecoratedBox),
62
    ));
63 64
    boxDecoration = decoratedBox.decoration as BoxDecoration;
    expect(boxDecoration.color!.value, const Color(0x10000000).value);
65 66 67 68 69

    // Release the down gesture.
    await gesture.up();
    await tester.pumpAndSettle();

70 71
    // Color is back to transparent.
    decoratedBox = tester.widget(find.descendant(
72
      of: find.byType(CupertinoTextSelectionToolbarButton),
73
      matching: find.byType(DecoratedBox),
74
    ));
75 76
    boxDecoration = decoratedBox.decoration as BoxDecoration;
    expect(boxDecoration.color, const Color(0x00000000));
77
  });
78

79
  testWidgetsWithLeakTracking('passing null to onPressed disables the button', (WidgetTester tester) async {
80 81 82 83 84 85 86 87 88 89 90 91 92 93
    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: CupertinoTextSelectionToolbarButton(
            child: Text('Tap me'),
          ),
        ),
      ),
    );

    expect(find.byType(CupertinoButton), findsOneWidget);
    final CupertinoButton button = tester.widget(find.byType(CupertinoButton));
    expect(button.enabled, isFalse);
  });
94
}