| 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 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 | 249 |
| 250 bool MockInputMethod::HasComposition() { | 250 bool MockInputMethod::HasComposition() { |
| 251 return composition_.text.length() || result_text_.length(); | 251 return composition_.text.length() || result_text_.length(); |
| 252 } | 252 } |
| 253 | 253 |
| 254 void MockInputMethod::ClearComposition() { | 254 void MockInputMethod::ClearComposition() { |
| 255 composition_.Clear(); | 255 composition_.Clear(); |
| 256 result_text_.clear(); | 256 result_text_.clear(); |
| 257 } | 257 } |
| 258 | 258 |
| 259 // A Textfield wrapper to intercept OnKey[Pressed|Released]() ressults. | |
| 260 class TestTextfield : public views::Textfield { | |
| 261 public: | |
| 262 TestTextfield() | |
| 263 : Textfield(), | |
| 264 key_handled_(false), | |
| 265 key_received_(false), | |
| 266 weak_ptr_factory_(this) {} | |
| 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: | |
| 292 void InsertChar(const ui::KeyEvent& e) override { | |
| 293 views::Textfield::InsertChar(e); | |
| 294 #if defined(OS_MACOSX) | |
| 295 // On Mac, characters are inserted directly rather than attempting to get a | |
| 296 // unicode character from the ui::KeyEvent (which isn't always possible). | |
| 297 key_received_ = true; | |
| 298 #endif | |
| 299 } | |
| 300 | |
| 301 bool key_handled() const { return key_handled_; } | |
| 302 bool key_received() const { return key_received_; } | |
| 303 | |
| 304 void clear() { key_received_ = key_handled_ = false; } | |
| 305 | |
| 306 private: | |
| 307 bool key_handled_; | |
| 308 bool key_received_; | |
| 309 | |
| 310 base::WeakPtrFactory<TestTextfield> weak_ptr_factory_; | |
| 311 | |
| 312 DISALLOW_COPY_AND_ASSIGN(TestTextfield); | |
| 313 }; | |
| 314 | |
| 315 // Convenience to make constructing a GestureEvent simpler. | 259 // Convenience to make constructing a GestureEvent simpler. |
| 316 class GestureEventForTest : public ui::GestureEvent { | 260 class GestureEventForTest : public ui::GestureEvent { |
| 317 public: | 261 public: |
| 318 GestureEventForTest(int x, int y, ui::GestureEventDetails details) | 262 GestureEventForTest(int x, int y, ui::GestureEventDetails details) |
| 319 : GestureEvent(x, y, 0, base::TimeTicks(), details) {} | 263 : GestureEvent(x, y, 0, base::TimeTicks(), details) {} |
| 320 | 264 |
| 321 private: | 265 private: |
| 322 DISALLOW_COPY_AND_ASSIGN(GestureEventForTest); | 266 DISALLOW_COPY_AND_ASSIGN(GestureEventForTest); |
| 323 }; | 267 }; |
| 324 | 268 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 353 } | 297 } |
| 354 | 298 |
| 355 void SetClipboardText(ui::ClipboardType type, const std::string& text) { | 299 void SetClipboardText(ui::ClipboardType type, const std::string& text) { |
| 356 ui::ScopedClipboardWriter(type).WriteText(ASCIIToUTF16(text)); | 300 ui::ScopedClipboardWriter(type).WriteText(ASCIIToUTF16(text)); |
| 357 } | 301 } |
| 358 | 302 |
| 359 } // namespace | 303 } // namespace |
| 360 | 304 |
| 361 namespace views { | 305 namespace views { |
| 362 | 306 |
| 307 // A Textfield wrapper to intercept OnKey[Pressed|Released]() ressults. |
| 308 class TestTextfield : public views::Textfield { |
| 309 public: |
| 310 TestTextfield() |
| 311 : Textfield(), |
| 312 key_handled_(false), |
| 313 key_received_(false), |
| 314 weak_ptr_factory_(this) {} |
| 315 |
| 316 // ui::TextInputClient overrides: |
| 317 void InsertChar(const ui::KeyEvent& e) override { |
| 318 views::Textfield::InsertChar(e); |
| 319 #if defined(OS_MACOSX) |
| 320 // On Mac, characters are inserted directly rather than attempting to get a |
| 321 // unicode character from the ui::KeyEvent (which isn't always possible). |
| 322 key_received_ = true; |
| 323 #endif |
| 324 } |
| 325 |
| 326 bool key_handled() const { return key_handled_; } |
| 327 bool key_received() const { return key_received_; } |
| 328 |
| 329 void clear() { key_received_ = key_handled_ = false; } |
| 330 |
| 331 private: |
| 332 bool OnKeyPressed(const ui::KeyEvent& e) override { |
| 333 key_received_ = true; |
| 334 |
| 335 // Since OnKeyPressed() might destroy |this|, get a weak pointer and |
| 336 // verify it isn't null before writing the bool value to key_handled_. |
| 337 base::WeakPtr<TestTextfield> textfield(weak_ptr_factory_.GetWeakPtr()); |
| 338 bool key = views::Textfield::OnKeyPressed(e); |
| 339 |
| 340 if (!textfield) |
| 341 return key; |
| 342 |
| 343 key_handled_ = key; |
| 344 |
| 345 return key_handled_; |
| 346 } |
| 347 |
| 348 bool OnKeyReleased(const ui::KeyEvent& e) override { |
| 349 key_received_ = true; |
| 350 key_handled_ = views::Textfield::OnKeyReleased(e); |
| 351 |
| 352 // Currently Textfield::OnKeyReleased always returns false. |
| 353 EXPECT_FALSE(key_handled_); |
| 354 return key_handled_; |
| 355 } |
| 356 |
| 357 bool key_handled_; |
| 358 bool key_received_; |
| 359 |
| 360 base::WeakPtrFactory<TestTextfield> weak_ptr_factory_; |
| 361 |
| 362 DISALLOW_COPY_AND_ASSIGN(TestTextfield); |
| 363 }; |
| 364 |
| 363 class TextfieldTest : public ViewsTestBase, public TextfieldController { | 365 class TextfieldTest : public ViewsTestBase, public TextfieldController { |
| 364 public: | 366 public: |
| 365 TextfieldTest() | 367 TextfieldTest() |
| 366 : widget_(NULL), | 368 : widget_(NULL), |
| 367 textfield_(NULL), | 369 textfield_(NULL), |
| 368 model_(NULL), | 370 model_(NULL), |
| 369 input_method_(NULL), | 371 input_method_(NULL), |
| 370 on_before_user_action_(0), | 372 on_before_user_action_(0), |
| 371 on_after_user_action_(0), | 373 on_after_user_action_(0), |
| 372 copied_to_clipboard_(ui::CLIPBOARD_TYPE_LAST) { | 374 copied_to_clipboard_(ui::CLIPBOARD_TYPE_LAST) { |
| (...skipping 2380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2753 | 2755 |
| 2754 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); | 2756 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); |
| 2755 ui::AXViewState state_protected; | 2757 ui::AXViewState state_protected; |
| 2756 textfield_->GetAccessibleState(&state_protected); | 2758 textfield_->GetAccessibleState(&state_protected); |
| 2757 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); | 2759 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); |
| 2758 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); | 2760 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); |
| 2759 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); | 2761 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); |
| 2760 } | 2762 } |
| 2761 | 2763 |
| 2762 } // namespace views | 2764 } // namespace views |
| OLD | NEW |