Flutter Windows Embedder
text_input_model_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <limits>
8 #include <map>
9 #include <vector>
10 
11 #include "gtest/gtest.h"
12 
13 namespace flutter {
14 
15 TEST(TextInputModel, SetText) {
16  auto model = std::make_unique<TextInputModel>();
17  model->SetText("ABCDE");
18  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
19 }
20 
21 TEST(TextInputModel, SetTextWideCharacters) {
22  auto model = std::make_unique<TextInputModel>();
23  model->SetText("๐Ÿ˜„๐Ÿ™ƒ๐Ÿคช๐Ÿง");
24  EXPECT_STREQ(model->GetText().c_str(), "๐Ÿ˜„๐Ÿ™ƒ๐Ÿคช๐Ÿง");
25 }
26 
27 TEST(TextInputModel, SetTextEmpty) {
28  auto model = std::make_unique<TextInputModel>();
29  model->SetText("");
30  EXPECT_STREQ(model->GetText().c_str(), "");
31 }
32 
33 TEST(TextInputModel, SetTextReplaceText) {
34  auto model = std::make_unique<TextInputModel>();
35  model->SetText("ABCDE");
36  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
37  model->SetText("");
38  EXPECT_STREQ(model->GetText().c_str(), "");
39 }
40 
41 TEST(TextInputModel, SetTextResetsSelection) {
42  auto model = std::make_unique<TextInputModel>();
43  model->SetText("ABCDE");
44  EXPECT_TRUE(model->SetSelection(TextRange(3)));
45  EXPECT_EQ(model->selection(), TextRange(3));
46  model->SetText("FGHJI");
47  EXPECT_EQ(model->selection(), TextRange(0));
48 }
49 
50 TEST(TextInputModel, SetTextRejectingSelectionLeavesModelUnchanged) {
51  auto model = std::make_unique<TextInputModel>();
52  EXPECT_TRUE(model->SetText("ABCDE", TextRange(3), TextRange(0)));
53  // A rejected update must not apply the text either. Applying it alone would
54  // leave the selection pointing past the end of the new text, and every
55  // subsequent edit indexes into the text with that selection.
56  EXPECT_FALSE(model->SetText("AB", TextRange(5), TextRange(0)));
57  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
58  EXPECT_EQ(model->selection(), TextRange(3));
59  EXPECT_EQ(model->composing_range(), TextRange(0));
60 }
61 
62 TEST(TextInputModel, SetTextRejectingComposingRangeLeavesModelUnchanged) {
63  auto model = std::make_unique<TextInputModel>();
64  EXPECT_TRUE(model->SetText("ABCDE", TextRange(2), TextRange(1, 3)));
65  EXPECT_FALSE(model->SetText("AB", TextRange(2), TextRange(1, 3)));
66  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
67  EXPECT_EQ(model->selection(), TextRange(2));
68  EXPECT_EQ(model->composing_range(), TextRange(1, 3));
69  EXPECT_TRUE(model->composing());
70 }
71 
72 TEST(TextInputModel, UpdateComposingTextWithSelectionPastComposingText) {
73  auto model = std::make_unique<TextInputModel>();
74  EXPECT_TRUE(model->SetText("ABCDE", TextRange(5), TextRange(0)));
75  model->BeginComposing();
76 
77  // The selection accompanying composing text is relative to that text, but an
78  // input method is free to report one that does not fit inside it. The offset
79  // must not reach the text buffer, where a later edit would index out of
80  // bounds and abort the process from inside the standard library.
81  model->UpdateComposingText(u"n", TextRange(99));
82  EXPECT_STREQ(model->GetText().c_str(), "ABCDEn");
83  EXPECT_EQ(model->selection(), TextRange(6));
84  EXPECT_EQ(model->composing_range(), TextRange(5, 6));
85 
86  // Clearing the composing text collapses the composing range, which makes the
87  // next update replace the selection instead. This is the point at which an
88  // unclamped offset becomes an out of bounds index into the text.
89  model->UpdateComposingText(u"", TextRange(99));
90  EXPECT_EQ(model->composing_range(), TextRange(5));
91  EXPECT_EQ(model->selection(), TextRange(5));
92  model->UpdateComposingText(u"m", TextRange(1));
93  EXPECT_STREQ(model->GetText().c_str(), "ABCDEm");
94  EXPECT_EQ(model->selection(), TextRange(6));
95 }
96 
97 TEST(TextInputModel, SetSelectionStart) {
98  auto model = std::make_unique<TextInputModel>();
99  model->SetText("ABCDE");
100  EXPECT_TRUE(model->SetSelection(TextRange(0)));
101  EXPECT_EQ(model->selection(), TextRange(0));
102  EXPECT_EQ(model->composing_range(), TextRange(0));
103  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
104 }
105 
106 TEST(TextInputModel, SetSelectionComposingStart) {
107  auto model = std::make_unique<TextInputModel>();
108  model->SetText("ABCDE");
109  model->BeginComposing();
110  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
111  EXPECT_TRUE(model->SetSelection(TextRange(1)));
112  EXPECT_EQ(model->selection(), TextRange(1));
113  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
114  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
115 }
116 
117 TEST(TextInputModel, SetSelectionMiddle) {
118  auto model = std::make_unique<TextInputModel>();
119  model->SetText("ABCDE");
120  EXPECT_TRUE(model->SetSelection(TextRange(2)));
121  EXPECT_EQ(model->selection(), TextRange(2));
122  EXPECT_EQ(model->composing_range(), TextRange(0));
123  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
124 }
125 
126 TEST(TextInputModel, SetSelectionComposingMiddle) {
127  auto model = std::make_unique<TextInputModel>();
128  model->SetText("ABCDE");
129  model->BeginComposing();
130  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
131  EXPECT_TRUE(model->SetSelection(TextRange(2)));
132  EXPECT_EQ(model->selection(), TextRange(2));
133  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
134  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
135 }
136 
137 TEST(TextInputModel, SetSelectionEnd) {
138  auto model = std::make_unique<TextInputModel>();
139  model->SetText("ABCDE");
140  EXPECT_TRUE(model->SetSelection(TextRange(5)));
141  EXPECT_EQ(model->selection(), TextRange(5));
142  EXPECT_EQ(model->composing_range(), TextRange(0));
143  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
144 }
145 
146 TEST(TextInputModel, SetSelectionComposingEnd) {
147  auto model = std::make_unique<TextInputModel>();
148  model->SetText("ABCDE");
149  model->BeginComposing();
150  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
151  EXPECT_TRUE(model->SetSelection(TextRange(4)));
152  EXPECT_EQ(model->selection(), TextRange(4));
153  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
154  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
155 }
156 
157 TEST(TextInputModel, SetSelectionWthExtent) {
158  auto model = std::make_unique<TextInputModel>();
159  model->SetText("ABCDE");
160  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
161  EXPECT_EQ(model->selection(), TextRange(1, 4));
162  EXPECT_EQ(model->composing_range(), TextRange(0));
163  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
164 }
165 
166 TEST(TextInputModel, SetSelectionWthExtentComposing) {
167  auto model = std::make_unique<TextInputModel>();
168  model->SetText("ABCDE");
169  model->BeginComposing();
170  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
171  EXPECT_FALSE(model->SetSelection(TextRange(1, 4)));
172  EXPECT_EQ(model->selection(), TextRange(1));
173  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
174  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
175 }
176 
177 TEST(TextInputModel, SetSelectionReverseExtent) {
178  auto model = std::make_unique<TextInputModel>();
179  model->SetText("ABCDE");
180  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
181  EXPECT_EQ(model->selection(), TextRange(4, 1));
182  EXPECT_EQ(model->composing_range(), TextRange(0));
183  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
184 }
185 
186 TEST(TextInputModel, SetSelectionReverseExtentComposing) {
187  auto model = std::make_unique<TextInputModel>();
188  model->SetText("ABCDE");
189  model->BeginComposing();
190  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
191  EXPECT_FALSE(model->SetSelection(TextRange(4, 1)));
192  EXPECT_EQ(model->selection(), TextRange(1));
193  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
194  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
195 }
196 
197 TEST(TextInputModel, SetSelectionOutsideString) {
198  auto model = std::make_unique<TextInputModel>();
199  model->SetText("ABCDE");
200  EXPECT_FALSE(model->SetSelection(TextRange(4, 6)));
201  EXPECT_FALSE(model->SetSelection(TextRange(5, 6)));
202  EXPECT_FALSE(model->SetSelection(TextRange(6)));
203 }
204 
205 TEST(TextInputModel, SetSelectionOutsideComposingRange) {
206  auto model = std::make_unique<TextInputModel>();
207  model->SetText("ABCDE");
208  model->BeginComposing();
209  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
210  EXPECT_FALSE(model->SetSelection(TextRange(0)));
211  EXPECT_EQ(model->selection(), TextRange(1));
212  EXPECT_FALSE(model->SetSelection(TextRange(5)));
213  EXPECT_EQ(model->selection(), TextRange(1));
214  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
215 }
216 
217 TEST(TextInputModel, SetComposingRangeStart) {
218  auto model = std::make_unique<TextInputModel>();
219  model->SetText("ABCDE");
220  model->BeginComposing();
221  EXPECT_TRUE(model->SetComposingRange(TextRange(0, 0), 0));
222  EXPECT_EQ(model->selection(), TextRange(0));
223  EXPECT_EQ(model->composing_range(), TextRange(0));
224  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
225 }
226 
227 TEST(TextInputModel, SetComposingRangeMiddle) {
228  auto model = std::make_unique<TextInputModel>();
229  model->SetText("ABCDE");
230  model->BeginComposing();
231  EXPECT_TRUE(model->SetComposingRange(TextRange(2, 2), 0));
232  EXPECT_EQ(model->selection(), TextRange(2));
233  EXPECT_EQ(model->composing_range(), TextRange(2));
234  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
235 }
236 
237 TEST(TextInputModel, SetComposingRangeEnd) {
238  auto model = std::make_unique<TextInputModel>();
239  model->SetText("ABCDE");
240  model->BeginComposing();
241  EXPECT_TRUE(model->SetComposingRange(TextRange(5, 5), 0));
242  EXPECT_EQ(model->selection(), TextRange(5));
243  EXPECT_EQ(model->composing_range(), TextRange(5));
244  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
245 }
246 
247 TEST(TextInputModel, SetComposingRangeWithExtent) {
248  auto model = std::make_unique<TextInputModel>();
249  model->SetText("ABCDE");
250  model->BeginComposing();
251  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3));
252  EXPECT_EQ(model->selection(), TextRange(4));
253  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
254  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
255 }
256 
257 TEST(TextInputModel, SetComposingRangeReverseExtent) {
258  auto model = std::make_unique<TextInputModel>();
259  model->SetText("ABCDE");
260  model->BeginComposing();
261  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3));
262  EXPECT_EQ(model->selection(), TextRange(4));
263  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
264  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
265 }
266 
267 TEST(TextInputModel, SetComposingRangeWithOffsetOutsideString) {
268  auto model = std::make_unique<TextInputModel>();
269  model->SetText("ABCDE");
270  model->BeginComposing();
271  // The offset positions the caret relative to the composing range, and reaches
272  // the text as an index. An offset that runs past the end of the text must be
273  // rejected rather than stored as a selection.
274  EXPECT_FALSE(model->SetComposingRange(TextRange(1, 4), 5));
275  EXPECT_EQ(model->selection(), TextRange(0));
276  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 4));
277  EXPECT_EQ(model->selection(), TextRange(5));
278 }
279 
280 TEST(TextInputModel, SetComposingRangeOutsideString) {
281  auto model = std::make_unique<TextInputModel>();
282  model->SetText("ABCDE");
283  model->BeginComposing();
284  EXPECT_FALSE(model->SetComposingRange(TextRange(4, 6), 0));
285  EXPECT_FALSE(model->SetComposingRange(TextRange(5, 6), 0));
286  EXPECT_FALSE(model->SetComposingRange(TextRange(6, 6), 0));
287 }
288 
289 // Composing sequence with no initial selection and no text input.
290 TEST(TextInputModel, CommitComposingNoTextWithNoSelection) {
291  auto model = std::make_unique<TextInputModel>();
292  model->SetText("ABCDE");
293  model->SetSelection(TextRange(0));
294 
295  // Verify no changes on BeginComposing.
296  model->BeginComposing();
297  EXPECT_EQ(model->selection(), TextRange(0));
298  EXPECT_EQ(model->composing_range(), TextRange(0));
299  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
300 
301  // Verify no changes on CommitComposing.
302  model->CommitComposing();
303  EXPECT_EQ(model->selection(), TextRange(0));
304  EXPECT_EQ(model->composing_range(), TextRange(0));
305  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
306 
307  // Verify no changes on CommitComposing.
308  model->EndComposing();
309  EXPECT_EQ(model->selection(), TextRange(0));
310  EXPECT_EQ(model->composing_range(), TextRange(0));
311  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
312 }
313 
314 // Composing sequence with an initial selection and no text input.
315 TEST(TextInputModel, CommitComposingNoTextWithSelection) {
316  auto model = std::make_unique<TextInputModel>();
317  model->SetText("ABCDE");
318  model->SetSelection(TextRange(1, 3));
319 
320  // Verify no changes on BeginComposing.
321  model->BeginComposing();
322  EXPECT_EQ(model->selection(), TextRange(1, 3));
323  EXPECT_EQ(model->composing_range(), TextRange(1));
324  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
325 
326  // Verify no changes on CommitComposing.
327  model->CommitComposing();
328  EXPECT_EQ(model->selection(), TextRange(1, 3));
329  EXPECT_EQ(model->composing_range(), TextRange(1));
330  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
331 
332  // Verify no changes on CommitComposing.
333  model->EndComposing();
334  EXPECT_EQ(model->selection(), TextRange(1, 3));
335  EXPECT_EQ(model->composing_range(), TextRange(0));
336  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
337 }
338 
339 // Composing sequence with no initial selection.
340 TEST(TextInputModel, CommitComposingTextWithNoSelection) {
341  auto model = std::make_unique<TextInputModel>();
342  model->SetText("ABCDE");
343  model->SetSelection(TextRange(1));
344 
345  // Verify no changes on BeginComposing.
346  model->BeginComposing();
347  EXPECT_EQ(model->selection(), TextRange(1));
348  EXPECT_EQ(model->composing_range(), TextRange(1));
349  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
350 
351  // Verify selection base, extent and composing extent increment as text is
352  // entered. Verify composing base does not change.
353  model->UpdateComposingText("ใค");
354  EXPECT_EQ(model->selection(), TextRange(2));
355  EXPECT_EQ(model->composing_range(), TextRange(1, 2));
356  EXPECT_STREQ(model->GetText().c_str(), "AใคBCDE");
357  model->UpdateComposingText("ใคใ‚‹");
358  EXPECT_EQ(model->selection(), TextRange(3));
359  EXPECT_EQ(model->composing_range(), TextRange(1, 3));
360  EXPECT_STREQ(model->GetText().c_str(), "Aใคใ‚‹BCDE");
361 
362  // Verify that cursor position is set to correct offset from composing base.
363  model->UpdateComposingText("้ถด");
364  EXPECT_TRUE(model->SetSelection(TextRange(1)));
365  EXPECT_EQ(model->selection(), TextRange(1));
366  EXPECT_EQ(model->composing_range(), TextRange(1, 2));
367  EXPECT_STREQ(model->GetText().c_str(), "A้ถดBCDE");
368 
369  // Verify composing base is set to composing extent on commit.
370  model->CommitComposing();
371  EXPECT_EQ(model->selection(), TextRange(2));
372  EXPECT_EQ(model->composing_range(), TextRange(2));
373  EXPECT_STREQ(model->GetText().c_str(), "A้ถดBCDE");
374 
375  // Verify that further text entry increments the selection base, extent and
376  // the composing extent. Verify that composing base does not change.
377  model->UpdateComposingText("ใŒ");
378  EXPECT_EQ(model->selection(), TextRange(3));
379  EXPECT_EQ(model->composing_range(), TextRange(2, 3));
380  EXPECT_STREQ(model->GetText().c_str(), "A้ถดใŒBCDE");
381 
382  // Verify composing base is set to composing extent on commit.
383  model->CommitComposing();
384  EXPECT_EQ(model->selection(), TextRange(3));
385  EXPECT_EQ(model->composing_range(), TextRange(3));
386  EXPECT_STREQ(model->GetText().c_str(), "A้ถดใŒBCDE");
387 
388  // Verify no changes on EndComposing.
389  model->EndComposing();
390  EXPECT_EQ(model->selection(), TextRange(3));
391  EXPECT_EQ(model->composing_range(), TextRange(0));
392  EXPECT_STREQ(model->GetText().c_str(), "A้ถดใŒBCDE");
393 }
394 
395 // Composing sequence with an initial selection.
396 TEST(TextInputModel, CommitComposingTextWithSelection) {
397  auto model = std::make_unique<TextInputModel>();
398  model->SetText("ABCDE");
399  model->SetSelection(TextRange(1, 3));
400 
401  // Verify no changes on BeginComposing.
402  model->BeginComposing();
403  EXPECT_EQ(model->selection(), TextRange(1, 3));
404  EXPECT_EQ(model->composing_range(), TextRange(1));
405  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
406 
407  // Verify selection is replaced and selection base, extent and composing
408  // extent increment to the position immediately after the composing text.
409  // Verify composing base does not change.
410  model->UpdateComposingText("ใค");
411  EXPECT_EQ(model->selection(), TextRange(2));
412  EXPECT_EQ(model->composing_range(), TextRange(1, 2));
413  EXPECT_STREQ(model->GetText().c_str(), "AใคDE");
414 
415  // Verify that further text entry increments the selection base, extent and
416  // the composing extent. Verify that composing base does not change.
417  model->UpdateComposingText("ใคใ‚‹");
418  EXPECT_EQ(model->selection(), TextRange(3));
419  EXPECT_EQ(model->composing_range(), TextRange(1, 3));
420  EXPECT_STREQ(model->GetText().c_str(), "Aใคใ‚‹DE");
421 
422  // Verify that cursor position is set to correct offset from composing base.
423  model->UpdateComposingText("้ถด");
424  EXPECT_TRUE(model->SetSelection(TextRange(1)));
425  EXPECT_EQ(model->selection(), TextRange(1));
426  EXPECT_EQ(model->composing_range(), TextRange(1, 2));
427  EXPECT_STREQ(model->GetText().c_str(), "A้ถดDE");
428 
429  // Verify composing base is set to composing extent on commit.
430  model->CommitComposing();
431  EXPECT_EQ(model->selection(), TextRange(2));
432  EXPECT_EQ(model->composing_range(), TextRange(2));
433  EXPECT_STREQ(model->GetText().c_str(), "A้ถดDE");
434 
435  // Verify that further text entry increments the selection base, extent and
436  // the composing extent. Verify that composing base does not change.
437  model->UpdateComposingText("ใŒ");
438  EXPECT_EQ(model->selection(), TextRange(3));
439  EXPECT_EQ(model->composing_range(), TextRange(2, 3));
440  EXPECT_STREQ(model->GetText().c_str(), "A้ถดใŒDE");
441 
442  // Verify composing base is set to composing extent on commit.
443  model->CommitComposing();
444  EXPECT_EQ(model->selection(), TextRange(3));
445  EXPECT_EQ(model->composing_range(), TextRange(3));
446  EXPECT_STREQ(model->GetText().c_str(), "A้ถดใŒDE");
447 
448  // Verify no changes on EndComposing.
449  model->EndComposing();
450  EXPECT_EQ(model->selection(), TextRange(3));
451  EXPECT_EQ(model->composing_range(), TextRange(0));
452  EXPECT_STREQ(model->GetText().c_str(), "A้ถดใŒDE");
453 }
454 
455 TEST(TextInputModel, UpdateComposingRemovesLastComposingCharacter) {
456  auto model = std::make_unique<TextInputModel>();
457  model->SetText("ABCDE");
458  model->BeginComposing();
459  model->SetComposingRange(TextRange(1, 2), 1);
460  model->UpdateComposingText("");
461  EXPECT_EQ(model->selection(), TextRange(1));
462  EXPECT_EQ(model->composing_range(), TextRange(1));
463  model->SetText("ACDE");
464 }
465 
466 TEST(TextInputModel, UpdateSelectionWhileComposing) {
467  auto model = std::make_unique<TextInputModel>();
468  model->SetText("ABCDE");
469  model->BeginComposing();
470  model->SetComposingRange(TextRange(4, 5), 1);
471  model->UpdateComposingText(u"ใดใ‚‡ใ‚“ใดใ‚‡ใ‚“", TextRange(3, 6));
472  EXPECT_STREQ(model->GetText().c_str(), "ABCDใดใ‚‡ใ‚“ใดใ‚‡ใ‚“");
473  EXPECT_EQ(model->selection(), TextRange(7, 10));
474  EXPECT_EQ(model->composing_range(), TextRange(4, 10));
475 }
476 
477 TEST(TextInputModel, AddCodePoint) {
478  auto model = std::make_unique<TextInputModel>();
479  model->AddCodePoint('A');
480  model->AddCodePoint('B');
481  model->AddCodePoint(0x1f604);
482  model->AddCodePoint('D');
483  model->AddCodePoint('E');
484  EXPECT_EQ(model->selection(), TextRange(6));
485  EXPECT_EQ(model->composing_range(), TextRange(0));
486  EXPECT_STREQ(model->GetText().c_str(), "AB๐Ÿ˜„DE");
487 }
488 
489 TEST(TextInputModel, AddCodePointSelection) {
490  auto model = std::make_unique<TextInputModel>();
491  model->SetText("ABCDE");
492  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
493  model->AddCodePoint('x');
494  EXPECT_EQ(model->selection(), TextRange(2));
495  EXPECT_EQ(model->composing_range(), TextRange(0));
496  EXPECT_STREQ(model->GetText().c_str(), "AxE");
497 }
498 
499 TEST(TextInputModel, AddCodePointReverseSelection) {
500  auto model = std::make_unique<TextInputModel>();
501  model->SetText("ABCDE");
502  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
503  model->AddCodePoint('x');
504  EXPECT_EQ(model->selection(), TextRange(2));
505  EXPECT_EQ(model->composing_range(), TextRange(0));
506  EXPECT_STREQ(model->GetText().c_str(), "AxE");
507 }
508 
509 TEST(TextInputModel, AddCodePointSelectionWideCharacter) {
510  auto model = std::make_unique<TextInputModel>();
511  model->SetText("ABCDE");
512  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
513  model->AddCodePoint(0x1f604);
514  EXPECT_EQ(model->selection(), TextRange(3));
515  EXPECT_EQ(model->composing_range(), TextRange(0));
516  EXPECT_STREQ(model->GetText().c_str(), "A๐Ÿ˜„E");
517 }
518 
519 TEST(TextInputModel, AddCodePointReverseSelectionWideCharacter) {
520  auto model = std::make_unique<TextInputModel>();
521  model->SetText("ABCDE");
522  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
523  model->AddCodePoint(0x1f604);
524  EXPECT_EQ(model->selection(), TextRange(3));
525  EXPECT_EQ(model->composing_range(), TextRange(0));
526  EXPECT_STREQ(model->GetText().c_str(), "A๐Ÿ˜„E");
527 }
528 
529 TEST(TextInputModel, AddText) {
530  auto model = std::make_unique<TextInputModel>();
531  model->AddText(u"ABCDE");
532  model->AddText("๐Ÿ˜„");
533  model->AddText("FGHIJ");
534  EXPECT_EQ(model->selection(), TextRange(12));
535  EXPECT_EQ(model->composing_range(), TextRange(0));
536  EXPECT_STREQ(model->GetText().c_str(), "ABCDE๐Ÿ˜„FGHIJ");
537 }
538 
539 TEST(TextInputModel, AddTextSelection) {
540  auto model = std::make_unique<TextInputModel>();
541  model->SetText("ABCDE");
542  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
543  model->AddText("xy");
544  EXPECT_EQ(model->selection(), TextRange(3));
545  EXPECT_EQ(model->composing_range(), TextRange(0));
546  EXPECT_STREQ(model->GetText().c_str(), "AxyE");
547 }
548 
549 TEST(TextInputModel, AddTextReverseSelection) {
550  auto model = std::make_unique<TextInputModel>();
551  model->SetText("ABCDE");
552  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
553  model->AddText("xy");
554  EXPECT_EQ(model->selection(), TextRange(3));
555  EXPECT_EQ(model->composing_range(), TextRange(0));
556  EXPECT_STREQ(model->GetText().c_str(), "AxyE");
557 }
558 
559 TEST(TextInputModel, AddTextSelectionWideCharacter) {
560  auto model = std::make_unique<TextInputModel>();
561  model->SetText("ABCDE");
562  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
563  model->AddText(u"๐Ÿ˜„๐Ÿ™ƒ");
564  EXPECT_EQ(model->selection(), TextRange(5));
565  EXPECT_EQ(model->composing_range(), TextRange(0));
566  EXPECT_STREQ(model->GetText().c_str(), "A๐Ÿ˜„๐Ÿ™ƒE");
567 }
568 
569 TEST(TextInputModel, AddTextReverseSelectionWideCharacter) {
570  auto model = std::make_unique<TextInputModel>();
571  model->SetText("ABCDE");
572  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
573  model->AddText(u"๐Ÿ˜„๐Ÿ™ƒ");
574  EXPECT_EQ(model->selection(), TextRange(5));
575  EXPECT_EQ(model->composing_range(), TextRange(0));
576  EXPECT_STREQ(model->GetText().c_str(), "A๐Ÿ˜„๐Ÿ™ƒE");
577 }
578 
579 TEST(TextInputModel, DeleteStart) {
580  auto model = std::make_unique<TextInputModel>();
581  model->SetText("ABCDE");
582  EXPECT_TRUE(model->SetSelection(TextRange(0)));
583  ASSERT_TRUE(model->Delete());
584  EXPECT_EQ(model->selection(), TextRange(0));
585  EXPECT_EQ(model->composing_range(), TextRange(0));
586  EXPECT_STREQ(model->GetText().c_str(), "BCDE");
587 }
588 
589 TEST(TextInputModel, DeleteMiddle) {
590  auto model = std::make_unique<TextInputModel>();
591  model->SetText("ABCDE");
592  EXPECT_TRUE(model->SetSelection(TextRange(2)));
593  ASSERT_TRUE(model->Delete());
594  EXPECT_EQ(model->selection(), TextRange(2));
595  EXPECT_EQ(model->composing_range(), TextRange(0));
596  EXPECT_STREQ(model->GetText().c_str(), "ABDE");
597 }
598 
599 TEST(TextInputModel, DeleteEnd) {
600  auto model = std::make_unique<TextInputModel>();
601  model->SetText("ABCDE");
602  EXPECT_TRUE(model->SetSelection(TextRange(5)));
603  ASSERT_FALSE(model->Delete());
604  EXPECT_EQ(model->selection(), TextRange(5));
605  EXPECT_EQ(model->composing_range(), TextRange(0));
606  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
607 }
608 
609 TEST(TextInputModel, DeleteWideCharacters) {
610  auto model = std::make_unique<TextInputModel>();
611  model->SetText("๐Ÿ˜„๐Ÿ™ƒ๐Ÿคช๐Ÿง");
612  EXPECT_TRUE(model->SetSelection(TextRange(4)));
613  ASSERT_TRUE(model->Delete());
614  EXPECT_EQ(model->selection(), TextRange(4));
615  EXPECT_EQ(model->composing_range(), TextRange(0));
616  EXPECT_STREQ(model->GetText().c_str(), "๐Ÿ˜„๐Ÿ™ƒ๐Ÿง");
617 }
618 
619 TEST(TextInputModel, DeleteSelection) {
620  auto model = std::make_unique<TextInputModel>();
621  model->SetText("ABCDE");
622  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
623  ASSERT_TRUE(model->Delete());
624  EXPECT_EQ(model->selection(), TextRange(1));
625  EXPECT_EQ(model->composing_range(), TextRange(0));
626  EXPECT_STREQ(model->GetText().c_str(), "AE");
627 }
628 
629 TEST(TextInputModel, DeleteReverseSelection) {
630  auto model = std::make_unique<TextInputModel>();
631  model->SetText("ABCDE");
632  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
633  ASSERT_TRUE(model->Delete());
634  EXPECT_EQ(model->selection(), TextRange(1));
635  EXPECT_EQ(model->composing_range(), TextRange(0));
636  EXPECT_STREQ(model->GetText().c_str(), "AE");
637 }
638 
639 TEST(TextInputModel, DeleteStartComposing) {
640  auto model = std::make_unique<TextInputModel>();
641  model->SetText("ABCDE");
642  model->BeginComposing();
643  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
644  EXPECT_EQ(model->selection(), TextRange(1));
645  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
646  ASSERT_TRUE(model->Delete());
647  EXPECT_EQ(model->selection(), TextRange(1));
648  EXPECT_EQ(model->composing_range(), TextRange(1, 3));
649  EXPECT_STREQ(model->GetText().c_str(), "ACDE");
650 }
651 
652 TEST(TextInputModel, DeleteStartReverseComposing) {
653  auto model = std::make_unique<TextInputModel>();
654  model->SetText("ABCDE");
655  model->BeginComposing();
656  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 0));
657  EXPECT_EQ(model->selection(), TextRange(1));
658  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
659  ASSERT_TRUE(model->Delete());
660  EXPECT_EQ(model->selection(), TextRange(1));
661  EXPECT_EQ(model->composing_range(), TextRange(3, 1));
662  EXPECT_STREQ(model->GetText().c_str(), "ACDE");
663 }
664 
665 TEST(TextInputModel, DeleteMiddleComposing) {
666  auto model = std::make_unique<TextInputModel>();
667  model->SetText("ABCDE");
668  model->BeginComposing();
669  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
670  ASSERT_TRUE(model->Delete());
671  EXPECT_EQ(model->selection(), TextRange(2));
672  EXPECT_EQ(model->composing_range(), TextRange(1, 3));
673  EXPECT_STREQ(model->GetText().c_str(), "ABDE");
674 }
675 
676 TEST(TextInputModel, DeleteMiddleReverseComposing) {
677  auto model = std::make_unique<TextInputModel>();
678  model->SetText("ABCDE");
679  model->BeginComposing();
680  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 1));
681  ASSERT_TRUE(model->Delete());
682  EXPECT_EQ(model->selection(), TextRange(2));
683  EXPECT_EQ(model->composing_range(), TextRange(3, 1));
684  EXPECT_STREQ(model->GetText().c_str(), "ABDE");
685 }
686 
687 TEST(TextInputModel, DeleteEndComposing) {
688  auto model = std::make_unique<TextInputModel>();
689  model->SetText("ABCDE");
690  model->BeginComposing();
691  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3));
692  ASSERT_FALSE(model->Delete());
693  EXPECT_EQ(model->selection(), TextRange(4));
694  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
695  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
696 }
697 
698 TEST(TextInputModel, DeleteEndReverseComposing) {
699  auto model = std::make_unique<TextInputModel>();
700  model->SetText("ABCDE");
701  model->BeginComposing();
702  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3));
703  ASSERT_FALSE(model->Delete());
704  EXPECT_EQ(model->selection(), TextRange(4));
705  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
706  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
707 }
708 
709 TEST(TextInputModel, DeleteSurroundingAtCursor) {
710  auto model = std::make_unique<TextInputModel>();
711  model->SetText("ABCDE");
712  EXPECT_TRUE(model->SetSelection(TextRange(2)));
713  EXPECT_TRUE(model->DeleteSurrounding(0, 1));
714  EXPECT_EQ(model->selection(), TextRange(2));
715  EXPECT_EQ(model->composing_range(), TextRange(0));
716  EXPECT_STREQ(model->GetText().c_str(), "ABDE");
717 }
718 
719 TEST(TextInputModel, DeleteSurroundingAtCursorComposing) {
720  auto model = std::make_unique<TextInputModel>();
721  model->SetText("ABCDE");
722  model->BeginComposing();
723  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
724  EXPECT_TRUE(model->DeleteSurrounding(0, 1));
725  EXPECT_EQ(model->selection(), TextRange(2));
726  EXPECT_EQ(model->composing_range(), TextRange(1, 3));
727  EXPECT_STREQ(model->GetText().c_str(), "ABDE");
728 }
729 
730 TEST(TextInputModel, DeleteSurroundingAtCursorAll) {
731  auto model = std::make_unique<TextInputModel>();
732  model->SetText("ABCDE");
733  EXPECT_TRUE(model->SetSelection(TextRange(2)));
734  EXPECT_TRUE(model->DeleteSurrounding(0, 3));
735  EXPECT_EQ(model->selection(), TextRange(2));
736  EXPECT_EQ(model->composing_range(), TextRange(0));
737  EXPECT_STREQ(model->GetText().c_str(), "AB");
738 }
739 
740 TEST(TextInputModel, DeleteSurroundingAtCursorAllComposing) {
741  auto model = std::make_unique<TextInputModel>();
742  model->SetText("ABCDE");
743  model->BeginComposing();
744  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
745  EXPECT_TRUE(model->DeleteSurrounding(0, 2));
746  EXPECT_EQ(model->selection(), TextRange(2));
747  EXPECT_EQ(model->composing_range(), TextRange(1, 2));
748  EXPECT_STREQ(model->GetText().c_str(), "ABE");
749 }
750 
751 TEST(TextInputModel, DeleteSurroundingAtCursorGreedy) {
752  auto model = std::make_unique<TextInputModel>();
753  model->SetText("ABCDE");
754  EXPECT_TRUE(model->SetSelection(TextRange(2)));
755  EXPECT_TRUE(model->DeleteSurrounding(0, 4));
756  EXPECT_EQ(model->selection(), TextRange(2));
757  EXPECT_EQ(model->composing_range(), TextRange(0));
758  EXPECT_STREQ(model->GetText().c_str(), "AB");
759 }
760 
761 TEST(TextInputModel, DeleteSurroundingAtCursorGreedyComposing) {
762  auto model = std::make_unique<TextInputModel>();
763  model->SetText("ABCDE");
764  model->BeginComposing();
765  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
766  EXPECT_TRUE(model->DeleteSurrounding(0, 4));
767  EXPECT_EQ(model->selection(), TextRange(2));
768  EXPECT_EQ(model->composing_range(), TextRange(1, 2));
769  EXPECT_STREQ(model->GetText().c_str(), "ABE");
770 }
771 
772 TEST(TextInputModel, DeleteSurroundingBeforeCursor) {
773  auto model = std::make_unique<TextInputModel>();
774  model->SetText("ABCDE");
775  EXPECT_TRUE(model->SetSelection(TextRange(2)));
776  EXPECT_TRUE(model->DeleteSurrounding(-1, 1));
777  EXPECT_EQ(model->selection(), TextRange(1));
778  EXPECT_EQ(model->composing_range(), TextRange(0));
779  EXPECT_STREQ(model->GetText().c_str(), "ACDE");
780 }
781 
782 TEST(TextInputModel, DeleteSurroundingBeforeCursorComposing) {
783  auto model = std::make_unique<TextInputModel>();
784  model->SetText("ABCDE");
785  model->BeginComposing();
786  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 2));
787  EXPECT_TRUE(model->DeleteSurrounding(-1, 1));
788  EXPECT_EQ(model->selection(), TextRange(2));
789  EXPECT_EQ(model->composing_range(), TextRange(1, 3));
790  EXPECT_STREQ(model->GetText().c_str(), "ABDE");
791 }
792 
793 TEST(TextInputModel, DeleteSurroundingBeforeCursorAll) {
794  auto model = std::make_unique<TextInputModel>();
795  model->SetText("ABCDE");
796  EXPECT_TRUE(model->SetSelection(TextRange(2)));
797  EXPECT_TRUE(model->DeleteSurrounding(-2, 2));
798  EXPECT_EQ(model->selection(), TextRange(0));
799  EXPECT_EQ(model->composing_range(), TextRange(0));
800  EXPECT_STREQ(model->GetText().c_str(), "CDE");
801 }
802 
803 TEST(TextInputModel, DeleteSurroundingBeforeCursorAllComposing) {
804  auto model = std::make_unique<TextInputModel>();
805  model->SetText("ABCDE");
806  model->BeginComposing();
807  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 2));
808  EXPECT_TRUE(model->DeleteSurrounding(-2, 2));
809  EXPECT_EQ(model->selection(), TextRange(1));
810  EXPECT_EQ(model->composing_range(), TextRange(1, 2));
811  EXPECT_STREQ(model->GetText().c_str(), "ADE");
812 }
813 
814 TEST(TextInputModel, DeleteSurroundingBeforeCursorGreedy) {
815  auto model = std::make_unique<TextInputModel>();
816  model->SetText("ABCDE");
817  EXPECT_TRUE(model->SetSelection(TextRange(2)));
818  EXPECT_TRUE(model->DeleteSurrounding(-3, 3));
819  EXPECT_EQ(model->selection(), TextRange(0));
820  EXPECT_EQ(model->composing_range(), TextRange(0));
821  EXPECT_STREQ(model->GetText().c_str(), "CDE");
822 }
823 
824 TEST(TextInputModel, DeleteSurroundingBeforeCursorGreedyComposing) {
825  auto model = std::make_unique<TextInputModel>();
826  model->SetText("ABCDE");
827  model->BeginComposing();
828  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 2));
829  EXPECT_TRUE(model->DeleteSurrounding(-3, 3));
830  EXPECT_EQ(model->selection(), TextRange(1));
831  EXPECT_EQ(model->composing_range(), TextRange(1, 2));
832  EXPECT_STREQ(model->GetText().c_str(), "ADE");
833 }
834 
835 TEST(TextInputModel, DeleteSurroundingAfterCursor) {
836  auto model = std::make_unique<TextInputModel>();
837  model->SetText("ABCDE");
838  EXPECT_TRUE(model->SetSelection(TextRange(2)));
839  EXPECT_TRUE(model->DeleteSurrounding(1, 1));
840  EXPECT_EQ(model->selection(), TextRange(2));
841  EXPECT_EQ(model->composing_range(), TextRange(0));
842  EXPECT_STREQ(model->GetText().c_str(), "ABCE");
843 }
844 
845 TEST(TextInputModel, DeleteSurroundingAfterCursorComposing) {
846  auto model = std::make_unique<TextInputModel>();
847  model->SetText("ABCDE");
848  model->BeginComposing();
849  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
850  EXPECT_TRUE(model->DeleteSurrounding(1, 1));
851  EXPECT_EQ(model->selection(), TextRange(1));
852  EXPECT_EQ(model->composing_range(), TextRange(1, 3));
853  EXPECT_STREQ(model->GetText().c_str(), "ABDE");
854 }
855 
856 TEST(TextInputModel, DeleteSurroundingAfterCursorAll) {
857  auto model = std::make_unique<TextInputModel>();
858  model->SetText("ABCDE");
859  EXPECT_TRUE(model->SetSelection(TextRange(2)));
860  EXPECT_TRUE(model->DeleteSurrounding(1, 2));
861  EXPECT_EQ(model->selection(), TextRange(2));
862  EXPECT_EQ(model->composing_range(), TextRange(0));
863  EXPECT_STREQ(model->GetText().c_str(), "ABC");
864 }
865 
866 TEST(TextInputModel, DeleteSurroundingAfterCursorAllComposing) {
867  auto model = std::make_unique<TextInputModel>();
868  model->SetText("ABCDE");
869  model->BeginComposing();
870  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
871  EXPECT_TRUE(model->DeleteSurrounding(1, 2));
872  EXPECT_EQ(model->selection(), TextRange(1));
873  EXPECT_EQ(model->composing_range(), TextRange(1, 2));
874  EXPECT_STREQ(model->GetText().c_str(), "ABE");
875 }
876 
877 TEST(TextInputModel, DeleteSurroundingAfterCursorGreedy) {
878  auto model = std::make_unique<TextInputModel>();
879  model->SetText("ABCDE");
880  EXPECT_TRUE(model->SetSelection(TextRange(2)));
881  EXPECT_TRUE(model->DeleteSurrounding(1, 3));
882  EXPECT_EQ(model->selection(), TextRange(2));
883  EXPECT_EQ(model->composing_range(), TextRange(0));
884  EXPECT_STREQ(model->GetText().c_str(), "ABC");
885 }
886 
887 TEST(TextInputModel, DeleteSurroundingAfterCursorGreedyComposing) {
888  auto model = std::make_unique<TextInputModel>();
889  model->SetText("ABCDE");
890  model->BeginComposing();
891  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
892  EXPECT_TRUE(model->DeleteSurrounding(1, 3));
893  EXPECT_EQ(model->selection(), TextRange(1));
894  EXPECT_EQ(model->composing_range(), TextRange(1, 2));
895  EXPECT_STREQ(model->GetText().c_str(), "ABE");
896 }
897 
898 TEST(TextInputModel, DeleteSurroundingSelection) {
899  auto model = std::make_unique<TextInputModel>();
900  model->SetText("ABCDE");
901  EXPECT_TRUE(model->SetSelection(TextRange(2, 3)));
902  EXPECT_TRUE(model->DeleteSurrounding(0, 1));
903  EXPECT_EQ(model->selection(), TextRange(3));
904  EXPECT_EQ(model->composing_range(), TextRange(0));
905  EXPECT_STREQ(model->GetText().c_str(), "ABCE");
906 }
907 
908 TEST(TextInputModel, DeleteSurroundingReverseSelection) {
909  auto model = std::make_unique<TextInputModel>();
910  model->SetText("ABCDE");
911  EXPECT_TRUE(model->SetSelection(TextRange(4, 3)));
912  EXPECT_TRUE(model->DeleteSurrounding(0, 1));
913  EXPECT_EQ(model->selection(), TextRange(3));
914  EXPECT_EQ(model->composing_range(), TextRange(0));
915  EXPECT_STREQ(model->GetText().c_str(), "ABCE");
916 }
917 
918 // Regression test for https://github.com/flutter/flutter/issues/190046.
919 // Deleting across a non-BMP code point must not leave a lone surrogate behind,
920 // which would abort the process on the next UTF-16 to UTF-8 conversion.
921 TEST(TextInputModel, DeleteSurroundingWideCharacterAfterCursor) {
922  auto model = std::make_unique<TextInputModel>();
923  model->SetText("A๐Ÿ˜„B");
924  EXPECT_TRUE(model->SetSelection(TextRange(0)));
925  EXPECT_TRUE(model->DeleteSurrounding(0, 2));
926  EXPECT_EQ(model->selection(), TextRange(0));
927  EXPECT_EQ(model->composing_range(), TextRange(0));
928  EXPECT_STREQ(model->GetText().c_str(), "B");
929 }
930 
931 TEST(TextInputModel, DeleteSurroundingWideCharacterBeforeCursor) {
932  auto model = std::make_unique<TextInputModel>();
933  model->SetText("A๐Ÿ˜„");
934  EXPECT_TRUE(model->SetSelection(TextRange(3)));
935  EXPECT_TRUE(model->DeleteSurrounding(-2, 2));
936  EXPECT_EQ(model->selection(), TextRange(0));
937  EXPECT_EQ(model->composing_range(), TextRange(0));
938  EXPECT_STREQ(model->GetText().c_str(), "");
939 }
940 
941 // The same defect over-deletes when the first code point is non-BMP and a
942 // later one is not.
943 TEST(TextInputModel, DeleteSurroundingWideCharacterFirst) {
944  auto model = std::make_unique<TextInputModel>();
945  model->SetText("๐Ÿ˜„AB");
946  EXPECT_TRUE(model->SetSelection(TextRange(0)));
947  EXPECT_TRUE(model->DeleteSurrounding(0, 2));
948  EXPECT_EQ(model->selection(), TextRange(0));
949  EXPECT_EQ(model->composing_range(), TextRange(0));
950  EXPECT_STREQ(model->GetText().c_str(), "B");
951 }
952 
953 TEST(TextInputModel, DeleteSurroundingWideCharactersAfterCursorOffset) {
954  auto model = std::make_unique<TextInputModel>();
955  model->SetText("A๐Ÿ˜„๐Ÿ™ƒB");
956  EXPECT_TRUE(model->SetSelection(TextRange(0)));
957  EXPECT_TRUE(model->DeleteSurrounding(1, 2));
958  EXPECT_EQ(model->selection(), TextRange(0));
959  EXPECT_EQ(model->composing_range(), TextRange(0));
960  EXPECT_STREQ(model->GetText().c_str(), "AB");
961 }
962 
963 TEST(TextInputModel, DeleteSurroundingWideCharacterGreedy) {
964  auto model = std::make_unique<TextInputModel>();
965  model->SetText("A๐Ÿ˜„");
966  EXPECT_TRUE(model->SetSelection(TextRange(0)));
967  EXPECT_TRUE(model->DeleteSurrounding(0, 5));
968  EXPECT_EQ(model->selection(), TextRange(0));
969  EXPECT_EQ(model->composing_range(), TextRange(0));
970  EXPECT_STREQ(model->GetText().c_str(), "");
971 }
972 
973 TEST(TextInputModel, DeleteSurroundingWideCharacterComposing) {
974  auto model = std::make_unique<TextInputModel>();
975  model->SetText("A๐Ÿ˜„B");
976  model->BeginComposing();
977  EXPECT_TRUE(model->SetComposingRange(TextRange(0, 3), 0));
978  EXPECT_TRUE(model->DeleteSurrounding(0, 2));
979  EXPECT_EQ(model->selection(), TextRange(0));
980  EXPECT_EQ(model->composing_range(), TextRange(0, 0));
981  EXPECT_STREQ(model->GetText().c_str(), "B");
982 }
983 
984 // Regression test isolating the `end != max_pos` -> `end < max_pos` guard
985 // change: a surrogate pair straddling the end of the composing range forces
986 // |end| past |max_pos| in a single 2-wide step. With the old `!=` guard the
987 // loop would never see |end| land exactly on |max_pos| and would keep
988 // deleting past the composing range; `<` stops it as soon as |end| reaches
989 // or passes |max_pos|.
990 TEST(TextInputModel, DeleteSurroundingGuardStopsAtComposingEnd) {
991  auto model = std::make_unique<TextInputModel>();
992  model->SetText("A๐Ÿ˜„B");
993  model->BeginComposing();
994  // Composing range deliberately ends in the middle of the surrogate pair.
995  EXPECT_TRUE(model->SetComposingRange(TextRange(0, 2), 0));
996  EXPECT_TRUE(model->DeleteSurrounding(0, 3));
997  EXPECT_EQ(model->selection(), TextRange(0));
998  EXPECT_EQ(model->composing_range(), TextRange(0, 0));
999  EXPECT_STREQ(model->GetText().c_str(), "B");
1000 }
1001 
1002 // Regression test for the mirror-image guard on the backward walk that
1003 // computes |start|: stepping back by 2 across a surrogate pair can land
1004 // |start| before the start of the editable range instead of exactly on it,
1005 // the same hazard fixed above for |end|. The old `start ==
1006 // editable_range().start()` check could never fire in that case, letting the
1007 // loop keep reading backward past the start of the composing range.
1008 TEST(TextInputModel, DeleteSurroundingGuardStopsAtComposingStart) {
1009  auto model = std::make_unique<TextInputModel>();
1010  model->SetText("A๐Ÿ˜„B");
1011  model->BeginComposing();
1012  // Composing range deliberately starts in the middle of the surrogate pair.
1013  EXPECT_TRUE(model->SetComposingRange(TextRange(2, 4), 2));
1014  EXPECT_TRUE(model->DeleteSurrounding(-3, 1));
1015  EXPECT_EQ(model->selection(), TextRange(1));
1016  EXPECT_EQ(model->composing_range(), TextRange(1, 1));
1017  EXPECT_STREQ(model->GetText().c_str(), "A");
1018 }
1019 
1020 TEST(TextInputModel, BackspaceStart) {
1021  auto model = std::make_unique<TextInputModel>();
1022  model->SetText("ABCDE");
1023  EXPECT_TRUE(model->SetSelection(TextRange(0)));
1024  ASSERT_FALSE(model->Backspace());
1025  EXPECT_EQ(model->selection(), TextRange(0));
1026  EXPECT_EQ(model->composing_range(), TextRange(0));
1027  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1028 }
1029 
1030 TEST(TextInputModel, BackspaceMiddle) {
1031  auto model = std::make_unique<TextInputModel>();
1032  model->SetText("ABCDE");
1033  EXPECT_TRUE(model->SetSelection(TextRange(2)));
1034  ASSERT_TRUE(model->Backspace());
1035  EXPECT_EQ(model->selection(), TextRange(1));
1036  EXPECT_EQ(model->composing_range(), TextRange(0));
1037  EXPECT_STREQ(model->GetText().c_str(), "ACDE");
1038 }
1039 
1040 TEST(TextInputModel, BackspaceEnd) {
1041  auto model = std::make_unique<TextInputModel>();
1042  model->SetText("ABCDE");
1043  EXPECT_TRUE(model->SetSelection(TextRange(5)));
1044  ASSERT_TRUE(model->Backspace());
1045  EXPECT_EQ(model->selection(), TextRange(4));
1046  EXPECT_EQ(model->composing_range(), TextRange(0));
1047  EXPECT_STREQ(model->GetText().c_str(), "ABCD");
1048 }
1049 
1050 TEST(TextInputModel, BackspaceWideCharacters) {
1051  auto model = std::make_unique<TextInputModel>();
1052  model->SetText("๐Ÿ˜„๐Ÿ™ƒ๐Ÿคช๐Ÿง");
1053  EXPECT_TRUE(model->SetSelection(TextRange(4)));
1054  ASSERT_TRUE(model->Backspace());
1055  EXPECT_EQ(model->selection(), TextRange(2));
1056  EXPECT_EQ(model->composing_range(), TextRange(0));
1057  EXPECT_STREQ(model->GetText().c_str(), "๐Ÿ˜„๐Ÿคช๐Ÿง");
1058 }
1059 
1060 TEST(TextInputModel, BackspaceSelection) {
1061  auto model = std::make_unique<TextInputModel>();
1062  model->SetText("ABCDE");
1063  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
1064  ASSERT_TRUE(model->Delete());
1065  EXPECT_EQ(model->selection(), TextRange(1));
1066  EXPECT_EQ(model->composing_range(), TextRange(0));
1067  EXPECT_STREQ(model->GetText().c_str(), "AE");
1068 }
1069 
1070 TEST(TextInputModel, BackspaceReverseSelection) {
1071  auto model = std::make_unique<TextInputModel>();
1072  model->SetText("ABCDE");
1073  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
1074  ASSERT_TRUE(model->Delete());
1075  EXPECT_EQ(model->selection(), TextRange(1));
1076  EXPECT_EQ(model->composing_range(), TextRange(0));
1077  EXPECT_STREQ(model->GetText().c_str(), "AE");
1078 }
1079 
1080 TEST(TextInputModel, BackspaceStartComposing) {
1081  auto model = std::make_unique<TextInputModel>();
1082  model->SetText("ABCDE");
1083  model->BeginComposing();
1084  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
1085  ASSERT_FALSE(model->Backspace());
1086  EXPECT_EQ(model->selection(), TextRange(1));
1087  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1088  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1089 }
1090 
1091 TEST(TextInputModel, BackspaceStartReverseComposing) {
1092  auto model = std::make_unique<TextInputModel>();
1093  model->SetText("ABCDE");
1094  model->BeginComposing();
1095  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 0));
1096  ASSERT_FALSE(model->Backspace());
1097  EXPECT_EQ(model->selection(), TextRange(1));
1098  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1099  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1100 }
1101 
1102 TEST(TextInputModel, BackspaceMiddleComposing) {
1103  auto model = std::make_unique<TextInputModel>();
1104  model->SetText("ABCDE");
1105  model->BeginComposing();
1106  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
1107  ASSERT_TRUE(model->Backspace());
1108  EXPECT_EQ(model->selection(), TextRange(1));
1109  EXPECT_EQ(model->composing_range(), TextRange(1, 3));
1110  EXPECT_STREQ(model->GetText().c_str(), "ACDE");
1111 }
1112 
1113 TEST(TextInputModel, BackspaceMiddleReverseComposing) {
1114  auto model = std::make_unique<TextInputModel>();
1115  model->SetText("ABCDE");
1116  model->BeginComposing();
1117  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 1));
1118  ASSERT_TRUE(model->Backspace());
1119  EXPECT_EQ(model->selection(), TextRange(1));
1120  EXPECT_EQ(model->composing_range(), TextRange(3, 1));
1121  EXPECT_STREQ(model->GetText().c_str(), "ACDE");
1122 }
1123 
1124 TEST(TextInputModel, BackspaceEndComposing) {
1125  auto model = std::make_unique<TextInputModel>();
1126  model->SetText("ABCDE");
1127  model->BeginComposing();
1128  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3));
1129  ASSERT_TRUE(model->Backspace());
1130  EXPECT_EQ(model->selection(), TextRange(3));
1131  EXPECT_EQ(model->composing_range(), TextRange(1, 3));
1132  EXPECT_STREQ(model->GetText().c_str(), "ABCE");
1133 }
1134 
1135 TEST(TextInputModel, BackspaceEndReverseComposing) {
1136  auto model = std::make_unique<TextInputModel>();
1137  model->SetText("ABCDE");
1138  model->BeginComposing();
1139  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3));
1140  ASSERT_TRUE(model->Backspace());
1141  EXPECT_EQ(model->selection(), TextRange(3));
1142  EXPECT_EQ(model->composing_range(), TextRange(3, 1));
1143  EXPECT_STREQ(model->GetText().c_str(), "ABCE");
1144 }
1145 
1146 TEST(TextInputModel, MoveCursorForwardStart) {
1147  auto model = std::make_unique<TextInputModel>();
1148  model->SetText("ABCDE");
1149  EXPECT_TRUE(model->SetSelection(TextRange(0)));
1150  EXPECT_TRUE(model->MoveCursorForward());
1151  EXPECT_EQ(model->selection(), TextRange(1));
1152  EXPECT_EQ(model->composing_range(), TextRange(0));
1153  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1154 }
1155 
1156 TEST(TextInputModel, MoveCursorForwardMiddle) {
1157  auto model = std::make_unique<TextInputModel>();
1158  model->SetText("ABCDE");
1159  EXPECT_TRUE(model->SetSelection(TextRange(2)));
1160  EXPECT_TRUE(model->MoveCursorForward());
1161  EXPECT_EQ(model->selection(), TextRange(3));
1162  EXPECT_EQ(model->composing_range(), TextRange(0));
1163  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1164 }
1165 
1166 TEST(TextInputModel, MoveCursorForwardEnd) {
1167  auto model = std::make_unique<TextInputModel>();
1168  model->SetText("ABCDE");
1169  EXPECT_TRUE(model->SetSelection(TextRange(5)));
1170  EXPECT_FALSE(model->MoveCursorForward());
1171  EXPECT_EQ(model->selection(), TextRange(5));
1172  EXPECT_EQ(model->composing_range(), TextRange(0));
1173  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1174 }
1175 
1176 TEST(TextInputModel, MoveCursorForwardWideCharacters) {
1177  auto model = std::make_unique<TextInputModel>();
1178  model->SetText("๐Ÿ˜„๐Ÿ™ƒ๐Ÿคช๐Ÿง");
1179  EXPECT_TRUE(model->SetSelection(TextRange(4)));
1180  ASSERT_TRUE(model->MoveCursorForward());
1181  EXPECT_EQ(model->selection(), TextRange(6));
1182  EXPECT_EQ(model->composing_range(), TextRange(0));
1183  EXPECT_STREQ(model->GetText().c_str(), "๐Ÿ˜„๐Ÿ™ƒ๐Ÿคช๐Ÿง");
1184 }
1185 
1186 TEST(TextInputModel, MoveCursorForwardSelection) {
1187  auto model = std::make_unique<TextInputModel>();
1188  model->SetText("ABCDE");
1189  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
1190  EXPECT_TRUE(model->MoveCursorForward());
1191  EXPECT_EQ(model->selection(), TextRange(4));
1192  EXPECT_EQ(model->composing_range(), TextRange(0));
1193  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1194 }
1195 
1196 TEST(TextInputModel, MoveCursorForwardReverseSelection) {
1197  auto model = std::make_unique<TextInputModel>();
1198  model->SetText("ABCDE");
1199  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
1200  EXPECT_TRUE(model->MoveCursorForward());
1201  EXPECT_EQ(model->selection(), TextRange(4));
1202  EXPECT_EQ(model->composing_range(), TextRange(0));
1203  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1204 }
1205 
1206 TEST(TextInputModel, MoveCursorForwardStartComposing) {
1207  auto model = std::make_unique<TextInputModel>();
1208  model->SetText("ABCDE");
1209  model->BeginComposing();
1210  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
1211  EXPECT_TRUE(model->MoveCursorForward());
1212  EXPECT_EQ(model->selection(), TextRange(2));
1213  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1214  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1215 }
1216 
1217 TEST(TextInputModel, MoveCursorForwardStartReverseComposing) {
1218  auto model = std::make_unique<TextInputModel>();
1219  model->SetText("ABCDE");
1220  model->BeginComposing();
1221  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 0));
1222  EXPECT_TRUE(model->MoveCursorForward());
1223  EXPECT_EQ(model->selection(), TextRange(2));
1224  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1225  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1226 }
1227 
1228 TEST(TextInputModel, MoveCursorForwardMiddleComposing) {
1229  auto model = std::make_unique<TextInputModel>();
1230  model->SetText("ABCDE");
1231  model->BeginComposing();
1232  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
1233  EXPECT_TRUE(model->MoveCursorForward());
1234  EXPECT_EQ(model->selection(), TextRange(3));
1235  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1236  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1237 }
1238 
1239 TEST(TextInputModel, MoveCursorForwardMiddleReverseComposing) {
1240  auto model = std::make_unique<TextInputModel>();
1241  model->SetText("ABCDE");
1242  model->BeginComposing();
1243  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 1));
1244  EXPECT_TRUE(model->MoveCursorForward());
1245  EXPECT_EQ(model->selection(), TextRange(3));
1246  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1247  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1248 }
1249 
1250 TEST(TextInputModel, MoveCursorForwardEndComposing) {
1251  auto model = std::make_unique<TextInputModel>();
1252  model->SetText("ABCDE");
1253  model->BeginComposing();
1254  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3));
1255  EXPECT_FALSE(model->MoveCursorForward());
1256  EXPECT_EQ(model->selection(), TextRange(4));
1257  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1258  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1259 }
1260 
1261 TEST(TextInputModel, MoveCursorForwardEndReverseComposing) {
1262  auto model = std::make_unique<TextInputModel>();
1263  model->SetText("ABCDE");
1264  model->BeginComposing();
1265  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3));
1266  EXPECT_FALSE(model->MoveCursorForward());
1267  EXPECT_EQ(model->selection(), TextRange(4));
1268  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1269  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1270 }
1271 
1272 TEST(TextInputModel, MoveCursorBackStart) {
1273  auto model = std::make_unique<TextInputModel>();
1274  model->SetText("ABCDE");
1275  EXPECT_TRUE(model->SetSelection(TextRange(0)));
1276  EXPECT_FALSE(model->MoveCursorBack());
1277  EXPECT_EQ(model->selection(), TextRange(0));
1278  EXPECT_EQ(model->composing_range(), TextRange(0));
1279  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1280 }
1281 
1282 TEST(TextInputModel, MoveCursorBackMiddle) {
1283  auto model = std::make_unique<TextInputModel>();
1284  model->SetText("ABCDE");
1285  EXPECT_TRUE(model->SetSelection(TextRange(2)));
1286  EXPECT_TRUE(model->MoveCursorBack());
1287  EXPECT_EQ(model->selection(), TextRange(1));
1288  EXPECT_EQ(model->composing_range(), TextRange(0));
1289  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1290 }
1291 
1292 TEST(TextInputModel, MoveCursorBackEnd) {
1293  auto model = std::make_unique<TextInputModel>();
1294  model->SetText("ABCDE");
1295  EXPECT_TRUE(model->SetSelection(TextRange(5)));
1296  EXPECT_TRUE(model->MoveCursorBack());
1297  EXPECT_EQ(model->selection(), TextRange(4));
1298  EXPECT_EQ(model->composing_range(), TextRange(0));
1299  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1300 }
1301 
1302 TEST(TextInputModel, MoveCursorBackWideCharacters) {
1303  auto model = std::make_unique<TextInputModel>();
1304  model->SetText("๐Ÿ˜„๐Ÿ™ƒ๐Ÿคช๐Ÿง");
1305  EXPECT_TRUE(model->SetSelection(TextRange(4)));
1306  ASSERT_TRUE(model->MoveCursorBack());
1307  EXPECT_EQ(model->selection(), TextRange(2));
1308  EXPECT_EQ(model->composing_range(), TextRange(0));
1309  EXPECT_STREQ(model->GetText().c_str(), "๐Ÿ˜„๐Ÿ™ƒ๐Ÿคช๐Ÿง");
1310 }
1311 
1312 TEST(TextInputModel, MoveCursorBackSelection) {
1313  auto model = std::make_unique<TextInputModel>();
1314  model->SetText("ABCDE");
1315  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
1316  EXPECT_TRUE(model->MoveCursorBack());
1317  EXPECT_EQ(model->selection(), TextRange(1));
1318  EXPECT_EQ(model->composing_range(), TextRange(0));
1319  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1320 }
1321 
1322 TEST(TextInputModel, MoveCursorBackReverseSelection) {
1323  auto model = std::make_unique<TextInputModel>();
1324  model->SetText("ABCDE");
1325  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
1326  EXPECT_TRUE(model->MoveCursorBack());
1327  EXPECT_EQ(model->selection(), TextRange(1));
1328  EXPECT_EQ(model->composing_range(), TextRange(0));
1329  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1330 }
1331 
1332 TEST(TextInputModel, MoveCursorBackStartComposing) {
1333  auto model = std::make_unique<TextInputModel>();
1334  model->SetText("ABCDE");
1335  model->BeginComposing();
1336  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
1337  EXPECT_TRUE(model->SetSelection(TextRange(1)));
1338  EXPECT_FALSE(model->MoveCursorBack());
1339  EXPECT_EQ(model->selection(), TextRange(1));
1340  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1341  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1342 }
1343 
1344 TEST(TextInputModel, MoveCursorBackStartReverseComposing) {
1345  auto model = std::make_unique<TextInputModel>();
1346  model->SetText("ABCDE");
1347  model->BeginComposing();
1348  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 0));
1349  EXPECT_TRUE(model->SetSelection(TextRange(1)));
1350  EXPECT_FALSE(model->MoveCursorBack());
1351  EXPECT_EQ(model->selection(), TextRange(1));
1352  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1353  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1354 }
1355 
1356 TEST(TextInputModel, MoveCursorBackMiddleComposing) {
1357  auto model = std::make_unique<TextInputModel>();
1358  model->SetText("ABCDE");
1359  model->BeginComposing();
1360  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
1361  EXPECT_TRUE(model->MoveCursorBack());
1362  EXPECT_EQ(model->selection(), TextRange(1));
1363  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1364  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1365 }
1366 
1367 TEST(TextInputModel, MoveCursorBackMiddleReverseComposing) {
1368  auto model = std::make_unique<TextInputModel>();
1369  model->SetText("ABCDE");
1370  model->BeginComposing();
1371  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 1));
1372  EXPECT_TRUE(model->MoveCursorBack());
1373  EXPECT_EQ(model->selection(), TextRange(1));
1374  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1375  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1376 }
1377 
1378 TEST(TextInputModel, MoveCursorBackEndComposing) {
1379  auto model = std::make_unique<TextInputModel>();
1380  model->SetText("ABCDE");
1381  model->BeginComposing();
1382  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3));
1383  EXPECT_TRUE(model->MoveCursorBack());
1384  EXPECT_EQ(model->selection(), TextRange(3));
1385  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1386  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1387 }
1388 
1389 TEST(TextInputModel, MoveCursorBackEndReverseComposing) {
1390  auto model = std::make_unique<TextInputModel>();
1391  model->SetText("ABCDE");
1392  model->BeginComposing();
1393  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3));
1394  EXPECT_TRUE(model->MoveCursorBack());
1395  EXPECT_EQ(model->selection(), TextRange(3));
1396  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1397  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1398 }
1399 
1400 TEST(TextInputModel, MoveCursorToBeginningStart) {
1401  auto model = std::make_unique<TextInputModel>();
1402  model->SetText("ABCDE");
1403  EXPECT_TRUE(model->SetSelection(TextRange(0)));
1404  EXPECT_FALSE(model->MoveCursorToBeginning());
1405  EXPECT_EQ(model->selection(), TextRange(0));
1406  EXPECT_EQ(model->composing_range(), TextRange(0));
1407  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1408 }
1409 
1410 TEST(TextInputModel, SelectToBeginningStart) {
1411  auto model = std::make_unique<TextInputModel>();
1412  model->SetText("ABCDE");
1413  EXPECT_TRUE(model->SetSelection(TextRange(0)));
1414  EXPECT_FALSE(model->SelectToBeginning());
1415  EXPECT_EQ(model->selection(), TextRange(0));
1416  EXPECT_EQ(model->composing_range(), TextRange(0));
1417  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1418 }
1419 
1420 TEST(TextInputModel, MoveCursorToBeginningMiddle) {
1421  auto model = std::make_unique<TextInputModel>();
1422  model->SetText("ABCDE");
1423  EXPECT_TRUE(model->SetSelection(TextRange(2)));
1424  EXPECT_TRUE(model->MoveCursorToBeginning());
1425  EXPECT_EQ(model->selection(), TextRange(0));
1426  EXPECT_EQ(model->composing_range(), TextRange(0));
1427  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1428 }
1429 
1430 TEST(TextInputModel, SelectToBeginningMiddle) {
1431  auto model = std::make_unique<TextInputModel>();
1432  model->SetText("ABCDE");
1433  EXPECT_TRUE(model->SetSelection(TextRange(2)));
1434  EXPECT_TRUE(model->SelectToBeginning());
1435  EXPECT_EQ(model->selection(), TextRange(2, 0));
1436  EXPECT_EQ(model->composing_range(), TextRange(0));
1437  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1438 }
1439 
1440 TEST(TextInputModel, MoveCursorToBeginningEnd) {
1441  auto model = std::make_unique<TextInputModel>();
1442  model->SetText("ABCDE");
1443  EXPECT_TRUE(model->SetSelection(TextRange(5)));
1444  EXPECT_TRUE(model->MoveCursorToBeginning());
1445  EXPECT_EQ(model->selection(), TextRange(0));
1446  EXPECT_EQ(model->composing_range(), TextRange(0));
1447  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1448 }
1449 
1450 TEST(TextInputModel, SelectToBeginningEnd) {
1451  auto model = std::make_unique<TextInputModel>();
1452  model->SetText("ABCDE");
1453  EXPECT_TRUE(model->SetSelection(TextRange(5)));
1454  EXPECT_TRUE(model->SelectToBeginning());
1455  EXPECT_EQ(model->selection(), TextRange(5, 0));
1456  EXPECT_EQ(model->composing_range(), TextRange(0));
1457  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1458 }
1459 
1460 TEST(TextInputModel, MoveCursorToBeginningSelection) {
1461  auto model = std::make_unique<TextInputModel>();
1462  model->SetText("ABCDE");
1463  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
1464  EXPECT_TRUE(model->MoveCursorToBeginning());
1465  EXPECT_EQ(model->selection(), TextRange(0));
1466  EXPECT_EQ(model->composing_range(), TextRange(0));
1467  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1468 }
1469 
1470 TEST(TextInputModel, SelectToBeginningSelection) {
1471  auto model = std::make_unique<TextInputModel>();
1472  model->SetText("ABCDE");
1473  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
1474  EXPECT_TRUE(model->SelectToBeginning());
1475  EXPECT_EQ(model->selection(), TextRange(1, 0));
1476  EXPECT_EQ(model->composing_range(), TextRange(0));
1477  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1478 }
1479 
1480 TEST(TextInputModel, MoveCursorToBeginningReverseSelection) {
1481  auto model = std::make_unique<TextInputModel>();
1482  model->SetText("ABCDE");
1483  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
1484  EXPECT_TRUE(model->MoveCursorToBeginning());
1485  EXPECT_EQ(model->selection(), TextRange(0));
1486  EXPECT_EQ(model->composing_range(), TextRange(0));
1487  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1488 }
1489 
1490 TEST(TextInputModel, SelectToBeginningReverseSelection) {
1491  auto model = std::make_unique<TextInputModel>();
1492  model->SetText("ABCDE");
1493  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
1494  EXPECT_TRUE(model->SelectToBeginning());
1495  EXPECT_EQ(model->selection(), TextRange(4, 0));
1496  EXPECT_EQ(model->composing_range(), TextRange(0));
1497  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1498 }
1499 
1500 TEST(TextInputModel, MoveCursorToBeginningStartComposing) {
1501  auto model = std::make_unique<TextInputModel>();
1502  model->SetText("ABCDE");
1503  model->BeginComposing();
1504  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
1505  EXPECT_FALSE(model->MoveCursorToBeginning());
1506  EXPECT_EQ(model->selection(), TextRange(1));
1507  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1508  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1509 }
1510 
1511 TEST(TextInputModel, SelectToBeginningStartComposing) {
1512  auto model = std::make_unique<TextInputModel>();
1513  model->SetText("ABCDE");
1514  model->BeginComposing();
1515  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
1516  EXPECT_FALSE(model->SelectToBeginning());
1517  EXPECT_EQ(model->selection(), TextRange(1));
1518  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1519  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1520 }
1521 
1522 TEST(TextInputModel, MoveCursorToBeginningStartReverseComposing) {
1523  auto model = std::make_unique<TextInputModel>();
1524  model->SetText("ABCDE");
1525  model->BeginComposing();
1526  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 0));
1527  EXPECT_FALSE(model->MoveCursorToBeginning());
1528  EXPECT_EQ(model->selection(), TextRange(1));
1529  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1530  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1531 }
1532 
1533 TEST(TextInputModel, SelectToBeginningStartReverseComposing) {
1534  auto model = std::make_unique<TextInputModel>();
1535  model->SetText("ABCDE");
1536  model->BeginComposing();
1537  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 0));
1538  EXPECT_FALSE(model->SelectToBeginning());
1539  EXPECT_EQ(model->selection(), TextRange(1));
1540  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1541  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1542 }
1543 
1544 TEST(TextInputModel, MoveCursorToBeginningMiddleComposing) {
1545  auto model = std::make_unique<TextInputModel>();
1546  model->SetText("ABCDE");
1547  model->BeginComposing();
1548  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
1549  EXPECT_TRUE(model->MoveCursorToBeginning());
1550  EXPECT_EQ(model->selection(), TextRange(1));
1551  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1552  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1553 }
1554 
1555 TEST(TextInputModel, SelectToBeginningMiddleComposing) {
1556  auto model = std::make_unique<TextInputModel>();
1557  model->SetText("ABCDE");
1558  model->BeginComposing();
1559  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
1560  EXPECT_TRUE(model->SelectToBeginning());
1561  EXPECT_EQ(model->selection(), TextRange(2, 1));
1562  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1563  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1564 }
1565 
1566 TEST(TextInputModel, MoveCursorToBeginningMiddleReverseComposing) {
1567  auto model = std::make_unique<TextInputModel>();
1568  model->SetText("ABCDE");
1569  model->BeginComposing();
1570  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 1));
1571  EXPECT_TRUE(model->MoveCursorToBeginning());
1572  EXPECT_EQ(model->selection(), TextRange(1));
1573  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1574  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1575 }
1576 
1577 TEST(TextInputModel, SelectToBeginningMiddleReverseComposing) {
1578  auto model = std::make_unique<TextInputModel>();
1579  model->SetText("ABCDE");
1580  model->BeginComposing();
1581  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 1));
1582  EXPECT_TRUE(model->SelectToBeginning());
1583  EXPECT_EQ(model->selection(), TextRange(2, 1));
1584  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1585  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1586 }
1587 
1588 TEST(TextInputModel, MoveCursorToBeginningEndComposing) {
1589  auto model = std::make_unique<TextInputModel>();
1590  model->SetText("ABCDE");
1591  model->BeginComposing();
1592  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3));
1593  EXPECT_TRUE(model->MoveCursorToBeginning());
1594  EXPECT_EQ(model->selection(), TextRange(1));
1595  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1596  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1597 }
1598 
1599 TEST(TextInputModel, SelectToBeginningEndComposing) {
1600  auto model = std::make_unique<TextInputModel>();
1601  model->SetText("ABCDE");
1602  model->BeginComposing();
1603  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3));
1604  EXPECT_TRUE(model->MoveCursorToBeginning());
1605  EXPECT_EQ(model->selection(), TextRange(1));
1606  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1607  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1608 }
1609 
1610 TEST(TextInputModel, MoveCursorToBeginningEndReverseComposing) {
1611  auto model = std::make_unique<TextInputModel>();
1612  model->SetText("ABCDE");
1613  model->BeginComposing();
1614  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3));
1615  EXPECT_TRUE(model->MoveCursorToBeginning());
1616  EXPECT_EQ(model->selection(), TextRange(1));
1617  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1618  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1619 }
1620 
1621 TEST(TextInputModel, SelectToBeginningEndReverseComposing) {
1622  auto model = std::make_unique<TextInputModel>();
1623  model->SetText("ABCDE");
1624  model->BeginComposing();
1625  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3));
1626  EXPECT_TRUE(model->SelectToBeginning());
1627  EXPECT_EQ(model->selection(), TextRange(4, 1));
1628  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1629  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1630 }
1631 
1632 TEST(TextInputModel, MoveCursorToEndStart) {
1633  auto model = std::make_unique<TextInputModel>();
1634  model->SetText("ABCDE");
1635  EXPECT_TRUE(model->SetSelection(TextRange(0)));
1636  EXPECT_TRUE(model->MoveCursorToEnd());
1637  EXPECT_EQ(model->selection(), TextRange(5));
1638  EXPECT_EQ(model->composing_range(), TextRange(0));
1639  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1640 }
1641 
1642 TEST(TextInputModel, SelectToEndStart) {
1643  auto model = std::make_unique<TextInputModel>();
1644  model->SetText("ABCDE");
1645  EXPECT_TRUE(model->SetSelection(TextRange(0)));
1646  EXPECT_TRUE(model->SelectToEnd());
1647  EXPECT_EQ(model->selection(), TextRange(0, 5));
1648  EXPECT_EQ(model->composing_range(), TextRange(0));
1649  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1650 }
1651 
1652 TEST(TextInputModel, MoveCursorToEndMiddle) {
1653  auto model = std::make_unique<TextInputModel>();
1654  model->SetText("ABCDE");
1655  EXPECT_TRUE(model->SetSelection(TextRange(2)));
1656  EXPECT_TRUE(model->MoveCursorToEnd());
1657  EXPECT_EQ(model->selection(), TextRange(5));
1658  EXPECT_EQ(model->composing_range(), TextRange(0));
1659  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1660 }
1661 
1662 TEST(TextInputModel, SelectToEndMiddle) {
1663  auto model = std::make_unique<TextInputModel>();
1664  model->SetText("ABCDE");
1665  EXPECT_TRUE(model->SetSelection(TextRange(2)));
1666  EXPECT_TRUE(model->SelectToEnd());
1667  EXPECT_EQ(model->selection(), TextRange(2, 5));
1668  EXPECT_EQ(model->composing_range(), TextRange(0));
1669  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1670 }
1671 
1672 TEST(TextInputModel, MoveCursorToEndEnd) {
1673  auto model = std::make_unique<TextInputModel>();
1674  model->SetText("ABCDE");
1675  EXPECT_TRUE(model->SetSelection(TextRange(5)));
1676  EXPECT_FALSE(model->MoveCursorToEnd());
1677  EXPECT_EQ(model->selection(), TextRange(5));
1678  EXPECT_EQ(model->composing_range(), TextRange(0));
1679  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1680 }
1681 
1682 TEST(TextInputModel, SelectToEndEnd) {
1683  auto model = std::make_unique<TextInputModel>();
1684  model->SetText("ABCDE");
1685  EXPECT_TRUE(model->SetSelection(TextRange(5)));
1686  EXPECT_FALSE(model->SelectToEnd());
1687  EXPECT_EQ(model->selection(), TextRange(5));
1688  EXPECT_EQ(model->composing_range(), TextRange(0));
1689  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1690 }
1691 
1692 TEST(TextInputModel, MoveCursorToEndSelection) {
1693  auto model = std::make_unique<TextInputModel>();
1694  model->SetText("ABCDE");
1695  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
1696  EXPECT_TRUE(model->MoveCursorToEnd());
1697  EXPECT_EQ(model->selection(), TextRange(5));
1698  EXPECT_EQ(model->composing_range(), TextRange(0));
1699  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1700 }
1701 
1702 TEST(TextInputModel, SelectToEndSelection) {
1703  auto model = std::make_unique<TextInputModel>();
1704  model->SetText("ABCDE");
1705  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
1706  EXPECT_TRUE(model->SelectToEnd());
1707  EXPECT_EQ(model->selection(), TextRange(1, 5));
1708  EXPECT_EQ(model->composing_range(), TextRange(0));
1709  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1710 }
1711 
1712 TEST(TextInputModel, MoveCursorToEndReverseSelection) {
1713  auto model = std::make_unique<TextInputModel>();
1714  model->SetText("ABCDE");
1715  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
1716  EXPECT_TRUE(model->MoveCursorToEnd());
1717  EXPECT_EQ(model->selection(), TextRange(5));
1718  EXPECT_EQ(model->composing_range(), TextRange(0));
1719  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1720 }
1721 
1722 TEST(TextInputModel, SelectToEndReverseSelection) {
1723  auto model = std::make_unique<TextInputModel>();
1724  model->SetText("ABCDE");
1725  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
1726  EXPECT_TRUE(model->SelectToEnd());
1727  EXPECT_EQ(model->selection(), TextRange(4, 5));
1728  EXPECT_EQ(model->composing_range(), TextRange(0));
1729  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1730 }
1731 
1732 TEST(TextInputModel, MoveCursorToEndStartComposing) {
1733  auto model = std::make_unique<TextInputModel>();
1734  model->SetText("ABCDE");
1735  model->BeginComposing();
1736  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
1737  EXPECT_TRUE(model->MoveCursorToEnd());
1738  EXPECT_EQ(model->selection(), TextRange(4));
1739  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1740  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1741 }
1742 
1743 TEST(TextInputModel, SelectToEndStartComposing) {
1744  auto model = std::make_unique<TextInputModel>();
1745  model->SetText("ABCDE");
1746  model->BeginComposing();
1747  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
1748  EXPECT_TRUE(model->SelectToEnd());
1749  EXPECT_EQ(model->selection(), TextRange(1, 4));
1750  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1751  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1752 }
1753 
1754 TEST(TextInputModel, MoveCursorToEndStartReverseComposing) {
1755  auto model = std::make_unique<TextInputModel>();
1756  model->SetText("ABCDE");
1757  model->BeginComposing();
1758  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
1759  EXPECT_TRUE(model->MoveCursorToEnd());
1760  EXPECT_EQ(model->selection(), TextRange(4));
1761  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1762  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1763 }
1764 
1765 TEST(TextInputModel, SelectToEndStartReverseComposing) {
1766  auto model = std::make_unique<TextInputModel>();
1767  model->SetText("ABCDE");
1768  model->BeginComposing();
1769  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0));
1770  EXPECT_TRUE(model->SelectToEnd());
1771  EXPECT_EQ(model->selection(), TextRange(1, 4));
1772  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1773  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1774 }
1775 
1776 TEST(TextInputModel, MoveCursorToEndMiddleComposing) {
1777  auto model = std::make_unique<TextInputModel>();
1778  model->SetText("ABCDE");
1779  model->BeginComposing();
1780  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
1781  EXPECT_TRUE(model->MoveCursorToEnd());
1782  EXPECT_EQ(model->selection(), TextRange(4));
1783  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1784  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1785 }
1786 
1787 TEST(TextInputModel, SelectToEndMiddleComposing) {
1788  auto model = std::make_unique<TextInputModel>();
1789  model->SetText("ABCDE");
1790  model->BeginComposing();
1791  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1));
1792  EXPECT_TRUE(model->SelectToEnd());
1793  EXPECT_EQ(model->selection(), TextRange(2, 4));
1794  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1795  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1796 }
1797 
1798 TEST(TextInputModel, MoveCursorToEndMiddleReverseComposing) {
1799  auto model = std::make_unique<TextInputModel>();
1800  model->SetText("ABCDE");
1801  model->BeginComposing();
1802  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 1));
1803  EXPECT_TRUE(model->MoveCursorToEnd());
1804  EXPECT_EQ(model->selection(), TextRange(4));
1805  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1806  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1807 }
1808 
1809 TEST(TextInputModel, SelectToEndMiddleReverseComposing) {
1810  auto model = std::make_unique<TextInputModel>();
1811  model->SetText("ABCDE");
1812  model->BeginComposing();
1813  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 1));
1814  EXPECT_TRUE(model->SelectToEnd());
1815  EXPECT_EQ(model->selection(), TextRange(2, 4));
1816  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1817  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1818 }
1819 
1820 TEST(TextInputModel, MoveCursorToEndEndComposing) {
1821  auto model = std::make_unique<TextInputModel>();
1822  model->SetText("ABCDE");
1823  model->BeginComposing();
1824  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3));
1825  EXPECT_FALSE(model->MoveCursorToEnd());
1826  EXPECT_EQ(model->selection(), TextRange(4));
1827  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1828  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1829 }
1830 
1831 TEST(TextInputModel, SelectToEndEndComposing) {
1832  auto model = std::make_unique<TextInputModel>();
1833  model->SetText("ABCDE");
1834  model->BeginComposing();
1835  EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3));
1836  EXPECT_FALSE(model->SelectToEnd());
1837  EXPECT_EQ(model->selection(), TextRange(4));
1838  EXPECT_EQ(model->composing_range(), TextRange(1, 4));
1839  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1840 }
1841 
1842 TEST(TextInputModel, MoveCursorToEndEndReverseComposing) {
1843  auto model = std::make_unique<TextInputModel>();
1844  model->SetText("ABCDE");
1845  model->BeginComposing();
1846  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3));
1847  EXPECT_FALSE(model->MoveCursorToEnd());
1848  EXPECT_EQ(model->selection(), TextRange(4));
1849  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1850  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1851 }
1852 
1853 TEST(TextInputModel, SelectToEndEndReverseComposing) {
1854  auto model = std::make_unique<TextInputModel>();
1855  model->SetText("ABCDE");
1856  model->BeginComposing();
1857  EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3));
1858  EXPECT_FALSE(model->SelectToEnd());
1859  EXPECT_EQ(model->selection(), TextRange(4));
1860  EXPECT_EQ(model->composing_range(), TextRange(4, 1));
1861  EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
1862 }
1863 
1864 TEST(TextInputModel, GetCursorOffset) {
1865  auto model = std::make_unique<TextInputModel>();
1866  // These characters take 1, 2, 3 and 4 bytes in UTF-8.
1867  model->SetText("$ยขโ‚ฌ๐ˆ");
1868  EXPECT_TRUE(model->SetSelection(TextRange(0)));
1869  EXPECT_EQ(model->GetCursorOffset(), 0);
1870  EXPECT_TRUE(model->MoveCursorForward());
1871  EXPECT_EQ(model->GetCursorOffset(), 1);
1872  EXPECT_TRUE(model->MoveCursorForward());
1873  EXPECT_EQ(model->GetCursorOffset(), 3);
1874  EXPECT_TRUE(model->MoveCursorForward());
1875  EXPECT_EQ(model->GetCursorOffset(), 6);
1876  EXPECT_TRUE(model->MoveCursorForward());
1877  EXPECT_EQ(model->GetCursorOffset(), 10);
1878 }
1879 
1880 TEST(TextInputModel, GetCursorOffsetSelection) {
1881  auto model = std::make_unique<TextInputModel>();
1882  model->SetText("ABCDE");
1883  EXPECT_TRUE(model->SetSelection(TextRange(1, 4)));
1884  EXPECT_EQ(model->GetCursorOffset(), 4);
1885 }
1886 
1887 TEST(TextInputModel, GetCursorOffsetReverseSelection) {
1888  auto model = std::make_unique<TextInputModel>();
1889  model->SetText("ABCDE");
1890  EXPECT_TRUE(model->SetSelection(TextRange(4, 1)));
1891  EXPECT_EQ(model->GetCursorOffset(), 1);
1892 }
1893 
1894 } // namespace flutter
TEST(FlutterEngineTest, CreateDestroy)