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
3 // found in the LICENSE file.
5 #include "ui/views/controls/button/custom_button.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/base/layout.h"
9 #include "ui/gfx/screen.h"
10 #include "ui/views/test/views_test_base.h"
13 #include "ui/aura/root_window.h"
14 #include "ui/aura/test/test_cursor_client.h"
21 class TestCustomButton : public CustomButton {
23 explicit TestCustomButton(ButtonListener* listener)
24 : CustomButton(listener) {
27 virtual ~TestCustomButton() {}
30 DISALLOW_COPY_AND_ASSIGN(TestCustomButton);
35 typedef ViewsTestBase CustomButtonTest;
37 // Tests that hover state changes correctly when visiblity/enableness changes.
38 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) {
39 // Create a widget so that the CustomButton can query the hover state
41 scoped_ptr<Widget> widget(new Widget);
42 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
43 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
44 params.bounds = gfx::Rect(0, 0, 650, 650);
49 aura::test::TestCursorClient cursor_client(
50 widget->GetNativeView()->GetRootWindow());
53 // Position the widget in a way so that it is under the cursor.
54 gfx::Point cursor = gfx::Screen::GetScreenFor(
55 widget->GetNativeView())->GetCursorScreenPoint();
56 gfx::Rect widget_bounds = widget->GetWindowBoundsInScreen();
57 widget_bounds.set_origin(cursor);
58 widget->SetBounds(widget_bounds);
60 TestCustomButton* button = new TestCustomButton(NULL);
61 widget->SetContentsView(button);
63 gfx::Point center(10, 10);
64 button->OnMousePressed(ui::MouseEvent(ui::ET_MOUSE_PRESSED, center, center,
65 ui::EF_LEFT_MOUSE_BUTTON));
66 EXPECT_EQ(CustomButton::STATE_PRESSED, button->state());
68 button->OnMouseReleased(ui::MouseEvent(ui::ET_MOUSE_RELEASED, center, center,
69 ui::EF_LEFT_MOUSE_BUTTON));
70 EXPECT_EQ(CustomButton::STATE_HOVERED, button->state());
72 button->SetEnabled(false);
73 EXPECT_EQ(CustomButton::STATE_DISABLED, button->state());
75 button->SetEnabled(true);
76 EXPECT_EQ(CustomButton::STATE_HOVERED, button->state());
78 button->SetVisible(false);
79 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state());
81 button->SetVisible(true);
82 EXPECT_EQ(CustomButton::STATE_HOVERED, button->state());
85 // In Aura views, no new hover effects are invoked if mouse events
87 cursor_client.DisableMouseEvents();
89 button->SetEnabled(false);
90 EXPECT_EQ(CustomButton::STATE_DISABLED, button->state());
92 button->SetEnabled(true);
93 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state());
95 button->SetVisible(false);
96 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state());
98 button->SetVisible(true);
99 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state());