| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/combobox/combobox.h" | 5 #include "ui/views/controls/combobox/combobox.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 void SetSeparators(const std::set<int>& separators) { | 110 void SetSeparators(const std::set<int>& separators) { |
| 111 separators_ = separators; | 111 separators_ = separators; |
| 112 } | 112 } |
| 113 | 113 |
| 114 private: | 114 private: |
| 115 std::set<int> separators_; | 115 std::set<int> separators_; |
| 116 | 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); | 117 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); |
| 118 }; | 118 }; |
| 119 | 119 |
| 120 // A combobox model which refers to a vector. |
| 121 class VectorComboboxModel : public ui::ComboboxModel { |
| 122 public: |
| 123 VectorComboboxModel(std::vector<std::string>* values) |
| 124 : values_(values) { |
| 125 } |
| 126 virtual ~VectorComboboxModel() {} |
| 127 |
| 128 // ui::ComboboxModel: |
| 129 virtual int GetItemCount() const OVERRIDE { |
| 130 return values_->size(); |
| 131 } |
| 132 virtual base::string16 GetItemAt(int index) OVERRIDE { |
| 133 return ASCIIToUTF16(values_->at(index)); |
| 134 } |
| 135 virtual bool IsItemSeparatorAt(int index) OVERRIDE { |
| 136 return false; |
| 137 } |
| 138 |
| 139 private: |
| 140 std::vector<std::string>* values_; |
| 141 }; |
| 142 |
| 120 class EvilListener : public ComboboxListener { | 143 class EvilListener : public ComboboxListener { |
| 121 public: | 144 public: |
| 122 EvilListener() : deleted_(false) {}; | 145 EvilListener() : deleted_(false) {}; |
| 123 virtual ~EvilListener() {}; | 146 virtual ~EvilListener() {}; |
| 124 | 147 |
| 125 // ComboboxListener: | 148 // ComboboxListener: |
| 126 virtual void OnSelectedIndexChanged(Combobox* combobox) OVERRIDE { | 149 virtual void OnSelectedIndexChanged(Combobox* combobox) OVERRIDE { |
| 127 delete combobox; | 150 delete combobox; |
| 128 deleted_ = true; | 151 deleted_ = true; |
| 129 } | 152 } |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 | 572 |
| 550 // When the combobox's style is STYLE_NOTIFY_ON_CLICK, pressing events of | 573 // When the combobox's style is STYLE_NOTIFY_ON_CLICK, pressing events of |
| 551 // a space key or an enter key will be consumed. | 574 // a space key or an enter key will be consumed. |
| 552 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); | 575 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); |
| 553 EXPECT_TRUE(combobox_->OnKeyPressed( | 576 EXPECT_TRUE(combobox_->OnKeyPressed( |
| 554 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false))); | 577 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false))); |
| 555 EXPECT_TRUE(combobox_->OnKeyPressed( | 578 EXPECT_TRUE(combobox_->OnKeyPressed( |
| 556 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false))); | 579 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0, false))); |
| 557 } | 580 } |
| 558 | 581 |
| 582 TEST_F(ComboboxTest, ContentWidth) { |
| 583 std::vector<std::string> values; |
| 584 |
| 585 scoped_ptr<VectorComboboxModel> model(new VectorComboboxModel(&values)); |
| 586 combobox_ = new TestCombobox(model.get()); |
| 587 |
| 588 std::string long_item = "this is the long item"; |
| 589 std::string short_item = "s"; |
| 590 |
| 591 values.resize(1); |
| 592 values[0] = long_item; |
| 593 combobox_->ModelChanged(); |
| 594 |
| 595 const int long_item_width = combobox_->content_size_.width(); |
| 596 |
| 597 values[0] = short_item; |
| 598 combobox_->ModelChanged(); |
| 599 |
| 600 const int short_item_width = combobox_->content_size_.width(); |
| 601 |
| 602 values.resize(2); |
| 603 values[0] = short_item; |
| 604 values[1] = long_item; |
| 605 combobox_->ModelChanged(); |
| 606 |
| 607 // When the style is STYLE_SHOW_DROP_DOWN_ON_CLICK, the width will fit with |
| 608 // the longest item. |
| 609 combobox_->SetStyle(Combobox::STYLE_SHOW_DROP_DOWN_ON_CLICK); |
| 610 EXPECT_EQ(long_item_width, combobox_->content_size_.width()); |
| 611 |
| 612 // When the style is STYLE_NOTIFY_ON_CLICK, the width will fit with the first |
| 613 // items' width. |
| 614 combobox_->SetStyle(Combobox::STYLE_NOTIFY_ON_CLICK); |
| 615 EXPECT_EQ(short_item_width, combobox_->content_size_.width()); |
| 616 } |
| 617 |
| 559 } // namespace views | 618 } // namespace views |
| OLD | NEW |