-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixed unresponsive behaviour of context menu items (#349)
* fix: fixed unresponsive behaviour of context menu items * test: added test for context menu
- Loading branch information
1 parent
b26a2ea
commit c3f62b3
Showing
2 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
14 changes: 11 additions & 3 deletions
14
lib/src/service/context_menu/built_in_context_menu_item.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
import 'package:appflowy_editor/src/service/context_menu/context_menu.dart'; | ||
|
||
import '../internal_key_event_handlers/copy_paste_handler.dart'; | ||
|
||
final builtInContextMenuItems = [ | ||
[ | ||
// cut | ||
ContextMenuItem( | ||
name: 'Cut', | ||
onPressed: (editorState) {}, | ||
onPressed: (editorState) { | ||
handleCut(editorState); | ||
}, | ||
), | ||
// copy | ||
ContextMenuItem( | ||
name: 'Copy', | ||
onPressed: (editorState) {}, | ||
onPressed: (editorState) { | ||
handleCopy(editorState); | ||
}, | ||
), | ||
// Paste | ||
ContextMenuItem( | ||
name: 'Paste', | ||
onPressed: (editorState) {}, | ||
onPressed: (editorState) { | ||
handlePaste(editorState); | ||
}, | ||
), | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import 'package:appflowy_editor/src/service/context_menu/context_menu.dart'; | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
|
||
import '../infra/clipboard_test.dart'; | ||
import '../new/infra/testable_editor.dart'; | ||
|
||
void main() async { | ||
late MockClipboard mockClipboard; | ||
setUpAll(() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
mockClipboard = const MockClipboard(html: null, text: null); | ||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger | ||
.setMockMethodCallHandler(SystemChannels.platform, (message) async { | ||
switch (message.method) { | ||
case "Clipboard.getData": | ||
return mockClipboard.getData; | ||
case "Clipboard.setData": | ||
final args = message.arguments as Map<String, dynamic>; | ||
mockClipboard = mockClipboard.copyWith( | ||
text: args['text'], | ||
); | ||
} | ||
return null; | ||
}); | ||
}); | ||
group('context menu test', () { | ||
void rightClick() { | ||
GestureBinding.instance.handlePointerEvent( | ||
const PointerDownEvent( | ||
buttons: kSecondaryMouseButton, | ||
), | ||
); | ||
|
||
GestureBinding.instance.handlePointerEvent( | ||
const PointerUpEvent(), | ||
); | ||
} | ||
|
||
testWidgets('context menu test', (tester) async { | ||
final editor = tester.editor | ||
..addParagraph(initialText: 'Welcome to AppFlowy'); | ||
await editor.startTesting(); | ||
expect(find.byType(ContextMenu), findsNothing); | ||
rightClick(); | ||
await tester.pump(); | ||
expect(find.byType(ContextMenu), findsOneWidget); | ||
await editor.dispose(); | ||
}); | ||
|
||
testWidgets('context menu cut test ', (tester) async { | ||
final editor = tester.editor | ||
..addParagraph(initialText: 'Welcome to AppFlowy'); | ||
await editor.startTesting(); | ||
expect( | ||
find.text('Welcome to AppFlowy', findRichText: true), | ||
findsOneWidget, | ||
); | ||
await editor.updateSelection( | ||
Selection( | ||
start: Position(path: [0], offset: 0), | ||
end: Position(path: [0], offset: 18), | ||
), | ||
); | ||
final text = | ||
editor.editorState.getTextInSelection(editor.selection).join('/n'); | ||
rightClick(); | ||
await tester.pump(); | ||
final cutButton = find.text('Cut'); | ||
expect(cutButton, findsOneWidget); | ||
await tester.tap(cutButton); | ||
await tester.pumpAndSettle(const Duration(milliseconds: 500)); | ||
expect( | ||
find.text('Welcome to AppFlowy', findRichText: true), | ||
findsNothing, | ||
); | ||
final clipBoardData = await AppFlowyClipboard.getData(); | ||
expect(clipBoardData.text, text); | ||
await editor.dispose(); | ||
}); | ||
|
||
testWidgets('context menu copy and paste test', (tester) async { | ||
final editor = tester.editor | ||
..addParagraph(initialText: 'Welcome to AppFlowy'); | ||
editor.addParagraph(initialText: 'Hello'); | ||
await editor.startTesting(); | ||
expect( | ||
find.text('Welcome to AppFlowy', findRichText: true), | ||
findsOneWidget, | ||
); | ||
await editor.updateSelection( | ||
Selection( | ||
start: Position(path: [1], offset: 0), | ||
end: Position(path: [1], offset: 5), | ||
), | ||
); | ||
final text = | ||
editor.editorState.getTextInSelection(editor.selection).join('/n'); | ||
rightClick(); | ||
await tester.pump(); | ||
final copyButton = find.text('Copy'); | ||
expect(copyButton, findsOneWidget); | ||
await tester.tap(copyButton); | ||
await tester.pumpAndSettle(const Duration(milliseconds: 500)); | ||
expect( | ||
find.text('Welcome to AppFlowy', findRichText: true), | ||
findsOneWidget, | ||
); | ||
final clipBoardData = await AppFlowyClipboard.getData(); | ||
expect(clipBoardData.text, text); | ||
await editor.updateSelection( | ||
Selection( | ||
start: Position(path: [0], offset: 0), | ||
end: Position(path: [0], offset: 7), | ||
), | ||
); | ||
rightClick(); | ||
await tester.pump(); | ||
final pasteButton = find.text('Paste'); | ||
expect(pasteButton, findsOneWidget); | ||
await tester.tap(pasteButton); | ||
await tester.pumpAndSettle(const Duration(milliseconds: 500)); | ||
expect( | ||
find.text('Hello to AppFlowy', findRichText: true), | ||
findsOneWidget, | ||
); | ||
await editor.dispose(); | ||
}); | ||
}); | ||
} |