Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Dec 4, 2023
1 parent 9863a64 commit 735a6a3
Showing 1 changed file with 102 additions and 22 deletions.
124 changes: 102 additions & 22 deletions packages/fleather/test/widgets/editor_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:fleather/fleather.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -31,28 +32,6 @@ void main() {
expect(text.children!.first.style!.color, Colors.red);
});

testWidgets('Hides toolbar and selection handles when text changed',
(tester) async {
const delta = TextEditingDeltaInsertion(
oldText: 'Add ',
textInserted: 'Test',
insertionOffset: 0,
selection: TextSelection.collapsed(offset: 0),
composing: TextRange.empty,
);
final editor = EditorSandBox(tester: tester);
await editor.pump();
await tester.longPressAt(const Offset(20, 20));
await tester.pump();
expect(editor.findSelectionHandles(), findsNWidgets(2));
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
final state = tester.state(find.byType(RawEditor)) as RawEditorState;
state.updateEditingValueWithDeltas([delta]);
await tester.pump(throttleDuration);
expect(editor.findSelectionHandles(), findsNothing);
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
});

testWidgets('collapses selection when unfocused', (tester) async {
final editor = EditorSandBox(tester: tester, autofocus: true);
await editor.pumpAndTap();
Expand Down Expand Up @@ -121,6 +100,42 @@ void main() {
// Fails if thrown
});

group('Context menu', () {
testWidgets('Hides toolbar and selection handles when text changed',
(tester) async {
const delta = TextEditingDeltaInsertion(
oldText: 'Add ',
textInserted: 'Test',
insertionOffset: 0,
selection: TextSelection.collapsed(offset: 0),
composing: TextRange.empty,
);
final editor = EditorSandBox(tester: tester);
await editor.pump();
await tester.longPressAt(const Offset(20, 20));
await tester.pump();
expect(editor.findSelectionHandles(), findsNWidgets(2));
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
final state = tester.state(find.byType(RawEditor)) as RawEditorState;
state.updateEditingValueWithDeltas([delta]);
await tester.pump(throttleDuration);
expect(editor.findSelectionHandles(), findsNothing);
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
});

testWidgets('Secondary tap opens context menu', (tester) async {
final document = ParchmentDocument.fromJson([
{'insert': 'Test\n'}
]);
final editor = EditorSandBox(tester: tester, document: document);
await editor.pump();
await tester.tapAt(tester.getCenter(find.byType(FleatherEditor)),
buttons: kSecondaryMouseButton);
await tester.pump();
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
});
});

group('Text selection', () {
testWidgets('Can select last separated character in paragraph on iOS',
(tester) async {
Expand Down Expand Up @@ -261,6 +276,71 @@ void main() {
const TypeMatcher<DesktopTextSelectionControls>());
debugDefaultTargetPlatformOverride = null;
});

testWidgets('Triple tap selects paragraph on platforms other than Linux',
(tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
const text =
'This is a relatively long paragraph with multiple lines that'
' we are going to triple tap on it in order to select it.';
final document = ParchmentDocument.fromJson([
{'insert': '$text\n'},
{'insert': 'Some other text in another paragraph\n'},
]);
final editor = EditorSandBox(
tester: tester,
document: document,
autofocus: true,
theme: ThemeData(platform: TargetPlatform.iOS),
);
await editor.pump();
await tester.tapAt(tester.getTopLeft(find.byType(FleatherEditor)) +
const Offset(1, 1));
await tester.tapAt(tester.getTopLeft(find.byType(FleatherEditor)) +
const Offset(1, 1));
await tester.tapAt(tester.getTopLeft(find.byType(FleatherEditor)) +
const Offset(1, 1));
await tester.pump();
expect(
editor.selection,
const TextSelection(
baseOffset: 0,
extentOffset: 117,
affinity: TextAffinity.upstream));
debugDefaultTargetPlatformOverride = null;
});

testWidgets('Triple tap selects a line on Linux', (tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.linux;
const text =
'This is a relatively long paragraph with multiple lines that'
' we are going to triple tap on it in order to select it.';
final document = ParchmentDocument.fromJson([
{'insert': '$text\n'},
{'insert': 'Some other text in another paragraph\n'},
]);
final editor = EditorSandBox(
tester: tester,
document: document,
autofocus: true,
theme: ThemeData(platform: TargetPlatform.iOS),
);
await editor.pump();
await tester.tapAt(tester.getTopLeft(find.byType(FleatherEditor)) +
const Offset(1, 1));
await tester.tapAt(tester.getTopLeft(find.byType(FleatherEditor)) +
const Offset(1, 1));
await tester.tapAt(tester.getTopLeft(find.byType(FleatherEditor)) +
const Offset(1, 1));
await tester.pump();
expect(
editor.selection,
const TextSelection(
baseOffset: 0,
extentOffset: 50,
affinity: TextAffinity.upstream));
debugDefaultTargetPlatformOverride = null;
});
});

group('didUpdateWidget', () {
Expand Down

0 comments on commit 735a6a3

Please sign in to comment.