-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathTest_RichSuggestBox.cs
More file actions
365 lines (299 loc) · 16.6 KB
/
Test_RichSuggestBox.cs
File metadata and controls
365 lines (299 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using CommunityToolkit.Tests;
using CommunityToolkit.WinUI.Controls;
#if WINAPPSDK
using Microsoft.UI.Text;
#else
using Windows.UI.Text;
#endif
namespace RichSuggestBoxTests;
[TestClass]
public class Test_RichSuggestBox : VisualUITestBase
{
[TestCategory(nameof(RichSuggestBox))]
[TestMethod]
[DataRow("@Token1", "@Token2", "@Token3")]
[DataRow("@Token1", "@Token2", "#Token3")]
[DataRow("#Token1", "@Token2", "@Token3")]
public async Task Test_RichSuggestBox_AddTokens(string tokenText1, string tokenText2, string tokenText3)
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var rsb = new RichSuggestBox() { Prefixes = "@#" };
await LoadTestContentAsync(rsb);
var document = rsb.TextDocument;
// Adding token 1
await TestAddTokenAsync(rsb, tokenText1);
Assert.AreEqual(1, rsb.Tokens.Count, "Token count is not 1 after committing 1 token.");
var token1 = rsb.Tokens.Last();
AssertToken(rsb, token1);
var expectedStory = $"{token1} \r";
document!.GetText(TextGetOptions.None, out var actualStory);
Assert.AreEqual(expectedStory, actualStory);
// Adding token 2 with space between previous token
await TestAddTokenAsync(rsb, tokenText2);
Assert.AreEqual(2, rsb.Tokens.Count, "Token count is not 2 after committing 2 token.");
var token2 = rsb.Tokens.Last();
AssertToken(rsb, token2);
expectedStory = $"{token1} {token2} \r";
document.GetText(TextGetOptions.None, out actualStory);
Assert.AreEqual(expectedStory, actualStory);
// Adding token 3 without space between previous token
rsb.TextDocument!.Selection.Delete(TextRangeUnit.Character, -1);
await TestAddTokenAsync(rsb, tokenText3);
Assert.AreEqual(3, rsb.Tokens.Count, "Token count is not 3 after committing 3 token.");
var token3 = rsb.Tokens.Last();
AssertToken(rsb, token3);
expectedStory = $"{token1} {token2}{token3} \r";
document.GetText(TextGetOptions.None, out actualStory);
Assert.AreEqual(expectedStory, actualStory);
document.Selection.Delete(TextRangeUnit.Character, -1);
Assert.AreEqual(3, rsb.Tokens.Count, "Token at the end of the document is not recognized.");
});
}
[TestCategory(nameof(RichSuggestBox))]
[TestMethod]
public async Task Test_RichSuggestBox_CustomizeToken()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var rsb = new RichSuggestBox() { Prefixes = "@" };
await LoadTestContentAsync(rsb);
var inputText = "@Placeholder";
var expectedText = "@Token";
rsb.SuggestionChosen += (rsb, e) =>
{
e.DisplayText = expectedText.Substring(1);
var format = e.Format;
format!.BackgroundColor = Colors.Beige;
format.ForegroundColor = Colors.Azure;
format.Bold = FormatEffect.On;
format.Italic = FormatEffect.On;
format.Size = 9;
};
await AddTokenAsync(rsb, inputText);
Assert.AreEqual(1, rsb.Tokens.Count, "Token count is not 1 after committing 1 token.");
var defaultFormat = rsb.TextDocument!.GetDefaultCharacterFormat();
var token = rsb.Tokens[0];
var range = rsb.TextDocument.GetRange((int)token.RangeStart!, (int)token.RangeEnd!);
Assert.AreEqual(expectedText, token.DisplayText, "Unexpected token text.");
Assert.AreEqual(range.Text, token.ToString());
var prePad = range.GetClone();
prePad.SetRange(range.StartPosition, range.StartPosition + 1);
Assert.AreEqual(defaultFormat.BackgroundColor, prePad.CharacterFormat.BackgroundColor, "Unexpected background color for pre padding.");
Assert.AreEqual(defaultFormat.ForegroundColor, prePad.CharacterFormat.ForegroundColor, "Unexpected foreground color for pre padding.");
var postPad = range.GetClone();
postPad.SetRange(range.EndPosition - 1, range.EndPosition);
Assert.AreEqual(defaultFormat.BackgroundColor, postPad.CharacterFormat.BackgroundColor, "Unexpected background color for post padding.");
Assert.AreEqual(defaultFormat.ForegroundColor, postPad.CharacterFormat.ForegroundColor, "Unexpected foreground color for post padding.");
var hiddenText = $"HYPERLINK \"{token.Id}\"\u200B";
range.SetRange(range.StartPosition + hiddenText.Length, range.EndPosition - 1);
Assert.AreEqual(Colors.Beige, range.CharacterFormat.BackgroundColor, "Unexpected token background color.");
Assert.AreEqual(Colors.Azure, range.CharacterFormat.ForegroundColor, "Unexpected token foreground color.");
Assert.AreEqual(FormatEffect.On, range.CharacterFormat.Bold, "Token is expected to be bold.");
Assert.AreEqual(FormatEffect.On, range.CharacterFormat.Italic, "Token is expected to be italic.");
Assert.AreEqual(9, range.CharacterFormat.Size, "Unexpected token font size.");
});
}
[TestCategory(nameof(RichSuggestBox))]
[TestMethod]
[DataRow("@Token1", "@Token2")]
[DataRow("@Token1", "#Token2")]
[DataRow("#Token1", "@Token2")]
public async Task Test_RichSuggestBox_DeleteTokens(string token1, string token2)
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var rsb = new RichSuggestBox() { Prefixes = "@#" };
await LoadTestContentAsync(rsb);
var document = rsb.TextDocument;
var selection = document!.Selection;
await AddTokenAsync(rsb, token1);
await AddTokenAsync(rsb, token2);
Assert.AreEqual(2, rsb.Tokens.Count, "Unexpected token count after adding.");
// Delete token as a whole
selection.Delete(TextRangeUnit.Character, -1);
selection.Delete(TextRangeUnit.Link, -1);
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
Assert.AreEqual(1, rsb.Tokens.Count, "Unexpected token count after deleting token 2");
// Partially delete a token
selection.Delete(TextRangeUnit.Character, -2);
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
Assert.AreEqual(0, rsb.Tokens.Count, "Unexpected token count after deleting token 1");
});
}
[TestCategory(nameof(RichSuggestBox))]
[TestMethod]
public async Task Test_RichSuggestBox_ReplaceToken()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var rsb = new RichSuggestBox() { Prefixes = "@" };
await LoadTestContentAsync(rsb);
var document = rsb.TextDocument;
var selection = document!.Selection;
await AddTokenAsync(rsb, "@Before");
var tokenBefore = rsb.Tokens[0];
AssertToken(rsb, tokenBefore);
selection.Delete(TextRangeUnit.Character, -2);
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
await AddTokenAsync(rsb, "@After");
var tokenAfter = rsb.Tokens[0];
AssertToken(rsb, tokenAfter);
Assert.AreNotSame(tokenBefore, tokenAfter, "Token before and token after are the same.");
Assert.AreNotEqual(tokenBefore.Id, tokenAfter.Id, "Token ID before and token ID after are the same.");
});
}
[TestCategory(nameof(RichSuggestBox))]
[TestMethod]
public async Task Test_RichSuggestBox_FormatReset()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var rsb = new RichSuggestBox() { Prefixes = "@" };
rsb.TokenBackground = new SolidColorBrush(Colors.Azure);
await LoadTestContentAsync(rsb);
var document = rsb.TextDocument;
var selection = document!.Selection;
var defaultFormat = document.GetDefaultCharacterFormat();
await AddTokenAsync(rsb, "@Token1");
selection.Delete(TextRangeUnit.Character, -1);
var middlePosition = selection.StartPosition;
await AddTokenAsync(rsb, "@Token2");
selection.Delete(TextRangeUnit.Character, -1);
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
selection.SetText(TextSetOptions.Unhide, "text");
Assert.AreEqual(defaultFormat.BackgroundColor, selection.CharacterFormat.BackgroundColor, "Raw text have background color after a token.");
selection.SetRange(middlePosition, middlePosition);
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
selection.SetText(TextSetOptions.Unhide, "text");
Assert.AreEqual(defaultFormat.BackgroundColor, selection.CharacterFormat.BackgroundColor, "Raw text have background color when sandwiched between 2 tokens.");
selection.SetRange(0, 0);
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
selection.SetText(TextSetOptions.Unhide, "text");
Assert.AreEqual(defaultFormat.BackgroundColor, selection.CharacterFormat.BackgroundColor, "Raw text have background color when insert at beginning of the document.");
});
}
[TestCategory(nameof(RichSuggestBox))]
[TestMethod]
public async Task Test_RichSuggestBox_Clear()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var rsb = new RichSuggestBox();
await LoadTestContentAsync(rsb);
var document = rsb.TextDocument;
var selection = document!.Selection;
selection.TypeText("before ");
await AddTokenAsync(rsb, "@Token");
selection.TypeText("after");
document.GetText(TextGetOptions.NoHidden, out var text);
Assert.AreEqual(1, rsb.Tokens.Count, "Unexpected tokens count before clear.");
Assert.IsTrue(document.CanUndo(), "Document cannot undo before clear.");
Assert.AreEqual("before \u200B@Token\u200B after", text);
rsb.Clear();
document.GetText(TextGetOptions.NoHidden, out text);
Assert.AreEqual(0, rsb.Tokens.Count, "Unexpected tokens count after clear.");
Assert.IsFalse(document.CanUndo(), "Document can undo after clear.");
Assert.AreEqual(string.Empty, text);
});
}
[TestCategory(nameof(RichSuggestBox))]
[TestMethod]
public async Task Test_RichSuggestBox_ClearUndoRedoHistory()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var rsb = new RichSuggestBox();
await LoadTestContentAsync(rsb);
var document = rsb.TextDocument;
var selection = document!.Selection;
selection.TypeText("before ");
await AddTokenAsync(rsb, "@Token");
selection.TypeText("after");
document.GetText(TextGetOptions.NoHidden, out var text);
Assert.AreEqual(1, rsb.Tokens.Count, "Unexpected tokens count before clear.");
Assert.IsTrue(document.CanUndo(), "Document cannot undo before clear.");
Assert.AreEqual("before \u200B@Token\u200B after", text);
rsb.ClearUndoRedoSuggestionHistory();
document.GetText(TextGetOptions.NoHidden, out text);
Assert.AreEqual(1, rsb.Tokens.Count, "Unexpected tokens count after clear.");
Assert.IsFalse(document.CanUndo(), "Document can undo after clear.");
Assert.AreEqual("before \u200B@Token\u200B after", text);
});
}
[TestCategory(nameof(RichSuggestBox))]
[TestMethod]
public async Task Test_RichSuggestBox_Load()
{
const string rtf = @"{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}}
{\colortbl ;\red255\green255\blue255;\red0\green0\blue255;\red41\green150\blue204;}
{\*\generator Riched20 10.0.19041}\viewkind4\uc1
\pard\tx720\cf1\f0\fs21\lang4105 Hello {{\field{\*\fldinst{HYPERLINK ""c3b58ee9-df54-4686-b295-f203a5d8809a""}}{\fldrslt{\ul\cf2\u8203?\cf3\highlight1 @Michael Hawker\cf1\highlight0\u8203?}}}}\f1\fs21 \f0 from {{\field{\*\fldinst{HYPERLINK ""1c6a71c3-f81f-4a27-8f17-50d64acd5b61""}}{\fldrslt{\ul\cf2\u8203?\cf3\highlight1 @Tung Huynh\cf1\highlight0\u8203?}}}}\f1\fs21\par
}
";
var token1 = new RichSuggestToken(Guid.Parse("c3b58ee9-df54-4686-b295-f203a5d8809a"), "@Michael Hawker");
var token2 = new RichSuggestToken(Guid.Parse("1c6a71c3-f81f-4a27-8f17-50d64acd5b61"), "@Tung Huynh");
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var rsb = new RichSuggestBox();
await LoadTestContentAsync(rsb);
var document = rsb.TextDocument;
var selection = document!.Selection;
selection.TypeText("before ");
await AddTokenAsync(rsb, "@Token");
selection.TypeText("after");
rsb.Load(rtf, new[] { token1, token2 });
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
document.GetText(TextGetOptions.NoHidden, out var text);
Assert.AreEqual(2, rsb.Tokens.Count, "Unexpected tokens count after load.");
Assert.AreEqual("Hello \u200b@Michael Hawker\u200b from \u200b@Tung Huynh\u200b\r", text, "Unexpected document text.");
AssertToken(rsb, token1);
AssertToken(rsb, token2);
});
}
private static void AssertToken(RichSuggestBox rsb, RichSuggestToken token)
{
var document = rsb.TextDocument;
var tokenRange = document!.GetRange((int)token.RangeStart!, (int)token.RangeEnd!);
Assert.AreEqual(token.ToString(), tokenRange.Text);
Assert.AreEqual($"\"{token.Id}\"", tokenRange.Link, "Unexpected link value.");
Assert.AreEqual(LinkType.FriendlyLinkAddress, tokenRange.CharacterFormat.LinkType, "Unexpected link type.");
}
private static async Task TestAddTokenAsync(RichSuggestBox rsb, string tokenText)
{
bool suggestionsRequestedCalled = false;
bool suggestionChosenCalled = false;
void SuggestionsRequestedHandler(RichSuggestBox sender, SuggestionRequestedEventArgs args)
{
suggestionsRequestedCalled = true;
Assert.AreEqual(tokenText[0].ToString(), args.Prefix, $"Unexpected prefix in {nameof(RichSuggestBox.SuggestionRequested)}.");
Assert.AreEqual(tokenText.Substring(1), args.QueryText, $"Unexpected query in {nameof(RichSuggestBox.SuggestionRequested)}.");
}
void SuggestionChosenHandler(RichSuggestBox sender, SuggestionChosenEventArgs args)
{
suggestionChosenCalled = true;
Assert.AreEqual(tokenText[0].ToString(), args.Prefix, $"Unexpected prefix in {nameof(RichSuggestBox.SuggestionChosen)}.");
Assert.AreEqual(tokenText.Substring(1), args.QueryText, $"Unexpected query in {nameof(RichSuggestBox.SuggestionChosen)}.");
Assert.AreEqual(args.QueryText, args.DisplayText, $"Unexpected display text in {nameof(RichSuggestBox.SuggestionChosen)}.");
Assert.AreSame(tokenText, args.SelectedItem, $"Selected item has unknown object {args.SelectedItem} in {nameof(RichSuggestBox.SuggestionChosen)}.");
}
rsb.SuggestionRequested += SuggestionsRequestedHandler;
rsb.SuggestionChosen += SuggestionChosenHandler;
await AddTokenAsync(rsb, tokenText);
rsb.SuggestionRequested -= SuggestionsRequestedHandler;
rsb.SuggestionChosen -= SuggestionChosenHandler;
Assert.IsTrue(suggestionsRequestedCalled, $"{nameof(RichSuggestBox.SuggestionRequested)} was not invoked.");
Assert.IsTrue(suggestionChosenCalled, $"{nameof(RichSuggestBox.SuggestionChosen)} was not invoked.");
}
private static async Task AddTokenAsync(RichSuggestBox rsb, string tokenText)
{
var selection = rsb.TextDocument!.Selection;
selection.TypeText(tokenText);
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); // Wait for SelectionChanged to be invoked
await rsb.CommitSuggestionAsync(tokenText);
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); // Wait for TextChanged to be invoked
}
}