Chromium Code Reviews| 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 <limits.h> | 7 #include <limits.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 466 return static_cast<RenderTextHarfBuzz*>(GetRenderText()); | 466 return static_cast<RenderTextHarfBuzz*>(GetRenderText()); |
| 467 } | 467 } |
| 468 | 468 |
| 469 #if defined(OS_MACOSX) | 469 #if defined(OS_MACOSX) |
| 470 RenderTextMac* GetRenderTextMac() { | 470 RenderTextMac* GetRenderTextMac() { |
| 471 DCHECK_EQ(RENDER_TEXT_MAC, GetParam()); | 471 DCHECK_EQ(RENDER_TEXT_MAC, GetParam()); |
| 472 return static_cast<RenderTextMac*>(GetRenderText()); | 472 return static_cast<RenderTextMac*>(GetRenderText()); |
| 473 } | 473 } |
| 474 #endif | 474 #endif |
| 475 | 475 |
| 476 Rect GetSubstringBoundsUnionForRange(const Range& range) { | |
|
msw
2017/01/20 16:45:32
optional nit: drop |ForRange|
karandeepb
2017/01/23 00:20:25
Done.
| |
| 477 const std::vector<Rect> bounds = | |
| 478 render_text_->GetSubstringBoundsForTesting(range); | |
| 479 return std::accumulate(bounds.begin(), bounds.end(), Rect(), UnionRects); | |
| 480 } | |
| 481 | |
| 476 Rect GetSelectionBoundsUnion() { | 482 Rect GetSelectionBoundsUnion() { |
| 477 const std::vector<Rect> bounds = | 483 return GetSubstringBoundsUnionForRange(render_text_->selection()); |
| 478 render_text_->GetSubstringBoundsForTesting(render_text_->selection()); | |
| 479 return std::accumulate(bounds.begin(), bounds.end(), Rect(), UnionRects); | |
| 480 } | 484 } |
| 481 | 485 |
| 482 Canvas* canvas() { return &canvas_; } | 486 Canvas* canvas() { return &canvas_; } |
| 483 TestSkiaTextRenderer* renderer() { return &renderer_; } | 487 TestSkiaTextRenderer* renderer() { return &renderer_; } |
| 484 test::RenderTextTestApi* test_api() { return test_api_.get(); }; | 488 test::RenderTextTestApi* test_api() { return test_api_.get(); }; |
| 485 | 489 |
| 486 private: | 490 private: |
| 487 std::unique_ptr<RenderText> render_text_; | 491 std::unique_ptr<RenderText> render_text_; |
| 488 std::unique_ptr<test::RenderTextTestApi> test_api_; | 492 std::unique_ptr<test::RenderTextTestApi> test_api_; |
| 489 Canvas canvas_; | 493 Canvas canvas_; |
| (...skipping 3663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4153 if (i < kWordTwoStartIndex) { | 4157 if (i < kWordTwoStartIndex) { |
| 4154 VerifyDecoratedWordsAreEqual(expected_word_1, decorated_word); | 4158 VerifyDecoratedWordsAreEqual(expected_word_1, decorated_word); |
| 4155 EXPECT_TRUE(left_glyph_word_1.Contains(baseline_point)); | 4159 EXPECT_TRUE(left_glyph_word_1.Contains(baseline_point)); |
| 4156 } else { | 4160 } else { |
| 4157 VerifyDecoratedWordsAreEqual(expected_word_2, decorated_word); | 4161 VerifyDecoratedWordsAreEqual(expected_word_2, decorated_word); |
| 4158 EXPECT_TRUE(left_glyph_word_2.Contains(baseline_point)); | 4162 EXPECT_TRUE(left_glyph_word_2.Contains(baseline_point)); |
| 4159 } | 4163 } |
| 4160 } | 4164 } |
| 4161 } | 4165 } |
| 4162 | 4166 |
| 4167 // Test that GetDecoratedWordAtPoint behaves correctly for multiline text. | |
| 4168 TEST_P(RenderTextHarfBuzzTest, GetDecoratedWordAtPoint_Multiline) { | |
| 4169 const base::string16 text = ASCIIToUTF16("a b\n..\nc."); | |
|
msw
2017/01/20 16:45:32
nit: make at least one of these words multi-charac
karandeepb
2017/01/23 00:20:25
Done.
| |
| 4170 const size_t kWordOneIndex = 0; // Index of character 'a'. | |
| 4171 const size_t kWordTwoIndex = 2; // Index of character 'b'. | |
| 4172 const size_t kWordThreeIndex = 7; // Index of character 'c'. | |
| 4173 | |
| 4174 // Set up render text. | |
| 4175 RenderText* render_text = GetRenderText(); | |
| 4176 render_text->SetMultiline(true); | |
| 4177 render_text->SetDisplayRect(Rect(500, 500)); | |
| 4178 render_text->SetText(text); | |
| 4179 render_text->ApplyWeight(Font::Weight::SEMIBOLD, Range(0, 3)); | |
| 4180 render_text->ApplyStyle(UNDERLINE, true, Range(1, 7)); | |
| 4181 render_text->ApplyStyle(STRIKE, true, Range(1, 8)); | |
| 4182 render_text->ApplyStyle(ITALIC, true, Range(5, 8)); | |
| 4183 | |
| 4184 // Set up test expectations. | |
| 4185 const std::vector<RenderText::FontSpan> font_spans = | |
| 4186 render_text->GetFontSpansForTesting(); | |
| 4187 | |
| 4188 DecoratedText expected_word_1; | |
| 4189 expected_word_1.text = ASCIIToUTF16("a"); | |
| 4190 expected_word_1.attributes.push_back(CreateRangedAttribute( | |
| 4191 font_spans, 0, kWordOneIndex, Font::Weight::SEMIBOLD, 0)); | |
| 4192 const Rect left_glyph_word_1 = | |
| 4193 GetSubstringBoundsUnionForRange(Range(kWordOneIndex, kWordOneIndex + 1)); | |
| 4194 | |
| 4195 DecoratedText expected_word_2; | |
| 4196 expected_word_2.text = ASCIIToUTF16("b"); | |
| 4197 expected_word_2.attributes.push_back(CreateRangedAttribute( | |
| 4198 font_spans, 0, kWordTwoIndex, Font::Weight::SEMIBOLD, | |
| 4199 UNDERLINE_MASK | STRIKE_MASK)); | |
| 4200 const Rect left_glyph_word_2 = | |
| 4201 GetSubstringBoundsUnionForRange(Range(kWordTwoIndex, kWordTwoIndex + 1)); | |
| 4202 | |
| 4203 DecoratedText expected_word_3; | |
| 4204 expected_word_3.text = ASCIIToUTF16("c"); | |
| 4205 expected_word_3.attributes.push_back( | |
| 4206 CreateRangedAttribute(font_spans, 0, kWordThreeIndex, | |
| 4207 Font::Weight::NORMAL, STRIKE_MASK | ITALIC_MASK)); | |
| 4208 const Rect left_glyph_word_3 = GetSubstringBoundsUnionForRange( | |
| 4209 Range(kWordThreeIndex, kWordThreeIndex + 1)); | |
| 4210 | |
| 4211 DecoratedText decorated_word; | |
| 4212 Point baseline_point; | |
| 4213 { | |
| 4214 // Query to the left of the first line. | |
| 4215 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint( | |
| 4216 Point(-5, GetCursorYForTesting(0)), &decorated_word, &baseline_point)); | |
| 4217 VerifyDecoratedWordsAreEqual(expected_word_1, decorated_word); | |
| 4218 EXPECT_TRUE(left_glyph_word_1.Contains(baseline_point)); | |
| 4219 } | |
| 4220 { | |
| 4221 // Query on the second line. | |
| 4222 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint( | |
| 4223 Point(5, GetCursorYForTesting(1)), &decorated_word, &baseline_point)); | |
| 4224 VerifyDecoratedWordsAreEqual(expected_word_2, decorated_word); | |
| 4225 EXPECT_TRUE(left_glyph_word_2.Contains(baseline_point)); | |
| 4226 } | |
| 4227 { | |
| 4228 // Query at the center point of the character 'c'. | |
| 4229 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint( | |
| 4230 left_glyph_word_3.CenterPoint(), &decorated_word, &baseline_point)); | |
| 4231 VerifyDecoratedWordsAreEqual(expected_word_3, decorated_word); | |
| 4232 EXPECT_TRUE(left_glyph_word_3.Contains(baseline_point)); | |
| 4233 } | |
| 4234 { | |
| 4235 // Query to the right of the third line. | |
| 4236 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint( | |
| 4237 Point(505, GetCursorYForTesting(2)), &decorated_word, &baseline_point)); | |
| 4238 VerifyDecoratedWordsAreEqual(expected_word_3, decorated_word); | |
| 4239 EXPECT_TRUE(left_glyph_word_3.Contains(baseline_point)); | |
| 4240 } | |
| 4241 } | |
| 4242 | |
| 4163 // Verify the boolean return value of GetDecoratedWordAtPoint. | 4243 // Verify the boolean return value of GetDecoratedWordAtPoint. |
| 4164 TEST_P(RenderTextHarfBuzzTest, GetDecoratedWordAtPoint_Return) { | 4244 TEST_P(RenderTextHarfBuzzTest, GetDecoratedWordAtPoint_Return) { |
| 4165 RenderText* render_text = GetRenderText(); | 4245 RenderText* render_text = GetRenderText(); |
| 4166 render_text->SetText(ASCIIToUTF16("...")); | 4246 render_text->SetText(ASCIIToUTF16("...")); |
| 4167 | 4247 |
| 4168 DecoratedText decorated_word; | 4248 DecoratedText decorated_word; |
| 4169 Point baseline_point; | 4249 Point baseline_point; |
| 4170 | 4250 |
| 4171 // False should be returned, when the text does not contain any word. | 4251 // False should be returned, when the text does not contain any word. |
| 4172 Point query = | 4252 Point query = |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4283 ::testing::Values(RENDER_TEXT_HARFBUZZ), | 4363 ::testing::Values(RENDER_TEXT_HARFBUZZ), |
| 4284 PrintRenderTextBackend()); | 4364 PrintRenderTextBackend()); |
| 4285 #endif | 4365 #endif |
| 4286 | 4366 |
| 4287 INSTANTIATE_TEST_CASE_P(, | 4367 INSTANTIATE_TEST_CASE_P(, |
| 4288 RenderTextHarfBuzzTest, | 4368 RenderTextHarfBuzzTest, |
| 4289 ::testing::Values(RENDER_TEXT_HARFBUZZ), | 4369 ::testing::Values(RENDER_TEXT_HARFBUZZ), |
| 4290 PrintRenderTextBackend()); | 4370 PrintRenderTextBackend()); |
| 4291 | 4371 |
| 4292 } // namespace gfx | 4372 } // namespace gfx |
| OLD | NEW |