Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Unified Diff: ui/views/controls/combobox/combobox_unittest.cc

Issue 59383003: Add the button style for combobox (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sky's review (5) Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3294677aee92c8f91042055e189c577b1c191319 100644
--- a/ui/views/controls/combobox/combobox_unittest.cc
+++ b/ui/views/controls/combobox/combobox_unittest.cc
@@ -12,6 +12,8 @@
#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_core.h"
#include "ui/views/ime/mock_input_method.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h"
@@ -20,6 +22,31 @@ namespace views {
namespace {
+// An dummy implementation of MenuRunnerCore to check if the dropdown menu is
+// shown or not.
+class TestMenuRunnerCore : public MenuRunnerCore {
+ public:
+ TestMenuRunnerCore()
+ : 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(TestMenuRunnerCore);
+};
+
// A wrapper of Combobox to intercept the result of OnKeyPressed() and
// OnKeyReleased() methods.
class TestCombobox : public Combobox {
@@ -106,6 +133,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 +186,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 +199,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 +216,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 +445,83 @@ 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.
+ TestMenuRunnerCore* menu_runner_core = new TestMenuRunnerCore();
+ combobox_->SetMenuRunnerCore(menu_runner_core);
+ PerformClick(gfx::Point(combobox_->x() + 1,
+ combobox_->y() + combobox_->height() / 2));
+ EXPECT_FALSE(listener.on_combobox_text_button_clicked_called());
+ EXPECT_TRUE(menu_runner_core->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.
+ TestMenuRunnerCore* menu_runner_core = new TestMenuRunnerCore();
+ combobox_->SetMenuRunnerCore(menu_runner_core);
+ 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_core->executed());
+
+ // Click the left side (text button). The click event is notified.
+ menu_runner_core = new TestMenuRunnerCore();
+ combobox_->SetMenuRunnerCore(menu_runner_core);
+ PerformClick(gfx::Point(combobox_->x() + 1,
+ combobox_->y() + combobox_->height() / 2));
+ EXPECT_TRUE(listener.on_combobox_text_button_clicked_called());
+ EXPECT_FALSE(menu_runner_core->executed());
+}
+
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698