| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/gfx/render_text.h" | 5 #include "ui/gfx/render_text.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 // Various weak, LTR, RTL, and Bidi string cases with three characters each. | 33 // Various weak, LTR, RTL, and Bidi string cases with three characters each. |
| 34 const wchar_t kWeak[] = L" . "; | 34 const wchar_t kWeak[] = L" . "; |
| 35 const wchar_t kLtr[] = L"abc"; | 35 const wchar_t kLtr[] = L"abc"; |
| 36 const wchar_t kLtrRtl[] = L"a" L"\x5d0\x5d1"; | 36 const wchar_t kLtrRtl[] = L"a" L"\x5d0\x5d1"; |
| 37 const wchar_t kLtrRtlLtr[] = L"a" L"\x5d1" L"b"; | 37 const wchar_t kLtrRtlLtr[] = L"a" L"\x5d1" L"b"; |
| 38 const wchar_t kRtl[] = L"\x5d0\x5d1\x5d2"; | 38 const wchar_t kRtl[] = L"\x5d0\x5d1\x5d2"; |
| 39 const wchar_t kRtlLtr[] = L"\x5d0\x5d1" L"a"; | 39 const wchar_t kRtlLtr[] = L"\x5d0\x5d1" L"a"; |
| 40 const wchar_t kRtlLtrRtl[] = L"\x5d0" L"a" L"\x5d1"; | 40 const wchar_t kRtlLtrRtl[] = L"\x5d0" L"a" L"\x5d1"; |
| 41 | 41 |
| 42 // Checks whether |range| contains |index|. This is not the same as calling | 42 // Checks whether |range| contains |index|. This is not the same as calling |
| 43 // |range.Contains(ui::Range(index))| - as that would return true when | 43 // |range.Contains(gfx::Range(index))| - as that would return true when |
| 44 // |index| == |range.end()|. | 44 // |index| == |range.end()|. |
| 45 bool IndexInRange(const ui::Range& range, size_t index) { | 45 bool IndexInRange(const gfx::Range& range, size_t index) { |
| 46 return index >= range.start() && index < range.end(); | 46 return index >= range.start() && index < range.end(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 base::string16 GetSelectedText(RenderText* render_text) { | 49 base::string16 GetSelectedText(RenderText* render_text) { |
| 50 return render_text->text().substr(render_text->selection().GetMin(), | 50 return render_text->text().substr(render_text->selection().GetMin(), |
| 51 render_text->selection().length()); | 51 render_text->selection().length()); |
| 52 } | 52 } |
| 53 | 53 |
| 54 // A test utility function to set the application default text direction. | 54 // A test utility function to set the application default text direction. |
| 55 void SetRTL(bool rtl) { | 55 void SetRTL(bool rtl) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 if (i >= 1) | 117 if (i >= 1) |
| 118 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true)); | 118 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true)); |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 | 121 |
| 122 TEST_F(RenderTextTest, ApplyColorAndStyle) { | 122 TEST_F(RenderTextTest, ApplyColorAndStyle) { |
| 123 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 123 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 124 render_text->SetText(ASCIIToUTF16("012345678")); | 124 render_text->SetText(ASCIIToUTF16("012345678")); |
| 125 | 125 |
| 126 // Apply a ranged color and style and check the resulting breaks. | 126 // Apply a ranged color and style and check the resulting breaks. |
| 127 render_text->ApplyColor(SK_ColorRED, ui::Range(1, 4)); | 127 render_text->ApplyColor(SK_ColorRED, gfx::Range(1, 4)); |
| 128 render_text->ApplyStyle(BOLD, true, ui::Range(2, 5)); | 128 render_text->ApplyStyle(BOLD, true, gfx::Range(2, 5)); |
| 129 std::vector<std::pair<size_t, SkColor> > expected_color; | 129 std::vector<std::pair<size_t, SkColor> > expected_color; |
| 130 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK)); | 130 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK)); |
| 131 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED)); | 131 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED)); |
| 132 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK)); | 132 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK)); |
| 133 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color)); | 133 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color)); |
| 134 std::vector<std::pair<size_t, bool> > expected_style; | 134 std::vector<std::pair<size_t, bool> > expected_style; |
| 135 expected_style.push_back(std::pair<size_t, bool>(0, false)); | 135 expected_style.push_back(std::pair<size_t, bool>(0, false)); |
| 136 expected_style.push_back(std::pair<size_t, bool>(2, true)); | 136 expected_style.push_back(std::pair<size_t, bool>(2, true)); |
| 137 expected_style.push_back(std::pair<size_t, bool>(5, false)); | 137 expected_style.push_back(std::pair<size_t, bool>(5, false)); |
| 138 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style)); | 138 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style)); |
| 139 | 139 |
| 140 // Ensure setting a color and style overrides the ranged colors and styles. | 140 // Ensure setting a color and style overrides the ranged colors and styles. |
| 141 render_text->SetColor(SK_ColorBLUE); | 141 render_text->SetColor(SK_ColorBLUE); |
| 142 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE)); | 142 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE)); |
| 143 render_text->SetStyle(BOLD, false); | 143 render_text->SetStyle(BOLD, false); |
| 144 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false)); | 144 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false)); |
| 145 | 145 |
| 146 // Apply a color and style over the text end and check the resulting breaks. | 146 // Apply a color and style over the text end and check the resulting breaks. |
| 147 // (INT_MAX should be used instead of the text length for the range end) | 147 // (INT_MAX should be used instead of the text length for the range end) |
| 148 const size_t text_length = render_text->text().length(); | 148 const size_t text_length = render_text->text().length(); |
| 149 render_text->ApplyColor(SK_ColorRED, ui::Range(0, text_length)); | 149 render_text->ApplyColor(SK_ColorRED, gfx::Range(0, text_length)); |
| 150 render_text->ApplyStyle(BOLD, true, ui::Range(2, text_length)); | 150 render_text->ApplyStyle(BOLD, true, gfx::Range(2, text_length)); |
| 151 std::vector<std::pair<size_t, SkColor> > expected_color_end; | 151 std::vector<std::pair<size_t, SkColor> > expected_color_end; |
| 152 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED)); | 152 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED)); |
| 153 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end)); | 153 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end)); |
| 154 std::vector<std::pair<size_t, bool> > expected_style_end; | 154 std::vector<std::pair<size_t, bool> > expected_style_end; |
| 155 expected_style_end.push_back(std::pair<size_t, bool>(0, false)); | 155 expected_style_end.push_back(std::pair<size_t, bool>(0, false)); |
| 156 expected_style_end.push_back(std::pair<size_t, bool>(2, true)); | 156 expected_style_end.push_back(std::pair<size_t, bool>(2, true)); |
| 157 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end)); | 157 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end)); |
| 158 | 158 |
| 159 // Ensure ranged values adjust to accommodate text length changes. | 159 // Ensure ranged values adjust to accommodate text length changes. |
| 160 render_text->ApplyStyle(ITALIC, true, ui::Range(0, 2)); | 160 render_text->ApplyStyle(ITALIC, true, gfx::Range(0, 2)); |
| 161 render_text->ApplyStyle(ITALIC, true, ui::Range(3, 6)); | 161 render_text->ApplyStyle(ITALIC, true, gfx::Range(3, 6)); |
| 162 render_text->ApplyStyle(ITALIC, true, ui::Range(7, text_length)); | 162 render_text->ApplyStyle(ITALIC, true, gfx::Range(7, text_length)); |
| 163 std::vector<std::pair<size_t, bool> > expected_italic; | 163 std::vector<std::pair<size_t, bool> > expected_italic; |
| 164 expected_italic.push_back(std::pair<size_t, bool>(0, true)); | 164 expected_italic.push_back(std::pair<size_t, bool>(0, true)); |
| 165 expected_italic.push_back(std::pair<size_t, bool>(2, false)); | 165 expected_italic.push_back(std::pair<size_t, bool>(2, false)); |
| 166 expected_italic.push_back(std::pair<size_t, bool>(3, true)); | 166 expected_italic.push_back(std::pair<size_t, bool>(3, true)); |
| 167 expected_italic.push_back(std::pair<size_t, bool>(6, false)); | 167 expected_italic.push_back(std::pair<size_t, bool>(6, false)); |
| 168 expected_italic.push_back(std::pair<size_t, bool>(7, true)); | 168 expected_italic.push_back(std::pair<size_t, bool>(7, true)); |
| 169 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic)); | 169 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic)); |
| 170 | 170 |
| 171 // Truncating the text should trim any corresponding breaks. | 171 // Truncating the text should trim any corresponding breaks. |
| 172 render_text->SetText(ASCIIToUTF16("0123456")); | 172 render_text->SetText(ASCIIToUTF16("0123456")); |
| 173 expected_italic.resize(4); | 173 expected_italic.resize(4); |
| 174 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic)); | 174 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic)); |
| 175 render_text->SetText(ASCIIToUTF16("01234")); | 175 render_text->SetText(ASCIIToUTF16("01234")); |
| 176 expected_italic.resize(3); | 176 expected_italic.resize(3); |
| 177 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic)); | 177 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic)); |
| 178 | 178 |
| 179 // Appending text should extend the terminal styles without changing breaks. | 179 // Appending text should extend the terminal styles without changing breaks. |
| 180 render_text->SetText(ASCIIToUTF16("012345678")); | 180 render_text->SetText(ASCIIToUTF16("012345678")); |
| 181 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic)); | 181 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic)); |
| 182 } | 182 } |
| 183 | 183 |
| 184 #if defined(OS_LINUX) | 184 #if defined(OS_LINUX) |
| 185 TEST_F(RenderTextTest, PangoAttributes) { | 185 TEST_F(RenderTextTest, PangoAttributes) { |
| 186 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 186 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 187 render_text->SetText(ASCIIToUTF16("012345678")); | 187 render_text->SetText(ASCIIToUTF16("012345678")); |
| 188 | 188 |
| 189 // Apply ranged BOLD/ITALIC styles and check the resulting Pango attributes. | 189 // Apply ranged BOLD/ITALIC styles and check the resulting Pango attributes. |
| 190 render_text->ApplyStyle(BOLD, true, ui::Range(2, 4)); | 190 render_text->ApplyStyle(BOLD, true, gfx::Range(2, 4)); |
| 191 render_text->ApplyStyle(ITALIC, true, ui::Range(1, 3)); | 191 render_text->ApplyStyle(ITALIC, true, gfx::Range(1, 3)); |
| 192 | 192 |
| 193 struct { | 193 struct { |
| 194 int start; | 194 int start; |
| 195 int end; | 195 int end; |
| 196 bool bold; | 196 bool bold; |
| 197 bool italic; | 197 bool italic; |
| 198 } cases[] = { | 198 } cases[] = { |
| 199 { 0, 1, false, false }, | 199 { 0, 1, false, false }, |
| 200 { 1, 2, false, true }, | 200 { 1, 2, false, true }, |
| 201 { 2, 3, true, true }, | 201 { 2, 3, true, true }, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 232 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac | 232 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac |
| 233 // does not implement this yet. http://crbug.com/131618 | 233 // does not implement this yet. http://crbug.com/131618 |
| 234 #if !defined(OS_MACOSX) | 234 #if !defined(OS_MACOSX) |
| 235 void TestVisualCursorMotionInObscuredField(RenderText* render_text, | 235 void TestVisualCursorMotionInObscuredField(RenderText* render_text, |
| 236 const base::string16& text, | 236 const base::string16& text, |
| 237 bool select) { | 237 bool select) { |
| 238 ASSERT_TRUE(render_text->obscured()); | 238 ASSERT_TRUE(render_text->obscured()); |
| 239 render_text->SetText(text); | 239 render_text->SetText(text); |
| 240 int len = text.length(); | 240 int len = text.length(); |
| 241 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, select); | 241 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, select); |
| 242 EXPECT_EQ(SelectionModel(ui::Range(select ? 0 : len, len), CURSOR_FORWARD), | 242 EXPECT_EQ(SelectionModel(gfx::Range(select ? 0 : len, len), CURSOR_FORWARD), |
| 243 render_text->selection_model()); | 243 render_text->selection_model()); |
| 244 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, select); | 244 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, select); |
| 245 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model()); | 245 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model()); |
| 246 for (int j = 1; j <= len; ++j) { | 246 for (int j = 1; j <= len; ++j) { |
| 247 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, select); | 247 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, select); |
| 248 EXPECT_EQ(SelectionModel(ui::Range(select ? 0 : j, j), CURSOR_BACKWARD), | 248 EXPECT_EQ(SelectionModel(gfx::Range(select ? 0 : j, j), CURSOR_BACKWARD), |
| 249 render_text->selection_model()); | 249 render_text->selection_model()); |
| 250 } | 250 } |
| 251 for (int j = len - 1; j >= 0; --j) { | 251 for (int j = len - 1; j >= 0; --j) { |
| 252 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, select); | 252 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, select); |
| 253 EXPECT_EQ(SelectionModel(ui::Range(select ? 0 : j, j), CURSOR_FORWARD), | 253 EXPECT_EQ(SelectionModel(gfx::Range(select ? 0 : j, j), CURSOR_FORWARD), |
| 254 render_text->selection_model()); | 254 render_text->selection_model()); |
| 255 } | 255 } |
| 256 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, select); | 256 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, select); |
| 257 EXPECT_EQ(SelectionModel(ui::Range(select ? 0 : len, len), CURSOR_FORWARD), | 257 EXPECT_EQ(SelectionModel(gfx::Range(select ? 0 : len, len), CURSOR_FORWARD), |
| 258 render_text->selection_model()); | 258 render_text->selection_model()); |
| 259 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, select); | 259 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, select); |
| 260 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model()); | 260 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model()); |
| 261 } | 261 } |
| 262 | 262 |
| 263 TEST_F(RenderTextTest, ObscuredText) { | 263 TEST_F(RenderTextTest, ObscuredText) { |
| 264 const base::string16 seuss = ASCIIToUTF16("hop on pop"); | 264 const base::string16 seuss = ASCIIToUTF16("hop on pop"); |
| 265 const base::string16 no_seuss = ASCIIToUTF16("**********"); | 265 const base::string16 no_seuss = ASCIIToUTF16("**********"); |
| 266 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 266 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 267 | 267 |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 } | 871 } |
| 872 } | 872 } |
| 873 | 873 |
| 874 TEST_F(RenderTextTest, SelectAll) { | 874 TEST_F(RenderTextTest, SelectAll) { |
| 875 const wchar_t* const cases[] = | 875 const wchar_t* const cases[] = |
| 876 { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl }; | 876 { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl }; |
| 877 | 877 |
| 878 // Ensure that SelectAll respects the |reversed| argument regardless of | 878 // Ensure that SelectAll respects the |reversed| argument regardless of |
| 879 // application locale and text content directionality. | 879 // application locale and text content directionality. |
| 880 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 880 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 881 const SelectionModel expected_reversed(ui::Range(3, 0), CURSOR_FORWARD); | 881 const SelectionModel expected_reversed(gfx::Range(3, 0), CURSOR_FORWARD); |
| 882 const SelectionModel expected_forwards(ui::Range(0, 3), CURSOR_BACKWARD); | 882 const SelectionModel expected_forwards(gfx::Range(0, 3), CURSOR_BACKWARD); |
| 883 const bool was_rtl = base::i18n::IsRTL(); | 883 const bool was_rtl = base::i18n::IsRTL(); |
| 884 | 884 |
| 885 for (size_t i = 0; i < 2; ++i) { | 885 for (size_t i = 0; i < 2; ++i) { |
| 886 SetRTL(!base::i18n::IsRTL()); | 886 SetRTL(!base::i18n::IsRTL()); |
| 887 // Test that an empty string produces an empty selection model. | 887 // Test that an empty string produces an empty selection model. |
| 888 render_text->SetText(base::string16()); | 888 render_text->SetText(base::string16()); |
| 889 EXPECT_EQ(render_text->selection_model(), SelectionModel()); | 889 EXPECT_EQ(render_text->selection_model(), SelectionModel()); |
| 890 | 890 |
| 891 // Test the weak, LTR, RTL, and Bidi string cases. | 891 // Test the weak, LTR, RTL, and Bidi string cases. |
| 892 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(cases); j++) { | 892 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(cases); j++) { |
| 893 render_text->SetText(WideToUTF16(cases[j])); | 893 render_text->SetText(WideToUTF16(cases[j])); |
| 894 render_text->SelectAll(false); | 894 render_text->SelectAll(false); |
| 895 EXPECT_EQ(render_text->selection_model(), expected_forwards); | 895 EXPECT_EQ(render_text->selection_model(), expected_forwards); |
| 896 render_text->SelectAll(true); | 896 render_text->SelectAll(true); |
| 897 EXPECT_EQ(render_text->selection_model(), expected_reversed); | 897 EXPECT_EQ(render_text->selection_model(), expected_reversed); |
| 898 } | 898 } |
| 899 } | 899 } |
| 900 | 900 |
| 901 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); | 901 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); |
| 902 } | 902 } |
| 903 | 903 |
| 904 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) { | 904 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) { |
| 905 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 905 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 906 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); | 906 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); |
| 907 // Left arrow on select ranging (6, 4). | 907 // Left arrow on select ranging (6, 4). |
| 908 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false); | 908 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false); |
| 909 EXPECT_EQ(ui::Range(6), render_text->selection()); | 909 EXPECT_EQ(gfx::Range(6), render_text->selection()); |
| 910 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); | 910 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); |
| 911 EXPECT_EQ(ui::Range(4), render_text->selection()); | 911 EXPECT_EQ(gfx::Range(4), render_text->selection()); |
| 912 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); | 912 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); |
| 913 EXPECT_EQ(ui::Range(5), render_text->selection()); | 913 EXPECT_EQ(gfx::Range(5), render_text->selection()); |
| 914 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); | 914 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); |
| 915 EXPECT_EQ(ui::Range(6), render_text->selection()); | 915 EXPECT_EQ(gfx::Range(6), render_text->selection()); |
| 916 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true); | 916 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true); |
| 917 EXPECT_EQ(ui::Range(6, 5), render_text->selection()); | 917 EXPECT_EQ(gfx::Range(6, 5), render_text->selection()); |
| 918 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true); | 918 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true); |
| 919 EXPECT_EQ(ui::Range(6, 4), render_text->selection()); | 919 EXPECT_EQ(gfx::Range(6, 4), render_text->selection()); |
| 920 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); | 920 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); |
| 921 EXPECT_EQ(ui::Range(6), render_text->selection()); | 921 EXPECT_EQ(gfx::Range(6), render_text->selection()); |
| 922 | 922 |
| 923 // Right arrow on select ranging (4, 6). | 923 // Right arrow on select ranging (4, 6). |
| 924 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false); | 924 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false); |
| 925 EXPECT_EQ(ui::Range(0), render_text->selection()); | 925 EXPECT_EQ(gfx::Range(0), render_text->selection()); |
| 926 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); | 926 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); |
| 927 EXPECT_EQ(ui::Range(1), render_text->selection()); | 927 EXPECT_EQ(gfx::Range(1), render_text->selection()); |
| 928 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); | 928 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); |
| 929 EXPECT_EQ(ui::Range(2), render_text->selection()); | 929 EXPECT_EQ(gfx::Range(2), render_text->selection()); |
| 930 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); | 930 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); |
| 931 EXPECT_EQ(ui::Range(3), render_text->selection()); | 931 EXPECT_EQ(gfx::Range(3), render_text->selection()); |
| 932 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); | 932 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); |
| 933 EXPECT_EQ(ui::Range(5), render_text->selection()); | 933 EXPECT_EQ(gfx::Range(5), render_text->selection()); |
| 934 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); | 934 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); |
| 935 EXPECT_EQ(ui::Range(4), render_text->selection()); | 935 EXPECT_EQ(gfx::Range(4), render_text->selection()); |
| 936 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true); | 936 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true); |
| 937 EXPECT_EQ(ui::Range(4, 5), render_text->selection()); | 937 EXPECT_EQ(gfx::Range(4, 5), render_text->selection()); |
| 938 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true); | 938 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true); |
| 939 EXPECT_EQ(ui::Range(4, 6), render_text->selection()); | 939 EXPECT_EQ(gfx::Range(4, 6), render_text->selection()); |
| 940 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); | 940 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); |
| 941 EXPECT_EQ(ui::Range(4), render_text->selection()); | 941 EXPECT_EQ(gfx::Range(4), render_text->selection()); |
| 942 } | 942 } |
| 943 #endif // !defined(OS_MACOSX) | 943 #endif // !defined(OS_MACOSX) |
| 944 | 944 |
| 945 // TODO(xji): Make these work on Windows. | 945 // TODO(xji): Make these work on Windows. |
| 946 #if defined(OS_LINUX) | 946 #if defined(OS_LINUX) |
| 947 void MoveLeftRightByWordVerifier(RenderText* render_text, | 947 void MoveLeftRightByWordVerifier(RenderText* render_text, |
| 948 const wchar_t* str) { | 948 const wchar_t* str) { |
| 949 render_text->SetText(WideToUTF16(str)); | 949 render_text->SetText(WideToUTF16(str)); |
| 950 | 950 |
| 951 // Test moving by word from left ro right. | 951 // Test moving by word from left ro right. |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1218 | 1218 |
| 1219 const int plain_width = render_text->GetStringSize().width(); | 1219 const int plain_width = render_text->GetStringSize().width(); |
| 1220 EXPECT_GT(plain_width, 0); | 1220 EXPECT_GT(plain_width, 0); |
| 1221 | 1221 |
| 1222 // Apply a bold style and check that the new width is greater. | 1222 // Apply a bold style and check that the new width is greater. |
| 1223 render_text->SetStyle(BOLD, true); | 1223 render_text->SetStyle(BOLD, true); |
| 1224 const int bold_width = render_text->GetStringSize().width(); | 1224 const int bold_width = render_text->GetStringSize().width(); |
| 1225 EXPECT_GT(bold_width, plain_width); | 1225 EXPECT_GT(bold_width, plain_width); |
| 1226 | 1226 |
| 1227 // Now, apply a plain style over the first word only. | 1227 // Now, apply a plain style over the first word only. |
| 1228 render_text->ApplyStyle(BOLD, false, ui::Range(0, 5)); | 1228 render_text->ApplyStyle(BOLD, false, gfx::Range(0, 5)); |
| 1229 const int plain_bold_width = render_text->GetStringSize().width(); | 1229 const int plain_bold_width = render_text->GetStringSize().width(); |
| 1230 EXPECT_GT(plain_bold_width, plain_width); | 1230 EXPECT_GT(plain_bold_width, plain_width); |
| 1231 EXPECT_LT(plain_bold_width, bold_width); | 1231 EXPECT_LT(plain_bold_width, bold_width); |
| 1232 } | 1232 } |
| 1233 | 1233 |
| 1234 TEST_F(RenderTextTest, StringSizeHeight) { | 1234 TEST_F(RenderTextTest, StringSizeHeight) { |
| 1235 base::string16 cases[] = { | 1235 base::string16 cases[] = { |
| 1236 WideToUTF16(L"Hello World!"), // English | 1236 WideToUTF16(L"Hello World!"), // English |
| 1237 WideToUTF16(L"\x6328\x62f6"), // Japanese | 1237 WideToUTF16(L"\x6328\x62f6"), // Japanese |
| 1238 WideToUTF16(L"\x0915\x093f"), // Hindi | 1238 WideToUTF16(L"\x0915\x093f"), // Hindi |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1457 { 12, 12, 13 }, | 1457 { 12, 12, 13 }, |
| 1458 { 13, 13, 16 }, | 1458 { 13, 13, 16 }, |
| 1459 { 14, 13, 16 }, | 1459 { 14, 13, 16 }, |
| 1460 { 15, 13, 16 }, | 1460 { 15, 13, 16 }, |
| 1461 { 16, 13, 16 }, | 1461 { 16, 13, 16 }, |
| 1462 }; | 1462 }; |
| 1463 | 1463 |
| 1464 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 1464 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 1465 render_text->SetCursorPosition(cases[i].cursor); | 1465 render_text->SetCursorPosition(cases[i].cursor); |
| 1466 render_text->SelectWord(); | 1466 render_text->SelectWord(); |
| 1467 EXPECT_EQ(ui::Range(cases[i].selection_start, cases[i].selection_end), | 1467 EXPECT_EQ(gfx::Range(cases[i].selection_start, cases[i].selection_end), |
| 1468 render_text->selection()); | 1468 render_text->selection()); |
| 1469 } | 1469 } |
| 1470 } | 1470 } |
| 1471 | 1471 |
| 1472 // Make sure the last word is selected when the cursor is at text.length(). | 1472 // Make sure the last word is selected when the cursor is at text.length(). |
| 1473 TEST_F(RenderTextTest, LastWordSelected) { | 1473 TEST_F(RenderTextTest, LastWordSelected) { |
| 1474 const std::string kTestURL1 = "http://www.google.com"; | 1474 const std::string kTestURL1 = "http://www.google.com"; |
| 1475 const std::string kTestURL2 = "http://www.google.com/something/"; | 1475 const std::string kTestURL2 = "http://www.google.com/something/"; |
| 1476 | 1476 |
| 1477 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 1477 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1490 } | 1490 } |
| 1491 | 1491 |
| 1492 // When given a non-empty selection, SelectWord should expand the selection to | 1492 // When given a non-empty selection, SelectWord should expand the selection to |
| 1493 // nearest word boundaries. | 1493 // nearest word boundaries. |
| 1494 TEST_F(RenderTextTest, SelectMultipleWords) { | 1494 TEST_F(RenderTextTest, SelectMultipleWords) { |
| 1495 const std::string kTestURL = "http://www.google.com"; | 1495 const std::string kTestURL = "http://www.google.com"; |
| 1496 | 1496 |
| 1497 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 1497 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 1498 | 1498 |
| 1499 render_text->SetText(ASCIIToUTF16(kTestURL)); | 1499 render_text->SetText(ASCIIToUTF16(kTestURL)); |
| 1500 render_text->SelectRange(ui::Range(16, 20)); | 1500 render_text->SelectRange(gfx::Range(16, 20)); |
| 1501 render_text->SelectWord(); | 1501 render_text->SelectWord(); |
| 1502 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get())); | 1502 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get())); |
| 1503 EXPECT_FALSE(render_text->selection().is_reversed()); | 1503 EXPECT_FALSE(render_text->selection().is_reversed()); |
| 1504 | 1504 |
| 1505 // SelectWord should preserve the selection direction. | 1505 // SelectWord should preserve the selection direction. |
| 1506 render_text->SelectRange(ui::Range(20, 16)); | 1506 render_text->SelectRange(gfx::Range(20, 16)); |
| 1507 render_text->SelectWord(); | 1507 render_text->SelectWord(); |
| 1508 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get())); | 1508 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get())); |
| 1509 EXPECT_TRUE(render_text->selection().is_reversed()); | 1509 EXPECT_TRUE(render_text->selection().is_reversed()); |
| 1510 } | 1510 } |
| 1511 | 1511 |
| 1512 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac | 1512 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac |
| 1513 // does not implement this yet. http://crbug.com/131618 | 1513 // does not implement this yet. http://crbug.com/131618 |
| 1514 #if !defined(OS_MACOSX) | 1514 #if !defined(OS_MACOSX) |
| 1515 TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) { | 1515 TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) { |
| 1516 ASSERT_FALSE(base::i18n::IsRTL()); | 1516 ASSERT_FALSE(base::i18n::IsRTL()); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1635 L"\x633\x627" | 1635 L"\x633\x627" |
| 1636 }; | 1636 }; |
| 1637 | 1637 |
| 1638 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 1638 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 1639 render_text->set_selection_color(SK_ColorRED); | 1639 render_text->set_selection_color(SK_ColorRED); |
| 1640 Canvas canvas; | 1640 Canvas canvas; |
| 1641 | 1641 |
| 1642 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { | 1642 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { |
| 1643 render_text->SetText(WideToUTF16(kTestStrings[i])); | 1643 render_text->SetText(WideToUTF16(kTestStrings[i])); |
| 1644 const int expected_width = render_text->GetStringSize().width(); | 1644 const int expected_width = render_text->GetStringSize().width(); |
| 1645 render_text->MoveCursorTo(SelectionModel(ui::Range(0, 1), CURSOR_FORWARD)); | 1645 render_text->MoveCursorTo(SelectionModel(gfx::Range(0, 1), CURSOR_FORWARD)); |
| 1646 EXPECT_EQ(expected_width, render_text->GetStringSize().width()); | 1646 EXPECT_EQ(expected_width, render_text->GetStringSize().width()); |
| 1647 // Draw the text. It shouldn't hit any DCHECKs or crash. | 1647 // Draw the text. It shouldn't hit any DCHECKs or crash. |
| 1648 // See http://crbug.com/214150 | 1648 // See http://crbug.com/214150 |
| 1649 render_text->Draw(&canvas); | 1649 render_text->Draw(&canvas); |
| 1650 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD)); | 1650 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD)); |
| 1651 } | 1651 } |
| 1652 } | 1652 } |
| 1653 | 1653 |
| 1654 } // namespace gfx | 1654 } // namespace gfx |
| OLD | NEW |