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

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

Issue 2767163003: Vertically center multi-line RenderTextHarfBuzz that uses SetMinLineHeight(). (Closed)
Patch Set: awesomer test 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 4387 matching lines...) Expand 10 before | Expand all | Expand 10 after
4398 #endif 4398 #endif
4399 const std::string font_name = "invalid_font"; 4399 const std::string font_name = "invalid_font";
4400 const int kFontSize = 13; 4400 const int kFontSize = 13;
4401 RenderText* render_text = GetRenderText(); 4401 RenderText* render_text = GetRenderText();
4402 render_text->SetFontList(FontList(Font(font_name, kFontSize))); 4402 render_text->SetFontList(FontList(Font(font_name, kFontSize)));
4403 render_text->SetText(ASCIIToUTF16("abc")); 4403 render_text->SetText(ASCIIToUTF16("abc"));
4404 4404
4405 DrawVisualText(); 4405 DrawVisualText();
4406 } 4406 }
4407 4407
4408 // Ensures that text is centered vertically and consistently when either the
4409 // display rectangle height changes, or when the minimum line height changes.
4410 // The difference between the two is the selection rectangle, which should match
4411 // the line height.
4412 TEST_P(RenderTextHarfBuzzTest, BaselineWithLineHeight) {
4413 RenderText* render_text = GetRenderText();
4414 const int font_height = render_text->font_list().GetHeight();
4415 render_text->SetDisplayRect(Rect(500, font_height));
4416 render_text->SetText(ASCIIToUTF16("abc"));
4417
4418 // Select everything so the test can use GetSelectionBoundsUnion().
4419 render_text->SelectAll(false);
4420
4421 test_api()->EnsureLayout();
4422 ASSERT_EQ(1u, test_api()->lines().size());
4423 EXPECT_EQ(font_height, test_api()->lines()[0].size.height());
4424
4425 // Note this is equivalent to RenderText::GetDisplayTextBaseline().
msw 2017/03/31 17:19:27 q: why not just use that here and on 4457, 4482, a
tapted 2017/04/02 23:52:42 Done (added to TestApi + replaced the EnsureLayout
4426 const int normal_baseline = test_api()->lines()[0].baseline;
4427
4428 // With a matching display height, the baseline calculated using font metrics
4429 // and the baseline from the layout engine should agree. This works because
4430 // the text string is simple (exotic glyphs may use fonts with different
4431 // metrics).
4432 EXPECT_EQ(normal_baseline, render_text->GetBaseline());
4433 EXPECT_EQ(0, render_text->GetLineOffset(0).y());
4434
4435 const gfx::Rect normal_selection_bounds = GetSelectionBoundsUnion();
4436
4437 // Sanity check: selection should start from (0,0).
4438 EXPECT_EQ(gfx::Vector2d(), normal_selection_bounds.OffsetFromOrigin());
4439
4440 constexpr int kDelta = 16;
4441
4442 // Grow just the display rectangle.
4443 render_text->SetDisplayRect(Rect(500, font_height + kDelta));
4444 test_api()->EnsureLayout();
4445 ASSERT_EQ(1u, test_api()->lines().size());
4446 EXPECT_EQ(font_height, test_api()->lines()[0].size.height());
4447
4448 // Save the baseline calculated using the display rectangle before enabling
4449 // multi-line or SetMinLineHeight().
4450 const int reported_baseline = render_text->GetBaseline();
4451 const int baseline_shift = reported_baseline - normal_baseline;
4452
4453 // When line height matches font height, this should match the line offset.
4454 EXPECT_EQ(baseline_shift, render_text->GetLineOffset(0).y());
4455
4456 // The display text baseline does not move: GetLineOffset() moves it instead.
4457 EXPECT_EQ(normal_baseline, test_api()->lines()[0].baseline);
4458
4459 // The actual shift depends on font metrics, but it should be within a pixel
msw 2017/03/31 17:19:27 Can we use the actual font metrics to make this a
tapted 2017/04/02 23:52:42 We'd have to repeat all the calculations done in D
4460 // of (kDelta / 2). That is, 7, 8 or 9 pixels (for a delta of 16). An unusual
4461 // font in future may need more leeway.
4462 constexpr int kFuzz = 1; // If the next EXPECT fails, try increasing this.
4463 EXPECT_LE(abs(baseline_shift - kDelta / 2), kFuzz);
4464
4465 // Increasing display height (but not line height) should shift the selection
4466 // bounds down by |baseline_shift|, but leave a matching size.
4467 gfx::Rect current_selection_bounds = GetSelectionBoundsUnion();
4468 EXPECT_EQ(baseline_shift, current_selection_bounds.y());
4469 EXPECT_EQ(0, current_selection_bounds.x());
4470 EXPECT_EQ(normal_selection_bounds.size(), current_selection_bounds.size());
4471
4472 // Now increase the line height, but remain single-line. Note the line height
4473 // now matches the display rect.
4474 render_text->SetMinLineHeight(font_height + kDelta);
4475 test_api()->EnsureLayout();
4476 ASSERT_EQ(1u, test_api()->lines().size());
4477 EXPECT_EQ(font_height + kDelta, test_api()->lines()[0].size.height());
4478
4479 // The line offset should go back to zero, but now the display text baseline
4480 // should shift down to compensate, and the shift amount should match.
4481 EXPECT_EQ(0, render_text->GetLineOffset(0).y());
4482 EXPECT_EQ(normal_baseline + baseline_shift, test_api()->lines()[0].baseline);
tapted 2017/03/31 10:31:14 (without the fix, these are the first two lines th
4483
4484 // Now selection bounds should grow in height, but not shift its origin.
4485 current_selection_bounds = GetSelectionBoundsUnion();
4486 EXPECT_EQ(font_height + kDelta, current_selection_bounds.height());
4487 EXPECT_EQ(normal_selection_bounds.width(), current_selection_bounds.width());
4488 EXPECT_EQ(gfx::Vector2d(), current_selection_bounds.OffsetFromOrigin());
tapted 2017/03/31 10:31:14 (which means this also fails - the selection bound
4489
4490 // Flipping the multiline flag should change nothing.
4491 render_text->SetMultiline(true);
4492 test_api()->EnsureLayout();
4493 ASSERT_EQ(1u, test_api()->lines().size());
4494 EXPECT_EQ(font_height + kDelta, test_api()->lines()[0].size.height());
4495 EXPECT_EQ(0, render_text->GetLineOffset(0).y());
4496 EXPECT_EQ(normal_baseline + baseline_shift, test_api()->lines()[0].baseline);
tapted 2017/03/31 10:31:14 (for the multiline case, this is the only case tha
4497 current_selection_bounds = GetSelectionBoundsUnion();
4498 EXPECT_EQ(font_height + kDelta, current_selection_bounds.height());
4499 EXPECT_EQ(normal_selection_bounds.width(), current_selection_bounds.width());
4500 EXPECT_EQ(gfx::Vector2d(), current_selection_bounds.OffsetFromOrigin());
4501 }
4502
4408 // Prefix for test instantiations intentionally left blank since each test 4503 // Prefix for test instantiations intentionally left blank since each test
4409 // fixture class has a single parameterization. 4504 // fixture class has a single parameterization.
4410 #if defined(OS_MACOSX) 4505 #if defined(OS_MACOSX)
4411 INSTANTIATE_TEST_CASE_P(, 4506 INSTANTIATE_TEST_CASE_P(,
4412 RenderTextTest, 4507 RenderTextTest,
4413 ::testing::Values(RENDER_TEXT_HARFBUZZ, 4508 ::testing::Values(RENDER_TEXT_HARFBUZZ,
4414 RENDER_TEXT_MAC), 4509 RENDER_TEXT_MAC),
4415 PrintRenderTextBackend()); 4510 PrintRenderTextBackend());
4416 INSTANTIATE_TEST_CASE_P(, 4511 INSTANTIATE_TEST_CASE_P(,
4417 RenderTextMacTest, 4512 RenderTextMacTest,
4418 ::testing::Values(RENDER_TEXT_MAC), 4513 ::testing::Values(RENDER_TEXT_MAC),
4419 PrintRenderTextBackend()); 4514 PrintRenderTextBackend());
4420 #else 4515 #else
4421 INSTANTIATE_TEST_CASE_P(, 4516 INSTANTIATE_TEST_CASE_P(,
4422 RenderTextTest, 4517 RenderTextTest,
4423 ::testing::Values(RENDER_TEXT_HARFBUZZ), 4518 ::testing::Values(RENDER_TEXT_HARFBUZZ),
4424 PrintRenderTextBackend()); 4519 PrintRenderTextBackend());
4425 #endif 4520 #endif
4426 4521
4427 INSTANTIATE_TEST_CASE_P(, 4522 INSTANTIATE_TEST_CASE_P(,
4428 RenderTextHarfBuzzTest, 4523 RenderTextHarfBuzzTest,
4429 ::testing::Values(RENDER_TEXT_HARFBUZZ), 4524 ::testing::Values(RENDER_TEXT_HARFBUZZ),
4430 PrintRenderTextBackend()); 4525 PrintRenderTextBackend());
4431 4526
4432 } // namespace gfx 4527 } // 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