Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / views / controls / button / menu_button_unittest.cc
bloba41ac38428521a8e522d1bbeb00ce36d520bd17e
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
3 // found in the LICENSE file.
5 #include "ui/views/controls/button/menu_button.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/events/test/event_generator.h"
9 #include "ui/views/controls/button/menu_button_listener.h"
10 #include "ui/views/test/views_test_base.h"
12 using base::ASCIIToUTF16;
14 namespace views {
16 class MenuButtonTest : public ViewsTestBase {
17 public:
18 MenuButtonTest() : widget_(NULL), button_(NULL) {}
19 virtual ~MenuButtonTest() {}
21 virtual void TearDown() OVERRIDE {
22 if (widget_ && !widget_->IsClosed())
23 widget_->Close();
25 ViewsTestBase::TearDown();
28 Widget* widget() { return widget_; }
29 MenuButton* button() { return button_; }
31 protected:
32 // Creates a MenuButton with a ButtonListener. In this case, the MenuButton
33 // acts like a regular button.
34 void CreateMenuButtonWithButtonListener(ButtonListener* button_listener) {
35 CreateWidget();
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_);
42 widget_->Show();
45 // Creates a MenuButton with a MenuButtonListener. In this case, when the
46 // MenuButton is pushed, it notifies the MenuButtonListener to open a
47 // drop-down menu.
48 void CreateMenuButtonWithMenuButtonListener(
49 MenuButtonListener* menu_button_listener) {
50 CreateWidget();
52 const base::string16 label(ASCIIToUTF16("button"));
53 button_ = new MenuButton(NULL, label, menu_button_listener, false);
54 button_->SetBoundsRect(gfx::Rect(0, 0, 200, 20));
55 widget_->SetContentsView(button_);
57 widget_->Show();
60 private:
61 void CreateWidget() {
62 DCHECK(!widget_);
64 widget_ = new Widget;
65 Widget::InitParams params =
66 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
67 params.bounds = gfx::Rect(0, 0, 200, 200);
68 widget_->Init(params);
71 Widget* widget_;
72 MenuButton* button_;
75 class TestButtonListener : public ButtonListener {
76 public:
77 TestButtonListener()
78 : last_sender_(NULL),
79 last_sender_state_(Button::STATE_NORMAL),
80 last_event_type_(ui::ET_UNKNOWN) {}
81 virtual ~TestButtonListener() {}
83 virtual void ButtonPressed(Button* sender, const ui::Event& event) OVERRIDE {
84 last_sender_ = sender;
85 CustomButton* custom_button = CustomButton::AsCustomButton(sender);
86 DCHECK(custom_button);
87 last_sender_state_ = custom_button->state();
88 last_event_type_ = event.type();
91 Button* last_sender() { return last_sender_; }
92 Button::ButtonState last_sender_state() { return last_sender_state_; }
93 ui::EventType last_event_type() { return last_event_type_; }
95 private:
96 Button* last_sender_;
97 Button::ButtonState last_sender_state_;
98 ui::EventType last_event_type_;
100 DISALLOW_COPY_AND_ASSIGN(TestButtonListener);
103 class TestMenuButtonListener : public MenuButtonListener {
104 public:
105 TestMenuButtonListener() {}
106 virtual ~TestMenuButtonListener() {}
108 virtual void OnMenuButtonClicked(View* source,
109 const gfx::Point& /*point*/) OVERRIDE {
110 last_source_ = source;
111 CustomButton* custom_button = CustomButton::AsCustomButton(source);
112 DCHECK(custom_button);
113 last_source_state_ = custom_button->state();
116 View* last_source() { return last_source_; }
117 Button::ButtonState last_source_state() { return last_source_state_; }
119 private:
120 View* last_source_;
121 Button::ButtonState last_source_state_;
124 // Tests if the listener is notified correctly, when a mouse click happens on a
125 // MenuButton that has a regular ButtonListener.
126 TEST_F(MenuButtonTest, ActivateNonDropDownOnMouseClick) {
127 scoped_ptr<TestButtonListener> button_listener(new TestButtonListener);
128 CreateMenuButtonWithButtonListener(button_listener.get());
130 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow());
132 generator.set_current_location(gfx::Point(10, 10));
133 generator.ClickLeftButton();
135 // Check that MenuButton has notified the listener on mouse-released event,
136 // while it was in hovered state.
137 EXPECT_EQ(button(), button_listener->last_sender());
138 EXPECT_EQ(ui::ET_MOUSE_RELEASED, button_listener->last_event_type());
139 EXPECT_EQ(Button::STATE_HOVERED, button_listener->last_sender_state());
142 // Tests if the listener is notified correctly when a gesture tap happens on a
143 // MenuButton that has a regular ButtonListener.
144 TEST_F(MenuButtonTest, ActivateNonDropDownOnGestureTap) {
145 scoped_ptr<TestButtonListener> button_listener(new TestButtonListener);
146 CreateMenuButtonWithButtonListener(button_listener.get());
148 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow());
149 generator.GestureTapAt(gfx::Point(10, 10));
151 // Check that MenuButton has notified the listener on gesture tap event, while
152 // it was in hovered state.
153 EXPECT_EQ(button(), button_listener->last_sender());
154 EXPECT_EQ(ui::ET_GESTURE_TAP, button_listener->last_event_type());
155 EXPECT_EQ(Button::STATE_HOVERED, button_listener->last_sender_state());
158 // Tests if the listener is notified correctly when a mouse click happens on a
159 // MenuButton that has a MenuButtonListener.
160 TEST_F(MenuButtonTest, ActivateDropDownOnMouseClick) {
161 scoped_ptr<TestMenuButtonListener> menu_button_listener(
162 new TestMenuButtonListener);
163 CreateMenuButtonWithMenuButtonListener(menu_button_listener.get());
165 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow());
167 generator.set_current_location(gfx::Point(10, 10));
168 generator.ClickLeftButton();
170 // Check that MenuButton has notified the listener, while it was in pressed
171 // state.
172 EXPECT_EQ(button(), menu_button_listener->last_source());
173 EXPECT_EQ(Button::STATE_PRESSED, menu_button_listener->last_source_state());
176 // Tests if the listener is notified correctly when a gesture tap happens on a
177 // MenuButton that has a MenuButtonListener.
178 TEST_F(MenuButtonTest, ActivateDropDownOnGestureTap) {
179 scoped_ptr<TestMenuButtonListener> menu_button_listener(
180 new TestMenuButtonListener);
181 CreateMenuButtonWithMenuButtonListener(menu_button_listener.get());
183 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow());
184 generator.GestureTapAt(gfx::Point(10, 10));
186 // Check that MenuButton has notified the listener, while it was in pressed
187 // state.
188 EXPECT_EQ(button(), menu_button_listener->last_source());
189 EXPECT_EQ(Button::STATE_PRESSED, menu_button_listener->last_source_state());
192 } // namespace views