Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/textfield/textfield.h" | 5 #include "ui/views/controls/textfield/textfield.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 | 57 |
| 58 // A Textfield wrapper to intercept OnKey[Pressed|Released]() ressults. | 58 // A Textfield wrapper to intercept OnKey[Pressed|Released]() ressults. |
| 59 class TestTextfield : public views::Textfield { | 59 class TestTextfield : public views::Textfield { |
| 60 public: | 60 public: |
| 61 TestTextfield() | 61 TestTextfield() |
| 62 : Textfield(), | 62 : Textfield(), |
| 63 key_handled_(false), | 63 key_handled_(false), |
| 64 key_received_(false), | 64 key_received_(false), |
| 65 weak_ptr_factory_(this) {} | 65 weak_ptr_factory_(this) {} |
| 66 | 66 |
| 67 virtual bool OnKeyPressed(const ui::KeyEvent& e) override { | 67 bool OnKeyPressed(const ui::KeyEvent& e) override { |
| 68 key_received_ = true; | 68 key_received_ = true; |
| 69 | 69 |
| 70 // Since OnKeyPressed() might destroy |this|, get a weak pointer and | 70 // Since OnKeyPressed() might destroy |this|, get a weak pointer and |
| 71 // verify it isn't null before writing the bool value to key_handled_. | 71 // verify it isn't null before writing the bool value to key_handled_. |
| 72 base::WeakPtr<TestTextfield> textfield(weak_ptr_factory_.GetWeakPtr()); | 72 base::WeakPtr<TestTextfield> textfield(weak_ptr_factory_.GetWeakPtr()); |
| 73 bool key = views::Textfield::OnKeyPressed(e); | 73 bool key = views::Textfield::OnKeyPressed(e); |
| 74 | 74 |
| 75 if (!textfield) | 75 if (!textfield) |
| 76 return key; | 76 return key; |
| 77 | 77 |
| 78 key_handled_ = key; | 78 key_handled_ = key; |
| 79 | 79 |
| 80 return key_handled_; | 80 return key_handled_; |
| 81 } | 81 } |
| 82 | 82 |
| 83 virtual bool OnKeyReleased(const ui::KeyEvent& e) override { | 83 bool OnKeyReleased(const ui::KeyEvent& e) override { |
| 84 key_received_ = true; | 84 key_received_ = true; |
| 85 key_handled_ = views::Textfield::OnKeyReleased(e); | 85 key_handled_ = views::Textfield::OnKeyReleased(e); |
| 86 return key_handled_; | 86 return key_handled_; |
| 87 } | 87 } |
| 88 | 88 |
| 89 bool key_handled() const { return key_handled_; } | 89 bool key_handled() const { return key_handled_; } |
| 90 bool key_received() const { return key_received_; } | 90 bool key_received() const { return key_received_; } |
| 91 | 91 |
| 92 void clear() { key_received_ = key_handled_ = false; } | 92 void clear() { key_received_ = key_handled_ = false; } |
| 93 | 93 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 115 class TextfieldDestroyerController : public views::TextfieldController { | 115 class TextfieldDestroyerController : public views::TextfieldController { |
| 116 public: | 116 public: |
| 117 explicit TextfieldDestroyerController(views::Textfield* target) | 117 explicit TextfieldDestroyerController(views::Textfield* target) |
| 118 : target_(target) { | 118 : target_(target) { |
| 119 target_->set_controller(this); | 119 target_->set_controller(this); |
| 120 } | 120 } |
| 121 | 121 |
| 122 views::Textfield* target() { return target_.get(); } | 122 views::Textfield* target() { return target_.get(); } |
| 123 | 123 |
| 124 // views::TextfieldController: | 124 // views::TextfieldController: |
| 125 virtual bool HandleKeyEvent(views::Textfield* sender, | 125 bool HandleKeyEvent(views::Textfield* sender, |
| 126 const ui::KeyEvent& key_event) override { | 126 const ui::KeyEvent& key_event) override { |
| 127 target_.reset(); | 127 target_.reset(); |
| 128 return false; | 128 return false; |
| 129 } | 129 } |
| 130 | 130 |
| 131 private: | 131 private: |
| 132 scoped_ptr<views::Textfield> target_; | 132 scoped_ptr<views::Textfield> target_; |
| 133 }; | 133 }; |
| 134 | 134 |
| 135 base::string16 GetClipboardText(ui::ClipboardType type) { | 135 base::string16 GetClipboardText(ui::ClipboardType type) { |
| 136 base::string16 text; | 136 base::string16 text; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 152 : widget_(NULL), | 152 : widget_(NULL), |
| 153 textfield_(NULL), | 153 textfield_(NULL), |
| 154 model_(NULL), | 154 model_(NULL), |
| 155 input_method_(NULL), | 155 input_method_(NULL), |
| 156 on_before_user_action_(0), | 156 on_before_user_action_(0), |
| 157 on_after_user_action_(0), | 157 on_after_user_action_(0), |
| 158 copied_to_clipboard_(ui::CLIPBOARD_TYPE_LAST) { | 158 copied_to_clipboard_(ui::CLIPBOARD_TYPE_LAST) { |
| 159 } | 159 } |
| 160 | 160 |
| 161 // ::testing::Test: | 161 // ::testing::Test: |
| 162 virtual void SetUp() { | 162 void SetUp() override { ViewsTestBase::SetUp(); } |
|
msw
2014/10/27 20:43:57
nit: I guess this can be removed altogether!
dcheng
2014/10/27 20:46:36
o.O
Let me clean this up in a followup, given the
msw
2014/10/27 20:47:44
Even doing this in a followup is optional for you.
| |
| 163 ViewsTestBase::SetUp(); | |
| 164 } | |
| 165 | 163 |
| 166 virtual void TearDown() { | 164 void TearDown() override { |
| 167 if (widget_) | 165 if (widget_) |
| 168 widget_->Close(); | 166 widget_->Close(); |
| 169 ViewsTestBase::TearDown(); | 167 ViewsTestBase::TearDown(); |
| 170 } | 168 } |
| 171 | 169 |
| 172 ui::ClipboardType GetAndResetCopiedToClipboard() { | 170 ui::ClipboardType GetAndResetCopiedToClipboard() { |
| 173 ui::ClipboardType clipboard_type = copied_to_clipboard_; | 171 ui::ClipboardType clipboard_type = copied_to_clipboard_; |
| 174 copied_to_clipboard_ = ui::CLIPBOARD_TYPE_LAST; | 172 copied_to_clipboard_ = ui::CLIPBOARD_TYPE_LAST; |
| 175 return clipboard_type; | 173 return clipboard_type; |
| 176 } | 174 } |
| 177 | 175 |
| 178 // TextfieldController: | 176 // TextfieldController: |
| 179 virtual void ContentsChanged(Textfield* sender, | 177 void ContentsChanged(Textfield* sender, |
| 180 const base::string16& new_contents) override { | 178 const base::string16& new_contents) override { |
| 181 // Paste calls TextfieldController::ContentsChanged() explicitly even if the | 179 // Paste calls TextfieldController::ContentsChanged() explicitly even if the |
| 182 // paste action did not change the content. So |new_contents| may match | 180 // paste action did not change the content. So |new_contents| may match |
| 183 // |last_contents_|. For more info, see http://crbug.com/79002 | 181 // |last_contents_|. For more info, see http://crbug.com/79002 |
| 184 last_contents_ = new_contents; | 182 last_contents_ = new_contents; |
| 185 } | 183 } |
| 186 | 184 |
| 187 virtual void OnBeforeUserAction(Textfield* sender) override { | 185 void OnBeforeUserAction(Textfield* sender) override { |
| 188 ++on_before_user_action_; | 186 ++on_before_user_action_; |
| 189 } | 187 } |
| 190 | 188 |
| 191 virtual void OnAfterUserAction(Textfield* sender) override { | 189 void OnAfterUserAction(Textfield* sender) override { |
| 192 ++on_after_user_action_; | 190 ++on_after_user_action_; |
| 193 } | 191 } |
| 194 | 192 |
| 195 virtual void OnAfterCutOrCopy(ui::ClipboardType clipboard_type) override { | 193 void OnAfterCutOrCopy(ui::ClipboardType clipboard_type) override { |
| 196 copied_to_clipboard_ = clipboard_type; | 194 copied_to_clipboard_ = clipboard_type; |
| 197 } | 195 } |
| 198 | 196 |
| 199 void InitTextfield() { | 197 void InitTextfield() { |
| 200 InitTextfields(1); | 198 InitTextfields(1); |
| 201 } | 199 } |
| 202 | 200 |
| 203 void InitTextfields(int count) { | 201 void InitTextfields(int count) { |
| 204 ASSERT_FALSE(textfield_); | 202 ASSERT_FALSE(textfield_); |
| 205 textfield_ = new TestTextfield(); | 203 textfield_ = new TestTextfield(); |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 621 | 619 |
| 622 // Tests that default key bindings are handled even with a delegate installed. | 620 // Tests that default key bindings are handled even with a delegate installed. |
| 623 TEST_F(TextfieldTest, OnKeyPressBinding) { | 621 TEST_F(TextfieldTest, OnKeyPressBinding) { |
| 624 InitTextfield(); | 622 InitTextfield(); |
| 625 | 623 |
| 626 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 624 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 627 // Install a TextEditKeyBindingsDelegateAuraLinux that does nothing. | 625 // Install a TextEditKeyBindingsDelegateAuraLinux that does nothing. |
| 628 class TestDelegate : public ui::TextEditKeyBindingsDelegateAuraLinux { | 626 class TestDelegate : public ui::TextEditKeyBindingsDelegateAuraLinux { |
| 629 public: | 627 public: |
| 630 TestDelegate() {} | 628 TestDelegate() {} |
| 631 virtual ~TestDelegate() {} | 629 ~TestDelegate() override {} |
| 632 | 630 |
| 633 virtual bool MatchEvent( | 631 bool MatchEvent( |
| 634 const ui::Event& event, | 632 const ui::Event& event, |
| 635 std::vector<ui::TextEditCommandAuraLinux>* commands) override { | 633 std::vector<ui::TextEditCommandAuraLinux>* commands) override { |
| 636 return false; | 634 return false; |
| 637 } | 635 } |
| 638 | 636 |
| 639 private: | 637 private: |
| 640 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 638 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 641 }; | 639 }; |
| 642 | 640 |
| 643 TestDelegate delegate; | 641 TestDelegate delegate; |
| (...skipping 1420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2064 TextfieldDestroyerController controller(textfield_); | 2062 TextfieldDestroyerController controller(textfield_); |
| 2065 EXPECT_TRUE(controller.target()); | 2063 EXPECT_TRUE(controller.target()); |
| 2066 | 2064 |
| 2067 // Send a key to trigger OnKeyEvent(). | 2065 // Send a key to trigger OnKeyEvent(). |
| 2068 SendKeyEvent('X'); | 2066 SendKeyEvent('X'); |
| 2069 | 2067 |
| 2070 EXPECT_FALSE(controller.target()); | 2068 EXPECT_FALSE(controller.target()); |
| 2071 } | 2069 } |
| 2072 | 2070 |
| 2073 } // namespace views | 2071 } // namespace views |
| OLD | NEW |