Skip to content

Commit e20eee2

Browse files
[SuperReader][Android] - Fix crash when rotating the phone with an expanded selection (Resolves #2580) (#2586)
1 parent 068aeb0 commit e20eee2

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

super_editor/lib/src/super_reader/read_only_document_android_touch_interactor.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:math';
33

44
import 'package:flutter/gestures.dart';
55
import 'package:flutter/material.dart';
6+
import 'package:flutter/rendering.dart';
67
import 'package:flutter/services.dart';
78
import 'package:follow_the_leader/follow_the_leader.dart';
89
import 'package:super_editor/src/core/document.dart';
@@ -26,6 +27,7 @@ import 'package:super_editor/src/infrastructure/platforms/android/magnifier.dart
2627
import 'package:super_editor/src/infrastructure/platforms/android/selection_handles.dart';
2728
import 'package:super_editor/src/infrastructure/platforms/mobile_documents.dart';
2829
import 'package:super_editor/src/infrastructure/platforms/platform.dart';
30+
import 'package:super_editor/src/infrastructure/render_sliver_ext.dart';
2931
import 'package:super_editor/src/infrastructure/signal_notifier.dart';
3032
import 'package:super_editor/src/infrastructure/sliver_hybrid_stack.dart';
3133
import 'package:super_editor/src/infrastructure/toolbar_position_delegate.dart';
@@ -322,7 +324,7 @@ class _ReadOnlyAndroidDocumentTouchInteractorState extends State<ReadOnlyAndroid
322324
}
323325

324326
// Determines the offset of the editor in the viewport coordinate
325-
final editorBox = widget.documentKey.currentContext!.findRenderObject() as RenderBox;
327+
final editorBox = widget.documentKey.currentContext!.findRenderObject() as RenderSliver;
326328
final editorInViewportOffset = viewportBox.localToGlobal(Offset.zero) - editorBox.localToGlobal(Offset.zero);
327329

328330
// Determines the offset of the handle in the viewport coordinate
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import 'dart:ui';
2+
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:flutter_test_runners/flutter_test_runners.dart';
5+
import 'package:super_editor/super_editor_test.dart';
6+
7+
import 'reader_test_tools.dart';
8+
9+
void main() {
10+
group('SuperReader > phone rotation >', () {
11+
const screenSizePortrait = Size(400.0, 1000.0);
12+
const screenSizeLandscape = Size(1000.0, 400);
13+
14+
testWidgetsOnMobile('does not crash the app when there is no selection', (tester) async {
15+
// Start the test in portrait mode.
16+
tester.view
17+
..devicePixelRatio = 1.0
18+
..platformDispatcher.textScaleFactorTestValue = 1.0
19+
..physicalSize = screenSizePortrait;
20+
21+
addTearDown(() => tester.platformDispatcher.clearAllTestValues());
22+
23+
await tester //
24+
.createDocument()
25+
.withSingleParagraph()
26+
.pump();
27+
28+
// Simulate a phone rotation.
29+
tester.view.physicalSize = screenSizeLandscape;
30+
await tester.pumpAndSettle();
31+
32+
// Reaching this point means the reader didn't crash.
33+
});
34+
35+
testWidgetsOnMobile('does not crash the app when the selection is collapsed', (tester) async {
36+
// Start the test in portrait mode.
37+
tester.view
38+
..devicePixelRatio = 1.0
39+
..platformDispatcher.textScaleFactorTestValue = 1.0
40+
..physicalSize = screenSizePortrait;
41+
addTearDown(() => tester.platformDispatcher.clearAllTestValues());
42+
43+
await tester //
44+
.createDocument()
45+
.withSingleParagraph()
46+
.pump();
47+
48+
// Place the caret at the beginning of the document.
49+
await tester.placeCaretInParagraph('1', 0);
50+
51+
// Simulate a phone rotation.
52+
tester.view.physicalSize = screenSizeLandscape;
53+
await tester.pumpAndSettle();
54+
55+
// Reaching this point means the reader didn't crash.
56+
});
57+
58+
testWidgetsOnMobile('does not crash the app when the selection is expanded', (tester) async {
59+
// Start the test in portrait mode.
60+
tester.view
61+
..devicePixelRatio = 1.0
62+
..platformDispatcher.textScaleFactorTestValue = 1.0
63+
..physicalSize = screenSizePortrait;
64+
addTearDown(() => tester.platformDispatcher.clearAllTestValues());
65+
66+
await tester //
67+
.createDocument()
68+
.withSingleParagraph()
69+
.pump();
70+
71+
// Double tap to select the first word.
72+
await tester.doubleTapInParagraph('1', 0);
73+
74+
// Simulate a phone rotation.
75+
tester.view.physicalSize = screenSizeLandscape;
76+
await tester.pumpAndSettle();
77+
78+
// Reaching this point means the reader didn't crash.
79+
});
80+
});
81+
}

0 commit comments

Comments
 (0)