summaryrefslogtreecommitdiff
path: root/chromium/ui/views/controls/combobox/combobox_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/ui/views/controls/combobox/combobox_unittest.cc')
-rw-r--r--chromium/ui/views/controls/combobox/combobox_unittest.cc199
1 files changed, 36 insertions, 163 deletions
diff --git a/chromium/ui/views/controls/combobox/combobox_unittest.cc b/chromium/ui/views/controls/combobox/combobox_unittest.cc
index 5c32debbaf9..f1c486f26d1 100644
--- a/chromium/ui/views/controls/combobox/combobox_unittest.cc
+++ b/chromium/ui/views/controls/combobox/combobox_unittest.cc
@@ -25,6 +25,7 @@
#include "ui/views/test/combobox_test_api.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h"
+#include "ui/views/widget/widget_utils.h"
using base::ASCIIToUTF16;
@@ -38,8 +39,8 @@ namespace {
// OnKeyReleased() methods.
class TestCombobox : public Combobox {
public:
- TestCombobox(ui::ComboboxModel* model, Combobox::Style style)
- : Combobox(model, style), key_handled_(false), key_received_(false) {}
+ explicit TestCombobox(ui::ComboboxModel* model)
+ : Combobox(model), key_handled_(false), key_received_(false) {}
bool OnKeyPressed(const ui::KeyEvent& e) override {
key_received_ = true;
@@ -196,14 +197,14 @@ class ComboboxTest : public ViewsTestBase {
ViewsTestBase::TearDown();
}
- void InitCombobox(const std::set<int>* separators, Combobox::Style style) {
+ void InitCombobox(const std::set<int>* separators) {
model_.reset(new TestComboboxModel());
if (separators)
model_->SetSeparators(*separators);
ASSERT_FALSE(combobox_);
- combobox_ = new TestCombobox(model_.get(), style);
+ combobox_ = new TestCombobox(model_.get());
test_api_.reset(new ComboboxTestApi(combobox_));
test_api_->InstallTestMenuRunner(&menu_show_count_);
combobox_->set_id(1);
@@ -222,7 +223,7 @@ class ComboboxTest : public ViewsTestBase {
combobox_->SizeToPreferredSize();
event_generator_ =
- std::make_unique<ui::test::EventGenerator>(widget_->GetNativeWindow());
+ std::make_unique<ui::test::EventGenerator>(GetRootWindow(widget_));
event_generator_->set_target(ui::test::EventGenerator::Target::WINDOW);
}
@@ -282,7 +283,7 @@ class ComboboxTest : public ViewsTestBase {
// Tests whether the various Mac specific keyboard shortcuts invoke the dropdown
// menu or not.
TEST_F(ComboboxTest, KeyTestMac) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
PressKey(ui::VKEY_END);
EXPECT_EQ(0, combobox_->selected_index());
EXPECT_EQ(1, menu_show_count_);
@@ -331,7 +332,7 @@ TEST_F(ComboboxTest, DisabilityTest) {
model_.reset(new TestComboboxModel());
ASSERT_FALSE(combobox_);
- combobox_ = new TestCombobox(model_.get(), Combobox::STYLE_NORMAL);
+ combobox_ = new TestCombobox(model_.get());
combobox_->SetEnabled(false);
widget_ = new Widget;
@@ -352,7 +353,7 @@ TEST_F(ComboboxTest, DisabilityTest) {
// Tests the behavior of various keyboard shortcuts on the currently selected
// index.
TEST_F(ComboboxTest, KeyTest) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
PressKey(ui::VKEY_END);
EXPECT_EQ(model_->GetItemCount(), combobox_->selected_index() + 1);
PressKey(ui::VKEY_HOME);
@@ -377,7 +378,7 @@ TEST_F(ComboboxTest, KeyTest) {
TEST_F(ComboboxTest, SkipSeparatorSimple) {
std::set<int> separators;
separators.insert(2);
- InitCombobox(&separators, Combobox::STYLE_NORMAL);
+ InitCombobox(&separators);
EXPECT_EQ(0, combobox_->selected_index());
PressKey(ui::VKEY_DOWN);
EXPECT_EQ(1, combobox_->selected_index());
@@ -398,7 +399,7 @@ TEST_F(ComboboxTest, SkipSeparatorSimple) {
TEST_F(ComboboxTest, SkipSeparatorBeginning) {
std::set<int> separators;
separators.insert(0);
- InitCombobox(&separators, Combobox::STYLE_NORMAL);
+ InitCombobox(&separators);
EXPECT_EQ(1, combobox_->selected_index());
PressKey(ui::VKEY_DOWN);
EXPECT_EQ(2, combobox_->selected_index());
@@ -419,7 +420,7 @@ TEST_F(ComboboxTest, SkipSeparatorBeginning) {
TEST_F(ComboboxTest, SkipSeparatorEnd) {
std::set<int> separators;
separators.insert(TestComboboxModel::kItemCount - 1);
- InitCombobox(&separators, Combobox::STYLE_NORMAL);
+ InitCombobox(&separators);
combobox_->SetSelectedIndex(8);
PressKey(ui::VKEY_DOWN);
EXPECT_EQ(8, combobox_->selected_index());
@@ -437,7 +438,7 @@ TEST_F(ComboboxTest, SkipMultipleSeparatorsAtBeginning) {
separators.insert(0);
separators.insert(1);
separators.insert(2);
- InitCombobox(&separators, Combobox::STYLE_NORMAL);
+ InitCombobox(&separators);
EXPECT_EQ(3, combobox_->selected_index());
PressKey(ui::VKEY_DOWN);
EXPECT_EQ(4, combobox_->selected_index());
@@ -461,7 +462,7 @@ TEST_F(ComboboxTest, SkipMultipleAdjacentSeparatorsAtMiddle) {
separators.insert(4);
separators.insert(5);
separators.insert(6);
- InitCombobox(&separators, Combobox::STYLE_NORMAL);
+ InitCombobox(&separators);
combobox_->SetSelectedIndex(3);
PressKey(ui::VKEY_DOWN);
EXPECT_EQ(7, combobox_->selected_index());
@@ -477,7 +478,7 @@ TEST_F(ComboboxTest, SkipMultipleSeparatorsAtEnd) {
separators.insert(7);
separators.insert(8);
separators.insert(9);
- InitCombobox(&separators, Combobox::STYLE_NORMAL);
+ InitCombobox(&separators);
combobox_->SetSelectedIndex(6);
PressKey(ui::VKEY_DOWN);
EXPECT_EQ(6, combobox_->selected_index());
@@ -499,7 +500,7 @@ TEST_F(ComboboxTest, GetTextForRowTest) {
separators.insert(0);
separators.insert(1);
separators.insert(9);
- InitCombobox(&separators, Combobox::STYLE_NORMAL);
+ InitCombobox(&separators);
for (int i = 0; i < combobox_->GetRowCount(); ++i) {
if (separators.count(i) != 0) {
EXPECT_TRUE(combobox_->GetTextForRow(i).empty()) << i;
@@ -512,7 +513,7 @@ TEST_F(ComboboxTest, GetTextForRowTest) {
// Verifies selecting the first matching value (and returning whether found).
TEST_F(ComboboxTest, SelectValue) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index());
EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER")));
EXPECT_EQ(0, combobox_->selected_index());
@@ -522,50 +523,19 @@ TEST_F(ComboboxTest, SelectValue) {
EXPECT_EQ(1, combobox_->selected_index());
}
-TEST_F(ComboboxTest, SelectValueActionStyle) {
- // With the action style, the selected index is always 0.
- InitCombobox(nullptr, 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(nullptr, Combobox::STYLE_ACTION);
-
- // With the action style, the selected index is always 0.
- 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) {
TestComboboxModel model;
// |combobox| will be deleted on change.
- TestCombobox* combobox = new TestCombobox(&model, Combobox::STYLE_NORMAL);
+ TestCombobox* combobox = new TestCombobox(&model);
std::unique_ptr<EvilListener> evil_listener(new EvilListener());
combobox->set_listener(evil_listener.get());
ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2));
EXPECT_TRUE(evil_listener->deleted());
-
- // With STYLE_ACTION
- // |combobox| will be deleted on change.
- combobox = new TestCombobox(&model, Combobox::STYLE_ACTION);
- evil_listener.reset(new EvilListener());
- combobox->set_listener(evil_listener.get());
- ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2));
- EXPECT_TRUE(evil_listener->deleted());
}
TEST_F(ComboboxTest, Click) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
TestComboboxListener listener;
combobox_->set_listener(&listener);
@@ -580,7 +550,7 @@ TEST_F(ComboboxTest, Click) {
}
TEST_F(ComboboxTest, ClickButDisabled) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
TestComboboxListener listener;
combobox_->set_listener(&listener);
@@ -596,44 +566,25 @@ TEST_F(ComboboxTest, ClickButDisabled) {
}
TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
TestComboboxListener listener;
combobox_->set_listener(&listener);
- // With STYLE_NORMAL, the click event is ignored. Instead the menu is shown.
+ // The click event is ignored. Instead the menu is shown.
PressKey(ui::VKEY_RETURN);
EXPECT_EQ(PlatformStyle::kReturnClicksFocusedControl ? 1 : 0,
menu_show_count_);
EXPECT_FALSE(listener.on_perform_action_called());
}
-TEST_F(ComboboxTest, NotifyOnClickWithReturnKeyActionStyle) {
- InitCombobox(nullptr, Combobox::STYLE_ACTION);
-
- TestComboboxListener listener;
- combobox_->set_listener(&listener);
-
- // With STYLE_ACTION, the click event is notified and the menu is not shown.
- PressKey(ui::VKEY_RETURN);
- EXPECT_EQ(0, menu_show_count_);
-
- if (PlatformStyle::kReturnClicksFocusedControl) {
- EXPECT_TRUE(listener.on_perform_action_called());
- EXPECT_EQ(0, listener.perform_action_index());
- } else {
- EXPECT_FALSE(listener.on_perform_action_called());
- EXPECT_EQ(-1, listener.perform_action_index());
- }
-}
-
TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
TestComboboxListener listener;
combobox_->set_listener(&listener);
- // With STYLE_NORMAL, the click event is ignored. Instead the menu is shwon.
+ // The click event is ignored. Instead the menu is shwon.
PressKey(ui::VKEY_SPACE);
EXPECT_EQ(1, menu_show_count_);
EXPECT_FALSE(listener.on_perform_action_called());
@@ -645,7 +596,7 @@ TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) {
// Test that accessibility action events show the combobox dropdown.
TEST_F(ComboboxTest, ShowViaAccessibleAction) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
ui::AXActionData data;
data.action = ax::mojom::Action::kDoDefault;
@@ -673,37 +624,8 @@ TEST_F(ComboboxTest, ShowViaAccessibleAction) {
EXPECT_EQ(1, menu_show_count_); // No change.
}
-TEST_F(ComboboxTest, NotifyOnClickWithSpaceKeyActionStyle) {
- InitCombobox(nullptr, Combobox::STYLE_ACTION);
-
- TestComboboxListener listener;
- combobox_->set_listener(&listener);
-
- // With STYLE_ACTION, the click event is notified after releasing and the menu
- // is not shown. On Mac, the menu should be shown.
- PressKey(ui::VKEY_SPACE);
-#if defined(OS_MACOSX)
- EXPECT_EQ(1, menu_show_count_);
-#else
- EXPECT_EQ(0, menu_show_count_);
-#endif
- EXPECT_FALSE(listener.on_perform_action_called());
- EXPECT_EQ(-1, listener.perform_action_index());
-
- ReleaseKey(ui::VKEY_SPACE);
-#if defined(OS_MACOSX)
- EXPECT_EQ(1, menu_show_count_);
- EXPECT_FALSE(listener.on_perform_action_called());
- EXPECT_EQ(-1, listener.perform_action_index());
-#else
- EXPECT_EQ(0, menu_show_count_);
- EXPECT_TRUE(listener.on_perform_action_called());
- EXPECT_EQ(0, listener.perform_action_index());
-#endif
-}
-
TEST_F(ComboboxTest, NotifyOnClickWithMouse) {
- InitCombobox(nullptr, Combobox::STYLE_ACTION);
+ InitCombobox(nullptr);
TestComboboxListener listener;
combobox_->set_listener(&listener);
@@ -735,12 +657,13 @@ TEST_F(ComboboxTest, NotifyOnClickWithMouse) {
PerformMousePress(left_point);
PerformMouseRelease(left_point);
- EXPECT_EQ(1, menu_show_count_); // Unchanged.
- EXPECT_EQ(0, listener.perform_action_index());
+ // Both the text and the arrow may toggle the menu.
+ EXPECT_EQ(2, menu_show_count_);
+ EXPECT_EQ(-1, listener.perform_action_index()); // Nothing selected.
}
TEST_F(ComboboxTest, ConsumingPressKeyEvents) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
EXPECT_TRUE(combobox_->OnKeyPressed(
ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE)));
@@ -756,33 +679,11 @@ TEST_F(ComboboxTest, ConsumingPressKeyEvents) {
}
}
-TEST_F(ComboboxTest, ConsumingKeyPressEventsActionStyle) {
- // When the combobox's style is STYLE_ACTION, pressing events of a space key
- // or an enter key will be consumed and the menu is not shown. However, on
- // Mac, space will show the menu.
- InitCombobox(nullptr, Combobox::STYLE_ACTION);
-
- EXPECT_EQ(PlatformStyle::kReturnClicksFocusedControl,
- combobox_->OnKeyPressed(ui::KeyEvent(
- ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE)));
- EXPECT_EQ(0, menu_show_count_);
-
- EXPECT_TRUE(combobox_->OnKeyPressed(
- ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE)));
-#if defined(OS_MACOSX)
- EXPECT_EQ(1, menu_show_count_);
-#else
- EXPECT_EQ(0, menu_show_count_);
-#endif
-}
-
TEST_F(ComboboxTest, ContentWidth) {
std::vector<std::string> values;
VectorComboboxModel model(&values);
- TestCombobox combobox(&model, Combobox::STYLE_NORMAL);
- TestCombobox action_combobox(&model, Combobox::STYLE_ACTION);
+ TestCombobox combobox(&model);
ComboboxTestApi test_api(&combobox);
- ComboboxTestApi action_test_api(&action_combobox);
std::string long_item = "this is the long item";
std::string short_item = "s";
@@ -790,13 +691,11 @@ TEST_F(ComboboxTest, ContentWidth) {
values.resize(1);
values[0] = long_item;
combobox.ModelChanged();
- action_combobox.ModelChanged();
const int long_item_width = test_api.content_size().width();
values[0] = short_item;
combobox.ModelChanged();
- action_combobox.ModelChanged();
const int short_item_width = test_api.content_size().width();
@@ -804,20 +703,16 @@ TEST_F(ComboboxTest, ContentWidth) {
values[0] = short_item;
values[1] = long_item;
combobox.ModelChanged();
- action_combobox.ModelChanged();
- // When the style is STYLE_NORMAL, the width will fit with the longest item.
+ // The width will fit with the longest item.
EXPECT_EQ(long_item_width, test_api.content_size().width());
-
- // When the style is STYLE_ACTION, the width will fit with the selected item's
- // width.
- EXPECT_EQ(short_item_width, action_test_api.content_size().width());
+ EXPECT_LT(short_item_width, test_api.content_size().width());
}
// Test that model updates preserve the selected index, so long as it is in
// range.
TEST_F(ComboboxTest, ModelChanged) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
EXPECT_EQ(0, combobox_->GetSelectedRow());
EXPECT_EQ(10, combobox_->GetRowCount());
@@ -859,7 +754,7 @@ TEST_F(ComboboxTest, ModelChanged) {
}
TEST_F(ComboboxTest, TypingPrefixNotifiesListener) {
- InitCombobox(nullptr, Combobox::STYLE_NORMAL);
+ InitCombobox(nullptr);
TestComboboxListener listener;
combobox_->set_listener(&listener);
@@ -902,7 +797,7 @@ TEST_F(ComboboxTest, MenuModel) {
const int kSeparatorIndex = 3;
std::set<int> separators;
separators.insert(kSeparatorIndex);
- InitCombobox(&separators, Combobox::STYLE_NORMAL);
+ InitCombobox(&separators);
ui::MenuModel* menu_model = test_api_->menu_model();
@@ -931,26 +826,4 @@ TEST_F(ComboboxTest, MenuModel) {
EXPECT_TRUE(menu_model->IsVisibleAt(0));
}
-// Check that with STYLE_ACTION, the first item (only) is not shown.
-TEST_F(ComboboxTest, MenuModelActionStyleMac) {
- const int kSeparatorIndex = 3;
- std::set<int> separators;
- separators.insert(kSeparatorIndex);
- InitCombobox(&separators, Combobox::STYLE_ACTION);
-
- ui::MenuModel* menu_model = test_api_->menu_model();
-
- EXPECT_EQ(TestComboboxModel::kItemCount, menu_model->GetItemCount());
- EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR,
- menu_model->GetTypeAt(kSeparatorIndex));
-
- EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(0));
- EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(1));
-
- EXPECT_EQ(ASCIIToUTF16("PEANUT BUTTER"), menu_model->GetLabelAt(0));
- EXPECT_EQ(ASCIIToUTF16("JELLY"), menu_model->GetLabelAt(1));
- EXPECT_FALSE(menu_model->IsVisibleAt(0));
- EXPECT_TRUE(menu_model->IsVisibleAt(1));
-}
-
} // namespace views