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

Side by Side Diff: ui/views/controls/label_unittest.cc

Issue 1053143002: Make View::Paint use ui::PaintRecorder to access PaintContext's canvas (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: paintrecorder: . Created 5 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
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/views/controls/label.h" 5 #include "ui/views/controls/label.h"
6 6
7 #include "base/i18n/rtl.h" 7 #include "base/i18n/rtl.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/accessibility/ax_view_state.h" 10 #include "ui/accessibility/ax_view_state.h"
11 #include "ui/base/l10n/l10n_util.h" 11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/compositor/paint_context.h"
12 #include "ui/gfx/canvas.h" 13 #include "ui/gfx/canvas.h"
13 #include "ui/views/border.h" 14 #include "ui/views/border.h"
14 #include "ui/views/test/focus_manager_test.h" 15 #include "ui/views/test/focus_manager_test.h"
15 #include "ui/views/test/views_test_base.h" 16 #include "ui/views/test/views_test_base.h"
16 #include "ui/views/widget/widget.h" 17 #include "ui/views/widget/widget.h"
17 18
18 using base::ASCIIToUTF16; 19 using base::ASCIIToUTF16;
19 20
20 namespace views { 21 namespace views {
21 22
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 EXPECT_EQ(label.text(), state.name); 299 EXPECT_EQ(label.text(), state.name);
299 EXPECT_TRUE(state.HasStateFlag(ui::AX_STATE_READ_ONLY)); 300 EXPECT_TRUE(state.HasStateFlag(ui::AX_STATE_READ_ONLY));
300 } 301 }
301 302
302 TEST_F(LabelTest, TextChangeWithoutLayout) { 303 TEST_F(LabelTest, TextChangeWithoutLayout) {
303 Label label; 304 Label label;
304 label.SetText(ASCIIToUTF16("Example")); 305 label.SetText(ASCIIToUTF16("Example"));
305 label.SetBounds(0, 0, 200, 200); 306 label.SetBounds(0, 0, 200, 200);
306 307
307 gfx::Canvas canvas(gfx::Size(200, 200), 1.0f, true); 308 gfx::Canvas canvas(gfx::Size(200, 200), 1.0f, true);
308 label.Paint(View::PaintContext(&canvas)); 309 label.Paint(ui::PaintContext(&canvas));
309 EXPECT_EQ(1u, label.lines_.size()); 310 EXPECT_EQ(1u, label.lines_.size());
310 EXPECT_EQ(ASCIIToUTF16("Example"), label.lines_[0]->GetDisplayText()); 311 EXPECT_EQ(ASCIIToUTF16("Example"), label.lines_[0]->GetDisplayText());
311 312
312 label.SetText(ASCIIToUTF16("Altered")); 313 label.SetText(ASCIIToUTF16("Altered"));
313 // The altered text should be painted even though Layout() or SetBounds() are 314 // The altered text should be painted even though Layout() or SetBounds() are
314 // not called. 315 // not called.
315 label.Paint(View::PaintContext(&canvas)); 316 label.Paint(ui::PaintContext(&canvas));
316 EXPECT_EQ(1u, label.lines_.size()); 317 EXPECT_EQ(1u, label.lines_.size());
317 EXPECT_EQ(ASCIIToUTF16("Altered"), label.lines_[0]->GetDisplayText()); 318 EXPECT_EQ(ASCIIToUTF16("Altered"), label.lines_[0]->GetDisplayText());
318 } 319 }
319 320
320 TEST_F(LabelTest, EmptyLabelSizing) { 321 TEST_F(LabelTest, EmptyLabelSizing) {
321 Label label; 322 Label label;
322 const gfx::Size expected_size(0, gfx::FontList().GetHeight()); 323 const gfx::Size expected_size(0, gfx::FontList().GetHeight());
323 EXPECT_EQ(expected_size, label.GetPreferredSize()); 324 EXPECT_EQ(expected_size, label.GetPreferredSize());
324 label.SetMultiLine(!label.multi_line()); 325 label.SetMultiLine(!label.multi_line());
325 EXPECT_EQ(expected_size, label.GetPreferredSize()); 326 EXPECT_EQ(expected_size, label.GetPreferredSize());
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 gfx::Size narrow_size = label.GetPreferredSize(); 476 gfx::Size narrow_size = label.GetPreferredSize();
476 EXPECT_GT(required_size.width(), narrow_size.width()); 477 EXPECT_GT(required_size.width(), narrow_size.width());
477 EXPECT_LT(required_size.height(), narrow_size.height()); 478 EXPECT_LT(required_size.height(), narrow_size.height());
478 479
479 // SetBounds() doesn't change the preferred size. 480 // SetBounds() doesn't change the preferred size.
480 label.SetBounds(0, 0, narrow_size.width() - 1, narrow_size.height()); 481 label.SetBounds(0, 0, narrow_size.width() - 1, narrow_size.height());
481 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString()); 482 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString());
482 483
483 // Paint() doesn't change the preferred size. 484 // Paint() doesn't change the preferred size.
484 gfx::Canvas canvas; 485 gfx::Canvas canvas;
485 label.Paint(View::PaintContext(&canvas)); 486 label.Paint(ui::PaintContext(&canvas));
486 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString()); 487 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString());
487 } 488 }
488 489
489 // Check that labels support GetTooltipHandlerForPoint. 490 // Check that labels support GetTooltipHandlerForPoint.
490 TEST_F(LabelTest, GetTooltipHandlerForPoint) { 491 TEST_F(LabelTest, GetTooltipHandlerForPoint) {
491 // A root view must be defined for this test because the hit-testing 492 // A root view must be defined for this test because the hit-testing
492 // behaviour used by GetTooltipHandlerForPoint() is defined by 493 // behaviour used by GetTooltipHandlerForPoint() is defined by
493 // the ViewTargeter installed on the root view. 494 // the ViewTargeter installed on the root view.
494 Widget widget; 495 Widget widget;
495 Widget::InitParams init_params = 496 Widget::InitParams init_params =
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 TEST_F(LabelTest, ResetRenderTextData) { 549 TEST_F(LabelTest, ResetRenderTextData) {
549 Label label; 550 Label label;
550 label.SetText(ASCIIToUTF16("Example")); 551 label.SetText(ASCIIToUTF16("Example"));
551 label.SizeToPreferredSize(); 552 label.SizeToPreferredSize();
552 gfx::Size preferred_size = label.GetPreferredSize(); 553 gfx::Size preferred_size = label.GetPreferredSize();
553 554
554 EXPECT_NE(gfx::Size().ToString(), preferred_size.ToString()); 555 EXPECT_NE(gfx::Size().ToString(), preferred_size.ToString());
555 EXPECT_EQ(0u, label.lines_.size()); 556 EXPECT_EQ(0u, label.lines_.size());
556 557
557 gfx::Canvas canvas(preferred_size, 1.0f, true); 558 gfx::Canvas canvas(preferred_size, 1.0f, true);
558 label.Paint(View::PaintContext(&canvas)); 559 label.Paint(ui::PaintContext(&canvas));
559 EXPECT_EQ(1u, label.lines_.size()); 560 EXPECT_EQ(1u, label.lines_.size());
560 561
561 // Label should recreate its RenderText object when it's invisible, to release 562 // Label should recreate its RenderText object when it's invisible, to release
562 // the layout structures and data. 563 // the layout structures and data.
563 label.SetVisible(false); 564 label.SetVisible(false);
564 EXPECT_EQ(0u, label.lines_.size()); 565 EXPECT_EQ(0u, label.lines_.size());
565 566
566 // Querying fields or size information should not recompute the layout 567 // Querying fields or size information should not recompute the layout
567 // unnecessarily. 568 // unnecessarily.
568 EXPECT_EQ(ASCIIToUTF16("Example"), label.text()); 569 EXPECT_EQ(ASCIIToUTF16("Example"), label.text());
569 EXPECT_EQ(0u, label.lines_.size()); 570 EXPECT_EQ(0u, label.lines_.size());
570 571
571 EXPECT_EQ(preferred_size.ToString(), label.GetPreferredSize().ToString()); 572 EXPECT_EQ(preferred_size.ToString(), label.GetPreferredSize().ToString());
572 EXPECT_EQ(0u, label.lines_.size()); 573 EXPECT_EQ(0u, label.lines_.size());
573 574
574 // RenderText data should be back when it's necessary. 575 // RenderText data should be back when it's necessary.
575 label.SetVisible(true); 576 label.SetVisible(true);
576 EXPECT_EQ(0u, label.lines_.size()); 577 EXPECT_EQ(0u, label.lines_.size());
577 578
578 label.Paint(View::PaintContext(&canvas)); 579 label.Paint(ui::PaintContext(&canvas));
579 EXPECT_EQ(1u, label.lines_.size()); 580 EXPECT_EQ(1u, label.lines_.size());
580 581
581 // Changing layout just resets |lines_|. It'll recover next time it's drawn. 582 // Changing layout just resets |lines_|. It'll recover next time it's drawn.
582 label.SetBounds(0, 0, 10, 10); 583 label.SetBounds(0, 0, 10, 10);
583 EXPECT_EQ(0u, label.lines_.size()); 584 EXPECT_EQ(0u, label.lines_.size());
584 585
585 label.Paint(View::PaintContext(&canvas)); 586 label.Paint(ui::PaintContext(&canvas));
586 EXPECT_EQ(1u, label.lines_.size()); 587 EXPECT_EQ(1u, label.lines_.size());
587 } 588 }
588 589
589 #if !defined(OS_MACOSX) 590 #if !defined(OS_MACOSX)
590 TEST_F(LabelTest, MultilineSupportedRenderText) { 591 TEST_F(LabelTest, MultilineSupportedRenderText) {
591 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance()); 592 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance());
592 ASSERT_TRUE(render_text->MultilineSupported()); 593 ASSERT_TRUE(render_text->MultilineSupported());
593 594
594 Label label; 595 Label label;
595 label.SetText(ASCIIToUTF16("Example of\nmultilined label")); 596 label.SetText(ASCIIToUTF16("Example of\nmultilined label"));
596 label.SetMultiLine(true); 597 label.SetMultiLine(true);
597 label.SizeToPreferredSize(); 598 label.SizeToPreferredSize();
598 599
599 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true); 600 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true);
600 label.Paint(View::PaintContext(&canvas)); 601 label.Paint(ui::PaintContext(&canvas));
601 602
602 // There's only one 'line', RenderText itself supports multiple lines. 603 // There's only one 'line', RenderText itself supports multiple lines.
603 EXPECT_EQ(1u, label.lines_.size()); 604 EXPECT_EQ(1u, label.lines_.size());
604 } 605 }
605 #endif 606 #endif
606 607
607 TEST_F(LabelFocusTest, FocusBounds) { 608 TEST_F(LabelFocusTest, FocusBounds) {
608 label()->SetText(ASCIIToUTF16("Example")); 609 label()->SetText(ASCIIToUTF16("Example"));
609 gfx::Size normal_size = label()->GetPreferredSize(); 610 gfx::Size normal_size = label()->GetPreferredSize();
610 611
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
648 label()->SetFocusable(true); 649 label()->SetFocusable(true);
649 label()->RequestFocus(); 650 label()->RequestFocus();
650 label()->SizeToPreferredSize(); 651 label()->SizeToPreferredSize();
651 652
652 gfx::Rect focus_bounds = label()->GetFocusBounds(); 653 gfx::Rect focus_bounds = label()->GetFocusBounds();
653 EXPECT_FALSE(focus_bounds.IsEmpty()); 654 EXPECT_FALSE(focus_bounds.IsEmpty());
654 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height()); 655 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height());
655 } 656 }
656 657
657 } // namespace views 658 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698