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/aura/test/test_cursor_client.h"
9 #include "ui/aura/window.h"
10 #include "ui/aura/window_event_dispatcher.h"
11 #include "ui/base/layout.h"
12 #include "ui/events/event_utils.h"
13 #include "ui/gfx/screen.h"
14 #include "ui/views/controls/button/checkbox.h"
15 #include "ui/views/controls/button/image_button.h"
16 #include "ui/views/controls/button/label_button.h"
17 #include "ui/views/controls/button/menu_button.h"
18 #include "ui/views/controls/button/radio_button.h"
19 #include "ui/views/controls/link.h"
20 #include "ui/views/controls/textfield/textfield.h"
21 #include "ui/views/test/views_test_base.h"
27 class TestCustomButton
: public CustomButton
{
29 explicit TestCustomButton(ButtonListener
* listener
)
30 : CustomButton(listener
) {
33 ~TestCustomButton() override
{}
36 DISALLOW_COPY_AND_ASSIGN(TestCustomButton
);
39 void PerformGesture(CustomButton
* button
, ui::EventType event_type
) {
40 ui::GestureEventDetails
gesture_details(event_type
);
41 base::TimeDelta time_stamp
= base::TimeDelta::FromMicroseconds(0);
42 ui::GestureEvent
gesture_event(0, 0, 0, time_stamp
, gesture_details
);
43 button
->OnGestureEvent(&gesture_event
);
48 typedef ViewsTestBase CustomButtonTest
;
50 // Tests that hover state changes correctly when visiblity/enableness changes.
51 TEST_F(CustomButtonTest
, HoverStateOnVisibilityChange
) {
52 // Create a widget so that the CustomButton can query the hover state
54 scoped_ptr
<Widget
> widget(new Widget
);
55 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
56 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
57 params
.bounds
= gfx::Rect(0, 0, 650, 650);
61 aura::test::TestCursorClient
cursor_client(
62 widget
->GetNativeView()->GetRootWindow());
64 // Position the widget in a way so that it is under the cursor.
65 gfx::Point cursor
= gfx::Screen::GetScreenFor(
66 widget
->GetNativeView())->GetCursorScreenPoint();
67 gfx::Rect widget_bounds
= widget
->GetWindowBoundsInScreen();
68 widget_bounds
.set_origin(cursor
);
69 widget
->SetBounds(widget_bounds
);
71 TestCustomButton
* button
= new TestCustomButton(NULL
);
72 widget
->SetContentsView(button
);
74 gfx::Point
center(10, 10);
75 button
->OnMousePressed(ui::MouseEvent(
76 ui::ET_MOUSE_PRESSED
, center
, center
, ui::EventTimeForNow(),
77 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
));
78 EXPECT_EQ(CustomButton::STATE_PRESSED
, button
->state());
80 button
->OnMouseReleased(ui::MouseEvent(
81 ui::ET_MOUSE_RELEASED
, center
, center
, ui::EventTimeForNow(),
82 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
));
83 EXPECT_EQ(CustomButton::STATE_HOVERED
, button
->state());
85 button
->SetEnabled(false);
86 EXPECT_EQ(CustomButton::STATE_DISABLED
, button
->state());
88 button
->SetEnabled(true);
89 EXPECT_EQ(CustomButton::STATE_HOVERED
, button
->state());
91 button
->SetVisible(false);
92 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
94 button
->SetVisible(true);
95 EXPECT_EQ(CustomButton::STATE_HOVERED
, button
->state());
97 // In Aura views, no new hover effects are invoked if mouse events
99 cursor_client
.DisableMouseEvents();
101 button
->SetEnabled(false);
102 EXPECT_EQ(CustomButton::STATE_DISABLED
, button
->state());
104 button
->SetEnabled(true);
105 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
107 button
->SetVisible(false);
108 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
110 button
->SetVisible(true);
111 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
114 // Tests that gesture events correctly change the button state.
115 TEST_F(CustomButtonTest
, GestureEventsSetState
) {
116 // Create a widget so that the CustomButton can query the hover state
118 scoped_ptr
<Widget
> widget(new Widget
);
119 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
120 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
121 params
.bounds
= gfx::Rect(0, 0, 650, 650);
122 widget
->Init(params
);
125 aura::test::TestCursorClient
cursor_client(
126 widget
->GetNativeView()->GetRootWindow());
128 TestCustomButton
* button
= new TestCustomButton(NULL
);
129 widget
->SetContentsView(button
);
131 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
133 PerformGesture(button
, ui::ET_GESTURE_TAP_DOWN
);
134 EXPECT_EQ(CustomButton::STATE_PRESSED
, button
->state());
136 PerformGesture(button
, ui::ET_GESTURE_SHOW_PRESS
);
137 EXPECT_EQ(CustomButton::STATE_PRESSED
, button
->state());
139 PerformGesture(button
, ui::ET_GESTURE_TAP_CANCEL
);
140 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
143 // Ensure subclasses of CustomButton are correctly recognized as CustomButton.
144 TEST_F(CustomButtonTest
, AsCustomButton
) {
147 LabelButton
label_button(NULL
, text
);
148 EXPECT_TRUE(CustomButton::AsCustomButton(&label_button
));
150 ImageButton
image_button(NULL
);
151 EXPECT_TRUE(CustomButton::AsCustomButton(&image_button
));
153 Checkbox
checkbox(text
);
154 EXPECT_TRUE(CustomButton::AsCustomButton(&checkbox
));
156 RadioButton
radio_button(text
, 0);
157 EXPECT_TRUE(CustomButton::AsCustomButton(&radio_button
));
159 MenuButton
menu_button(NULL
, text
, NULL
, false);
160 EXPECT_TRUE(CustomButton::AsCustomButton(&menu_button
));
163 EXPECT_FALSE(CustomButton::AsCustomButton(&label
));
166 EXPECT_FALSE(CustomButton::AsCustomButton(&link
));
169 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield
));