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

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

Issue 141523005: Combobox: Rename styles to STYLE_NORMAL and STYLE_ACTION and modify behaviors (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix around selected_index_ Created 6 years, 11 months 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 580b79f0245801a02b90f94d66efced477dc7ec6..85537e548a2751564e21db16d55b0bd6a5419abd 100644
--- a/ui/views/controls/combobox/combobox_unittest.cc
+++ b/ui/views/controls/combobox/combobox_unittest.cc
@@ -117,6 +117,29 @@ 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) {};
@@ -140,16 +163,19 @@ class TestComboboxListener : public views::ComboboxListener {
public:
TestComboboxListener()
: on_selected_index_changed_called_(false),
- on_combobox_text_button_clicked_called_(false) {
+ on_combobox_text_button_clicked_called_(false),
+ last_selected_index_(-1) {
}
virtual ~TestComboboxListener() {}
virtual void OnSelectedIndexChanged(views::Combobox* combobox) OVERRIDE {
on_selected_index_changed_called_ = true;
+ last_selected_index_ = combobox->selected_index();
}
virtual void OnComboboxTextButtonClicked(views::Combobox* combobox) OVERRIDE {
on_combobox_text_button_clicked_called_ = true;
+ last_selected_index_ = combobox->selected_index();
}
bool on_selected_index_changed_called() const {
@@ -160,9 +186,14 @@ class TestComboboxListener : public views::ComboboxListener {
return on_combobox_text_button_clicked_called_;
}
+ int last_selected_index() const {
+ return last_selected_index_;
+ }
+
private:
bool on_selected_index_changed_called_;
bool on_combobox_text_button_clicked_called_;
+ int last_selected_index_;
private:
DISALLOW_COPY_AND_ASSIGN(TestComboboxListener);
@@ -482,8 +513,11 @@ TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) {
// With STYLE_NOTIFY_ON_CLICK, the click event is notified.
combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK);
+ combobox_->SetSelectedIndex(1);
SendKeyEvent(ui::VKEY_RETURN);
EXPECT_TRUE(listener.on_combobox_text_button_clicked_called());
+ // The selected index is always 0 when pressing a key.
+ EXPECT_EQ(0, listener.last_selected_index());
}
TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) {
@@ -500,10 +534,13 @@ TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) {
// With STYLE_NOTIFY_ON_CLICK, the click event is notified after releasing.
combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK);
+ combobox_->SetSelectedIndex(1);
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());
+ // The selected index is always 0 when pressing a key.
+ EXPECT_EQ(0, listener.last_selected_index());
}
TEST_F(ComboboxTest, NotifyOnClickWithMouse) {
@@ -513,6 +550,7 @@ TEST_F(ComboboxTest, NotifyOnClickWithMouse) {
combobox_->set_listener(&listener);
combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK);
+ combobox_->SetSelectedIndex(1);
combobox_->Layout();
// Click the right side (arrow button). The menu is shown.
@@ -537,6 +575,8 @@ TEST_F(ComboboxTest, NotifyOnClickWithMouse) {
combobox_->y() + combobox_->height() / 2));
EXPECT_TRUE(listener.on_combobox_text_button_clicked_called());
EXPECT_FALSE(test_menu_runner_handler->executed());
+ // The selected index is always 0 when clicking.
+ EXPECT_EQ(0, listener.last_selected_index());
}
TEST_F(ComboboxTest, ConsumingPressKeyEvents) {
@@ -556,4 +596,40 @@ TEST_F(ComboboxTest, ConsumingPressKeyEvents) {
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_SHOW_DROP_DOWN_ON_CLICK, the width will fit with
+ // the longest item.
+ combobox_->SetStyle(Combobox::STYLE_SHOW_DROP_DOWN_ON_CLICK);
+ EXPECT_EQ(long_item_width, combobox_->content_size_.width());
+
+ // When the style is STYLE_NOTIFY_ON_CLICK, the width will fit with the first
+ // items' width.
+ combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK);
+ EXPECT_EQ(short_item_width, combobox_->content_size_.width());
+}
+
} // namespace views
« ui/views/controls/combobox/combobox.cc ('K') | « ui/views/controls/combobox/combobox.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698