context_menu_action_test.dart 5.82 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// 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 7 8
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
9
import 'package:flutter_test/flutter_test.dart';
10

11 12
import '../rendering/mock_canvas.dart';

13 14
void main() {
  // Constants taken from _ContextMenuActionState.
15
  const CupertinoDynamicColor kBackgroundColor = CupertinoDynamicColor.withBrightness(
16 17 18
    color: Color(0xFFEEEEEE),
    darkColor: Color(0xFF212122),
  );
19
  const CupertinoDynamicColor kBackgroundColorPressed = CupertinoDynamicColor.withBrightness(
20 21 22
    color: Color(0xFFDDDDDD),
    darkColor: Color(0xFF3F3F40),
  );
23 24
  const Color kDestructiveActionColor = CupertinoColors.destructiveRed;
  const FontWeight kDefaultActionWeight = FontWeight.w600;
25

26
  Widget getApp({
27 28 29 30 31
    VoidCallback? onPressed,
    bool isDestructiveAction = false,
    bool isDefaultAction = false,
    Brightness? brightness,
  }) {
32 33 34 35
    final UniqueKey actionKey = UniqueKey();
    final CupertinoContextMenuAction action = CupertinoContextMenuAction(
      key: actionKey,
      onPressed: onPressed,
36 37 38
      trailingIcon: CupertinoIcons.home,
      isDestructiveAction: isDestructiveAction,
      isDefaultAction: isDefaultAction,
39
      child: const Text('I am a CupertinoContextMenuAction'),
40 41 42
    );

    return CupertinoApp(
43 44 45
      theme: CupertinoThemeData(
        brightness: brightness ?? Brightness.light,
      ),
46 47 48 49 50 51 52 53
      home: CupertinoPageScaffold(
        child: Center(
          child: action,
        ),
      ),
    );
  }

54
  TextStyle getTextStyle(WidgetTester tester) {
55 56 57 58 59 60
    final Finder finder = find.descendant(
      of: find.byType(CupertinoContextMenuAction),
      matching: find.byType(DefaultTextStyle),
    );
    expect(finder, findsOneWidget);
    final DefaultTextStyle defaultStyle = tester.widget(finder);
Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
61
    return defaultStyle.style;
62 63
  }

64
  Icon getIcon(WidgetTester tester) {
65 66 67 68 69 70 71 72 73
    final Finder finder = find.descendant(
      of: find.byType(CupertinoContextMenuAction),
      matching: find.byType(Icon),
    );
    expect(finder, findsOneWidget);
    final Icon icon = tester.widget(finder);
    return icon;
  }

74 75
  testWidgets('responds to taps', (WidgetTester tester) async {
    bool wasPressed = false;
76
    await tester.pumpWidget(getApp(onPressed: () {
77 78 79 80 81 82 83 84 85
      wasPressed = true;
    }));

    expect(wasPressed, false);
    await tester.tap(find.byType(CupertinoContextMenuAction));
    expect(wasPressed, true);
  });

  testWidgets('turns grey when pressed and held', (WidgetTester tester) async {
86
    await tester.pumpWidget(getApp());
87
    expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColor.color));
88 89 90 91

    final Offset actionCenterLight = tester.getCenter(find.byType(CupertinoContextMenuAction));
    final TestGesture gestureLight = await tester.startGesture(actionCenterLight);
    await tester.pump();
92
    expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColorPressed.color));
93 94 95

    await gestureLight.up();
    await tester.pump();
96
    expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColor.color));
97

98
    await tester.pumpWidget(getApp(brightness: Brightness.dark));
99
    expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColor.darkColor));
100

101 102
    final Offset actionCenterDark = tester.getCenter(find.byType(CupertinoContextMenuAction));
    final TestGesture gestureDark = await tester.startGesture(actionCenterDark);
103
    await tester.pump();
104
    expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColorPressed.darkColor));
105

106
    await gestureDark.up();
107
    await tester.pump();
108
    expect(find.byType(CupertinoContextMenuAction), paints..rect(color: kBackgroundColor.darkColor));
109
  });
110 111

  testWidgets('icon and textStyle colors are correct out of the box', (WidgetTester tester) async {
112 113 114
    await tester.pumpWidget(getApp());
    expect(getTextStyle(tester).color, CupertinoColors.label);
    expect(getIcon(tester).color,  CupertinoColors.label);
115 116 117
  });

  testWidgets('icon and textStyle colors are correct for destructive actions', (WidgetTester tester) async {
118 119 120
    await tester.pumpWidget(getApp(isDestructiveAction: true));
    expect(getTextStyle(tester).color, kDestructiveActionColor);
    expect(getIcon(tester).color, kDestructiveActionColor);
121 122 123
  });

  testWidgets('textStyle is correct for defaultAction', (WidgetTester tester) async {
124 125
    await tester.pumpWidget(getApp(isDefaultAction: true));
    expect(getTextStyle(tester).fontWeight, kDefaultActionWeight);
126 127
  });

128 129 130 131
  testWidgets(
    'Hovering over Cupertino context menu action updates cursor to clickable on Web',
    (WidgetTester tester) async {
      /// Cupertino context menu action without "onPressed" callback.
132
      await tester.pumpWidget(getApp());
133 134 135 136 137 138 139
      final Offset contextMenuAction = tester.getCenter(find.text('I am a CupertinoContextMenuAction'));
      final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
      await gesture.addPointer(location: contextMenuAction);
      await tester.pumpAndSettle();
      expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);

      // / Cupertino context menu action with "onPressed" callback.
140
      await tester.pumpWidget(getApp(onPressed: (){}));
141 142 143 144 145 146 147 148 149 150 151
      await gesture.moveTo(const Offset(10, 10));
      await tester.pumpAndSettle();
      expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);

      await gesture.moveTo(contextMenuAction);
      await tester.pumpAndSettle();
      expect(
        RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
        kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic,
      );
  });
152
}