OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/menu_button.h" | 5 #include "ui/views/controls/button/menu_button.h" |
6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" |
7 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
8 #include "ui/events/test/event_generator.h" | 9 #include "ui/events/test/event_generator.h" |
9 #include "ui/views/controls/button/menu_button_listener.h" | 10 #include "ui/views/controls/button/menu_button_listener.h" |
10 #include "ui/views/test/views_test_base.h" | 11 #include "ui/views/test/views_test_base.h" |
11 | 12 |
12 using base::ASCIIToUTF16; | 13 using base::ASCIIToUTF16; |
13 | 14 |
14 namespace views { | 15 namespace views { |
15 | 16 |
16 class MenuButtonTest : public ViewsTestBase { | 17 class MenuButtonTest : public ViewsTestBase { |
17 public: | 18 public: |
18 MenuButtonTest() : widget_(NULL), button_(NULL) {} | 19 MenuButtonTest() : widget_(NULL), button_(NULL) {} |
19 virtual ~MenuButtonTest() {} | 20 virtual ~MenuButtonTest() {} |
20 | 21 |
21 virtual void TearDown() OVERRIDE { | 22 virtual void TearDown() OVERRIDE { |
22 if (widget_ && !widget_->IsClosed()) | 23 if (widget_ && !widget_->IsClosed()) |
23 widget_->Close(); | 24 widget_->Close(); |
24 | 25 |
25 ViewsTestBase::TearDown(); | 26 ViewsTestBase::TearDown(); |
26 } | 27 } |
27 | 28 |
28 Widget* widget() { return widget_; } | 29 Widget* widget() { return widget_; } |
29 MenuButton* button() { return button_; } | 30 MenuButton* button() { return button_; } |
30 | 31 |
31 protected: | 32 protected: |
| 33 // Creates a MenuButton with no button listener. |
| 34 void CreateMenuButtonWithNoListener() { |
| 35 CreateMenuButton(NULL, NULL); |
| 36 } |
| 37 |
32 // Creates a MenuButton with a ButtonListener. In this case, the MenuButton | 38 // Creates a MenuButton with a ButtonListener. In this case, the MenuButton |
33 // acts like a regular button. | 39 // acts like a regular button. |
34 void CreateMenuButtonWithButtonListener(ButtonListener* button_listener) { | 40 void CreateMenuButtonWithButtonListener(ButtonListener* button_listener) { |
35 CreateWidget(); | 41 CreateMenuButton(button_listener, NULL); |
36 | |
37 const base::string16 label(ASCIIToUTF16("button")); | |
38 button_ = new MenuButton(button_listener, label, NULL, false); | |
39 button_->SetBoundsRect(gfx::Rect(0, 0, 200, 20)); | |
40 widget_->SetContentsView(button_); | |
41 | |
42 widget_->Show(); | |
43 } | 42 } |
44 | 43 |
45 // Creates a MenuButton with a MenuButtonListener. In this case, when the | 44 // Creates a MenuButton with a MenuButtonListener. In this case, when the |
46 // MenuButton is pushed, it notifies the MenuButtonListener to open a | 45 // MenuButton is pushed, it notifies the MenuButtonListener to open a |
47 // drop-down menu. | 46 // drop-down menu. |
48 void CreateMenuButtonWithMenuButtonListener( | 47 void CreateMenuButtonWithMenuButtonListener( |
49 MenuButtonListener* menu_button_listener) { | 48 MenuButtonListener* menu_button_listener) { |
| 49 CreateMenuButton(NULL, menu_button_listener); |
| 50 } |
| 51 |
| 52 private: |
| 53 void CreateMenuButton(ButtonListener* button_listener, |
| 54 MenuButtonListener* menu_button_listener) { |
50 CreateWidget(); | 55 CreateWidget(); |
51 | 56 |
52 const base::string16 label(ASCIIToUTF16("button")); | 57 const base::string16 label(ASCIIToUTF16("button")); |
53 button_ = new MenuButton(NULL, label, menu_button_listener, false); | 58 button_ = |
| 59 new MenuButton(button_listener, label, menu_button_listener, false); |
54 button_->SetBoundsRect(gfx::Rect(0, 0, 200, 20)); | 60 button_->SetBoundsRect(gfx::Rect(0, 0, 200, 20)); |
55 widget_->SetContentsView(button_); | 61 widget_->SetContentsView(button_); |
56 | 62 |
57 widget_->Show(); | 63 widget_->Show(); |
58 } | 64 } |
59 | 65 |
60 private: | |
61 void CreateWidget() { | 66 void CreateWidget() { |
62 DCHECK(!widget_); | 67 DCHECK(!widget_); |
63 | 68 |
64 widget_ = new Widget; | 69 widget_ = new Widget; |
65 Widget::InitParams params = | 70 Widget::InitParams params = |
66 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 71 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
67 params.bounds = gfx::Rect(0, 0, 200, 200); | 72 params.bounds = gfx::Rect(0, 0, 200, 200); |
68 widget_->Init(params); | 73 widget_->Init(params); |
69 } | 74 } |
70 | 75 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 187 |
183 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 188 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); |
184 generator.GestureTapAt(gfx::Point(10, 10)); | 189 generator.GestureTapAt(gfx::Point(10, 10)); |
185 | 190 |
186 // Check that MenuButton has notified the listener, while it was in pressed | 191 // Check that MenuButton has notified the listener, while it was in pressed |
187 // state. | 192 // state. |
188 EXPECT_EQ(button(), menu_button_listener->last_source()); | 193 EXPECT_EQ(button(), menu_button_listener->last_source()); |
189 EXPECT_EQ(Button::STATE_PRESSED, menu_button_listener->last_source_state()); | 194 EXPECT_EQ(Button::STATE_PRESSED, menu_button_listener->last_source_state()); |
190 } | 195 } |
191 | 196 |
| 197 // Test that the MenuButton stays pressed while there are any PressedLocks. |
| 198 TEST_F(MenuButtonTest, MenuButtonPressedLock) { |
| 199 CreateMenuButtonWithNoListener(); |
| 200 |
| 201 // Move the mouse over the button; the button should be in a hovered state. |
| 202 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); |
| 203 generator.MoveMouseTo(gfx::Point(10, 10)); |
| 204 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 205 |
| 206 // Introduce a PressedLock, which should make the button pressed. |
| 207 scoped_ptr<MenuButton::PressedLock> pressed_lock1( |
| 208 new MenuButton::PressedLock(button())); |
| 209 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 210 |
| 211 // Even if we move the mouse outside of the button, it should remain pressed. |
| 212 generator.MoveMouseTo(gfx::Point(300, 10)); |
| 213 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 214 |
| 215 // Creating a new lock should obviously keep the button pressed. |
| 216 scoped_ptr<MenuButton::PressedLock> pressed_lock2( |
| 217 new MenuButton::PressedLock(button())); |
| 218 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 219 |
| 220 // The button should remain pressed while any locks are active. |
| 221 pressed_lock1.reset(); |
| 222 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 223 |
| 224 // Reseting the final lock should return the button's state to normal... |
| 225 pressed_lock2.reset(); |
| 226 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); |
| 227 |
| 228 // ...And it should respond to mouse movement again. |
| 229 generator.MoveMouseTo(gfx::Point(10, 10)); |
| 230 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 231 } |
| 232 |
192 } // namespace views | 233 } // namespace views |
OLD | NEW |