Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick "[SuperTextField][mobile] Fix exception when updateTextAndSelection (Resolves #1229) (#1234)" to stable #1237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,11 @@ class TextScrollController with ChangeNotifier {
return;
}

if (_textController.text.text.isEmpty) {
// There is no text to make visible.
return;
}

final baseCharacterRect = _delegate!.getCharacterRectAtPosition(_textController.selection.base);
_ensureRectIsVisible(baseCharacterRect);
}
Expand All @@ -987,6 +992,11 @@ class TextScrollController with ChangeNotifier {
return;
}

if (_textController.text.text.isEmpty) {
// There is no text to make visible.
return;
}

final characterIndex = _textController.selection.extentOffset >= _textController.text.text.length
? _textController.text.text.length - 1
: _textController.selection.extentOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import 'package:super_editor/src/infrastructure/super_textfield/metrics.dart';
import 'package:super_editor/super_editor.dart';

import '../test_tools.dart';
import 'super_textfield_robot.dart';

const screenSizeWithoutKeyboard = Size(400, 800);
const screenSizeWithKeyboard = Size(400, 300);

void main() {
group('SuperTextField on mobile', () {
group('with an ancestor Scrollable', () {
group('SuperTextField', () {
group('on mobile with an ancestor Scrollable', () {
_testWidgetsOnMobileWithKeyboard('auto scrolls when focused in single-line', (tester, keyboardToggle) async {
await _pumpTestApp(
tester,
Expand Down Expand Up @@ -116,6 +117,37 @@ void main() {
expect(finalTopLeft, initialTopLeft);
});
});

testWidgetsOnAllPlatforms('auto scroll doesn\'t crash when text is empty', (tester) async {
final controller = AttributedTextEditingController(
text: AttributedText(text: 'Text before'),
);

await _pumpScaffold(
tester,
SuperTextField(
textController: controller,
),
);

// Place caret at the end of the text field.
await tester.placeCaretInSuperTextField(11);

// Clear the text and changes the selection to the beginning of the text.
controller.updateTextAndSelection(
text: AttributedText(),
selection: const TextSelection.collapsed(offset: 0),
);
await tester.pump();

/// When text or selection changes, we auto scroll to ensure that the selecion is visible.
/// To do so, we need to get the bounds of the character at the selection extent.
///
/// If we attempt to auto scroll when the text is empty, a crash happens because there isn't a
/// character at the selection extent.
///
/// Reaching this point means we didn't attempt to auto scroll after clearing the text.
});
});
}

Expand Down Expand Up @@ -149,6 +181,22 @@ Future<void> _pumpTestApp(
await tester.pump();
}

/// Pumps a scaffold with a centered [child].
Future<void> _pumpScaffold(WidgetTester tester, Widget child) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: SizedBox(
width: 300,
child: child,
),
),
),
),
);
}

@isTestGroup
void _testWidgetsOnMobileWithKeyboard(
String description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_test_robots/flutter_test_robots.dart';
import 'package:super_editor/src/test/super_editor_test/supereditor_robot.dart';
import 'package:super_editor/super_editor.dart';

import '../test_tools.dart';
import 'super_textfield_inspector.dart';
import 'super_textfield_robot.dart';

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified super_editor/test_goldens/editor/goldens/text-scaling-header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified super_editor/test_goldens/editor/goldens/text-scaling-paragraph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified super_text_layout/test_goldens/goldens/CaretLayer_multi-caret.png
Binary file modified super_text_layout/test_goldens/goldens/SuperText_layers_caret.png