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

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

Issue 2767163003: Vertically center multi-line RenderTextHarfBuzz that uses SetMinLineHeight(). (Closed)
Patch Set: respond to comments Created 3 years, 8 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') | 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) 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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 size_t DisplayIndexToTextIndex(size_t index) { 109 size_t DisplayIndexToTextIndex(size_t index) {
110 return render_text_->DisplayIndexToTextIndex(index); 110 return render_text_->DisplayIndexToTextIndex(index);
111 } 111 }
112 112
113 void EnsureLayout() { render_text_->EnsureLayout(); } 113 void EnsureLayout() { render_text_->EnsureLayout(); }
114 114
115 Vector2d GetAlignmentOffset(size_t line_number) { 115 Vector2d GetAlignmentOffset(size_t line_number) {
116 return render_text_->GetAlignmentOffset(line_number); 116 return render_text_->GetAlignmentOffset(line_number);
117 } 117 }
118 118
119 int GetDisplayTextBaseline() {
120 return render_text_->GetDisplayTextBaseline();
121 }
122
119 private: 123 private:
120 RenderText* render_text_; 124 RenderText* render_text_;
121 125
122 DISALLOW_COPY_AND_ASSIGN(RenderTextTestApi); 126 DISALLOW_COPY_AND_ASSIGN(RenderTextTestApi);
123 }; 127 };
124 128
125 } // namespace test 129 } // namespace test
126 130
127 namespace { 131 namespace {
128 132
(...skipping 4269 matching lines...) Expand 10 before | Expand all | Expand 10 after
4398 #endif 4402 #endif
4399 const std::string font_name = "invalid_font"; 4403 const std::string font_name = "invalid_font";
4400 const int kFontSize = 13; 4404 const int kFontSize = 13;
4401 RenderText* render_text = GetRenderText(); 4405 RenderText* render_text = GetRenderText();
4402 render_text->SetFontList(FontList(Font(font_name, kFontSize))); 4406 render_text->SetFontList(FontList(Font(font_name, kFontSize)));
4403 render_text->SetText(ASCIIToUTF16("abc")); 4407 render_text->SetText(ASCIIToUTF16("abc"));
4404 4408
4405 DrawVisualText(); 4409 DrawVisualText();
4406 } 4410 }
4407 4411
4412 // Ensures that text is centered vertically and consistently when either the
4413 // display rectangle height changes, or when the minimum line height changes.
4414 // The difference between the two is the selection rectangle, which should match
4415 // the line height.
4416 TEST_P(RenderTextHarfBuzzTest, BaselineWithLineHeight) {
4417 RenderText* render_text = GetRenderText();
4418 const int font_height = render_text->font_list().GetHeight();
4419 render_text->SetDisplayRect(Rect(500, font_height));
4420 render_text->SetText(ASCIIToUTF16("abc"));
4421
4422 // Select everything so the test can use GetSelectionBoundsUnion().
4423 render_text->SelectAll(false);
4424
4425 const int normal_baseline = test_api()->GetDisplayTextBaseline();
4426 ASSERT_EQ(1u, test_api()->lines().size());
4427 EXPECT_EQ(font_height, test_api()->lines()[0].size.height());
4428
4429 // With a matching display height, the baseline calculated using font metrics
4430 // and the baseline from the layout engine should agree. This works because
4431 // the text string is simple (exotic glyphs may use fonts with different
4432 // metrics).
4433 EXPECT_EQ(normal_baseline, render_text->GetBaseline());
4434 EXPECT_EQ(0, render_text->GetLineOffset(0).y());
4435
4436 const gfx::Rect normal_selection_bounds = GetSelectionBoundsUnion();
4437
4438 // Sanity check: selection should start from (0,0).
4439 EXPECT_EQ(gfx::Vector2d(), normal_selection_bounds.OffsetFromOrigin());
4440
4441 constexpr int kDelta = 16;
4442
4443 // Grow just the display rectangle.
4444 render_text->SetDisplayRect(Rect(500, font_height + kDelta));
4445
4446 // The display text baseline does not move: GetLineOffset() moves it instead.
4447 EXPECT_EQ(normal_baseline, test_api()->GetDisplayTextBaseline());
4448
4449 ASSERT_EQ(1u, test_api()->lines().size());
4450 EXPECT_EQ(font_height, test_api()->lines()[0].size.height());
4451
4452 // Save the baseline calculated using the display rectangle before enabling
4453 // multi-line or SetMinLineHeight().
4454 const int reported_baseline = render_text->GetBaseline();
4455 const int baseline_shift = reported_baseline - normal_baseline;
4456
4457 // When line height matches font height, this should match the line offset.
4458 EXPECT_EQ(baseline_shift, render_text->GetLineOffset(0).y());
4459
4460 // The actual shift depends on font metrics, and the calculations done in
4461 // RenderText::DetermineBaselineCenteringText(). Do a sanity check that the
4462 // "centering" part is happening within some tolerance by ensuring the shift
4463 // is within a pixel of (kDelta / 2). That is, 7, 8 or 9 pixels (for a delta
4464 // of 16). An unusual font in future may need more leeway.
4465 constexpr int kFuzz = 1; // If the next EXPECT fails, try increasing this.
4466 EXPECT_LE(abs(baseline_shift - kDelta / 2), kFuzz);
4467
4468 // Increasing display height (but not line height) should shift the selection
4469 // bounds down by |baseline_shift|, but leave a matching size.
4470 gfx::Rect current_selection_bounds = GetSelectionBoundsUnion();
4471 EXPECT_EQ(baseline_shift, current_selection_bounds.y());
4472 EXPECT_EQ(0, current_selection_bounds.x());
4473 EXPECT_EQ(normal_selection_bounds.size(), current_selection_bounds.size());
4474
4475 // Now increase the line height, but remain single-line. Note the line height
4476 // now matches the display rect.
4477 render_text->SetMinLineHeight(font_height + kDelta);
4478 int display_text_baseline = test_api()->GetDisplayTextBaseline();
4479 ASSERT_EQ(1u, test_api()->lines().size());
4480 EXPECT_EQ(font_height + kDelta, test_api()->lines()[0].size.height());
4481
4482 // The line offset should go back to zero, but now the display text baseline
4483 // should shift down to compensate, and the shift amount should match.
4484 EXPECT_EQ(0, render_text->GetLineOffset(0).y());
4485 EXPECT_EQ(normal_baseline + baseline_shift, display_text_baseline);
4486
4487 // Now selection bounds should grow in height, but not shift its origin.
4488 current_selection_bounds = GetSelectionBoundsUnion();
4489 EXPECT_EQ(font_height + kDelta, current_selection_bounds.height());
4490 EXPECT_EQ(normal_selection_bounds.width(), current_selection_bounds.width());
4491 EXPECT_EQ(gfx::Vector2d(), current_selection_bounds.OffsetFromOrigin());
4492
4493 // Flipping the multiline flag should change nothing.
4494 render_text->SetMultiline(true);
4495 display_text_baseline = test_api()->GetDisplayTextBaseline();
4496 ASSERT_EQ(1u, test_api()->lines().size());
4497 EXPECT_EQ(font_height + kDelta, test_api()->lines()[0].size.height());
4498 EXPECT_EQ(0, render_text->GetLineOffset(0).y());
4499 EXPECT_EQ(normal_baseline + baseline_shift, display_text_baseline);
4500 current_selection_bounds = GetSelectionBoundsUnion();
4501 EXPECT_EQ(font_height + kDelta, current_selection_bounds.height());
4502 EXPECT_EQ(normal_selection_bounds.width(), current_selection_bounds.width());
4503 EXPECT_EQ(gfx::Vector2d(), current_selection_bounds.OffsetFromOrigin());
4504 }
4505
4408 // Prefix for test instantiations intentionally left blank since each test 4506 // Prefix for test instantiations intentionally left blank since each test
4409 // fixture class has a single parameterization. 4507 // fixture class has a single parameterization.
4410 #if defined(OS_MACOSX) 4508 #if defined(OS_MACOSX)
4411 INSTANTIATE_TEST_CASE_P(, 4509 INSTANTIATE_TEST_CASE_P(,
4412 RenderTextTest, 4510 RenderTextTest,
4413 ::testing::Values(RENDER_TEXT_HARFBUZZ, 4511 ::testing::Values(RENDER_TEXT_HARFBUZZ,
4414 RENDER_TEXT_MAC), 4512 RENDER_TEXT_MAC),
4415 PrintRenderTextBackend()); 4513 PrintRenderTextBackend());
4416 INSTANTIATE_TEST_CASE_P(, 4514 INSTANTIATE_TEST_CASE_P(,
4417 RenderTextMacTest, 4515 RenderTextMacTest,
4418 ::testing::Values(RENDER_TEXT_MAC), 4516 ::testing::Values(RENDER_TEXT_MAC),
4419 PrintRenderTextBackend()); 4517 PrintRenderTextBackend());
4420 #else 4518 #else
4421 INSTANTIATE_TEST_CASE_P(, 4519 INSTANTIATE_TEST_CASE_P(,
4422 RenderTextTest, 4520 RenderTextTest,
4423 ::testing::Values(RENDER_TEXT_HARFBUZZ), 4521 ::testing::Values(RENDER_TEXT_HARFBUZZ),
4424 PrintRenderTextBackend()); 4522 PrintRenderTextBackend());
4425 #endif 4523 #endif
4426 4524
4427 INSTANTIATE_TEST_CASE_P(, 4525 INSTANTIATE_TEST_CASE_P(,
4428 RenderTextHarfBuzzTest, 4526 RenderTextHarfBuzzTest,
4429 ::testing::Values(RENDER_TEXT_HARFBUZZ), 4527 ::testing::Values(RENDER_TEXT_HARFBUZZ),
4430 PrintRenderTextBackend()); 4528 PrintRenderTextBackend());
4431 4529
4432 } // namespace gfx 4530 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text_harfbuzz.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698