@@ -915,6 +915,145 @@ TEST(TextInputModel, DeleteSurroundingReverseSelection) {
915915 EXPECT_STREQ (model->GetText ().c_str (), " ABCE" );
916916}
917917
918+ TEST (TextInputModel, DeleteSurroundingAtCursorSurrogatePairFirst) {
919+ auto model = std::make_unique<TextInputModel>();
920+ // U+1F600 occupies two UTF-16 code units but counts as one character.
921+ model->SetText (" \U0001F600 ab" );
922+ EXPECT_TRUE (model->SetSelection (TextRange (0 )));
923+ // Deleting two characters must delete the whole pair and the 'a' that
924+ // follows it.
925+ EXPECT_TRUE (model->DeleteSurrounding (0 , 2 ));
926+ EXPECT_EQ (model->selection (), TextRange (0 ));
927+ EXPECT_STREQ (model->GetText ().c_str (), " b" );
928+ }
929+
930+ TEST (TextInputModel, DeleteSurroundingAtCursorSurrogatePairNotFirst) {
931+ auto model = std::make_unique<TextInputModel>();
932+ // U+1F600 occupies two UTF-16 code units but counts as one character.
933+ model->SetText (" a\U0001F600 b" );
934+ EXPECT_TRUE (model->SetSelection (TextRange (0 )));
935+ // Deleting two characters must delete the 'a' and the whole pair that
936+ // follows it.
937+ EXPECT_TRUE (model->DeleteSurrounding (0 , 2 ));
938+ EXPECT_EQ (model->selection (), TextRange (0 ));
939+ EXPECT_STREQ (model->GetText ().c_str (), " b" );
940+ }
941+
942+ TEST (TextInputModel, DeleteSurroundingAfterCursorSplitPairAtComposingEnd) {
943+ auto model = std::make_unique<TextInputModel>();
944+ // The composing range ends between the two halves of U+1F600.
945+ EXPECT_TRUE (model->SetText (" a\U0001F600 bcd" , TextRange (1 ), TextRange (1 , 2 )));
946+ EXPECT_FALSE (model->DeleteSurrounding (1 , 1 ));
947+ EXPECT_STREQ (model->GetText ().c_str (), " a\U0001F600 bcd" );
948+ EXPECT_EQ (model->composing_range (), TextRange (1 , 2 ));
949+ }
950+
951+ TEST (TextInputModel, DeleteSurroundingAtCursorSplitPairAtComposingEnd) {
952+ auto model = std::make_unique<TextInputModel>();
953+ // The cursor sits on the leading surrogate, and the composing range ends
954+ // between the two halves.
955+ EXPECT_TRUE (model->SetText (" a\U0001F600 bcd" , TextRange (1 ), TextRange (1 , 2 )));
956+ // Only the leading half is inside the range. Verify we don't delete it and
957+ // leave an unpaired trailing surrogate that GetText cannot convert.
958+ EXPECT_FALSE (model->DeleteSurrounding (0 , 1 ));
959+ EXPECT_STREQ (model->GetText ().c_str (), " a\U0001F600 bcd" );
960+ EXPECT_EQ (model->composing_range (), TextRange (1 , 2 ));
961+ }
962+
963+ TEST (TextInputModel, DeleteSurroundingAtCursorStopsAtSplitPair) {
964+ auto model = std::make_unique<TextInputModel>();
965+ // "ab<U+1F600>cd" with a composing range ending between the two halves.
966+ EXPECT_TRUE (model->SetText (" ab\U0001F600 cd" , TextRange (1 ), TextRange (0 , 3 )));
967+ // The first character requested is whole and the second is not. Verify the
968+ // whole one is deleted and the walk stops.
969+ EXPECT_TRUE (model->DeleteSurrounding (0 , 2 ));
970+ EXPECT_STREQ (model->GetText ().c_str (), " a\U0001F600 cd" );
971+ EXPECT_EQ (model->composing_range (), TextRange (0 , 2 ));
972+ }
973+
974+ TEST (TextInputModel, DeleteSurroundingBeforeCursorSplitPairAtComposingStart) {
975+ auto model = std::make_unique<TextInputModel>();
976+ // The composing range starts between the two halves.
977+ EXPECT_TRUE (model->SetText (" \U0001F600 bcd" , TextRange (2 ), TextRange (1 , 5 )));
978+ EXPECT_FALSE (model->DeleteSurrounding (-1 , 1 ));
979+ EXPECT_STREQ (model->GetText ().c_str (), " \U0001F600 bcd" );
980+ EXPECT_EQ (model->composing_range (), TextRange (1 , 5 ));
981+ }
982+
983+ TEST (TextInputModel, DeleteSurroundingWithSelectionPastComposingRange) {
984+ auto model = std::make_unique<TextInputModel>();
985+ EXPECT_TRUE (model->SetText (" ABCDE" ));
986+ model->BeginComposing ();
987+ // The offset places the caret two characters beyond the end of the range.
988+ EXPECT_TRUE (model->SetComposingRange (TextRange (1 , 2 ), 4 ));
989+ EXPECT_EQ (model->selection (), TextRange (5 ));
990+ // Deletion is confined to the editable range, which the caret is already
991+ // past, so there is nothing after it to delete.
992+ EXPECT_FALSE (model->DeleteSurrounding (0 , 1 ));
993+ EXPECT_STREQ (model->GetText ().c_str (), " ABCDE" );
994+ EXPECT_EQ (model->composing_range (), TextRange (1 , 2 ));
995+ // Verify the walk backwards starts from the end of the range rather than from
996+ // the caret, deleting the character inside it.
997+ EXPECT_TRUE (model->DeleteSurrounding (-1 , 1 ));
998+ EXPECT_STREQ (model->GetText ().c_str (), " ACDE" );
999+ EXPECT_EQ (model->composing_range (), TextRange (1 , 1 ));
1000+ }
1001+
1002+ TEST (TextInputModel, DeleteSurroundingAfterCursorUnpairedSurrogateAtEnd) {
1003+ auto model = std::make_unique<TextInputModel>();
1004+ model->SetText (" ab" );
1005+ EXPECT_TRUE (model->SetSelection (TextRange (2 )));
1006+ // Composing text arrives from the input method as UTF-16 and is not
1007+ // well-formed, so the text can end in half a surrogate pair.
1008+ model->AddText (std::u16string (1 , 0xD83D ));
1009+ EXPECT_TRUE (model->SetSelection (TextRange (2 )));
1010+ // Stepping over that half runs the offset past the end of the text. Ensure
1011+ // the walk stops there rather than keep reading, and index out of bounds.
1012+ EXPECT_FALSE (model->DeleteSurrounding (2 , 1 ));
1013+ EXPECT_EQ (model->selection (), TextRange (2 ));
1014+ }
1015+
1016+ TEST (TextInputModel, DeleteSurroundingAtCursorUnpairedLeadingSurrogate) {
1017+ auto model = std::make_unique<TextInputModel>();
1018+ model->SetText (" ab" );
1019+ EXPECT_TRUE (model->SetSelection (TextRange (0 )));
1020+ // An unpaired leading surrogate followed by an ordinary character.
1021+ model->AddText (std::u16string (1 , 0xD83D ));
1022+ EXPECT_TRUE (model->SetSelection (TextRange (0 )));
1023+ // The surrogate is unpaired, so it is one character on its own. Verify we
1024+ // don't treat it as half of a pair and take the 'a' after it as well.
1025+ EXPECT_TRUE (model->DeleteSurrounding (0 , 1 ));
1026+ EXPECT_STREQ (model->GetText ().c_str (), " ab" );
1027+ EXPECT_EQ (model->selection (), TextRange (0 ));
1028+ }
1029+
1030+ TEST (TextInputModel, DeleteSurroundingAtCursorUnpairedSurrogateAtEnd) {
1031+ auto model = std::make_unique<TextInputModel>();
1032+ model->SetText (" ab" );
1033+ EXPECT_TRUE (model->SetSelection (TextRange (2 )));
1034+ model->AddText (std::u16string (1 , 0xD83D ));
1035+ EXPECT_TRUE (model->SetSelection (TextRange (2 )));
1036+ // The text ends in an unpaired surrogate, which is one whole character and
1037+ // must be deletable. Measuring it as half a pair leaves it permanently
1038+ // stuck, since two code units never fit before the end of the text.
1039+ EXPECT_TRUE (model->DeleteSurrounding (0 , 1 ));
1040+ EXPECT_STREQ (model->GetText ().c_str (), " ab" );
1041+ EXPECT_EQ (model->selection (), TextRange (2 ));
1042+ }
1043+
1044+ TEST (TextInputModel, DeleteSurroundingBeforeCursorUnpairedTrailingSurrogate) {
1045+ auto model = std::make_unique<TextInputModel>();
1046+ model->SetText (" ab" );
1047+ EXPECT_TRUE (model->SetSelection (TextRange (2 )));
1048+ // The mirror case: an unpaired trailing surrogate preceded by an ordinary
1049+ // character. Treating it as half of a pair would take the 'b' before it.
1050+ model->AddText (std::u16string (1 , 0xDE00 ));
1051+ EXPECT_TRUE (model->SetSelection (TextRange (3 )));
1052+ EXPECT_TRUE (model->DeleteSurrounding (-1 , 1 ));
1053+ EXPECT_STREQ (model->GetText ().c_str (), " ab" );
1054+ EXPECT_EQ (model->selection (), TextRange (2 ));
1055+ }
1056+
9181057TEST (TextInputModel, BackspaceStart) {
9191058 auto model = std::make_unique<TextInputModel>();
9201059 model->SetText (" ABCDE" );
0 commit comments