mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
test: implement pageup/down key test for large document
This commit is contained in:
parent
c66e1e4df8
commit
f5cc886b6e
@ -2,25 +2,16 @@ import 'package:flowy_editor/flowy_editor.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
double? getEditorHeight(EditorState editorState) {
|
||||
final renderObj =
|
||||
editorState.service.scrollServiceKey.currentContext?.findRenderObject();
|
||||
if (renderObj is RenderBox) {
|
||||
return renderObj.size.height;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
FlowyKeyEventHandler pageUpDownHandler = (editorState, event) {
|
||||
if (event.logicalKey == LogicalKeyboardKey.pageUp) {
|
||||
final scrollHeight = getEditorHeight(editorState);
|
||||
final scrollHeight = editorState.service.scrollService?.onePageHeight;
|
||||
final scrollService = editorState.service.scrollService;
|
||||
if (scrollHeight != null && scrollService != null) {
|
||||
scrollService.scrollTo(scrollService.dy - scrollHeight);
|
||||
}
|
||||
return KeyEventResult.handled;
|
||||
} else if (event.logicalKey == LogicalKeyboardKey.pageDown) {
|
||||
final scrollHeight = getEditorHeight(editorState);
|
||||
final scrollHeight = editorState.service.scrollService?.onePageHeight;
|
||||
final scrollService = editorState.service.scrollService;
|
||||
if (scrollHeight != null && scrollService != null) {
|
||||
scrollService.scrollTo(scrollService.dy + scrollHeight);
|
||||
|
@ -1,8 +1,15 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flowy_editor/src/extensions/object_extensions.dart';
|
||||
|
||||
abstract class FlowyScrollService {
|
||||
double get dy;
|
||||
double? get onePageHeight;
|
||||
|
||||
int? get page;
|
||||
|
||||
double get maxScrollExtent;
|
||||
double get minScrollExtent;
|
||||
|
||||
void scrollTo(double dy);
|
||||
|
||||
@ -32,6 +39,27 @@ class _FlowyScrollState extends State<FlowyScroll>
|
||||
@override
|
||||
double get dy => _scrollController.position.pixels;
|
||||
|
||||
@override
|
||||
double? get onePageHeight {
|
||||
final renderBox = context.findRenderObject()?.unwrapOrNull<RenderBox>();
|
||||
return renderBox?.size.height;
|
||||
}
|
||||
|
||||
@override
|
||||
double get maxScrollExtent => _scrollController.position.maxScrollExtent;
|
||||
|
||||
@override
|
||||
double get minScrollExtent => _scrollController.position.minScrollExtent;
|
||||
|
||||
@override
|
||||
int? get page {
|
||||
if (onePageHeight != null) {
|
||||
final scrollExtent = maxScrollExtent - minScrollExtent;
|
||||
return (scrollExtent / onePageHeight!).ceil();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Listener(
|
||||
|
@ -61,6 +61,12 @@ extension on LogicalKeyboardKey {
|
||||
if (this == LogicalKeyboardKey.delete) {
|
||||
return PhysicalKeyboardKey.delete;
|
||||
}
|
||||
if (this == LogicalKeyboardKey.pageDown) {
|
||||
return PhysicalKeyboardKey.pageDown;
|
||||
}
|
||||
if (this == LogicalKeyboardKey.pageUp) {
|
||||
return PhysicalKeyboardKey.pageUp;
|
||||
}
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,75 @@
|
||||
import 'package:flowy_editor/flowy_editor.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import '../../infra/test_editor.dart';
|
||||
|
||||
void main() async {
|
||||
setUpAll(() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
});
|
||||
|
||||
group('page_up_down_handler_test.dart', () {
|
||||
testWidgets('Presses PageUp and pageDown key in large document',
|
||||
(tester) async {
|
||||
const text = 'Welcome to Appflowy 😁';
|
||||
final editor = tester.editor;
|
||||
for (var i = 0; i < 1000; i++) {
|
||||
editor.insertTextNode(text);
|
||||
}
|
||||
await editor.startTesting();
|
||||
await editor.updateSelection(
|
||||
Selection.single(path: [0], startOffset: 0),
|
||||
);
|
||||
|
||||
final scrollService = editor.editorState.service.scrollService;
|
||||
|
||||
expect(scrollService != null, true);
|
||||
|
||||
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.
|
||||
var currentOffsetY = 0.0;
|
||||
for (int i = 1; i <= page!; i++) {
|
||||
await editor.pressLogicKey(
|
||||
LogicalKeyboardKey.pageDown,
|
||||
);
|
||||
currentOffsetY += onePageHeight!;
|
||||
final dy = scrollService.dy;
|
||||
expect(dy, currentOffsetY);
|
||||
}
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
await editor.pressLogicKey(
|
||||
LogicalKeyboardKey.pageDown,
|
||||
);
|
||||
final dy = scrollService.dy;
|
||||
expect(dy == scrollService.maxScrollExtent, true);
|
||||
}
|
||||
|
||||
// Pressing the pageUp key continuously.
|
||||
for (int i = page; i >= 1; i--) {
|
||||
await editor.pressLogicKey(
|
||||
LogicalKeyboardKey.pageUp,
|
||||
);
|
||||
currentOffsetY -= onePageHeight!;
|
||||
final dy = editor.editorState.service.scrollService?.dy;
|
||||
expect(dy, currentOffsetY);
|
||||
}
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
await editor.pressLogicKey(
|
||||
LogicalKeyboardKey.pageUp,
|
||||
);
|
||||
final dy = scrollService.dy;
|
||||
expect(dy == scrollService.minScrollExtent, true);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user