Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(786)

Unified Diff: ui/views/controls/textfield/textfield_unittest.cc

Issue 2973463002: MacViews: Don't InsertChar() for consumed events. (Closed)
Patch Set: Fix non-mac Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/controls/textfield/textfield_unittest.cc
diff --git a/ui/views/controls/textfield/textfield_unittest.cc b/ui/views/controls/textfield/textfield_unittest.cc
index 9d44b48415ff571eff370a6517ef52a158423869..831fe36bd4010999f0cd94cf570f0452f30ea650 100644
--- a/ui/views/controls/textfield/textfield_unittest.cc
+++ b/ui/views/controls/textfield/textfield_unittest.cc
@@ -182,7 +182,7 @@ ui::EventDispatchDetails MockInputMethod::DispatchKeyEvent(ui::KeyEvent* key) {
dispatch_details = DispatchKeyEventPostIME(key);
}
- if (dispatch_details.dispatcher_destroyed)
+ if (key->handled() || dispatch_details.dispatcher_destroyed)
return dispatch_details;
ui::TextInputClient* client = GetTextInputClient();
@@ -348,6 +348,29 @@ class TextfieldDestroyerController : public views::TextfieldController {
std::unique_ptr<views::Textfield> target_;
};
+// Class that focuses a textfield when it sees a KeyDown event.
+class TextfieldFocuser : public views::View {
+ public:
+ explicit TextfieldFocuser(views::Textfield* textfield)
+ : textfield_(textfield) {
+ SetFocusBehavior(FocusBehavior::ALWAYS);
+ }
+
+ void PretendNotConsumed() { consume_ = false; }
msw 2017/07/05 19:21:21 nit: do a simple |void set_consume(bool consume) {
tapted 2017/07/06 05:58:07 Done.
+
+ // View:
+ bool OnKeyPressed(const ui::KeyEvent& event) override {
+ textfield_->RequestFocus();
+ return consume_;
+ }
+
+ private:
+ bool consume_ = true;
+ views::Textfield* textfield_;
+
+ DISALLOW_COPY_AND_ASSIGN(TextfieldFocuser);
+};
+
base::string16 GetClipboardText(ui::ClipboardType type) {
base::string16 text;
ui::Clipboard::GetForCurrentThread()->ReadText(type, &text);
@@ -3168,4 +3191,25 @@ TEST_F(TextfieldTest, CursorVisibility) {
EXPECT_TRUE(test_api_->IsCursorVisible());
}
+// Verify that if a textfield gains focus during key dispatch that an edit
+// command only results when the event is not consumed.
+TEST_F(TextfieldTest, SwitchFocusInKeyDown) {
+ InitTextfield();
+ TextfieldFocuser* focuser = new TextfieldFocuser(textfield_);
+ widget_->GetContentsView()->AddChildView(focuser);
+
+ focuser->RequestFocus();
+ EXPECT_EQ(focuser, GetFocusedView());
+ SendKeyPress(ui::VKEY_SPACE, 0);
+ EXPECT_EQ(textfield_, GetFocusedView());
+ EXPECT_EQ(base::string16(), textfield_->text());
+
+ focuser->PretendNotConsumed();
+ focuser->RequestFocus();
+ EXPECT_EQ(focuser, GetFocusedView());
+ SendKeyPress(ui::VKEY_SPACE, 0);
+ EXPECT_EQ(textfield_, GetFocusedView());
+ EXPECT_EQ(base::ASCIIToUTF16(" "), textfield_->text());
+}
+
} // namespace views
« ui/views/cocoa/bridged_content_view.mm ('K') | « ui/views/cocoa/bridged_content_view.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698