| 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/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "ui/base/models/combobox_model.h" | 11 #include "ui/base/models/combobox_model.h" |
| 11 #include "ui/events/event.h" | 12 #include "ui/events/event.h" |
| 12 #include "ui/events/keycodes/keyboard_codes.h" | 13 #include "ui/events/keycodes/keyboard_codes.h" |
| 14 #include "ui/views/controls/combobox/combobox_listener.h" |
| 13 #include "ui/views/ime/mock_input_method.h" | 15 #include "ui/views/ime/mock_input_method.h" |
| 14 #include "ui/views/test/views_test_base.h" | 16 #include "ui/views/test/views_test_base.h" |
| 15 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
| 16 | 18 |
| 19 namespace views { |
| 20 |
| 17 namespace { | 21 namespace { |
| 18 | 22 |
| 19 // A wrapper of Combobox to intercept the result of OnKeyPressed() and | 23 // A wrapper of Combobox to intercept the result of OnKeyPressed() and |
| 20 // OnKeyReleased() methods. | 24 // OnKeyReleased() methods. |
| 21 class TestCombobox : public views::Combobox { | 25 class TestCombobox : public Combobox { |
| 22 public: | 26 public: |
| 23 explicit TestCombobox(ui::ComboboxModel* model) | 27 explicit TestCombobox(ui::ComboboxModel* model) |
| 24 : Combobox(model), | 28 : Combobox(model), |
| 25 key_handled_(false), | 29 key_handled_(false), |
| 26 key_received_(false) { | 30 key_received_(false) { |
| 27 } | 31 } |
| 28 | 32 |
| 29 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE { | 33 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE { |
| 30 key_received_ = true; | 34 key_received_ = true; |
| 31 key_handled_ = views::Combobox::OnKeyPressed(e); | 35 key_handled_ = Combobox::OnKeyPressed(e); |
| 32 return key_handled_; | 36 return key_handled_; |
| 33 } | 37 } |
| 34 | 38 |
| 35 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE { | 39 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE { |
| 36 key_received_ = true; | 40 key_received_ = true; |
| 37 key_handled_ = views::Combobox::OnKeyReleased(e); | 41 key_handled_ = Combobox::OnKeyReleased(e); |
| 38 return key_handled_; | 42 return key_handled_; |
| 39 } | 43 } |
| 40 | 44 |
| 41 bool key_handled() const { return key_handled_; } | 45 bool key_handled() const { return key_handled_; } |
| 42 bool key_received() const { return key_received_; } | 46 bool key_received() const { return key_received_; } |
| 43 | 47 |
| 44 void clear() { | 48 void clear() { |
| 45 key_received_ = key_handled_ = false; | 49 key_received_ = key_handled_ = false; |
| 46 } | 50 } |
| 47 | 51 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 76 void SetSeparators(const std::set<int>& separators) { | 80 void SetSeparators(const std::set<int>& separators) { |
| 77 separators_ = separators; | 81 separators_ = separators; |
| 78 } | 82 } |
| 79 | 83 |
| 80 private: | 84 private: |
| 81 std::set<int> separators_; | 85 std::set<int> separators_; |
| 82 | 86 |
| 83 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); | 87 DISALLOW_COPY_AND_ASSIGN(TestComboboxModel); |
| 84 }; | 88 }; |
| 85 | 89 |
| 90 class EvilListener : public ComboboxListener { |
| 91 public: |
| 92 EvilListener() : deleted_(false) {}; |
| 93 virtual ~EvilListener() {}; |
| 94 |
| 95 // ComboboxListener: |
| 96 virtual void OnSelectedIndexChanged(Combobox* combobox) OVERRIDE { |
| 97 delete combobox; |
| 98 deleted_ = true; |
| 99 } |
| 100 |
| 101 bool deleted() const { return deleted_; } |
| 102 |
| 103 private: |
| 104 bool deleted_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(EvilListener); |
| 107 }; |
| 108 |
| 86 } // namespace | 109 } // namespace |
| 87 | 110 |
| 88 namespace views { | |
| 89 | |
| 90 class ComboboxTest : public ViewsTestBase { | 111 class ComboboxTest : public ViewsTestBase { |
| 91 public: | 112 public: |
| 92 ComboboxTest() : widget_(NULL), combobox_(NULL), input_method_(NULL) {} | 113 ComboboxTest() : widget_(NULL), combobox_(NULL), input_method_(NULL) {} |
| 93 | 114 |
| 94 virtual void TearDown() OVERRIDE { | 115 virtual void TearDown() OVERRIDE { |
| 95 if (widget_) | 116 if (widget_) |
| 96 widget_->Close(); | 117 widget_->Close(); |
| 97 ViewsTestBase::TearDown(); | 118 ViewsTestBase::TearDown(); |
| 98 } | 119 } |
| 99 | 120 |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 InitCombobox(); | 354 InitCombobox(); |
| 334 ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index()); | 355 ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index()); |
| 335 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); | 356 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); |
| 336 EXPECT_EQ(0, combobox_->selected_index()); | 357 EXPECT_EQ(0, combobox_->selected_index()); |
| 337 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); | 358 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); |
| 338 EXPECT_EQ(1, combobox_->selected_index()); | 359 EXPECT_EQ(1, combobox_->selected_index()); |
| 339 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); | 360 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); |
| 340 EXPECT_EQ(1, combobox_->selected_index()); | 361 EXPECT_EQ(1, combobox_->selected_index()); |
| 341 } | 362 } |
| 342 | 363 |
| 364 TEST_F(ComboboxTest, ListenerHandlesDelete) { |
| 365 TestComboboxModel model; |
| 366 TestCombobox* combobox = new TestCombobox(&model); // Deleted on change. |
| 367 EvilListener evil_listener; |
| 368 combobox->set_listener(&evil_listener); |
| 369 ASSERT_NO_FATAL_FAILURE(combobox->ExecuteCommand(2)); |
| 370 EXPECT_TRUE(evil_listener.deleted()); |
| 371 } |
| 372 |
| 343 } // namespace views | 373 } // namespace views |
| OLD | NEW |