Skip to content

Commit 0f33496

Browse files
committed
test: add more test cases to toolbar_service
1 parent 3eaa31c commit 0f33496

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

frontend/app_flowy/packages/appflowy_editor/test/service/toolbar_service_test.dart

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:appflowy_editor/appflowy_editor.dart';
2+
import 'package:appflowy_editor/src/render/rich_text/rich_text_style.dart';
23
import 'package:appflowy_editor/src/render/toolbar/toolbar_item.dart';
34
import 'package:appflowy_editor/src/render/toolbar/toolbar_item_widget.dart';
45
import 'package:appflowy_editor/src/render/toolbar/toolbar_widget.dart';
@@ -42,5 +43,125 @@ void main() async {
4243
true,
4344
);
4445
});
46+
47+
testWidgets(
48+
'Test toolbar service in single text selection with StyleKey.partialStyleKeys',
49+
(tester) async {
50+
final attributes = StyleKey.partialStyleKeys.fold<Attributes>({},
51+
(previousValue, element) {
52+
if (element == StyleKey.backgroundColor) {
53+
previousValue[element] = '0x6000BCF0';
54+
} else if (element == StyleKey.href) {
55+
previousValue[element] = 'appflowy.io';
56+
} else {
57+
previousValue[element] = true;
58+
}
59+
return previousValue;
60+
});
61+
62+
const text = 'Welcome to Appflowy 😁';
63+
final editor = tester.editor
64+
..insertTextNode(text)
65+
..insertTextNode(
66+
null,
67+
delta: Delta([
68+
TextInsert(text),
69+
TextInsert(text, attributes),
70+
TextInsert(text),
71+
]),
72+
);
73+
await editor.startTesting();
74+
await editor.updateSelection(
75+
Selection.single(path: [0], startOffset: 0, endOffset: text.length),
76+
);
77+
expect(find.byType(ToolbarWidget), findsOneWidget);
78+
79+
void testHighlight(bool expectedValue) {
80+
for (final styleKey in StyleKey.partialStyleKeys) {
81+
var key = styleKey;
82+
if (styleKey == StyleKey.backgroundColor) {
83+
key = 'highlight';
84+
} else if (styleKey == StyleKey.href) {
85+
key = 'link';
86+
}
87+
final itemWidget = _itemWidgetForId(tester, 'appflowy.toolbar.$key');
88+
expect(itemWidget.isHighlight, expectedValue);
89+
}
90+
}
91+
92+
await editor.updateSelection(
93+
Selection.single(path: [1], startOffset: 0, endOffset: text.length * 2),
94+
);
95+
testHighlight(false);
96+
97+
await editor.updateSelection(
98+
Selection.single(
99+
path: [1],
100+
startOffset: text.length,
101+
endOffset: text.length * 2,
102+
),
103+
);
104+
testHighlight(true);
105+
106+
await editor.updateSelection(
107+
Selection.single(
108+
path: [1],
109+
startOffset: text.length + 2,
110+
endOffset: text.length * 2 - 2,
111+
),
112+
);
113+
testHighlight(true);
114+
});
115+
116+
testWidgets(
117+
'Test toolbar service in single text selection with StyleKey.globalStyleKeys',
118+
(tester) async {
119+
const text = 'Welcome to Appflowy 😁';
120+
121+
final editor = tester.editor
122+
..insertTextNode(text, attributes: {
123+
StyleKey.subtype: StyleKey.heading,
124+
StyleKey.heading: StyleKey.h1,
125+
})
126+
..insertTextNode(
127+
text,
128+
attributes: {StyleKey.subtype: StyleKey.quote},
129+
)
130+
..insertTextNode(
131+
text,
132+
attributes: {StyleKey.subtype: StyleKey.bulletedList},
133+
);
134+
await editor.startTesting();
135+
136+
await editor.updateSelection(
137+
Selection.single(path: [0], startOffset: 0, endOffset: text.length),
138+
);
139+
expect(find.byType(ToolbarWidget), findsOneWidget);
140+
var itemWidget = _itemWidgetForId(tester, 'appflowy.toolbar.h1');
141+
expect(itemWidget.isHighlight, true);
142+
143+
await editor.updateSelection(
144+
Selection.single(path: [1], startOffset: 0, endOffset: text.length),
145+
);
146+
expect(find.byType(ToolbarWidget), findsOneWidget);
147+
itemWidget = _itemWidgetForId(tester, 'appflowy.toolbar.quote');
148+
expect(itemWidget.isHighlight, true);
149+
150+
await editor.updateSelection(
151+
Selection.single(path: [2], startOffset: 0, endOffset: text.length),
152+
);
153+
expect(find.byType(ToolbarWidget), findsOneWidget);
154+
itemWidget = _itemWidgetForId(tester, 'appflowy.toolbar.bulleted_list');
155+
expect(itemWidget.isHighlight, true);
156+
});
45157
});
46158
}
159+
160+
ToolbarItemWidget _itemWidgetForId(WidgetTester tester, String id) {
161+
final finder = find.byType(ToolbarItemWidget);
162+
final itemWidgets = tester
163+
.widgetList<ToolbarItemWidget>(finder)
164+
.where((element) => element.item.id == id);
165+
expect(itemWidgets.length, 1);
166+
return itemWidgets.first;
167+
}

0 commit comments

Comments
 (0)