Repository: flutter/flutter
File: packages/flutter/test/material/menu_anchor_test.dart (Dart)

Code before:
      await hoverOver(tester, find.text(TestMenu.subSubMenu110.label));
      await tester.pump();
      expect(find.byIcon(hoveredIcon), findsOneWidget);

      controller.close();
      await tester.pump();

      await tester.pumpWidget(buildMenu(icon: submenuIcon, enabled: false));
      await tester.tap(find.text(TestMenu.subMenu00.label));
      await tester.pump();
      expect(find.byIcon(disabledIcon), findsOneWidget);
    });
  });

  testWidgets('MenuAnchor.of can return the nearest MenuController', (WidgetTester tester) async {
    final MenuController controller = MenuController();
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: MenuAnchor(
              controller: controller,
              menuChildren: const <Widget>[MenuItemButton(child: Text('menu item'))],
              builder: (BuildContext context, MenuController controller, Widget? child) {
                return FilledButton(
                  onPressed: () {
                    if (controller.isOpen) {
                      controller.close();
                    } else {
                      controller.open();
                    }
                  },
                  child: Text(controller.isOpen ? 'close' : 'open'),
                );
              },
            ),
          ),
        ),
      ),
    );

    expect(MenuAnchor.of(tester.element(find.byType(FilledButton))), equals(controller));
  });

  testWidgets('MenuAnchor.of should throw if there is no MenuController', (
    WidgetTester tester,
  ) async {
    await tester.pumpWidget(
      MaterialApp(home: Scaffold(body: FilledButton(onPressed: () {}, child: const Text('open')))),
    );


Review comment (suggestion):
Technically this FilledButton can use a `onPressed: () {}` and `Text('Button')` to make the test shorter.

Code after:
      await hoverOver(tester, find.text(TestMenu.subSubMenu110.label));
      await tester.pump();
      expect(find.byIcon(hoveredIcon), findsOneWidget);

      controller.close();
      await tester.pump();

      await tester.pumpWidget(buildMenu(icon: submenuIcon, enabled: false));
      await tester.tap(find.text(TestMenu.subMenu00.label));
      await tester.pump();
      expect(find.byIcon(disabledIcon), findsOneWidget);
    });
  });
}

List<Widget> createTestMenus({
  void Function(TestMenu)? onPressed,
  void Function(TestMenu)? onOpen,
  void Function(TestMenu)? onClose,
  Map<TestMenu, MenuSerializableShortcut> shortcuts = const <TestMenu, MenuSerializableShortcut>{},
  bool includeExtraGroups = false,
  bool accelerators = false,
}) {
  Widget submenuButton(TestMenu menu, {required List<Widget> menuChildren}) {
    return SubmenuButton(
      onOpen: onOpen != null ? () => onOpen(menu) : null,
      onClose: onClose != null ? () => onClose(menu) : null,
      menuChildren: menuChildren,
      child: accelerators ? MenuAcceleratorLabel(menu.acceleratorLabel) : Text(menu.label),
    );
  }

  Widget menuItemButton(
    TestMenu menu, {
    bool enabled = true,
    Widget? leadingIcon,
    Widget? trailingIcon,
    Key? key,
  }) {
    return MenuItemButton(
      key: key,
      onPressed: enabled && onPressed != null ? () => onPressed(menu) : null,
      shortcut: shortcuts[menu],
      leadingIcon: leadingIcon,
      trailingIcon: trailingIcon,
      child: accelerators ? MenuAcceleratorLabel(menu.acceleratorLabel) : Text(menu.label),
    );
  }

  final List<Widget> result = <Widget>[
    submenuButton(