desktop_text_selection_toolbar_button_test.dart 4.06 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/gestures.dart';
7 8 9 10 11 12 13 14 15 16 17 18 19 20
import 'package:flutter_test/flutter_test.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  testWidgets('can press', (WidgetTester tester) async {
    bool pressed = false;
    await tester.pumpWidget(
      CupertinoApp(
        home: Center(
          child: CupertinoDesktopTextSelectionToolbarButton(
            onPressed: () {
              pressed = true;
            },
21
            child: const Text('Tap me'),
22 23 24 25 26 27 28 29 30 31 32
          ),
        ),
      ),
    );

    expect(pressed, false);

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

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  testWidgets('keeps contrast with background on hover',
      (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        home: Center(
          child: CupertinoDesktopTextSelectionToolbarButton.text(
            text: 'Tap me',
            onPressed: () {},
          ),
        ),
      ),
    );

    final BuildContext context =
        tester.element(find.byType(CupertinoDesktopTextSelectionToolbarButton));

    // The Text color is a CupertinoDynamicColor so we have to compare the color
    // values instead of just comparing the colors themselves.
    expect(
      (tester.firstWidget(find.text('Tap me')) as Text).style!.color!.value,
      CupertinoColors.black.value,
    );

    // Hover gesture
    final TestGesture gesture =
        await tester.createGesture(kind: PointerDeviceKind.mouse);
    await gesture.addPointer(location: Offset.zero);
    addTearDown(gesture.removePointer);
    await tester.pump();
    await gesture.moveTo(tester
        .getCenter(find.byType(CupertinoDesktopTextSelectionToolbarButton)));
    await tester.pumpAndSettle();

    // The color here should be a standard Color, there's no need to use value.
    expect(
      (tester.firstWidget(find.text('Tap me')) as Text).style!.color,
      CupertinoTheme.of(context).primaryContrastingColor,
    );
  });

73 74 75 76 77 78
  testWidgets('pressedOpacity defaults to 0.1', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        home: Center(
          child: CupertinoDesktopTextSelectionToolbarButton(
            onPressed: () { },
79
            child: const Text('Tap me'),
80 81 82 83 84 85 86 87 88 89 90 91 92
          ),
        ),
      ),
    );

    // Original at full opacity.
    FadeTransition opacity = tester.widget(find.descendant(
      of: find.byType(CupertinoDesktopTextSelectionToolbarButton),
      matching: find.byType(FadeTransition),
    ));
    expect(opacity.opacity.value, 1.0);

    // Make a "down" gesture on the button.
93 94
    final Offset center = tester
        .getCenter(find.byType(CupertinoDesktopTextSelectionToolbarButton));
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    final TestGesture gesture = await tester.startGesture(center);
    await tester.pumpAndSettle();

    // Opacity reduces during the down gesture.
    opacity = tester.widget(find.descendant(
      of: find.byType(CupertinoDesktopTextSelectionToolbarButton),
      matching: find.byType(FadeTransition),
    ));
    expect(opacity.opacity.value, 0.7);

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

    // Opacity is back to normal.
    opacity = tester.widget(find.descendant(
      of: find.byType(CupertinoDesktopTextSelectionToolbarButton),
      matching: find.byType(FadeTransition),
    ));
    expect(opacity.opacity.value, 1.0);
  });
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  testWidgets('passing null to onPressed disables the button', (WidgetTester tester) async {
    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: CupertinoDesktopTextSelectionToolbarButton(
            onPressed: null,
            child: Text('Tap me'),
          ),
        ),
      ),
    );

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