Index: ui/views/controls/combobox/combobox_unittest.cc |
diff --git a/ui/views/controls/combobox/combobox_unittest.cc b/ui/views/controls/combobox/combobox_unittest.cc |
index 087313f820f83186446a1b22aa2562da829cd8d9..2104e9d8c582661630a616cf3a3827cd0a6a8684 100644 |
--- a/ui/views/controls/combobox/combobox_unittest.cc |
+++ b/ui/views/controls/combobox/combobox_unittest.cc |
@@ -12,7 +12,10 @@ |
#include "ui/events/event.h" |
#include "ui/events/keycodes/keyboard_codes.h" |
#include "ui/views/controls/combobox/combobox_listener.h" |
+#include "ui/views/controls/menu/menu_runner.h" |
+#include "ui/views/controls/menu/menu_runner_handler.h" |
#include "ui/views/ime/mock_input_method.h" |
+#include "ui/views/test/menu_runner_test_api.h" |
#include "ui/views/test/views_test_base.h" |
#include "ui/views/widget/widget.h" |
@@ -20,6 +23,31 @@ namespace views { |
namespace { |
+// An dummy implementation of MenuRunnerHandler to check if the dropdown menu is |
+// shown or not. |
+class TestMenuRunnerHandler : public MenuRunnerHandler { |
+ public: |
+ TestMenuRunnerHandler() |
+ : executed_(false) {} |
+ |
+ bool executed() const { return executed_; } |
+ |
+ virtual MenuRunner::RunResult RunMenuAt(Widget* parent, |
+ MenuButton* button, |
+ const gfx::Rect& bounds, |
+ MenuItemView::AnchorPosition anchor, |
+ ui::MenuSourceType source_type, |
+ int32 types) OVERRIDE { |
+ executed_ = true; |
+ return MenuRunner::NORMAL_EXIT; |
+ } |
+ |
+ private: |
+ bool executed_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestMenuRunnerHandler); |
+}; |
+ |
// A wrapper of Combobox to intercept the result of OnKeyPressed() and |
// OnKeyReleased() methods. |
class TestCombobox : public Combobox { |
@@ -106,6 +134,38 @@ class EvilListener : public ComboboxListener { |
DISALLOW_COPY_AND_ASSIGN(EvilListener); |
}; |
+class TestComboboxListener : public views::ComboboxListener { |
+ public: |
+ TestComboboxListener() |
+ : on_selected_index_changed_called_(false), |
+ on_combobox_text_button_clicked_called_(false) { |
+ } |
+ virtual ~TestComboboxListener() {} |
+ |
+ virtual void OnSelectedIndexChanged(views::Combobox* combobox) OVERRIDE { |
+ on_selected_index_changed_called_ = true; |
+ } |
+ |
+ virtual void OnComboboxTextButtonClicked(views::Combobox* combobox) OVERRIDE { |
+ on_combobox_text_button_clicked_called_ = true; |
+ } |
+ |
+ bool on_selected_index_changed_called() const { |
+ return on_selected_index_changed_called_; |
+ } |
+ |
+ bool on_combobox_text_button_clicked_called() const { |
+ return on_combobox_text_button_clicked_called_; |
+ } |
+ |
+ private: |
+ bool on_selected_index_changed_called_; |
+ bool on_combobox_text_button_clicked_called_; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); |
+}; |
+ |
} // namespace |
class ComboboxTest : public ViewsTestBase { |
@@ -127,7 +187,7 @@ class ComboboxTest : public ViewsTestBase { |
widget_ = new Widget; |
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
- params.bounds = gfx::Rect(100, 100, 100, 100); |
+ params.bounds = gfx::Rect(200, 200, 200, 200); |
widget_->Init(params); |
View* container = new View(); |
widget_->SetContentsView(container); |
@@ -140,11 +200,16 @@ class ComboboxTest : public ViewsTestBase { |
input_method_->OnFocus(); |
combobox_->RequestFocus(); |
+ combobox_->SizeToPreferredSize(); |
} |
protected: |
void SendKeyEvent(ui::KeyboardCode key_code) { |
- ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, 0, false); |
+ SendKeyEventWithType(key_code, ui::ET_KEY_PRESSED); |
+ } |
+ |
+ void SendKeyEventWithType(ui::KeyboardCode key_code, ui::EventType type) { |
+ ui::KeyEvent event(type, key_code, 0, false); |
input_method_->DispatchKeyEvent(event); |
} |
@@ -152,6 +217,17 @@ class ComboboxTest : public ViewsTestBase { |
return widget_->GetFocusManager()->GetFocusedView(); |
} |
+ void PerformClick(const gfx::Point& point) { |
+ ui::MouseEvent pressed_event = ui::MouseEvent(ui::ET_MOUSE_PRESSED, point, |
+ point, |
+ ui::EF_LEFT_MOUSE_BUTTON); |
+ widget_->OnMouseEvent(&pressed_event); |
+ ui::MouseEvent released_event = ui::MouseEvent(ui::ET_MOUSE_RELEASED, point, |
+ point, |
+ ui::EF_LEFT_MOUSE_BUTTON); |
+ widget_->OnMouseEvent(&released_event); |
+ } |
+ |
// We need widget to populate wrapper class. |
Widget* widget_; |
@@ -370,4 +446,90 @@ TEST_F(ComboboxTest, ListenerHandlesDelete) { |
EXPECT_TRUE(evil_listener.deleted()); |
} |
+TEST_F(ComboboxTest, Click) { |
+ InitCombobox(); |
+ |
+ TestComboboxListener listener; |
+ combobox_->set_listener(&listener); |
+ |
+ combobox_->Layout(); |
+ |
+ // Click the left side. The menu is shown. |
+ TestMenuRunnerHandler* menu_runner_handler = new TestMenuRunnerHandler(); |
+ test::MenuRunnerTestAPI test_api( |
+ combobox_->dropdown_list_menu_runner_.get()); |
+ test_api.SetRunnerHandler(menu_runner_handler); |
+ PerformClick(gfx::Point(combobox_->x() + 1, |
+ combobox_->y() + combobox_->height() / 2)); |
+ EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
+ EXPECT_TRUE(menu_runner_handler->executed()); |
+} |
+ |
+TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) { |
+ InitCombobox(); |
+ |
+ TestComboboxListener listener; |
+ combobox_->set_listener(&listener); |
+ |
+ // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored. |
+ SendKeyEvent(ui::VKEY_RETURN); |
+ EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
+ |
+ // With STYLE_NOTIFY_ON_CLICK, the click event is notified. |
+ combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); |
+ SendKeyEvent(ui::VKEY_RETURN); |
+ EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); |
+} |
+ |
+TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { |
+ InitCombobox(); |
+ |
+ TestComboboxListener listener; |
+ combobox_->set_listener(&listener); |
+ |
+ // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored. |
+ SendKeyEvent(ui::VKEY_SPACE); |
+ EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
+ SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); |
+ EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
+ |
+ // With STYLE_NOTIFY_ON_CLICK, the click event is notified after releasing. |
+ combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); |
+ SendKeyEvent(ui::VKEY_SPACE); |
+ EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
+ SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); |
+ EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); |
+} |
+ |
+TEST_F(ComboboxTest, NotifyOnClickWithMouse) { |
+ InitCombobox(); |
+ |
+ TestComboboxListener listener; |
+ combobox_->set_listener(&listener); |
+ |
+ combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); |
+ combobox_->Layout(); |
+ |
+ // Click the right side (arrow button). The menu is shown. |
+ TestMenuRunnerHandler* menu_runner_handler = new TestMenuRunnerHandler(); |
+ scoped_ptr<test::MenuRunnerTestAPI> test_api( |
+ new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get())); |
+ test_api->SetRunnerHandler(menu_runner_handler); |
+ |
+ PerformClick(gfx::Point(combobox_->x() + combobox_->width() - 1, |
+ combobox_->y() + combobox_->height() / 2)); |
+ EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
+ EXPECT_TRUE(menu_runner_handler->executed()); |
+ |
+ // Click the left side (text button). The click event is notified. |
+ menu_runner_handler = new TestMenuRunnerHandler(); |
+ test_api.reset( |
+ new test::MenuRunnerTestAPI(combobox_->dropdown_list_menu_runner_.get())); |
+ test_api->SetRunnerHandler(menu_runner_handler); |
+ PerformClick(gfx::Point(combobox_->x() + 1, |
+ combobox_->y() + combobox_->height() / 2)); |
+ EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); |
+ EXPECT_FALSE(menu_runner_handler->executed()); |
+} |
+ |
} // namespace views |