| 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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 if (handled) { | 175 if (handled) { |
| 176 DCHECK(!key->is_char()); | 176 DCHECK(!key->is_char()); |
| 177 ui::KeyEvent mock_key(ui::ET_KEY_PRESSED, | 177 ui::KeyEvent mock_key(ui::ET_KEY_PRESSED, |
| 178 ui::VKEY_PROCESSKEY, | 178 ui::VKEY_PROCESSKEY, |
| 179 key->flags()); | 179 key->flags()); |
| 180 dispatch_details = DispatchKeyEventPostIME(&mock_key); | 180 dispatch_details = DispatchKeyEventPostIME(&mock_key); |
| 181 } else { | 181 } else { |
| 182 dispatch_details = DispatchKeyEventPostIME(key); | 182 dispatch_details = DispatchKeyEventPostIME(key); |
| 183 } | 183 } |
| 184 | 184 |
| 185 if (dispatch_details.dispatcher_destroyed) | 185 if (key->handled() || dispatch_details.dispatcher_destroyed) |
| 186 return dispatch_details; | 186 return dispatch_details; |
| 187 | 187 |
| 188 ui::TextInputClient* client = GetTextInputClient(); | 188 ui::TextInputClient* client = GetTextInputClient(); |
| 189 if (client) { | 189 if (client) { |
| 190 if (handled) { | 190 if (handled) { |
| 191 if (result_text_.length()) | 191 if (result_text_.length()) |
| 192 client->InsertText(result_text_); | 192 client->InsertText(result_text_); |
| 193 if (composition_.text.length()) | 193 if (composition_.text.length()) |
| 194 client->SetCompositionText(composition_); | 194 client->SetCompositionText(composition_); |
| 195 else | 195 else |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 if (target_) | 341 if (target_) |
| 342 target_->OnBlur(); | 342 target_->OnBlur(); |
| 343 target_.reset(); | 343 target_.reset(); |
| 344 return false; | 344 return false; |
| 345 } | 345 } |
| 346 | 346 |
| 347 private: | 347 private: |
| 348 std::unique_ptr<views::Textfield> target_; | 348 std::unique_ptr<views::Textfield> target_; |
| 349 }; | 349 }; |
| 350 | 350 |
| 351 // Class that focuses a textfield when it sees a KeyDown event. |
| 352 class TextfieldFocuser : public views::View { |
| 353 public: |
| 354 explicit TextfieldFocuser(views::Textfield* textfield) |
| 355 : textfield_(textfield) { |
| 356 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 357 } |
| 358 |
| 359 void set_consume(bool consume) { consume_ = consume; } |
| 360 |
| 361 // View: |
| 362 bool OnKeyPressed(const ui::KeyEvent& event) override { |
| 363 textfield_->RequestFocus(); |
| 364 return consume_; |
| 365 } |
| 366 |
| 367 private: |
| 368 bool consume_ = true; |
| 369 views::Textfield* textfield_; |
| 370 |
| 371 DISALLOW_COPY_AND_ASSIGN(TextfieldFocuser); |
| 372 }; |
| 373 |
| 351 base::string16 GetClipboardText(ui::ClipboardType type) { | 374 base::string16 GetClipboardText(ui::ClipboardType type) { |
| 352 base::string16 text; | 375 base::string16 text; |
| 353 ui::Clipboard::GetForCurrentThread()->ReadText(type, &text); | 376 ui::Clipboard::GetForCurrentThread()->ReadText(type, &text); |
| 354 return text; | 377 return text; |
| 355 } | 378 } |
| 356 | 379 |
| 357 void SetClipboardText(ui::ClipboardType type, const std::string& text) { | 380 void SetClipboardText(ui::ClipboardType type, const std::string& text) { |
| 358 ui::ScopedClipboardWriter(type).WriteText(ASCIIToUTF16(text)); | 381 ui::ScopedClipboardWriter(type).WriteText(ASCIIToUTF16(text)); |
| 359 } | 382 } |
| 360 | 383 |
| (...skipping 2832 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3193 // Increase the textfield size and check if the cursor moves to the new end. | 3216 // Increase the textfield size and check if the cursor moves to the new end. |
| 3194 textfield_->SetSize(gfx::Size(40, 100)); | 3217 textfield_->SetSize(gfx::Size(40, 100)); |
| 3195 EXPECT_LT(prev_x, GetCursorBounds().x()); | 3218 EXPECT_LT(prev_x, GetCursorBounds().x()); |
| 3196 | 3219 |
| 3197 prev_x = GetCursorBounds().x(); | 3220 prev_x = GetCursorBounds().x(); |
| 3198 // Decrease the textfield size and check if the cursor moves to the new end. | 3221 // Decrease the textfield size and check if the cursor moves to the new end. |
| 3199 textfield_->SetSize(gfx::Size(30, 100)); | 3222 textfield_->SetSize(gfx::Size(30, 100)); |
| 3200 EXPECT_GT(prev_x, GetCursorBounds().x()); | 3223 EXPECT_GT(prev_x, GetCursorBounds().x()); |
| 3201 } | 3224 } |
| 3202 | 3225 |
| 3226 // Verify that if a textfield gains focus during key dispatch that an edit |
| 3227 // command only results when the event is not consumed. |
| 3228 TEST_F(TextfieldTest, SwitchFocusInKeyDown) { |
| 3229 InitTextfield(); |
| 3230 TextfieldFocuser* focuser = new TextfieldFocuser(textfield_); |
| 3231 widget_->GetContentsView()->AddChildView(focuser); |
| 3232 |
| 3233 focuser->RequestFocus(); |
| 3234 EXPECT_EQ(focuser, GetFocusedView()); |
| 3235 SendKeyPress(ui::VKEY_SPACE, 0); |
| 3236 EXPECT_EQ(textfield_, GetFocusedView()); |
| 3237 EXPECT_EQ(base::string16(), textfield_->text()); |
| 3238 |
| 3239 focuser->set_consume(false); |
| 3240 focuser->RequestFocus(); |
| 3241 EXPECT_EQ(focuser, GetFocusedView()); |
| 3242 SendKeyPress(ui::VKEY_SPACE, 0); |
| 3243 EXPECT_EQ(textfield_, GetFocusedView()); |
| 3244 EXPECT_EQ(base::ASCIIToUTF16(" "), textfield_->text()); |
| 3245 } |
| 3246 |
| 3203 } // namespace views | 3247 } // namespace views |
| OLD | NEW |