test: migrate page up and down

This commit is contained in:
Lucas.Xu 2023-05-02 23:08:38 +08:00
commit fd1a1b1551
4 changed files with 18 additions and 22 deletions

View file

@ -158,6 +158,10 @@ class SimpleEditor extends StatelessWidget {
outdentCommand, outdentCommand,
exitEditingCommand, exitEditingCommand,
//
pageUpCommand,
pageDownCommand,
], ],
); );
} }

View file

@ -25,7 +25,7 @@ CommandShortcutEventHandler _pageUpCommandHandler = (editorState) {
final scrollHeight = scrollService.onePageHeight; final scrollHeight = scrollService.onePageHeight;
final dy = scrollService.dy; final dy = scrollService.dy;
if (dy <= 0 || scrollHeight == null) { if (dy < 0 || scrollHeight == null) {
return KeyEventResult.ignored; return KeyEventResult.ignored;
} }
scrollService.scrollTo( scrollService.scrollTo(

View file

@ -109,6 +109,10 @@ class TestableEditor {
outdentCommand, outdentCommand,
exitEditingCommand, exitEditingCommand,
//
pageUpCommand,
pageDownCommand,
], ],
); );
await tester.pumpWidget( await tester.pumpWidget(

View file

@ -1,7 +1,7 @@
import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import '../../infra/test_editor.dart'; import '../../new/infra/testable_editor.dart';
void main() async { void main() async {
setUpAll(() { setUpAll(() {
@ -12,38 +12,26 @@ void main() async {
testWidgets('Presses PageUp and pageDown key in large document', testWidgets('Presses PageUp and pageDown key in large document',
(tester) async { (tester) async {
const text = 'Welcome to Appflowy 😁'; const text = 'Welcome to Appflowy 😁';
final editor = tester.editor; final editor = tester.editor..addParagraphs(1000, initialText: text);
for (var i = 0; i < 1000; i++) {
editor.insertTextNode(text);
}
await editor.startTesting(); await editor.startTesting();
await editor.updateSelection( await editor.updateSelection(
Selection.single(path: [0], startOffset: 0), Selection.single(path: [0], startOffset: 0),
); );
final scrollService = editor.editorState.service.scrollService; final scrollService = editor.editorState.service.scrollService!;
final page = scrollService.page!;
expect(scrollService != null, true); final onePageHeight = scrollService.onePageHeight!;
if (scrollService == null) {
return;
}
final page = scrollService.page;
final onePageHeight = scrollService.onePageHeight;
expect(page != null, true);
expect(onePageHeight != null, true);
// Pressing the pageDown key continuously. // Pressing the pageDown key continuously.
var currentOffsetY = 0.0; var currentOffsetY = 0.0;
for (int i = 1; i <= page!; i++) { for (int i = 1; i <= page; i++) {
await editor.pressLogicKey( await editor.pressLogicKey(
key: LogicalKeyboardKey.pageDown, key: LogicalKeyboardKey.pageDown,
); );
if (i == page) { if (i == page) {
currentOffsetY = scrollService.maxScrollExtent; currentOffsetY = scrollService.maxScrollExtent;
} else { } else {
currentOffsetY += onePageHeight!; currentOffsetY += onePageHeight;
} }
final dy = scrollService.dy; final dy = scrollService.dy;
expect(dy, currentOffsetY); expect(dy, currentOffsetY);
@ -65,9 +53,9 @@ void main() async {
if (i == 1) { if (i == 1) {
currentOffsetY = scrollService.minScrollExtent; currentOffsetY = scrollService.minScrollExtent;
} else { } else {
currentOffsetY -= onePageHeight!; currentOffsetY -= onePageHeight;
} }
final dy = editor.editorState.service.scrollService?.dy; final dy = scrollService.dy;
expect(dy, currentOffsetY); expect(dy, currentOffsetY);
} }