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/views/controls/button/label_button.h" | 5 #include "ui/views/controls/button/label_button.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
11 #include "ui/accessibility/ax_view_state.h" | 11 #include "ui/accessibility/ax_view_state.h" |
12 #include "ui/base/material_design/material_design_controller.h" | 12 #include "ui/base/material_design/material_design_controller.h" |
13 #include "ui/base/test/material_design_controller_test_api.h" | 13 #include "ui/base/test/material_design_controller_test_api.h" |
14 #include "ui/base/ui_base_switches.h" | 14 #include "ui/base/ui_base_switches.h" |
15 #include "ui/events/test/event_generator.h" | 15 #include "ui/events/test/event_generator.h" |
16 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
17 #include "ui/gfx/font_list.h" | 17 #include "ui/gfx/font_list.h" |
18 #include "ui/gfx/geometry/size.h" | 18 #include "ui/gfx/geometry/size.h" |
19 #include "ui/gfx/geometry/vector2d.h" | 19 #include "ui/gfx/geometry/vector2d.h" |
20 #include "ui/gfx/text_utils.h" | 20 #include "ui/gfx/text_utils.h" |
21 #include "ui/native_theme/native_theme.h" | 21 #include "ui/native_theme/native_theme.h" |
22 #include "ui/views/animation/button_ink_drop_delegate.h" | 22 #include "ui/views/animation/test/ink_drop_host_view_test_api.h" |
| 23 #include "ui/views/animation/test/test_ink_drop.h" |
23 #include "ui/views/style/platform_style.h" | 24 #include "ui/views/style/platform_style.h" |
24 #include "ui/views/test/views_test_base.h" | 25 #include "ui/views/test/views_test_base.h" |
25 #include "ui/views/test/widget_test.h" | 26 #include "ui/views/test/widget_test.h" |
26 | 27 |
27 using base::ASCIIToUTF16; | 28 using base::ASCIIToUTF16; |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 gfx::ImageSkia CreateTestImage(int width, int height) { | 32 gfx::ImageSkia CreateTestImage(int width, int height) { |
32 SkBitmap bitmap; | 33 SkBitmap bitmap; |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 default_after->SetIsDefault(true); | 398 default_after->SetIsDefault(true); |
398 EXPECT_EQ(styled_highlight_text_color_, | 399 EXPECT_EQ(styled_highlight_text_color_, |
399 default_after->label()->enabled_color()); | 400 default_after->label()->enabled_color()); |
400 | 401 |
401 // Test becoming default before adding to the Widget. | 402 // Test becoming default before adding to the Widget. |
402 TestLabelButton* default_before = AddStyledButton("OK", true); | 403 TestLabelButton* default_before = AddStyledButton("OK", true); |
403 EXPECT_EQ(styled_highlight_text_color_, | 404 EXPECT_EQ(styled_highlight_text_color_, |
404 default_before->label()->enabled_color()); | 405 default_before->label()->enabled_color()); |
405 } | 406 } |
406 | 407 |
407 // A ButtonInkDropDelegate that tracks the last hover state requested. | |
408 class TestButtonInkDropDelegate : public ButtonInkDropDelegate { | |
409 public: | |
410 TestButtonInkDropDelegate(InkDropHost* ink_drop_host, View* view) | |
411 : ButtonInkDropDelegate(ink_drop_host, view), is_hovered_(false) {} | |
412 | |
413 ~TestButtonInkDropDelegate() override {} | |
414 | |
415 bool is_hovered() const { return is_hovered_; } | |
416 | |
417 // ButtonInkDropDelegate: | |
418 void SetHovered(bool is_hovered) override { | |
419 is_hovered_ = is_hovered; | |
420 ButtonInkDropDelegate::SetHovered(is_hovered); | |
421 } | |
422 | |
423 private: | |
424 // The last |is_hover| value passed to SetHovered(). | |
425 bool is_hovered_; | |
426 | |
427 DISALLOW_COPY_AND_ASSIGN(TestButtonInkDropDelegate); | |
428 }; | |
429 | |
430 // A generic LabelButton configured with an |InkDropDelegate|. | |
431 class InkDropLabelButton : public LabelButton { | |
432 public: | |
433 InkDropLabelButton() | |
434 : LabelButton(nullptr, base::string16()), | |
435 test_ink_drop_delegate_(new TestButtonInkDropDelegate(this, this)) { | |
436 set_ink_drop_delegate(base::WrapUnique(test_ink_drop_delegate_)); | |
437 } | |
438 | |
439 ~InkDropLabelButton() override {} | |
440 | |
441 TestButtonInkDropDelegate* test_ink_drop_delegate() { | |
442 return test_ink_drop_delegate_; | |
443 } | |
444 | |
445 private: | |
446 // Weak pointer. | |
447 TestButtonInkDropDelegate* test_ink_drop_delegate_; | |
448 | |
449 DISALLOW_COPY_AND_ASSIGN(InkDropLabelButton); | |
450 }; | |
451 | |
452 // Test fixture for a LabelButton that has an ink drop configured. | 408 // Test fixture for a LabelButton that has an ink drop configured. |
453 class InkDropLabelButtonTest : public ViewsTestBase { | 409 class InkDropLabelButtonTest : public ViewsTestBase { |
454 public: | 410 public: |
455 InkDropLabelButtonTest() {} | 411 InkDropLabelButtonTest() {} |
456 | 412 |
457 // ViewsTestBase: | 413 // ViewsTestBase: |
458 void SetUp() override { | 414 void SetUp() override { |
459 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 415 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
460 switches::kTopChromeMD, switches::kTopChromeMDMaterial); | 416 switches::kTopChromeMD, switches::kTopChromeMDMaterial); |
461 ViewsTestBase::SetUp(); | 417 ViewsTestBase::SetUp(); |
462 | 418 |
463 // Create a widget so that the CustomButton can query the hover state | 419 // Create a widget so that the CustomButton can query the hover state |
464 // correctly. | 420 // correctly. |
465 widget_.reset(new Widget); | 421 widget_.reset(new Widget); |
466 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 422 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
467 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 423 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
468 params.bounds = gfx::Rect(0, 0, 20, 20); | 424 params.bounds = gfx::Rect(0, 0, 20, 20); |
469 widget_->Init(params); | 425 widget_->Init(params); |
470 widget_->Show(); | 426 widget_->Show(); |
471 | 427 |
472 button_ = new InkDropLabelButton(); | 428 button_ = new LabelButton(nullptr, base::string16()); |
| 429 |
| 430 test_ink_drop_ = new test::TestInkDrop(); |
| 431 test::InkDropHostViewTestApi(button_).SetInkDrop( |
| 432 base::WrapUnique(test_ink_drop_)); |
| 433 |
473 widget_->SetContentsView(button_); | 434 widget_->SetContentsView(button_); |
474 } | 435 } |
475 | 436 |
476 void TearDown() override { | 437 void TearDown() override { |
477 widget_.reset(); | 438 widget_.reset(); |
478 ViewsTestBase::TearDown(); | 439 ViewsTestBase::TearDown(); |
479 ui::test::MaterialDesignControllerTestAPI::Uninitialize(); | 440 ui::test::MaterialDesignControllerTestAPI::Uninitialize(); |
480 } | 441 } |
481 | 442 |
482 protected: | 443 protected: |
483 // Required to host the test target. | 444 // Required to host the test target. |
484 std::unique_ptr<Widget> widget_; | 445 std::unique_ptr<Widget> widget_; |
485 | 446 |
486 // The test target. | 447 // The test target. |
487 InkDropLabelButton* button_ = nullptr; | 448 LabelButton* button_ = nullptr; |
| 449 |
| 450 // Weak ptr, |button_| owns the instance. |
| 451 test::TestInkDrop* test_ink_drop_ = nullptr; |
488 | 452 |
489 private: | 453 private: |
490 DISALLOW_COPY_AND_ASSIGN(InkDropLabelButtonTest); | 454 DISALLOW_COPY_AND_ASSIGN(InkDropLabelButtonTest); |
491 }; | 455 }; |
492 | 456 |
493 TEST_F(InkDropLabelButtonTest, HoverStateAfterMouseEnterAndExitEvents) { | 457 TEST_F(InkDropLabelButtonTest, HoverStateAfterMouseEnterAndExitEvents) { |
494 ui::test::EventGenerator event_generator(GetContext(), | 458 ui::test::EventGenerator event_generator(GetContext(), |
495 widget_->GetNativeWindow()); | 459 widget_->GetNativeWindow()); |
496 const gfx::Point out_of_bounds_point(button_->bounds().bottom_right() + | 460 const gfx::Point out_of_bounds_point(button_->bounds().bottom_right() + |
497 gfx::Vector2d(1, 1)); | 461 gfx::Vector2d(1, 1)); |
498 const gfx::Point in_bounds_point(button_->bounds().CenterPoint()); | 462 const gfx::Point in_bounds_point(button_->bounds().CenterPoint()); |
499 | 463 |
500 event_generator.MoveMouseTo(out_of_bounds_point); | 464 event_generator.MoveMouseTo(out_of_bounds_point); |
501 EXPECT_FALSE(button_->test_ink_drop_delegate()->is_hovered()); | 465 EXPECT_FALSE(test_ink_drop_->is_hovered()); |
502 | 466 |
503 event_generator.MoveMouseTo(in_bounds_point); | 467 event_generator.MoveMouseTo(in_bounds_point); |
504 EXPECT_TRUE(button_->test_ink_drop_delegate()->is_hovered()); | 468 EXPECT_TRUE(test_ink_drop_->is_hovered()); |
505 | 469 |
506 event_generator.MoveMouseTo(out_of_bounds_point); | 470 event_generator.MoveMouseTo(out_of_bounds_point); |
507 EXPECT_FALSE(button_->test_ink_drop_delegate()->is_hovered()); | 471 EXPECT_FALSE(test_ink_drop_->is_hovered()); |
508 } | 472 } |
509 | 473 |
510 // Verifies the target event handler View is the |LabelButton| and not any of | 474 // Verifies the target event handler View is the |LabelButton| and not any of |
511 // the child Views. | 475 // the child Views. |
512 TEST_F(InkDropLabelButtonTest, TargetEventHandler) { | 476 TEST_F(InkDropLabelButtonTest, TargetEventHandler) { |
513 View* target_view = widget_->GetRootView()->GetEventHandlerForPoint( | 477 View* target_view = widget_->GetRootView()->GetEventHandlerForPoint( |
514 button_->bounds().CenterPoint()); | 478 button_->bounds().CenterPoint()); |
515 EXPECT_EQ(button_, target_view); | 479 EXPECT_EQ(button_, target_view); |
516 } | 480 } |
517 | 481 |
518 } // namespace views | 482 } // namespace views |
OLD | NEW |