test: add more test cases to toolbar_service

This commit is contained in:
Lucas.Xu 2022-08-30 16:25:32 +08:00
parent 3eaa31c68c
commit 0f334962ce

View File

@ -1,4 +1,5 @@
import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/render/rich_text/rich_text_style.dart';
import 'package:appflowy_editor/src/render/toolbar/toolbar_item.dart'; import 'package:appflowy_editor/src/render/toolbar/toolbar_item.dart';
import 'package:appflowy_editor/src/render/toolbar/toolbar_item_widget.dart'; import 'package:appflowy_editor/src/render/toolbar/toolbar_item_widget.dart';
import 'package:appflowy_editor/src/render/toolbar/toolbar_widget.dart'; import 'package:appflowy_editor/src/render/toolbar/toolbar_widget.dart';
@ -42,5 +43,125 @@ void main() async {
true, true,
); );
}); });
testWidgets(
'Test toolbar service in single text selection with StyleKey.partialStyleKeys',
(tester) async {
final attributes = StyleKey.partialStyleKeys.fold<Attributes>({},
(previousValue, element) {
if (element == StyleKey.backgroundColor) {
previousValue[element] = '0x6000BCF0';
} else if (element == StyleKey.href) {
previousValue[element] = 'appflowy.io';
} else {
previousValue[element] = true;
}
return previousValue;
});
const text = 'Welcome to Appflowy 😁';
final editor = tester.editor
..insertTextNode(text)
..insertTextNode(
null,
delta: Delta([
TextInsert(text),
TextInsert(text, attributes),
TextInsert(text),
]),
);
await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: 0, endOffset: text.length),
);
expect(find.byType(ToolbarWidget), findsOneWidget);
void testHighlight(bool expectedValue) {
for (final styleKey in StyleKey.partialStyleKeys) {
var key = styleKey;
if (styleKey == StyleKey.backgroundColor) {
key = 'highlight';
} else if (styleKey == StyleKey.href) {
key = 'link';
}
final itemWidget = _itemWidgetForId(tester, 'appflowy.toolbar.$key');
expect(itemWidget.isHighlight, expectedValue);
}
}
await editor.updateSelection(
Selection.single(path: [1], startOffset: 0, endOffset: text.length * 2),
);
testHighlight(false);
await editor.updateSelection(
Selection.single(
path: [1],
startOffset: text.length,
endOffset: text.length * 2,
),
);
testHighlight(true);
await editor.updateSelection(
Selection.single(
path: [1],
startOffset: text.length + 2,
endOffset: text.length * 2 - 2,
),
);
testHighlight(true);
});
testWidgets(
'Test toolbar service in single text selection with StyleKey.globalStyleKeys',
(tester) async {
const text = 'Welcome to Appflowy 😁';
final editor = tester.editor
..insertTextNode(text, attributes: {
StyleKey.subtype: StyleKey.heading,
StyleKey.heading: StyleKey.h1,
})
..insertTextNode(
text,
attributes: {StyleKey.subtype: StyleKey.quote},
)
..insertTextNode(
text,
attributes: {StyleKey.subtype: StyleKey.bulletedList},
);
await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: 0, endOffset: text.length),
);
expect(find.byType(ToolbarWidget), findsOneWidget);
var itemWidget = _itemWidgetForId(tester, 'appflowy.toolbar.h1');
expect(itemWidget.isHighlight, true);
await editor.updateSelection(
Selection.single(path: [1], startOffset: 0, endOffset: text.length),
);
expect(find.byType(ToolbarWidget), findsOneWidget);
itemWidget = _itemWidgetForId(tester, 'appflowy.toolbar.quote');
expect(itemWidget.isHighlight, true);
await editor.updateSelection(
Selection.single(path: [2], startOffset: 0, endOffset: text.length),
);
expect(find.byType(ToolbarWidget), findsOneWidget);
itemWidget = _itemWidgetForId(tester, 'appflowy.toolbar.bulleted_list');
expect(itemWidget.isHighlight, true);
});
}); });
} }
ToolbarItemWidget _itemWidgetForId(WidgetTester tester, String id) {
final finder = find.byType(ToolbarItemWidget);
final itemWidgets = tester
.widgetList<ToolbarItemWidget>(finder)
.where((element) => element.item.id == id);
expect(itemWidgets.length, 1);
return itemWidgets.first;
}