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

Side by Side Diff: chrome/renderer/page_click_tracker_browsertest.cc

Issue 8351009: Updating PageClickTracker to Notify listeners When Text Input Loses Focus. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Making browser_tests work on all platforms Created 9 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/page_click_tracker.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "chrome/common/render_messages.h" 6 #include "chrome/common/render_messages.h"
7 #include "chrome/renderer/page_click_listener.h" 7 #include "chrome/renderer/page_click_listener.h"
8 #include "chrome/renderer/page_click_tracker.h" 8 #include "chrome/renderer/page_click_tracker.h"
9 #include "chrome/test/base/chrome_render_view_test.h" 9 #include "chrome/test/base/chrome_render_view_test.h"
10 #include "content/common/view_messages.h"
10 #include "content/public/renderer/render_view.h" 11 #include "content/public/renderer/render_view.h"
12 #include "content/renderer/render_view_impl.h"
11 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
Ilya Sherman 2011/11/03 21:32:11 nit: I think you wanted to remove the content/rend
csharp 2011/11/04 13:25:32 Done.
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
18 #include "ui/base/keycodes/keyboard_codes.h"
16 19
17 class TestPageClickListener : public PageClickListener { 20 class TestPageClickListener : public PageClickListener {
18 public: 21 public:
19 TestPageClickListener() 22 TestPageClickListener()
20 : called_(false), 23 : input_element_clicked_called_(false),
24 input_element_lost_focus_called_(false),
21 was_focused_(false), 25 was_focused_(false),
22 is_focused_(false), 26 is_focused_(false),
23 notification_response_(false) { 27 notification_response_(false) {
24 } 28 }
25 29
26 virtual bool InputElementClicked(const WebKit::WebInputElement& element, 30 virtual bool InputElementClicked(const WebKit::WebInputElement& element,
27 bool was_focused, 31 bool was_focused,
28 bool is_focused) { 32 bool is_focused) {
29 called_ = true; 33 input_element_clicked_called_ = true;
30 element_clicked_ = element; 34 element_clicked_ = element;
31 was_focused_ = was_focused; 35 was_focused_ = was_focused;
32 is_focused_ = is_focused; 36 is_focused_ = is_focused;
33 return notification_response_; 37 return notification_response_;
34 } 38 }
35 39
40 virtual bool InputElementLostFocus() {
41 input_element_lost_focus_called_ = true;
42 return notification_response_;
43 }
44
36 void ClearResults() { 45 void ClearResults() {
37 called_ = false; 46 input_element_clicked_called_ = false;
47 input_element_lost_focus_called_ = false;
38 element_clicked_.reset(); 48 element_clicked_.reset();
39 was_focused_ = false; 49 was_focused_ = false;
40 is_focused_ = false; 50 is_focused_ = false;
41 } 51 }
42 52
43 bool called_; 53 bool input_element_clicked_called_;
54 bool input_element_lost_focus_called_;
44 WebKit::WebInputElement element_clicked_; 55 WebKit::WebInputElement element_clicked_;
45 bool was_focused_; 56 bool was_focused_;
46 bool is_focused_; 57 bool is_focused_;
47 bool notification_response_; 58 bool notification_response_;
48 }; 59 };
49 60
61 class PageClickTrackerTest : public ChromeRenderViewTest {
62 protected:
63 virtual void SetUp() {
64 ChromeRenderViewTest::SetUp();
65
66 // RenderView creates PageClickTracker but it doesn't keep it around.
67 // Rather than make it do so for the test, we create a new object.
68 page_click_tracker_.reset(new PageClickTracker(view_));
69 page_click_tracker_->AddListener(&test_listener1_);
70 page_click_tracker_->AddListener(&test_listener2_);
71
72 LoadHTML("<form>"
73 " <input type='text' id='text_1'></input><br>"
74 " <input type='text' id='text_2'></input><br>"
75 " <input type='button' id='button'></input><br>"
76 "</form>");
77 GetWebWidget()->resize(WebKit::WebSize(500, 500));
78 GetWebWidget()->setFocus(true);
79 WebKit::WebDocument document = view_->GetWebView()->mainFrame()->document();
80 text_ = document.getElementById("text_1");
81 ASSERT_FALSE(text_.isNull());
82 }
83
84 // Send a raw keyboard event to the renderer.
85 void SendWebKeyboardEvent(WebKit::WebInputEvent::Type type, int key_code) {
86 WebKit::WebKeyboardEvent keyboard_event;
87 keyboard_event.type = type;
88 keyboard_event.windowsKeyCode = key_code;
89 keyboard_event.setKeyIdentifierFromWindowsKeyCode();
90
91 scoped_ptr<IPC::Message> input_message(new ViewMsg_HandleInputEvent(0));
92 input_message->WriteData(reinterpret_cast<const char*>(&keyboard_event),
93 sizeof(WebKit::WebKeyboardEvent));
94 RenderViewImpl* impl = static_cast<RenderViewImpl*>(view_);
95 impl->OnMessageReceived(*input_message);
96 }
97
98 // Send all the messages required for a complete key press.
99 void SendKeyPress(int key_code) {
100 SendWebKeyboardEvent(WebKit::WebInputEvent::RawKeyDown, key_code);
101 SendWebKeyboardEvent(WebKit::WebInputEvent::Char, key_code);
102 SendWebKeyboardEvent(WebKit::WebInputEvent::KeyUp, key_code);
103 }
104
105 scoped_ptr<PageClickTracker> page_click_tracker_;
106 TestPageClickListener test_listener1_;
107 TestPageClickListener test_listener2_;
108 WebKit::WebElement text_;
109 };
110
50 // Tests that PageClickTracker does notify correctly when a node is clicked. 111 // Tests that PageClickTracker does notify correctly when a node is clicked.
51 TEST_F(ChromeRenderViewTest, PageClickTracker) { 112 TEST_F(PageClickTrackerTest, PageClickTrackerInputClicked) {
52 // RenderView creates PageClickTracker but it doesn't keep it around. Rather
53 // than make it do so for the test, we create a new object.
54 PageClickTracker* page_click_tracker = new PageClickTracker(view_);
55
56 TestPageClickListener test_listener1;
57 TestPageClickListener test_listener2;
58 page_click_tracker->AddListener(&test_listener1);
59 page_click_tracker->AddListener(&test_listener2);
60
61 LoadHTML("<form>"
62 " <input type='text' id='text'></input><br>"
63 " <input type='button' id='button'></input><br>"
64 "</form>");
65 GetWebWidget()->resize(WebKit::WebSize(500, 500));
66 GetWebWidget()->setFocus(true);
67 WebKit::WebDocument document = view_->GetWebView()->mainFrame()->document();
68 WebKit::WebElement text = document.getElementById("text");
69 ASSERT_FALSE(text.isNull());
70 WebKit::WebElement button = document.getElementById("button");
71 ASSERT_FALSE(button.isNull());
72
73 // Click the text field once. 113 // Click the text field once.
74 EXPECT_TRUE(SimulateElementClick("text")); 114 EXPECT_TRUE(SimulateElementClick("text_1"));
75 EXPECT_TRUE(test_listener1.called_); 115 EXPECT_TRUE(test_listener1_.input_element_clicked_called_);
76 EXPECT_TRUE(test_listener2.called_); 116 EXPECT_TRUE(test_listener2_.input_element_clicked_called_);
77 EXPECT_FALSE(test_listener1.was_focused_); 117 EXPECT_FALSE(test_listener1_.was_focused_);
78 EXPECT_FALSE(test_listener2.was_focused_); 118 EXPECT_FALSE(test_listener2_.was_focused_);
79 EXPECT_TRUE(test_listener1.is_focused_); 119 EXPECT_TRUE(test_listener1_.is_focused_);
80 EXPECT_TRUE(test_listener2.is_focused_); 120 EXPECT_TRUE(test_listener2_.is_focused_);
81 EXPECT_TRUE(text == test_listener1.element_clicked_); 121 EXPECT_TRUE(text_ == test_listener1_.element_clicked_);
82 EXPECT_TRUE(text == test_listener2.element_clicked_); 122 EXPECT_TRUE(text_ == test_listener2_.element_clicked_);
83 test_listener1.ClearResults(); 123 test_listener1_.ClearResults();
84 test_listener2.ClearResults(); 124 test_listener2_.ClearResults();
85 125
86 // Click the text field again to test that was_focused_ is set correctly. 126 // Click the text field again to test that was_focused_ is set correctly.
87 EXPECT_TRUE(SimulateElementClick("text")); 127 EXPECT_TRUE(SimulateElementClick("text_1"));
88 EXPECT_TRUE(test_listener1.called_); 128 EXPECT_TRUE(test_listener1_.input_element_clicked_called_);
89 EXPECT_TRUE(test_listener2.called_); 129 EXPECT_TRUE(test_listener2_.input_element_clicked_called_);
90 EXPECT_TRUE(test_listener1.was_focused_); 130 EXPECT_TRUE(test_listener1_.was_focused_);
91 EXPECT_TRUE(test_listener2.was_focused_); 131 EXPECT_TRUE(test_listener2_.was_focused_);
92 EXPECT_TRUE(test_listener1.is_focused_); 132 EXPECT_TRUE(test_listener1_.is_focused_);
93 EXPECT_TRUE(test_listener2.is_focused_); 133 EXPECT_TRUE(test_listener2_.is_focused_);
94 EXPECT_TRUE(text == test_listener1.element_clicked_); 134 EXPECT_TRUE(text_ == test_listener1_.element_clicked_);
95 EXPECT_TRUE(text == test_listener2.element_clicked_); 135 EXPECT_TRUE(text_ == test_listener2_.element_clicked_);
96 test_listener1.ClearResults(); 136 test_listener1_.ClearResults();
97 test_listener2.ClearResults(); 137 test_listener2_.ClearResults();
98 138
99 // Click the button, no notification should happen (this is not a text-input). 139 // Click the button, no notification should happen (this is not a text-input).
100 EXPECT_TRUE(SimulateElementClick("button")); 140 EXPECT_TRUE(SimulateElementClick("button"));
101 EXPECT_FALSE(test_listener1.called_); 141 EXPECT_FALSE(test_listener1_.input_element_clicked_called_);
102 EXPECT_FALSE(test_listener2.called_); 142 EXPECT_FALSE(test_listener2_.input_element_clicked_called_);
103 143
104 // Make the first listener stop the event propagation, click the text field 144 // Make the first listener stop the event propagation, click the text field
105 // and make sure only the first listener is notified. 145 // and make sure only the first listener is notified.
106 test_listener1.notification_response_ = true; 146 test_listener1_.notification_response_ = true;
107 EXPECT_TRUE(SimulateElementClick("text")); 147 EXPECT_TRUE(SimulateElementClick("text_1"));
108 EXPECT_TRUE(test_listener1.called_); 148 EXPECT_TRUE(test_listener1_.input_element_clicked_called_);
109 EXPECT_FALSE(test_listener2.called_); 149 EXPECT_FALSE(test_listener2_.input_element_clicked_called_);
110 test_listener1.ClearResults(); 150 test_listener1_.ClearResults();
111 151
112 // Make sure removing a listener work. 152 // Make sure removing a listener work.
113 page_click_tracker->RemoveListener(&test_listener1); 153 page_click_tracker_->RemoveListener(&test_listener1_);
114 EXPECT_TRUE(SimulateElementClick("text")); 154 EXPECT_TRUE(SimulateElementClick("text_1"));
115 EXPECT_FALSE(test_listener1.called_); 155 EXPECT_FALSE(test_listener1_.input_element_clicked_called_);
116 EXPECT_TRUE(test_listener2.called_); 156 EXPECT_TRUE(test_listener2_.input_element_clicked_called_);
117 test_listener2.ClearResults(); 157 test_listener2_.ClearResults();
118 158
119 // Make sure we don't choke when no listeners are registered. 159 // Make sure we don't choke when no listeners are registered.
120 page_click_tracker->RemoveListener(&test_listener2); 160 page_click_tracker_->RemoveListener(&test_listener2_);
121 EXPECT_TRUE(SimulateElementClick("text")); 161 EXPECT_TRUE(SimulateElementClick("text_1"));
122 EXPECT_FALSE(test_listener1.called_); 162 EXPECT_FALSE(test_listener1_.input_element_clicked_called_);
123 EXPECT_FALSE(test_listener2.called_); 163 EXPECT_FALSE(test_listener2_.input_element_clicked_called_);
124 } 164 }
165
166 TEST_F(PageClickTrackerTest, PageClickTrackerInputFocusLost) {
167 // Gain focus on the text field by using tab.
168 EXPECT_NE(text_, text_.document().focusedNode());
169 SendKeyPress(ui::VKEY_TAB);
170 EXPECT_EQ(text_, text_.document().focusedNode());
171 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_);
172 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_);
173
174 // Click a button and ensure that the lost focus notification was sent,
175 // even though focus was gained without the mouse.
176 EXPECT_TRUE(SimulateElementClick("button"));
177 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_);
178 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_);
179 test_listener1_.ClearResults();
180 test_listener2_.ClearResults();
181
182 // Click a text field and test that no lost focus notifications are sent.
183 EXPECT_TRUE(SimulateElementClick("text_1"));
184 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_);
185 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_);
186 test_listener1_.ClearResults();
187 test_listener2_.ClearResults();
188
189 // Select another text field to test that the notifcation for the
190 // first text field losing focus is sent.
191 EXPECT_TRUE(SimulateElementClick("text_2"));
192 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_);
193 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_);
194 test_listener1_.ClearResults();
195 test_listener2_.ClearResults();
196
197 // Click the button, a notification should happen since a text field has
198 // lost focus.
199 EXPECT_TRUE(SimulateElementClick("button"));
200 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_);
201 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_);
202 test_listener1_.ClearResults();
203 test_listener2_.ClearResults();
204
205 // Click on a text field while the button has focus and ensure no lost focus
206 // notification is sent.
207 EXPECT_TRUE(SimulateElementClick("text_1"));
208 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_);
209 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_);
210
211 // Make the first listener stop the event propagation, then click a button
212 // and make sure only the first listener is notified.
213 test_listener1_.notification_response_ = true;
214 EXPECT_TRUE(SimulateElementClick("button"));
215 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_);
216 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_);
217 test_listener1_.ClearResults();
218
219 // Make sure removing a listener work.
220 page_click_tracker_->RemoveListener(&test_listener1_);
221 EXPECT_TRUE(SimulateElementClick("text_1"));
222 EXPECT_TRUE(SimulateElementClick("button"));
223 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_);
224 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_);
225 test_listener2_.ClearResults();
226
227 // Make sure we don't choke when no listeners are registered.
228 page_click_tracker_->RemoveListener(&test_listener2_);
229 EXPECT_TRUE(SimulateElementClick("text_1"));
230 EXPECT_TRUE(SimulateElementClick("button"));
231 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_);
232 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_);
233 }
OLDNEW
« no previous file with comments | « chrome/renderer/page_click_tracker.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698