| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <set> | 10 #include <set> |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 | 258 |
| 259 // A Textfield wrapper to intercept OnKey[Pressed|Released]() ressults. | 259 // A Textfield wrapper to intercept OnKey[Pressed|Released]() ressults. |
| 260 class TestTextfield : public views::Textfield { | 260 class TestTextfield : public views::Textfield { |
| 261 public: | 261 public: |
| 262 TestTextfield() | 262 TestTextfield() |
| 263 : Textfield(), | 263 : Textfield(), |
| 264 key_handled_(false), | 264 key_handled_(false), |
| 265 key_received_(false), | 265 key_received_(false), |
| 266 weak_ptr_factory_(this) {} | 266 weak_ptr_factory_(this) {} |
| 267 | 267 |
| 268 bool OnKeyPressed(const ui::KeyEvent& e) override { | |
| 269 key_received_ = true; | |
| 270 | |
| 271 // Since OnKeyPressed() might destroy |this|, get a weak pointer and | |
| 272 // verify it isn't null before writing the bool value to key_handled_. | |
| 273 base::WeakPtr<TestTextfield> textfield(weak_ptr_factory_.GetWeakPtr()); | |
| 274 bool key = views::Textfield::OnKeyPressed(e); | |
| 275 | |
| 276 if (!textfield) | |
| 277 return key; | |
| 278 | |
| 279 key_handled_ = key; | |
| 280 | |
| 281 return key_handled_; | |
| 282 } | |
| 283 | |
| 284 bool OnKeyReleased(const ui::KeyEvent& e) override { | |
| 285 key_received_ = true; | |
| 286 key_handled_ = views::Textfield::OnKeyReleased(e); | |
| 287 EXPECT_FALSE(key_handled_); // Textfield doesn't override OnKeyReleased. | |
| 288 return key_handled_; | |
| 289 } | |
| 290 | |
| 291 // ui::TextInputClient overrides: | 268 // ui::TextInputClient overrides: |
| 292 void InsertChar(const ui::KeyEvent& e) override { | 269 void InsertChar(const ui::KeyEvent& e) override { |
| 293 views::Textfield::InsertChar(e); | 270 views::Textfield::InsertChar(e); |
| 294 #if defined(OS_MACOSX) | 271 #if defined(OS_MACOSX) |
| 295 // On Mac, characters are inserted directly rather than attempting to get a | 272 // On Mac, characters are inserted directly rather than attempting to get a |
| 296 // unicode character from the ui::KeyEvent (which isn't always possible). | 273 // unicode character from the ui::KeyEvent (which isn't always possible). |
| 297 key_received_ = true; | 274 key_received_ = true; |
| 298 #endif | 275 #endif |
| 299 } | 276 } |
| 300 | 277 |
| 301 bool key_handled() const { return key_handled_; } | 278 bool key_handled() const { return key_handled_; } |
| 302 bool key_received() const { return key_received_; } | 279 bool key_received() const { return key_received_; } |
| 303 | 280 |
| 304 void clear() { key_received_ = key_handled_ = false; } | 281 void clear() { key_received_ = key_handled_ = false; } |
| 305 | 282 |
| 306 private: | 283 private: |
| 284 // views::View override: |
| 285 void OnKeyEvent(ui::KeyEvent* event) override { |
| 286 key_received_ = true; |
| 287 |
| 288 // Since Textfield::OnKeyPressed() might destroy |this|, get a weak pointer |
| 289 // and verify it isn't null before writing the bool value to key_handled_. |
| 290 base::WeakPtr<TestTextfield> textfield(weak_ptr_factory_.GetWeakPtr()); |
| 291 views::View::OnKeyEvent(event); |
| 292 |
| 293 if (!textfield) |
| 294 return; |
| 295 |
| 296 key_handled_ = event->handled(); |
| 297 |
| 298 // Currently, Textfield::OnKeyReleased always returns false. |
| 299 if (event->type() == ui::ET_KEY_RELEASED) |
| 300 EXPECT_FALSE(key_handled_); |
| 301 } |
| 302 |
| 307 bool key_handled_; | 303 bool key_handled_; |
| 308 bool key_received_; | 304 bool key_received_; |
| 309 | 305 |
| 310 base::WeakPtrFactory<TestTextfield> weak_ptr_factory_; | 306 base::WeakPtrFactory<TestTextfield> weak_ptr_factory_; |
| 311 | 307 |
| 312 DISALLOW_COPY_AND_ASSIGN(TestTextfield); | 308 DISALLOW_COPY_AND_ASSIGN(TestTextfield); |
| 313 }; | 309 }; |
| 314 | 310 |
| 315 // Convenience to make constructing a GestureEvent simpler. | 311 // Convenience to make constructing a GestureEvent simpler. |
| 316 class GestureEventForTest : public ui::GestureEvent { | 312 class GestureEventForTest : public ui::GestureEvent { |
| (...skipping 2436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2753 | 2749 |
| 2754 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); | 2750 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); |
| 2755 ui::AXViewState state_protected; | 2751 ui::AXViewState state_protected; |
| 2756 textfield_->GetAccessibleState(&state_protected); | 2752 textfield_->GetAccessibleState(&state_protected); |
| 2757 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); | 2753 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); |
| 2758 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); | 2754 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); |
| 2759 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); | 2755 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); |
| 2760 } | 2756 } |
| 2761 | 2757 |
| 2762 } // namespace views | 2758 } // namespace views |
| OLD | NEW |