Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Side by Side Diff: views/controls/textfield/textfield_views_model_unittest.cc

Issue 7461102: modification to RenderText for inheritance/SelectionModel (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: update per comments Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « views/controls/textfield/textfield_views_model.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/auto_reset.h" 5 #include "base/auto_reset.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/base/clipboard/clipboard.h" 10 #include "ui/base/clipboard/clipboard.h"
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 EXPECT_EQ(string16(), model.GetSelectedText()); 119 EXPECT_EQ(string16(), model.GetSelectedText());
120 model.SelectAll(); 120 model.SelectAll();
121 EXPECT_STR_EQ("HELLO", model.GetSelectedText()); 121 EXPECT_STR_EQ("HELLO", model.GetSelectedText());
122 // SelectAll should select towards the end. 122 // SelectAll should select towards the end.
123 ui::Range range; 123 ui::Range range;
124 model.GetSelectedRange(&range); 124 model.GetSelectedRange(&range);
125 EXPECT_EQ(0U, range.start()); 125 EXPECT_EQ(0U, range.start());
126 EXPECT_EQ(5U, range.end()); 126 EXPECT_EQ(5U, range.end());
127 127
128 // Select and move cursor 128 // Select and move cursor
129 model.MoveCursorTo(1U, false); 129 gfx::SelectionModel selection(1U);
130 model.MoveCursorTo(3U, true); 130 model.MoveCursorTo(selection);
131 selection.set_selection_end(3U);
132 model.MoveCursorTo(selection);
131 EXPECT_STR_EQ("EL", model.GetSelectedText()); 133 EXPECT_STR_EQ("EL", model.GetSelectedText());
132 model.MoveCursorLeft(gfx::CHARACTER_BREAK, false); 134 model.MoveCursorLeft(gfx::CHARACTER_BREAK, false);
133 EXPECT_EQ(1U, model.GetCursorPosition()); 135 EXPECT_EQ(1U, model.GetCursorPosition());
134 model.MoveCursorTo(1U, false); 136 selection.set_selection_end(1U);
135 model.MoveCursorTo(3U, true); 137 selection.set_selection_start(selection.selection_end());
138 model.MoveCursorTo(selection);
139 selection.set_selection_end(3U);
140 model.MoveCursorTo(selection);
136 model.MoveCursorRight(gfx::CHARACTER_BREAK, false); 141 model.MoveCursorRight(gfx::CHARACTER_BREAK, false);
137 EXPECT_EQ(3U, model.GetCursorPosition()); 142 EXPECT_EQ(3U, model.GetCursorPosition());
138 143
139 // Select all and move cursor 144 // Select all and move cursor
140 model.SelectAll(); 145 model.SelectAll();
141 model.MoveCursorLeft(gfx::CHARACTER_BREAK, false); 146 model.MoveCursorLeft(gfx::CHARACTER_BREAK, false);
142 EXPECT_EQ(0U, model.GetCursorPosition()); 147 EXPECT_EQ(0U, model.GetCursorPosition());
143 model.SelectAll(); 148 model.SelectAll();
144 model.MoveCursorRight(gfx::CHARACTER_BREAK, false); 149 model.MoveCursorRight(gfx::CHARACTER_BREAK, false);
145 EXPECT_EQ(5U, model.GetCursorPosition()); 150 EXPECT_EQ(5U, model.GetCursorPosition());
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 TEST_F(TextfieldViewsModelTest, SelectWordTest) { 329 TEST_F(TextfieldViewsModelTest, SelectWordTest) {
325 TextfieldViewsModel model(NULL); 330 TextfieldViewsModel model(NULL);
326 model.Append(ASCIIToUTF16(" HELLO !! WO RLD ")); 331 model.Append(ASCIIToUTF16(" HELLO !! WO RLD "));
327 332
328 // Test when cursor is at the beginning. 333 // Test when cursor is at the beginning.
329 model.MoveCursorLeft(gfx::LINE_BREAK, false); 334 model.MoveCursorLeft(gfx::LINE_BREAK, false);
330 model.SelectWord(); 335 model.SelectWord();
331 SelectWordTestVerifier(model, " ", 2U); 336 SelectWordTestVerifier(model, " ", 2U);
332 337
333 // Test when cursor is at the beginning of a word. 338 // Test when cursor is at the beginning of a word.
334 model.MoveCursorTo(2U, false); 339 gfx::SelectionModel selection(2U);
340 model.MoveCursorTo(selection);
335 model.SelectWord(); 341 model.SelectWord();
336 SelectWordTestVerifier(model, "HELLO", 7U); 342 SelectWordTestVerifier(model, "HELLO", 7U);
337 343
338 // Test when cursor is at the end of a word. 344 // Test when cursor is at the end of a word.
339 model.MoveCursorTo(15U, false); 345 selection.set_selection_end(15U);
346 selection.set_selection_start(selection.selection_end());
347 model.MoveCursorTo(selection);
340 model.SelectWord(); 348 model.SelectWord();
341 SelectWordTestVerifier(model, "WO", 15U); 349 SelectWordTestVerifier(model, "WO", 15U);
342 350
343 // Test when cursor is somewhere in a non-alph-numeric fragment. 351 // Test when cursor is somewhere in a non-alph-numeric fragment.
344 for (size_t cursor_pos = 8; cursor_pos < 13U; cursor_pos++) { 352 for (size_t cursor_pos = 8; cursor_pos < 13U; cursor_pos++) {
345 model.MoveCursorTo(cursor_pos, false); 353 selection.set_selection_end(cursor_pos);
354 selection.set_selection_start(selection.selection_end());
355 model.MoveCursorTo(selection);
346 model.SelectWord(); 356 model.SelectWord();
347 SelectWordTestVerifier(model, " !! ", 13U); 357 SelectWordTestVerifier(model, " !! ", 13U);
348 } 358 }
349 359
350 // Test when cursor is somewhere in a whitespace fragment. 360 // Test when cursor is somewhere in a whitespace fragment.
351 model.MoveCursorTo(17U, false); 361 selection.set_selection_end(17U);
362 selection.set_selection_start(selection.selection_end());
363 model.MoveCursorTo(selection);
352 model.SelectWord(); 364 model.SelectWord();
353 SelectWordTestVerifier(model, " ", 20U); 365 SelectWordTestVerifier(model, " ", 20U);
354 366
355 // Test when cursor is at the end. 367 // Test when cursor is at the end.
356 model.MoveCursorRight(gfx::LINE_BREAK, false); 368 model.MoveCursorRight(gfx::LINE_BREAK, false);
357 model.SelectWord(); 369 model.SelectWord();
358 SelectWordTestVerifier(model, " ", 24U); 370 SelectWordTestVerifier(model, " ", 24U);
359 } 371 }
360 372
361 TEST_F(TextfieldViewsModelTest, RangeTest) { 373 TEST_F(TextfieldViewsModelTest, RangeTest) {
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 composition_text_confirmed_or_cleared_ = false; 602 composition_text_confirmed_or_cleared_ = false;
591 EXPECT_STR_EQ("678678", model.GetText()); 603 EXPECT_STR_EQ("678678", model.GetText());
592 604
593 model.SetCompositionText(composition); 605 model.SetCompositionText(composition);
594 model.MoveCursorRight(gfx::LINE_BREAK, false); 606 model.MoveCursorRight(gfx::LINE_BREAK, false);
595 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 607 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
596 composition_text_confirmed_or_cleared_ = false; 608 composition_text_confirmed_or_cleared_ = false;
597 EXPECT_STR_EQ("678", model.GetText()); 609 EXPECT_STR_EQ("678", model.GetText());
598 610
599 model.SetCompositionText(composition); 611 model.SetCompositionText(composition);
600 model.MoveCursorTo(0, true); 612 gfx::SelectionModel sel(0);
613 sel.set_selection_start(model.render_text()->GetSelectionStart());
614 model.MoveCursorTo(sel);
601 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 615 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
602 composition_text_confirmed_or_cleared_ = false; 616 composition_text_confirmed_or_cleared_ = false;
603 EXPECT_STR_EQ("678678", model.GetText()); 617 EXPECT_STR_EQ("678678", model.GetText());
604 618
605 model.SetCompositionText(composition); 619 model.SetCompositionText(composition);
606 model.SelectRange(ui::Range(0, 3)); 620 model.SelectRange(ui::Range(0, 3));
607 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 621 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
608 composition_text_confirmed_or_cleared_ = false; 622 composition_text_confirmed_or_cleared_ = false;
609 EXPECT_STR_EQ("678", model.GetText()); 623 EXPECT_STR_EQ("678", model.GetText());
610 624
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 // Clear history 707 // Clear history
694 model.ClearEditHistory(); 708 model.ClearEditHistory();
695 EXPECT_FALSE(model.Undo()); 709 EXPECT_FALSE(model.Undo());
696 EXPECT_FALSE(model.Redo()); 710 EXPECT_FALSE(model.Redo());
697 EXPECT_STR_EQ("a", model.GetText()); 711 EXPECT_STR_EQ("a", model.GetText());
698 EXPECT_EQ(1U, model.GetCursorPosition()); 712 EXPECT_EQ(1U, model.GetCursorPosition());
699 713
700 // Delete =============================== 714 // Delete ===============================
701 model.SetText(ASCIIToUTF16("ABCDE")); 715 model.SetText(ASCIIToUTF16("ABCDE"));
702 model.ClearEditHistory(); 716 model.ClearEditHistory();
703 model.MoveCursorTo(2, false); 717 gfx::SelectionModel sel(2);
718 model.MoveCursorTo(sel);
704 EXPECT_TRUE(model.Delete()); 719 EXPECT_TRUE(model.Delete());
705 EXPECT_STR_EQ("ABDE", model.GetText()); 720 EXPECT_STR_EQ("ABDE", model.GetText());
706 model.MoveCursorLeft(gfx::LINE_BREAK, false); 721 model.MoveCursorLeft(gfx::LINE_BREAK, false);
707 EXPECT_TRUE(model.Delete()); 722 EXPECT_TRUE(model.Delete());
708 EXPECT_STR_EQ("BDE", model.GetText()); 723 EXPECT_STR_EQ("BDE", model.GetText());
709 EXPECT_TRUE(model.Undo()); 724 EXPECT_TRUE(model.Undo());
710 EXPECT_STR_EQ("ABDE", model.GetText()); 725 EXPECT_STR_EQ("ABDE", model.GetText());
711 EXPECT_EQ(0U, model.GetCursorPosition()); 726 EXPECT_EQ(0U, model.GetCursorPosition());
712 EXPECT_TRUE(model.Undo()); 727 EXPECT_TRUE(model.Undo());
713 EXPECT_STR_EQ("ABCDE", model.GetText()); 728 EXPECT_STR_EQ("ABCDE", model.GetText());
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 EXPECT_STR_EQ("www.youtube.com", model.GetText()); 802 EXPECT_STR_EQ("www.youtube.com", model.GetText());
788 EXPECT_EQ(5U, model.GetCursorPosition()); 803 EXPECT_EQ(5U, model.GetCursorPosition());
789 EXPECT_FALSE(model.Redo()); 804 EXPECT_FALSE(model.Redo());
790 } 805 }
791 806
792 TEST_F(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest) { 807 TEST_F(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest) {
793 TextfieldViewsModel model(NULL); 808 TextfieldViewsModel model(NULL);
794 model.SetText(ASCIIToUTF16("ABCDE")); 809 model.SetText(ASCIIToUTF16("ABCDE"));
795 EXPECT_FALSE(model.Redo()); // nothing to redo 810 EXPECT_FALSE(model.Redo()); // nothing to redo
796 // Cut 811 // Cut
797 model.MoveCursorTo(1, false); 812 gfx::SelectionModel sel(1);
798 model.MoveCursorTo(3, true); 813 model.MoveCursorTo(sel);
814 sel.set_selection_end(3);
815 model.MoveCursorTo(sel);
799 model.Cut(); 816 model.Cut();
800 EXPECT_STR_EQ("ADE", model.GetText()); 817 EXPECT_STR_EQ("ADE", model.GetText());
801 EXPECT_EQ(1U, model.GetCursorPosition()); 818 EXPECT_EQ(1U, model.GetCursorPosition());
802 EXPECT_TRUE(model.Undo()); 819 EXPECT_TRUE(model.Undo());
803 EXPECT_STR_EQ("ABCDE", model.GetText()); 820 EXPECT_STR_EQ("ABCDE", model.GetText());
804 EXPECT_EQ(3U, model.GetCursorPosition()); 821 EXPECT_EQ(3U, model.GetCursorPosition());
805 EXPECT_TRUE(model.Undo()); 822 EXPECT_TRUE(model.Undo());
806 EXPECT_STR_EQ("", model.GetText()); 823 EXPECT_STR_EQ("", model.GetText());
807 EXPECT_EQ(0U, model.GetCursorPosition()); 824 EXPECT_EQ(0U, model.GetCursorPosition());
808 EXPECT_FALSE(model.Undo()); // no more undo 825 EXPECT_FALSE(model.Undo()); // no more undo
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 // empty cut shouldn't create an edit. 891 // empty cut shouldn't create an edit.
875 EXPECT_TRUE(model.Undo()); 892 EXPECT_TRUE(model.Undo());
876 EXPECT_STR_EQ("ABCBCBCDE", model.GetText()); 893 EXPECT_STR_EQ("ABCBCBCDE", model.GetText());
877 EXPECT_EQ(3U, model.GetCursorPosition()); 894 EXPECT_EQ(3U, model.GetCursorPosition());
878 895
879 // Copy 896 // Copy
880 ResetModel(&model); 897 ResetModel(&model);
881 model.SetText(ASCIIToUTF16("12345")); 898 model.SetText(ASCIIToUTF16("12345"));
882 EXPECT_STR_EQ("12345", model.GetText()); 899 EXPECT_STR_EQ("12345", model.GetText());
883 EXPECT_EQ(0U, model.GetCursorPosition()); 900 EXPECT_EQ(0U, model.GetCursorPosition());
884 model.MoveCursorTo(1, false); 901 sel.set_selection_end(1);
885 model.MoveCursorTo(3, true); 902 sel.set_selection_start(sel.selection_end());
903 model.MoveCursorTo(sel);
904 sel.set_selection_end(3);
905 model.MoveCursorTo(sel);
886 model.Copy(); // Copy "23" 906 model.Copy(); // Copy "23"
887 EXPECT_STR_EQ("12345", model.GetText()); 907 EXPECT_STR_EQ("12345", model.GetText());
888 EXPECT_EQ(3U, model.GetCursorPosition()); 908 EXPECT_EQ(3U, model.GetCursorPosition());
889 model.Paste(); // Paste "23" into "23". 909 model.Paste(); // Paste "23" into "23".
890 EXPECT_STR_EQ("12345", model.GetText()); 910 EXPECT_STR_EQ("12345", model.GetText());
891 EXPECT_EQ(3U, model.GetCursorPosition()); 911 EXPECT_EQ(3U, model.GetCursorPosition());
892 model.Paste(); 912 model.Paste();
893 EXPECT_STR_EQ("1232345", model.GetText()); 913 EXPECT_STR_EQ("1232345", model.GetText());
894 EXPECT_EQ(5U, model.GetCursorPosition()); 914 EXPECT_EQ(5U, model.GetCursorPosition());
895 EXPECT_TRUE(model.Undo()); 915 EXPECT_TRUE(model.Undo());
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 EXPECT_EQ(5U, model.GetCursorPosition()); 1019 EXPECT_EQ(5U, model.GetCursorPosition());
1000 EXPECT_FALSE(model.Redo()); 1020 EXPECT_FALSE(model.Redo());
1001 } 1021 }
1002 1022
1003 TEST_F(TextfieldViewsModelTest, UndoRedo_ReplaceTest) { 1023 TEST_F(TextfieldViewsModelTest, UndoRedo_ReplaceTest) {
1004 // By Cursor 1024 // By Cursor
1005 { 1025 {
1006 SCOPED_TRACE("forward & insert by cursor"); 1026 SCOPED_TRACE("forward & insert by cursor");
1007 TextfieldViewsModel model(NULL); 1027 TextfieldViewsModel model(NULL);
1008 model.SetText(ASCIIToUTF16("abcd")); 1028 model.SetText(ASCIIToUTF16("abcd"));
1009 model.MoveCursorTo(1, false); 1029 gfx::SelectionModel sel(1);
1010 model.MoveCursorTo(3, true); 1030 model.MoveCursorTo(sel);
1031 sel.set_selection_end(3);
1032 model.MoveCursorTo(sel);
1011 RunInsertReplaceTest(model); 1033 RunInsertReplaceTest(model);
1012 } 1034 }
1013 { 1035 {
1014 SCOPED_TRACE("backward & insert by cursor"); 1036 SCOPED_TRACE("backward & insert by cursor");
1015 TextfieldViewsModel model(NULL); 1037 TextfieldViewsModel model(NULL);
1016 model.SetText(ASCIIToUTF16("abcd")); 1038 model.SetText(ASCIIToUTF16("abcd"));
1017 model.MoveCursorTo(3, false); 1039 gfx::SelectionModel sel(3);
1018 model.MoveCursorTo(1, true); 1040 model.MoveCursorTo(sel);
1041 sel.set_selection_end(1);
1042 model.MoveCursorTo(sel);
1019 RunInsertReplaceTest(model); 1043 RunInsertReplaceTest(model);
1020 } 1044 }
1021 { 1045 {
1022 SCOPED_TRACE("forward & overwrite by cursor"); 1046 SCOPED_TRACE("forward & overwrite by cursor");
1023 TextfieldViewsModel model(NULL); 1047 TextfieldViewsModel model(NULL);
1024 model.SetText(ASCIIToUTF16("abcd")); 1048 model.SetText(ASCIIToUTF16("abcd"));
1025 model.MoveCursorTo(1, false); 1049 gfx::SelectionModel sel(1);
1026 model.MoveCursorTo(3, true); 1050 model.MoveCursorTo(sel);
1051 sel.set_selection_end(3);
1052 model.MoveCursorTo(sel);
1027 RunOverwriteReplaceTest(model); 1053 RunOverwriteReplaceTest(model);
1028 } 1054 }
1029 { 1055 {
1030 SCOPED_TRACE("backward & overwrite by cursor"); 1056 SCOPED_TRACE("backward & overwrite by cursor");
1031 TextfieldViewsModel model(NULL); 1057 TextfieldViewsModel model(NULL);
1032 model.SetText(ASCIIToUTF16("abcd")); 1058 model.SetText(ASCIIToUTF16("abcd"));
1033 model.MoveCursorTo(3, false); 1059 gfx::SelectionModel sel(3);
1034 model.MoveCursorTo(1, true); 1060 model.MoveCursorTo(sel);
1061 sel.set_selection_end(1);
1062 model.MoveCursorTo(sel);
1035 RunOverwriteReplaceTest(model); 1063 RunOverwriteReplaceTest(model);
1036 } 1064 }
1037 // By SelectRange API 1065 // By SelectRange API
1038 { 1066 {
1039 SCOPED_TRACE("forward & insert by SelectRange"); 1067 SCOPED_TRACE("forward & insert by SelectRange");
1040 TextfieldViewsModel model(NULL); 1068 TextfieldViewsModel model(NULL);
1041 model.SetText(ASCIIToUTF16("abcd")); 1069 model.SetText(ASCIIToUTF16("abcd"));
1042 model.SelectRange(ui::Range(1, 3)); 1070 model.SelectRange(ui::Range(1, 3));
1043 RunInsertReplaceTest(model); 1071 RunInsertReplaceTest(model);
1044 } 1072 }
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
1136 EXPECT_TRUE(model.Undo()); 1164 EXPECT_TRUE(model.Undo());
1137 EXPECT_STR_EQ("ABCDE", model.GetText()); 1165 EXPECT_STR_EQ("ABCDE", model.GetText());
1138 EXPECT_TRUE(model.Redo()); 1166 EXPECT_TRUE(model.Redo());
1139 EXPECT_STR_EQ("1234", model.GetText()); 1167 EXPECT_STR_EQ("1234", model.GetText());
1140 EXPECT_FALSE(model.Redo()); 1168 EXPECT_FALSE(model.Redo());
1141 1169
1142 // TODO(oshima): We need MockInputMethod to test the behavior with IME. 1170 // TODO(oshima): We need MockInputMethod to test the behavior with IME.
1143 } 1171 }
1144 1172
1145 } // namespace views 1173 } // namespace views
OLDNEW
« no previous file with comments | « views/controls/textfield/textfield_views_model.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698