| 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/custom_button.h" | 5 #include "ui/views/controls/button/custom_button.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.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" |
| 8 #include "ui/base/layout.h" | 11 #include "ui/base/layout.h" |
| 9 #include "ui/gfx/screen.h" | 12 #include "ui/gfx/screen.h" |
| 10 #include "ui/views/controls/button/checkbox.h" | 13 #include "ui/views/controls/button/checkbox.h" |
| 11 #include "ui/views/controls/button/image_button.h" | 14 #include "ui/views/controls/button/image_button.h" |
| 12 #include "ui/views/controls/button/menu_button.h" | 15 #include "ui/views/controls/button/menu_button.h" |
| 13 #include "ui/views/controls/button/radio_button.h" | 16 #include "ui/views/controls/button/radio_button.h" |
| 14 #include "ui/views/controls/button/text_button.h" | 17 #include "ui/views/controls/button/text_button.h" |
| 15 #include "ui/views/controls/link.h" | 18 #include "ui/views/controls/link.h" |
| 16 #include "ui/views/controls/textfield/textfield.h" | 19 #include "ui/views/controls/textfield/textfield.h" |
| 17 #include "ui/views/test/views_test_base.h" | 20 #include "ui/views/test/views_test_base.h" |
| 18 | 21 |
| 19 #if defined(USE_AURA) | |
| 20 #include "ui/aura/test/test_cursor_client.h" | |
| 21 #include "ui/aura/window.h" | |
| 22 #include "ui/aura/window_event_dispatcher.h" | |
| 23 #endif | |
| 24 | |
| 25 namespace views { | 22 namespace views { |
| 26 | 23 |
| 27 namespace { | 24 namespace { |
| 28 | 25 |
| 29 class TestCustomButton : public CustomButton { | 26 class TestCustomButton : public CustomButton { |
| 30 public: | 27 public: |
| 31 explicit TestCustomButton(ButtonListener* listener) | 28 explicit TestCustomButton(ButtonListener* listener) |
| 32 : CustomButton(listener) { | 29 : CustomButton(listener) { |
| 33 } | 30 } |
| 34 | 31 |
| 35 virtual ~TestCustomButton() {} | 32 virtual ~TestCustomButton() {} |
| 36 | 33 |
| 37 private: | 34 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); | 35 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); |
| 39 }; | 36 }; |
| 40 | 37 |
| 41 #if defined(USE_AURA) | |
| 42 void PerformGesture(CustomButton* button, ui::EventType event_type) { | 38 void PerformGesture(CustomButton* button, ui::EventType event_type) { |
| 43 ui::GestureEventDetails gesture_details(event_type, 0, 0); | 39 ui::GestureEventDetails gesture_details(event_type, 0, 0); |
| 44 base::TimeDelta time_stamp = base::TimeDelta::FromMicroseconds(0); | 40 base::TimeDelta time_stamp = base::TimeDelta::FromMicroseconds(0); |
| 45 ui::GestureEvent gesture_event(gesture_details.type(), 0, 0, 0, time_stamp, | 41 ui::GestureEvent gesture_event(gesture_details.type(), 0, 0, 0, time_stamp, |
| 46 gesture_details, 1); | 42 gesture_details, 1); |
| 47 button->OnGestureEvent(&gesture_event); | 43 button->OnGestureEvent(&gesture_event); |
| 48 } | 44 } |
| 49 #endif // USE_AURA | |
| 50 | 45 |
| 51 } // namespace | 46 } // namespace |
| 52 | 47 |
| 53 typedef ViewsTestBase CustomButtonTest; | 48 typedef ViewsTestBase CustomButtonTest; |
| 54 | 49 |
| 55 // Tests that hover state changes correctly when visiblity/enableness changes. | 50 // Tests that hover state changes correctly when visiblity/enableness changes. |
| 56 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { | 51 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { |
| 57 // Create a widget so that the CustomButton can query the hover state | 52 // Create a widget so that the CustomButton can query the hover state |
| 58 // correctly. | 53 // correctly. |
| 59 scoped_ptr<Widget> widget(new Widget); | 54 scoped_ptr<Widget> widget(new Widget); |
| 60 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 55 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 61 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 56 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 62 params.bounds = gfx::Rect(0, 0, 650, 650); | 57 params.bounds = gfx::Rect(0, 0, 650, 650); |
| 63 widget->Init(params); | 58 widget->Init(params); |
| 64 widget->Show(); | 59 widget->Show(); |
| 65 | 60 |
| 66 #if defined(USE_AURA) | |
| 67 aura::test::TestCursorClient cursor_client( | 61 aura::test::TestCursorClient cursor_client( |
| 68 widget->GetNativeView()->GetRootWindow()); | 62 widget->GetNativeView()->GetRootWindow()); |
| 69 #endif | |
| 70 | 63 |
| 71 // Position the widget in a way so that it is under the cursor. | 64 // Position the widget in a way so that it is under the cursor. |
| 72 gfx::Point cursor = gfx::Screen::GetScreenFor( | 65 gfx::Point cursor = gfx::Screen::GetScreenFor( |
| 73 widget->GetNativeView())->GetCursorScreenPoint(); | 66 widget->GetNativeView())->GetCursorScreenPoint(); |
| 74 gfx::Rect widget_bounds = widget->GetWindowBoundsInScreen(); | 67 gfx::Rect widget_bounds = widget->GetWindowBoundsInScreen(); |
| 75 widget_bounds.set_origin(cursor); | 68 widget_bounds.set_origin(cursor); |
| 76 widget->SetBounds(widget_bounds); | 69 widget->SetBounds(widget_bounds); |
| 77 | 70 |
| 78 TestCustomButton* button = new TestCustomButton(NULL); | 71 TestCustomButton* button = new TestCustomButton(NULL); |
| 79 widget->SetContentsView(button); | 72 widget->SetContentsView(button); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 94 | 87 |
| 95 button->SetEnabled(true); | 88 button->SetEnabled(true); |
| 96 EXPECT_EQ(CustomButton::STATE_HOVERED, button->state()); | 89 EXPECT_EQ(CustomButton::STATE_HOVERED, button->state()); |
| 97 | 90 |
| 98 button->SetVisible(false); | 91 button->SetVisible(false); |
| 99 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); | 92 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); |
| 100 | 93 |
| 101 button->SetVisible(true); | 94 button->SetVisible(true); |
| 102 EXPECT_EQ(CustomButton::STATE_HOVERED, button->state()); | 95 EXPECT_EQ(CustomButton::STATE_HOVERED, button->state()); |
| 103 | 96 |
| 104 #if defined(USE_AURA) | |
| 105 // In Aura views, no new hover effects are invoked if mouse events | 97 // In Aura views, no new hover effects are invoked if mouse events |
| 106 // are disabled. | 98 // are disabled. |
| 107 cursor_client.DisableMouseEvents(); | 99 cursor_client.DisableMouseEvents(); |
| 108 | 100 |
| 109 button->SetEnabled(false); | 101 button->SetEnabled(false); |
| 110 EXPECT_EQ(CustomButton::STATE_DISABLED, button->state()); | 102 EXPECT_EQ(CustomButton::STATE_DISABLED, button->state()); |
| 111 | 103 |
| 112 button->SetEnabled(true); | 104 button->SetEnabled(true); |
| 113 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); | 105 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); |
| 114 | 106 |
| 115 button->SetVisible(false); | 107 button->SetVisible(false); |
| 116 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); | 108 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); |
| 117 | 109 |
| 118 button->SetVisible(true); | 110 button->SetVisible(true); |
| 119 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); | 111 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); |
| 120 #endif | |
| 121 } | 112 } |
| 122 | 113 |
| 123 #if defined(USE_AURA) | |
| 124 // Tests that gesture events correctly change the button state. | 114 // Tests that gesture events correctly change the button state. |
| 125 TEST_F(CustomButtonTest, GestureEventsSetState) { | 115 TEST_F(CustomButtonTest, GestureEventsSetState) { |
| 126 // Create a widget so that the CustomButton can query the hover state | 116 // Create a widget so that the CustomButton can query the hover state |
| 127 // correctly. | 117 // correctly. |
| 128 scoped_ptr<Widget> widget(new Widget); | 118 scoped_ptr<Widget> widget(new Widget); |
| 129 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 119 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 130 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 120 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 131 params.bounds = gfx::Rect(0, 0, 650, 650); | 121 params.bounds = gfx::Rect(0, 0, 650, 650); |
| 132 widget->Init(params); | 122 widget->Init(params); |
| 133 widget->Show(); | 123 widget->Show(); |
| 134 | 124 |
| 135 aura::test::TestCursorClient cursor_client( | 125 aura::test::TestCursorClient cursor_client( |
| 136 widget->GetNativeView()->GetRootWindow()); | 126 widget->GetNativeView()->GetRootWindow()); |
| 137 | 127 |
| 138 TestCustomButton* button = new TestCustomButton(NULL); | 128 TestCustomButton* button = new TestCustomButton(NULL); |
| 139 widget->SetContentsView(button); | 129 widget->SetContentsView(button); |
| 140 | 130 |
| 141 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); | 131 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); |
| 142 | 132 |
| 143 PerformGesture(button, ui::ET_GESTURE_TAP_DOWN); | 133 PerformGesture(button, ui::ET_GESTURE_TAP_DOWN); |
| 144 EXPECT_EQ(CustomButton::STATE_PRESSED, button->state()); | 134 EXPECT_EQ(CustomButton::STATE_PRESSED, button->state()); |
| 145 | 135 |
| 146 PerformGesture(button, ui::ET_GESTURE_SHOW_PRESS); | 136 PerformGesture(button, ui::ET_GESTURE_SHOW_PRESS); |
| 147 EXPECT_EQ(CustomButton::STATE_PRESSED, button->state()); | 137 EXPECT_EQ(CustomButton::STATE_PRESSED, button->state()); |
| 148 | 138 |
| 149 PerformGesture(button, ui::ET_GESTURE_TAP_CANCEL); | 139 PerformGesture(button, ui::ET_GESTURE_TAP_CANCEL); |
| 150 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); | 140 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); |
| 151 } | 141 } |
| 152 | 142 |
| 153 #endif // USE_AURA | |
| 154 | |
| 155 // Make sure all subclasses of CustomButton are correctly recognized | 143 // Make sure all subclasses of CustomButton are correctly recognized |
| 156 // as CustomButton. | 144 // as CustomButton. |
| 157 TEST_F(CustomButtonTest, AsCustomButton) { | 145 TEST_F(CustomButtonTest, AsCustomButton) { |
| 158 base::string16 text; | 146 base::string16 text; |
| 159 | 147 |
| 160 TextButton text_button(NULL, text); | 148 TextButton text_button(NULL, text); |
| 161 EXPECT_TRUE(CustomButton::AsCustomButton(&text_button)); | 149 EXPECT_TRUE(CustomButton::AsCustomButton(&text_button)); |
| 162 | 150 |
| 163 ImageButton image_button(NULL); | 151 ImageButton image_button(NULL); |
| 164 EXPECT_TRUE(CustomButton::AsCustomButton(&image_button)); | 152 EXPECT_TRUE(CustomButton::AsCustomButton(&image_button)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 176 EXPECT_FALSE(CustomButton::AsCustomButton(&label)); | 164 EXPECT_FALSE(CustomButton::AsCustomButton(&label)); |
| 177 | 165 |
| 178 Link link(text); | 166 Link link(text); |
| 179 EXPECT_FALSE(CustomButton::AsCustomButton(&link)); | 167 EXPECT_FALSE(CustomButton::AsCustomButton(&link)); |
| 180 | 168 |
| 181 Textfield textfield; | 169 Textfield textfield; |
| 182 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield)); | 170 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield)); |
| 183 } | 171 } |
| 184 | 172 |
| 185 } // namespace views | 173 } // namespace views |
| OLD | NEW |