flutter_flutter/packages/flutter/test/widgets/context_menu_controller_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

273 lines
7.7 KiB
Dart

// 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/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'clipboard_utils.dart';
import 'editable_text_utils.dart';
void main() {
final mockClipboard = MockClipboard();
TestWidgetsFlutterBinding.ensureInitialized().defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
mockClipboard.handleMethodCall,
);
setUp(() async {
// Fill the clipboard so that the Paste option is available in the text
// selection menu.
await Clipboard.setData(const ClipboardData(text: 'Clipboard data'));
});
testWidgets('Hides and shows only a single menu', (WidgetTester tester) async {
final GlobalKey key1 = GlobalKey();
final GlobalKey key2 = GlobalKey();
late final BuildContext context;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (BuildContext localContext) {
context = localContext;
return const SizedBox.shrink();
},
),
),
),
);
expect(find.byKey(key1), findsNothing);
expect(find.byKey(key2), findsNothing);
final controller1 = ContextMenuController();
await tester.pump();
expect(find.byKey(key1), findsNothing);
expect(find.byKey(key2), findsNothing);
controller1.show(
context: context,
contextMenuBuilder: (BuildContext context) {
return Placeholder(key: key1);
},
);
await tester.pump();
expect(find.byKey(key1), findsOneWidget);
expect(find.byKey(key2), findsNothing);
// Showing the same thing again does nothing and is not an error.
controller1.show(
context: context,
contextMenuBuilder: (BuildContext context) {
return Placeholder(key: key1);
},
);
await tester.pump();
expect(tester.takeException(), null);
expect(find.byKey(key1), findsOneWidget);
expect(find.byKey(key2), findsNothing);
// Showing a new menu hides the first.
final controller2 = ContextMenuController();
controller2.show(
context: context,
contextMenuBuilder: (BuildContext context) {
return Placeholder(key: key2);
},
);
await tester.pump();
expect(find.byKey(key1), findsNothing);
expect(find.byKey(key2), findsOneWidget);
controller2.remove();
await tester.pump();
expect(find.byKey(key1), findsNothing);
expect(find.byKey(key2), findsNothing);
});
testWidgets('A menu can be hidden and then reshown', (WidgetTester tester) async {
final GlobalKey key1 = GlobalKey();
late final BuildContext context;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (BuildContext localContext) {
context = localContext;
return const SizedBox.shrink();
},
),
),
),
);
expect(find.byKey(key1), findsNothing);
final controller = ContextMenuController();
addTearDown(controller.remove);
// Instantiating the controller does not shown it.
await tester.pump();
expect(find.byKey(key1), findsNothing);
controller.show(
context: context,
contextMenuBuilder: (BuildContext context) {
return Placeholder(key: key1);
},
);
await tester.pump();
expect(find.byKey(key1), findsOneWidget);
controller.remove();
await tester.pump();
expect(find.byKey(key1), findsNothing);
controller.show(
context: context,
contextMenuBuilder: (BuildContext context) {
return Placeholder(key: key1);
},
);
await tester.pump();
expect(find.byKey(key1), findsOneWidget);
});
testWidgets('markNeedsBuild causes the builder to update', (WidgetTester tester) async {
var buildCount = 0;
late final BuildContext context;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (BuildContext localContext) {
context = localContext;
return const SizedBox.shrink();
},
),
),
),
);
final controller = ContextMenuController();
controller.show(
context: context,
contextMenuBuilder: (BuildContext context) {
buildCount++;
return const Placeholder();
},
);
expect(buildCount, 0);
await tester.pump();
expect(buildCount, 1);
controller.markNeedsBuild();
expect(buildCount, 1);
await tester.pump();
expect(buildCount, 2);
controller.remove();
});
testWidgets(
'Calling show when a built-in widget is already showing its context menu hides the built-in menu',
(WidgetTester tester) async {
final GlobalKey builtInKey = GlobalKey();
final GlobalKey directKey = GlobalKey();
late final BuildContext context;
final textEditingController = TextEditingController();
addTearDown(textEditingController.dispose);
final focusNode = FocusNode();
addTearDown(focusNode.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (BuildContext localContext) {
context = localContext;
return EditableText(
controller: textEditingController,
backgroundCursorColor: Colors.grey,
focusNode: focusNode,
style: const TextStyle(),
cursorColor: Colors.red,
selectionControls: materialTextSelectionHandleControls,
contextMenuBuilder: (BuildContext context, EditableTextState editableTextState) {
return Placeholder(key: builtInKey);
},
);
},
),
),
),
);
expect(find.byKey(builtInKey), findsNothing);
expect(find.byKey(directKey), findsNothing);
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
await tester.tapAt(textOffsetToPosition(tester, 0));
await tester.pump();
expect(state.showToolbar(), true);
await tester.pump();
expect(find.byKey(builtInKey), findsOneWidget);
expect(find.byKey(directKey), findsNothing);
final controller = ContextMenuController();
controller.show(
context: context,
contextMenuBuilder: (BuildContext context) {
return Placeholder(key: directKey);
},
);
await tester.pump();
expect(find.byKey(builtInKey), findsNothing);
expect(find.byKey(directKey), findsOneWidget);
expect(controller.isShown, isTrue);
// And showing the built-in menu hides the directly shown menu.
expect(state.showToolbar(), isTrue);
await tester.pump();
expect(find.byKey(builtInKey), findsOneWidget);
expect(find.byKey(directKey), findsNothing);
expect(controller.isShown, isFalse);
// Calling remove on the hidden ContextMenuController does not hide the
// built-in menu.
controller.remove();
await tester.pump();
expect(find.byKey(builtInKey), findsOneWidget);
expect(find.byKey(directKey), findsNothing);
expect(controller.isShown, isFalse);
state.hideToolbar();
await tester.pump();
expect(find.byKey(builtInKey), findsNothing);
expect(find.byKey(directKey), findsNothing);
expect(controller.isShown, isFalse);
},
// [intended] no Flutter-drawn text selection toolbar on web.
skip: isContextMenuProvidedByPlatform,
);
}