Chromium Code Reviews| 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 580b79f0245801a02b90f94d66efced477dc7ec6..99e16bca2f4f1c0d14f32d3fec548cda65b9d227 100644 |
| --- a/ui/views/controls/combobox/combobox_unittest.cc |
| +++ b/ui/views/controls/combobox/combobox_unittest.cc |
| @@ -117,13 +117,36 @@ class TestComboboxModel : public ui::ComboboxModel { |
| DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); |
| }; |
| +// A combobox model which refers to a vector. |
| +class VectorComboboxModel : public ui::ComboboxModel { |
| + public: |
| + VectorComboboxModel(std::vector<std::string>* values) |
| + : values_(values) { |
| + } |
| + virtual ~VectorComboboxModel() {} |
| + |
| + // ui::ComboboxModel: |
| + virtual int GetItemCount() const OVERRIDE { |
| + return values_->size(); |
| + } |
| + virtual base::string16 GetItemAt(int index) OVERRIDE { |
| + return ASCIIToUTF16(values_->at(index)); |
| + } |
| + virtual bool IsItemSeparatorAt(int index) OVERRIDE { |
| + return false; |
| + } |
| + |
| + private: |
| + std::vector<std::string>* values_; |
| +}; |
| + |
| class EvilListener : public ComboboxListener { |
| public: |
| EvilListener() : deleted_(false) {}; |
| virtual ~EvilListener() {}; |
| // ComboboxListener: |
| - virtual void OnSelectedIndexChanged(Combobox* combobox) OVERRIDE { |
| + virtual void OnPerformAction(Combobox* combobox) OVERRIDE { |
| delete combobox; |
| deleted_ = true; |
| } |
| @@ -139,30 +162,27 @@ class EvilListener : public ComboboxListener { |
| class TestComboboxListener : public views::ComboboxListener { |
| public: |
| TestComboboxListener() |
| - : on_selected_index_changed_called_(false), |
| - on_combobox_text_button_clicked_called_(false) { |
| + : perform_action_index_(-1), |
| + on_perform_action_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; |
| + virtual void OnPerformAction(views::Combobox* combobox) OVERRIDE { |
| + perform_action_index_ = combobox->selected_index(); |
| + on_perform_action_called_ = true; |
| } |
| - bool on_selected_index_changed_called() const { |
| - return on_selected_index_changed_called_; |
| + int perform_action_index() const { |
| + return perform_action_index_; |
| } |
| - bool on_combobox_text_button_clicked_called() const { |
| - return on_combobox_text_button_clicked_called_; |
| + bool on_perform_action_called() const { |
| + return on_perform_action_called_; |
| } |
| private: |
| - bool on_selected_index_changed_called_; |
| - bool on_combobox_text_button_clicked_called_; |
| + int perform_action_index_; |
| + bool on_perform_action_called_; |
| private: |
| DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); |
| @@ -439,6 +459,28 @@ TEST_F(ComboboxTest, SelectValue) { |
| EXPECT_EQ(1, combobox_->selected_index()); |
| EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); |
| EXPECT_EQ(1, combobox_->selected_index()); |
| + |
| + // With the action style, the selected index is always 0. |
| + combobox_->SetStyle(Combobox::STYLE_ACTION); |
| + EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); |
| + EXPECT_EQ(0, combobox_->selected_index()); |
| + EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); |
| + EXPECT_EQ(0, combobox_->selected_index()); |
| + EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); |
| + EXPECT_EQ(0, combobox_->selected_index()); |
| +} |
| + |
| +TEST_F(ComboboxTest, SelectIndexActionStyle) { |
| + InitCombobox(); |
| + |
| + // With the action style, the selected index is always 0. |
| + combobox_->SetStyle(Combobox::STYLE_ACTION); |
| + combobox_->SetSelectedIndex(1); |
| + EXPECT_EQ(0, combobox_->selected_index()); |
| + combobox_->SetSelectedIndex(2); |
| + EXPECT_EQ(0, combobox_->selected_index()); |
| + combobox_->SetSelectedIndex(3); |
| + EXPECT_EQ(0, combobox_->selected_index()); |
| } |
| TEST_F(ComboboxTest, ListenerHandlesDelete) { |
| @@ -447,6 +489,7 @@ TEST_F(ComboboxTest, ListenerHandlesDelete) { |
| EvilListener evil_listener; |
| combobox->set_listener(&evil_listener); |
| ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2)); |
| + base::MessageLoop::current()->RunUntilIdle(); |
|
sky
2014/02/03 16:58:19
This shouldn't be necessary any where in this file
hajimehoshi
2014/02/04 04:06:32
Done.
|
| EXPECT_TRUE(evil_listener.deleted()); |
| } |
| @@ -466,7 +509,8 @@ TEST_F(ComboboxTest, Click) { |
| test_api.SetMenuRunnerHandler(menu_runner_handler.Pass()); |
| PerformClick(gfx::Point(combobox_->x() + 1, |
| combobox_->y() + combobox_->height() / 2)); |
| - EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_FALSE(listener.on_perform_action_called()); |
| EXPECT_TRUE(test_menu_runner_handler->executed()); |
| } |
| @@ -476,14 +520,17 @@ TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) { |
| TestComboboxListener listener; |
| combobox_->set_listener(&listener); |
| - // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored. |
| + // With STYLE_NORMAL, the click event is ignored. |
| SendKeyEvent(ui::VKEY_RETURN); |
| - EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_FALSE(listener.on_perform_action_called()); |
| - // With STYLE_NOTIFY_ON_CLICK, the click event is notified. |
| - combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); |
| + // With STYLE_ACTION, the click event is notified. |
| + combobox_->SetStyle(Combobox::STYLE_ACTION); |
| SendKeyEvent(ui::VKEY_RETURN); |
| - EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_TRUE(listener.on_perform_action_called()); |
| + EXPECT_EQ(0, listener.perform_action_index()); |
| } |
| TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { |
| @@ -492,18 +539,23 @@ TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { |
| TestComboboxListener listener; |
| combobox_->set_listener(&listener); |
| - // With STYLE_SHOW_DROP_DOWN_ON_CLICK, the click event is ignored. |
| + // With STYLE_NORMAL, the click event is ignored. |
| SendKeyEvent(ui::VKEY_SPACE); |
| - EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_FALSE(listener.on_perform_action_called()); |
| SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); |
| - EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_FALSE(listener.on_perform_action_called()); |
| - // With STYLE_NOTIFY_ON_CLICK, the click event is notified after releasing. |
| - combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); |
| + // With STYLE_ACTION, the click event is notified after releasing. |
| + combobox_->SetStyle(Combobox::STYLE_ACTION); |
| SendKeyEvent(ui::VKEY_SPACE); |
| - EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_FALSE(listener.on_perform_action_called()); |
| SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); |
| - EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_TRUE(listener.on_perform_action_called()); |
| + EXPECT_EQ(0, listener.perform_action_index()); |
| } |
| TEST_F(ComboboxTest, NotifyOnClickWithMouse) { |
| @@ -512,7 +564,7 @@ TEST_F(ComboboxTest, NotifyOnClickWithMouse) { |
| TestComboboxListener listener; |
| combobox_->set_listener(&listener); |
| - combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); |
| + combobox_->SetStyle(Combobox::STYLE_ACTION); |
| combobox_->Layout(); |
| // Click the right side (arrow button). The menu is shown. |
| @@ -524,7 +576,8 @@ TEST_F(ComboboxTest, NotifyOnClickWithMouse) { |
| PerformClick(gfx::Point(combobox_->x() + combobox_->width() - 1, |
| combobox_->y() + combobox_->height() / 2)); |
| - EXPECT_FALSE(listener.on_combobox_text_button_clicked_called()); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_FALSE(listener.on_perform_action_called()); |
| EXPECT_TRUE(test_menu_runner_handler->executed()); |
| // Click the left side (text button). The click event is notified. |
| @@ -535,8 +588,10 @@ TEST_F(ComboboxTest, NotifyOnClickWithMouse) { |
| test_api->SetMenuRunnerHandler(menu_runner_handler.Pass()); |
| PerformClick(gfx::Point(combobox_->x() + 1, |
| combobox_->y() + combobox_->height() / 2)); |
| - EXPECT_TRUE(listener.on_combobox_text_button_clicked_called()); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_TRUE(listener.on_perform_action_called()); |
| EXPECT_FALSE(test_menu_runner_handler->executed()); |
| + EXPECT_EQ(0, listener.perform_action_index()); |
| } |
| TEST_F(ComboboxTest, ConsumingPressKeyEvents) { |
| @@ -547,13 +602,48 @@ TEST_F(ComboboxTest, ConsumingPressKeyEvents) { |
| EXPECT_FALSE(combobox_->OnKeyPressed( |
| ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false))); |
| - // When the combobox's style is STYLE_NOTIFY_ON_CLICK, pressing events of |
| - // a space key or an enter key will be consumed. |
| - combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); |
| + // When the combobox's style is STYLE_ACTION, pressing events of a space key |
| + // or an enter key will be consumed. |
| + combobox_->SetStyle(Combobox::STYLE_ACTION); |
| EXPECT_TRUE(combobox_->OnKeyPressed( |
| ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false))); |
| EXPECT_TRUE(combobox_->OnKeyPressed( |
| ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false))); |
| } |
| +TEST_F(ComboboxTest, ContentWidth) { |
| + std::vector<std::string> values; |
| + |
| + scoped_ptr<VectorComboboxModel> model(new VectorComboboxModel(&values)); |
| + combobox_ = new TestCombobox(model.get()); |
| + |
| + std::string long_item = "this is the long item"; |
| + std::string short_item = "s"; |
| + |
| + values.resize(1); |
| + values[0] = long_item; |
| + combobox_->ModelChanged(); |
| + |
| + const int long_item_width = combobox_->content_size_.width(); |
| + |
| + values[0] = short_item; |
| + combobox_->ModelChanged(); |
| + |
| + const int short_item_width = combobox_->content_size_.width(); |
| + |
| + values.resize(2); |
| + values[0] = short_item; |
| + values[1] = long_item; |
| + combobox_->ModelChanged(); |
| + |
| + // When the style is STYLE_NORMAL, the width will fit with the longest item. |
| + combobox_->SetStyle(Combobox::STYLE_NORMAL); |
| + EXPECT_EQ(long_item_width, combobox_->content_size_.width()); |
| + |
| + // When the style is STYLE_ACTION, the width will fit with the first items' |
| + // width. |
| + combobox_->SetStyle(Combobox::STYLE_ACTION); |
| + EXPECT_EQ(short_item_width, combobox_->content_size_.width()); |
| +} |
| + |
| } // namespace views |