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

Side by Side Diff: ui/gfx/render_text_unittest.cc

Issue 2639493002: MacViews: Enable word lookup for selectable views::Labels and multi-line text. (Closed)
Patch Set: Address nits. Created 3 years, 11 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
« no previous file with comments | « ui/gfx/render_text_harfbuzz.cc ('k') | ui/views/controls/label.h » ('j') | 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) 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
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 GetSubstringBoundsUnion(const Range& range) {
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 GetSubstringBoundsUnion(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
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..\ncd.");
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, 9));
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 GetSubstringBoundsUnion(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 GetSubstringBoundsUnion(Range(kWordTwoIndex, kWordTwoIndex + 1));
4202
4203 DecoratedText expected_word_3;
4204 expected_word_3.text = ASCIIToUTF16("cd");
4205 expected_word_3.attributes.push_back(
4206 CreateRangedAttribute(font_spans, 0, kWordThreeIndex,
4207 Font::Weight::NORMAL, STRIKE_MASK | ITALIC_MASK));
4208 expected_word_3.attributes.push_back(CreateRangedAttribute(
4209 font_spans, 1, kWordThreeIndex + 1, Font::Weight::NORMAL, ITALIC_MASK));
4210
4211 const Rect left_glyph_word_3 =
4212 GetSubstringBoundsUnion(Range(kWordThreeIndex, kWordThreeIndex + 1));
4213
4214 DecoratedText decorated_word;
4215 Point baseline_point;
4216 {
4217 // Query to the left of the first line.
4218 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
4219 Point(-5, GetCursorYForTesting(0)), &decorated_word, &baseline_point));
4220 VerifyDecoratedWordsAreEqual(expected_word_1, decorated_word);
4221 EXPECT_TRUE(left_glyph_word_1.Contains(baseline_point));
4222 }
4223 {
4224 // Query on the second line.
4225 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
4226 Point(5, GetCursorYForTesting(1)), &decorated_word, &baseline_point));
4227 VerifyDecoratedWordsAreEqual(expected_word_2, decorated_word);
4228 EXPECT_TRUE(left_glyph_word_2.Contains(baseline_point));
4229 }
4230 {
4231 // Query at the center point of the character 'c'.
4232 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
4233 left_glyph_word_3.CenterPoint(), &decorated_word, &baseline_point));
4234 VerifyDecoratedWordsAreEqual(expected_word_3, decorated_word);
4235 EXPECT_TRUE(left_glyph_word_3.Contains(baseline_point));
4236 }
4237 {
4238 // Query to the right of the third line.
4239 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
4240 Point(505, GetCursorYForTesting(2)), &decorated_word, &baseline_point));
4241 VerifyDecoratedWordsAreEqual(expected_word_3, decorated_word);
4242 EXPECT_TRUE(left_glyph_word_3.Contains(baseline_point));
4243 }
4244 }
4245
4163 // Verify the boolean return value of GetDecoratedWordAtPoint. 4246 // Verify the boolean return value of GetDecoratedWordAtPoint.
4164 TEST_P(RenderTextHarfBuzzTest, GetDecoratedWordAtPoint_Return) { 4247 TEST_P(RenderTextHarfBuzzTest, GetDecoratedWordAtPoint_Return) {
4165 RenderText* render_text = GetRenderText(); 4248 RenderText* render_text = GetRenderText();
4166 render_text->SetText(ASCIIToUTF16("...")); 4249 render_text->SetText(ASCIIToUTF16("..."));
4167 4250
4168 DecoratedText decorated_word; 4251 DecoratedText decorated_word;
4169 Point baseline_point; 4252 Point baseline_point;
4170 4253
4171 // False should be returned, when the text does not contain any word. 4254 // False should be returned, when the text does not contain any word.
4172 Point query = 4255 Point query =
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
4283 ::testing::Values(RENDER_TEXT_HARFBUZZ), 4366 ::testing::Values(RENDER_TEXT_HARFBUZZ),
4284 PrintRenderTextBackend()); 4367 PrintRenderTextBackend());
4285 #endif 4368 #endif
4286 4369
4287 INSTANTIATE_TEST_CASE_P(, 4370 INSTANTIATE_TEST_CASE_P(,
4288 RenderTextHarfBuzzTest, 4371 RenderTextHarfBuzzTest,
4289 ::testing::Values(RENDER_TEXT_HARFBUZZ), 4372 ::testing::Values(RENDER_TEXT_HARFBUZZ),
4290 PrintRenderTextBackend()); 4373 PrintRenderTextBackend());
4291 4374
4292 } // namespace gfx 4375 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text_harfbuzz.cc ('k') | ui/views/controls/label.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698