| 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 22 matching lines...) Expand all Loading... |
| 33 public: | 33 public: |
| 34 TestMenuRunnerHandler() : executed_(false) {} | 34 TestMenuRunnerHandler() : executed_(false) {} |
| 35 | 35 |
| 36 bool executed() const { return executed_; } | 36 bool executed() const { return executed_; } |
| 37 | 37 |
| 38 virtual MenuRunner::RunResult RunMenuAt(Widget* parent, | 38 virtual MenuRunner::RunResult RunMenuAt(Widget* parent, |
| 39 MenuButton* button, | 39 MenuButton* button, |
| 40 const gfx::Rect& bounds, | 40 const gfx::Rect& bounds, |
| 41 MenuAnchorPosition anchor, | 41 MenuAnchorPosition anchor, |
| 42 ui::MenuSourceType source_type, | 42 ui::MenuSourceType source_type, |
| 43 int32 types) OVERRIDE { | 43 int32 types) override { |
| 44 executed_ = true; | 44 executed_ = true; |
| 45 return MenuRunner::NORMAL_EXIT; | 45 return MenuRunner::NORMAL_EXIT; |
| 46 } | 46 } |
| 47 | 47 |
| 48 private: | 48 private: |
| 49 bool executed_; | 49 bool executed_; |
| 50 | 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(TestMenuRunnerHandler); | 51 DISALLOW_COPY_AND_ASSIGN(TestMenuRunnerHandler); |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 // A wrapper of Combobox to intercept the result of OnKeyPressed() and | 54 // A wrapper of Combobox to intercept the result of OnKeyPressed() and |
| 55 // OnKeyReleased() methods. | 55 // OnKeyReleased() methods. |
| 56 class TestCombobox : public Combobox { | 56 class TestCombobox : public Combobox { |
| 57 public: | 57 public: |
| 58 explicit TestCombobox(ui::ComboboxModel* model) | 58 explicit TestCombobox(ui::ComboboxModel* model) |
| 59 : Combobox(model), | 59 : Combobox(model), |
| 60 key_handled_(false), | 60 key_handled_(false), |
| 61 key_received_(false) {} | 61 key_received_(false) {} |
| 62 | 62 |
| 63 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE { | 63 virtual bool OnKeyPressed(const ui::KeyEvent& e) override { |
| 64 key_received_ = true; | 64 key_received_ = true; |
| 65 key_handled_ = Combobox::OnKeyPressed(e); | 65 key_handled_ = Combobox::OnKeyPressed(e); |
| 66 return key_handled_; | 66 return key_handled_; |
| 67 } | 67 } |
| 68 | 68 |
| 69 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE { | 69 virtual bool OnKeyReleased(const ui::KeyEvent& e) override { |
| 70 key_received_ = true; | 70 key_received_ = true; |
| 71 key_handled_ = Combobox::OnKeyReleased(e); | 71 key_handled_ = Combobox::OnKeyReleased(e); |
| 72 return key_handled_; | 72 return key_handled_; |
| 73 } | 73 } |
| 74 | 74 |
| 75 bool key_handled() const { return key_handled_; } | 75 bool key_handled() const { return key_handled_; } |
| 76 bool key_received() const { return key_received_; } | 76 bool key_received() const { return key_received_; } |
| 77 | 77 |
| 78 void clear() { | 78 void clear() { |
| 79 key_received_ = key_handled_ = false; | 79 key_received_ = key_handled_ = false; |
| 80 } | 80 } |
| 81 | 81 |
| 82 private: | 82 private: |
| 83 bool key_handled_; | 83 bool key_handled_; |
| 84 bool key_received_; | 84 bool key_received_; |
| 85 | 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(TestCombobox); | 86 DISALLOW_COPY_AND_ASSIGN(TestCombobox); |
| 87 }; | 87 }; |
| 88 | 88 |
| 89 // A concrete class is needed to test the combobox. | 89 // A concrete class is needed to test the combobox. |
| 90 class TestComboboxModel : public ui::ComboboxModel { | 90 class TestComboboxModel : public ui::ComboboxModel { |
| 91 public: | 91 public: |
| 92 TestComboboxModel() {} | 92 TestComboboxModel() {} |
| 93 virtual ~TestComboboxModel() {} | 93 virtual ~TestComboboxModel() {} |
| 94 | 94 |
| 95 static const int kItemCount = 10; | 95 static const int kItemCount = 10; |
| 96 | 96 |
| 97 // ui::ComboboxModel: | 97 // ui::ComboboxModel: |
| 98 virtual int GetItemCount() const OVERRIDE { | 98 virtual int GetItemCount() const override { |
| 99 return kItemCount; | 99 return kItemCount; |
| 100 } | 100 } |
| 101 virtual base::string16 GetItemAt(int index) OVERRIDE { | 101 virtual base::string16 GetItemAt(int index) override { |
| 102 if (IsItemSeparatorAt(index)) { | 102 if (IsItemSeparatorAt(index)) { |
| 103 NOTREACHED(); | 103 NOTREACHED(); |
| 104 return ASCIIToUTF16("SEPARATOR"); | 104 return ASCIIToUTF16("SEPARATOR"); |
| 105 } | 105 } |
| 106 return ASCIIToUTF16(index % 2 == 0 ? "PEANUT BUTTER" : "JELLY"); | 106 return ASCIIToUTF16(index % 2 == 0 ? "PEANUT BUTTER" : "JELLY"); |
| 107 } | 107 } |
| 108 virtual bool IsItemSeparatorAt(int index) OVERRIDE { | 108 virtual bool IsItemSeparatorAt(int index) override { |
| 109 return separators_.find(index) != separators_.end(); | 109 return separators_.find(index) != separators_.end(); |
| 110 } | 110 } |
| 111 | 111 |
| 112 virtual int GetDefaultIndex() const OVERRIDE { | 112 virtual int GetDefaultIndex() const override { |
| 113 // Return the first index that is not a separator. | 113 // Return the first index that is not a separator. |
| 114 for (int index = 0; index < kItemCount; ++index) { | 114 for (int index = 0; index < kItemCount; ++index) { |
| 115 if (separators_.find(index) == separators_.end()) | 115 if (separators_.find(index) == separators_.end()) |
| 116 return index; | 116 return index; |
| 117 } | 117 } |
| 118 NOTREACHED(); | 118 NOTREACHED(); |
| 119 return 0; | 119 return 0; |
| 120 } | 120 } |
| 121 | 121 |
| 122 void SetSeparators(const std::set<int>& separators) { | 122 void SetSeparators(const std::set<int>& separators) { |
| 123 separators_ = separators; | 123 separators_ = separators; |
| 124 } | 124 } |
| 125 | 125 |
| 126 private: | 126 private: |
| 127 std::set<int> separators_; | 127 std::set<int> separators_; |
| 128 | 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); | 129 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); |
| 130 }; | 130 }; |
| 131 | 131 |
| 132 // A combobox model which refers to a vector. | 132 // A combobox model which refers to a vector. |
| 133 class VectorComboboxModel : public ui::ComboboxModel { | 133 class VectorComboboxModel : public ui::ComboboxModel { |
| 134 public: | 134 public: |
| 135 explicit VectorComboboxModel(std::vector<std::string>* values) | 135 explicit VectorComboboxModel(std::vector<std::string>* values) |
| 136 : values_(values) {} | 136 : values_(values) {} |
| 137 virtual ~VectorComboboxModel() {} | 137 virtual ~VectorComboboxModel() {} |
| 138 | 138 |
| 139 // ui::ComboboxModel: | 139 // ui::ComboboxModel: |
| 140 virtual int GetItemCount() const OVERRIDE { | 140 virtual int GetItemCount() const override { |
| 141 return (int)values_->size(); | 141 return (int)values_->size(); |
| 142 } | 142 } |
| 143 virtual base::string16 GetItemAt(int index) OVERRIDE { | 143 virtual base::string16 GetItemAt(int index) override { |
| 144 return ASCIIToUTF16(values_->at(index)); | 144 return ASCIIToUTF16(values_->at(index)); |
| 145 } | 145 } |
| 146 virtual bool IsItemSeparatorAt(int index) OVERRIDE { | 146 virtual bool IsItemSeparatorAt(int index) override { |
| 147 return false; | 147 return false; |
| 148 } | 148 } |
| 149 | 149 |
| 150 private: | 150 private: |
| 151 std::vector<std::string>* values_; | 151 std::vector<std::string>* values_; |
| 152 }; | 152 }; |
| 153 | 153 |
| 154 class EvilListener : public ComboboxListener { | 154 class EvilListener : public ComboboxListener { |
| 155 public: | 155 public: |
| 156 EvilListener() : deleted_(false) {} | 156 EvilListener() : deleted_(false) {} |
| 157 virtual ~EvilListener() {}; | 157 virtual ~EvilListener() {}; |
| 158 | 158 |
| 159 // ComboboxListener: | 159 // ComboboxListener: |
| 160 virtual void OnPerformAction(Combobox* combobox) OVERRIDE { | 160 virtual void OnPerformAction(Combobox* combobox) override { |
| 161 delete combobox; | 161 delete combobox; |
| 162 deleted_ = true; | 162 deleted_ = true; |
| 163 } | 163 } |
| 164 | 164 |
| 165 bool deleted() const { return deleted_; } | 165 bool deleted() const { return deleted_; } |
| 166 | 166 |
| 167 private: | 167 private: |
| 168 bool deleted_; | 168 bool deleted_; |
| 169 | 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(EvilListener); | 170 DISALLOW_COPY_AND_ASSIGN(EvilListener); |
| 171 }; | 171 }; |
| 172 | 172 |
| 173 class TestComboboxListener : public views::ComboboxListener { | 173 class TestComboboxListener : public views::ComboboxListener { |
| 174 public: | 174 public: |
| 175 TestComboboxListener() : perform_action_index_(-1), actions_performed_(0) {} | 175 TestComboboxListener() : perform_action_index_(-1), actions_performed_(0) {} |
| 176 virtual ~TestComboboxListener() {} | 176 virtual ~TestComboboxListener() {} |
| 177 | 177 |
| 178 virtual void OnPerformAction(views::Combobox* combobox) OVERRIDE { | 178 virtual void OnPerformAction(views::Combobox* combobox) override { |
| 179 perform_action_index_ = combobox->selected_index(); | 179 perform_action_index_ = combobox->selected_index(); |
| 180 actions_performed_++; | 180 actions_performed_++; |
| 181 } | 181 } |
| 182 | 182 |
| 183 int perform_action_index() const { | 183 int perform_action_index() const { |
| 184 return perform_action_index_; | 184 return perform_action_index_; |
| 185 } | 185 } |
| 186 | 186 |
| 187 bool on_perform_action_called() const { | 187 bool on_perform_action_called() const { |
| 188 return actions_performed_ > 0; | 188 return actions_performed_ > 0; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 199 private: | 199 private: |
| 200 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); | 200 DISALLOW_COPY_AND_ASSIGN(TestComboboxListener); |
| 201 }; | 201 }; |
| 202 | 202 |
| 203 } // namespace | 203 } // namespace |
| 204 | 204 |
| 205 class ComboboxTest : public ViewsTestBase { | 205 class ComboboxTest : public ViewsTestBase { |
| 206 public: | 206 public: |
| 207 ComboboxTest() : widget_(NULL), combobox_(NULL) {} | 207 ComboboxTest() : widget_(NULL), combobox_(NULL) {} |
| 208 | 208 |
| 209 virtual void TearDown() OVERRIDE { | 209 virtual void TearDown() override { |
| 210 if (widget_) | 210 if (widget_) |
| 211 widget_->Close(); | 211 widget_->Close(); |
| 212 ViewsTestBase::TearDown(); | 212 ViewsTestBase::TearDown(); |
| 213 } | 213 } |
| 214 | 214 |
| 215 void InitCombobox(const std::set<int>* separators) { | 215 void InitCombobox(const std::set<int>* separators) { |
| 216 model_.reset(new TestComboboxModel()); | 216 model_.reset(new TestComboboxModel()); |
| 217 | 217 |
| 218 if (separators) | 218 if (separators) |
| 219 model_->SetSeparators(*separators); | 219 model_->SetSeparators(*separators); |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 combobox_->OnBlur(); | 692 combobox_->OnBlur(); |
| 693 | 693 |
| 694 // Type the first character of "PEANUT BUTTER", which should change the | 694 // Type the first character of "PEANUT BUTTER", which should change the |
| 695 // selected index and perform an action. | 695 // selected index and perform an action. |
| 696 combobox_->GetTextInputClient()->InsertChar('P', ui::EF_NONE); | 696 combobox_->GetTextInputClient()->InsertChar('P', ui::EF_NONE); |
| 697 EXPECT_EQ(2, listener.actions_performed()); | 697 EXPECT_EQ(2, listener.actions_performed()); |
| 698 EXPECT_EQ(2, listener.perform_action_index()); | 698 EXPECT_EQ(2, listener.perform_action_index()); |
| 699 } | 699 } |
| 700 | 700 |
| 701 } // namespace views | 701 } // namespace views |
| OLD | NEW |