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/gfx/screen.h"
13 #include "ui/views/controls/button/checkbox.h"
14 #include "ui/views/controls/button/image_button.h"
15 #include "ui/views/controls/button/label_button.h"
16 #include "ui/views/controls/button/menu_button.h"
17 #include "ui/views/controls/button/radio_button.h"
18 #include "ui/views/controls/link.h"
19 #include "ui/views/controls/textfield/textfield.h"
20 #include "ui/views/test/views_test_base.h"
26 class TestCustomButton
: public CustomButton
{
28 explicit TestCustomButton(ButtonListener
* listener
)
29 : CustomButton(listener
) {
32 virtual ~TestCustomButton() {}
35 DISALLOW_COPY_AND_ASSIGN(TestCustomButton
);
38 void PerformGesture(CustomButton
* button
, ui::EventType event_type
) {
39 ui::GestureEventDetails
gesture_details(event_type
, 0, 0);
40 base::TimeDelta time_stamp
= base::TimeDelta::FromMicroseconds(0);
41 ui::GestureEvent
gesture_event(0, 0, 0, time_stamp
, gesture_details
);
42 button
->OnGestureEvent(&gesture_event
);
47 typedef ViewsTestBase CustomButtonTest
;
49 // Tests that hover state changes correctly when visiblity/enableness changes.
50 TEST_F(CustomButtonTest
, HoverStateOnVisibilityChange
) {
51 // Create a widget so that the CustomButton can query the hover state
53 scoped_ptr
<Widget
> widget(new Widget
);
54 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
55 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
56 params
.bounds
= gfx::Rect(0, 0, 650, 650);
60 aura::test::TestCursorClient
cursor_client(
61 widget
->GetNativeView()->GetRootWindow());
63 // Position the widget in a way so that it is under the cursor.
64 gfx::Point cursor
= gfx::Screen::GetScreenFor(
65 widget
->GetNativeView())->GetCursorScreenPoint();
66 gfx::Rect widget_bounds
= widget
->GetWindowBoundsInScreen();
67 widget_bounds
.set_origin(cursor
);
68 widget
->SetBounds(widget_bounds
);
70 TestCustomButton
* button
= new TestCustomButton(NULL
);
71 widget
->SetContentsView(button
);
73 gfx::Point
center(10, 10);
74 button
->OnMousePressed(ui::MouseEvent(ui::ET_MOUSE_PRESSED
, center
, center
,
75 ui::EF_LEFT_MOUSE_BUTTON
,
76 ui::EF_LEFT_MOUSE_BUTTON
));
77 EXPECT_EQ(CustomButton::STATE_PRESSED
, button
->state());
79 button
->OnMouseReleased(ui::MouseEvent(ui::ET_MOUSE_RELEASED
, center
, center
,
80 ui::EF_LEFT_MOUSE_BUTTON
,
81 ui::EF_LEFT_MOUSE_BUTTON
));
82 EXPECT_EQ(CustomButton::STATE_HOVERED
, button
->state());
84 button
->SetEnabled(false);
85 EXPECT_EQ(CustomButton::STATE_DISABLED
, button
->state());
87 button
->SetEnabled(true);
88 EXPECT_EQ(CustomButton::STATE_HOVERED
, button
->state());
90 button
->SetVisible(false);
91 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
93 button
->SetVisible(true);
94 EXPECT_EQ(CustomButton::STATE_HOVERED
, button
->state());
96 // In Aura views, no new hover effects are invoked if mouse events
98 cursor_client
.DisableMouseEvents();
100 button
->SetEnabled(false);
101 EXPECT_EQ(CustomButton::STATE_DISABLED
, button
->state());
103 button
->SetEnabled(true);
104 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
106 button
->SetVisible(false);
107 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
109 button
->SetVisible(true);
110 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
113 // Tests that gesture events correctly change the button state.
114 TEST_F(CustomButtonTest
, GestureEventsSetState
) {
115 // Create a widget so that the CustomButton can query the hover state
117 scoped_ptr
<Widget
> widget(new Widget
);
118 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
119 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
120 params
.bounds
= gfx::Rect(0, 0, 650, 650);
121 widget
->Init(params
);
124 aura::test::TestCursorClient
cursor_client(
125 widget
->GetNativeView()->GetRootWindow());
127 TestCustomButton
* button
= new TestCustomButton(NULL
);
128 widget
->SetContentsView(button
);
130 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
132 PerformGesture(button
, ui::ET_GESTURE_TAP_DOWN
);
133 EXPECT_EQ(CustomButton::STATE_PRESSED
, button
->state());
135 PerformGesture(button
, ui::ET_GESTURE_SHOW_PRESS
);
136 EXPECT_EQ(CustomButton::STATE_PRESSED
, button
->state());
138 PerformGesture(button
, ui::ET_GESTURE_TAP_CANCEL
);
139 EXPECT_EQ(CustomButton::STATE_NORMAL
, button
->state());
142 // Ensure subclasses of CustomButton are correctly recognized as CustomButton.
143 TEST_F(CustomButtonTest
, AsCustomButton
) {
146 LabelButton
label_button(NULL
, text
);
147 EXPECT_TRUE(CustomButton::AsCustomButton(&label_button
));
149 ImageButton
image_button(NULL
);
150 EXPECT_TRUE(CustomButton::AsCustomButton(&image_button
));
152 Checkbox
checkbox(text
);
153 EXPECT_TRUE(CustomButton::AsCustomButton(&checkbox
));
155 RadioButton
radio_button(text
, 0);
156 EXPECT_TRUE(CustomButton::AsCustomButton(&radio_button
));
158 MenuButton
menu_button(NULL
, text
, NULL
, false);
159 EXPECT_TRUE(CustomButton::AsCustomButton(&menu_button
));
162 EXPECT_FALSE(CustomButton::AsCustomButton(&label
));
165 EXPECT_FALSE(CustomButton::AsCustomButton(&link
));
168 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield
));