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

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: Updating page_click_tracker_browser test 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
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/public/renderer/render_view.h" 10 #include "content/public/renderer/render_view.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
16 16
17 class TestPageClickListener : public PageClickListener { 17 class TestPageClickListener : public PageClickListener {
18 public: 18 public:
19 TestPageClickListener() 19 TestPageClickListener()
20 : called_(false), 20 : input_element_clicked_called_(false),
21 input_element_lost_focus_called_(false),
21 was_focused_(false), 22 was_focused_(false),
22 is_focused_(false), 23 is_focused_(false),
23 notification_response_(false) { 24 notification_response_(false) {
24 } 25 }
25 26
26 virtual bool InputElementClicked(const WebKit::WebInputElement& element, 27 virtual bool InputElementClicked(const WebKit::WebInputElement& element,
27 bool was_focused, 28 bool was_focused,
28 bool is_focused) { 29 bool is_focused) {
29 called_ = true; 30 input_element_clicked_called_ = true;
30 element_clicked_ = element; 31 element_clicked_ = element;
31 was_focused_ = was_focused; 32 was_focused_ = was_focused;
32 is_focused_ = is_focused; 33 is_focused_ = is_focused;
33 return notification_response_; 34 return notification_response_;
34 } 35 }
35 36
37 virtual bool InputElementLostFocus() {
38 input_element_lost_focus_called_ = true;
39 return notification_response_;
40 }
41
36 void ClearResults() { 42 void ClearResults() {
37 called_ = false; 43 input_element_clicked_called_ = false;
44 input_element_lost_focus_called_ = false;
38 element_clicked_.reset(); 45 element_clicked_.reset();
39 was_focused_ = false; 46 was_focused_ = false;
40 is_focused_ = false; 47 is_focused_ = false;
41 } 48 }
42 49
43 bool called_; 50 bool input_element_clicked_called_;
51 bool input_element_lost_focus_called_;
44 WebKit::WebInputElement element_clicked_; 52 WebKit::WebInputElement element_clicked_;
45 bool was_focused_; 53 bool was_focused_;
46 bool is_focused_; 54 bool is_focused_;
47 bool notification_response_; 55 bool notification_response_;
48 }; 56 };
49 57
58 class PageClickTrackerTest : public ChromeRenderViewTest {
59 protected:
60 virtual void SetUp() {
61 ChromeRenderViewTest::SetUp();
62
63 // RenderView creates PageClickTracker but it doesn't keep it around.
64 // Rather than make it do so for the test, we create a new object.
65 page_click_tracker_.reset(new PageClickTracker(view_));
csharp 2011/10/25 19:35:06 This can't be changed because we need to wait for
Ilya Sherman 2011/10/25 20:01:15 Ok, fair enough :)
66 page_click_tracker_->AddListener(&test_listener1_);
67 page_click_tracker_->AddListener(&test_listener2_);
68
69 LoadHTML("<form>"
70 " <input type='text' id='text_1'></input><br>"
71 " <input type='text' id='text_2'></input><br>"
72 " <input type='button' id='button'></input><br>"
73 "</form>");
74 GetWebWidget()->resize(WebKit::WebSize(500, 500));
75 GetWebWidget()->setFocus(true);
76 WebKit::WebDocument document = view_->GetWebView()->mainFrame()->document();
77 text_ = document.getElementById("text_1");
78 ASSERT_FALSE(text_.isNull());
79 }
80
81 scoped_ptr<PageClickTracker> page_click_tracker_;
82 TestPageClickListener test_listener1_;
83 TestPageClickListener test_listener2_;
84 WebKit::WebElement text_;
85 };
86
50 // Tests that PageClickTracker does notify correctly when a node is clicked. 87 // Tests that PageClickTracker does notify correctly when a node is clicked.
51 TEST_F(ChromeRenderViewTest, PageClickTracker) { 88 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. 89 // Click the text field once.
74 EXPECT_TRUE(SimulateElementClick("text")); 90 EXPECT_TRUE(SimulateElementClick("text_1"));
75 EXPECT_TRUE(test_listener1.called_); 91 EXPECT_TRUE(test_listener1_.input_element_clicked_called_);
76 EXPECT_TRUE(test_listener2.called_); 92 EXPECT_TRUE(test_listener2_.input_element_clicked_called_);
77 EXPECT_FALSE(test_listener1.was_focused_); 93 EXPECT_FALSE(test_listener1_.was_focused_);
78 EXPECT_FALSE(test_listener2.was_focused_); 94 EXPECT_FALSE(test_listener2_.was_focused_);
79 EXPECT_TRUE(test_listener1.is_focused_); 95 EXPECT_TRUE(test_listener1_.is_focused_);
80 EXPECT_TRUE(test_listener2.is_focused_); 96 EXPECT_TRUE(test_listener2_.is_focused_);
81 EXPECT_TRUE(text == test_listener1.element_clicked_); 97 EXPECT_TRUE(text_ == test_listener1_.element_clicked_);
82 EXPECT_TRUE(text == test_listener2.element_clicked_); 98 EXPECT_TRUE(text_ == test_listener2_.element_clicked_);
83 test_listener1.ClearResults(); 99 test_listener1_.ClearResults();
84 test_listener2.ClearResults(); 100 test_listener2_.ClearResults();
85 101
86 // Click the text field again to test that was_focused_ is set correctly. 102 // Click the text field again to test that was_focused_ is set correctly.
87 EXPECT_TRUE(SimulateElementClick("text")); 103 EXPECT_TRUE(SimulateElementClick("text_1"));
88 EXPECT_TRUE(test_listener1.called_); 104 EXPECT_TRUE(test_listener1_.input_element_clicked_called_);
89 EXPECT_TRUE(test_listener2.called_); 105 EXPECT_TRUE(test_listener2_.input_element_clicked_called_);
90 EXPECT_TRUE(test_listener1.was_focused_); 106 EXPECT_TRUE(test_listener1_.was_focused_);
91 EXPECT_TRUE(test_listener2.was_focused_); 107 EXPECT_TRUE(test_listener2_.was_focused_);
92 EXPECT_TRUE(test_listener1.is_focused_); 108 EXPECT_TRUE(test_listener1_.is_focused_);
93 EXPECT_TRUE(test_listener2.is_focused_); 109 EXPECT_TRUE(test_listener2_.is_focused_);
94 EXPECT_TRUE(text == test_listener1.element_clicked_); 110 EXPECT_TRUE(text_ == test_listener1_.element_clicked_);
95 EXPECT_TRUE(text == test_listener2.element_clicked_); 111 EXPECT_TRUE(text_ == test_listener2_.element_clicked_);
96 test_listener1.ClearResults(); 112 test_listener1_.ClearResults();
97 test_listener2.ClearResults(); 113 test_listener2_.ClearResults();
98 114
99 // Click the button, no notification should happen (this is not a text-input). 115 // Click the button, no notification should happen (this is not a text-input).
100 EXPECT_TRUE(SimulateElementClick("button")); 116 EXPECT_TRUE(SimulateElementClick("button"));
101 EXPECT_FALSE(test_listener1.called_); 117 EXPECT_FALSE(test_listener1_.input_element_clicked_called_);
102 EXPECT_FALSE(test_listener2.called_); 118 EXPECT_FALSE(test_listener2_.input_element_clicked_called_);
103 119
104 // Make the first listener stop the event propagation, click the text field 120 // Make the first listener stop the event propagation, click the text field
105 // and make sure only the first listener is notified. 121 // and make sure only the first listener is notified.
106 test_listener1.notification_response_ = true; 122 test_listener1_.notification_response_ = true;
107 EXPECT_TRUE(SimulateElementClick("text")); 123 EXPECT_TRUE(SimulateElementClick("text_1"));
108 EXPECT_TRUE(test_listener1.called_); 124 EXPECT_TRUE(test_listener1_.input_element_clicked_called_);
109 EXPECT_FALSE(test_listener2.called_); 125 EXPECT_FALSE(test_listener2_.input_element_clicked_called_);
110 test_listener1.ClearResults(); 126 test_listener1_.ClearResults();
111 127
112 // Make sure removing a listener work. 128 // Make sure removing a listener work.
113 page_click_tracker->RemoveListener(&test_listener1); 129 page_click_tracker_->RemoveListener(&test_listener1_);
114 EXPECT_TRUE(SimulateElementClick("text")); 130 EXPECT_TRUE(SimulateElementClick("text_1"));
115 EXPECT_FALSE(test_listener1.called_); 131 EXPECT_FALSE(test_listener1_.input_element_clicked_called_);
116 EXPECT_TRUE(test_listener2.called_); 132 EXPECT_TRUE(test_listener2_.input_element_clicked_called_);
117 test_listener2.ClearResults(); 133 test_listener2_.ClearResults();
118 134
119 // Make sure we don't choke when no listeners are registered. 135 // Make sure we don't choke when no listeners are registered.
120 page_click_tracker->RemoveListener(&test_listener2); 136 page_click_tracker_->RemoveListener(&test_listener2_);
121 EXPECT_TRUE(SimulateElementClick("text")); 137 EXPECT_TRUE(SimulateElementClick("text_1"));
122 EXPECT_FALSE(test_listener1.called_); 138 EXPECT_FALSE(test_listener1_.input_element_clicked_called_);
123 EXPECT_FALSE(test_listener2.called_); 139 EXPECT_FALSE(test_listener2_.input_element_clicked_called_);
124 } 140 }
141
142 TEST_F(PageClickTrackerTest, PageClickTrackerInputFocusLost) {
143 // Click a text field and test that no lost focus notifications are sent.
144 EXPECT_TRUE(SimulateElementClick("text_1"));
145 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_);
146 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_);
147 test_listener1_.ClearResults();
148 test_listener2_.ClearResults();
149
150 // Select another text field to test that the notifcation for the
151 // first text field losing focus is sent.
152 EXPECT_TRUE(SimulateElementClick("text_2"));
153 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_);
154 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_);
155 test_listener1_.ClearResults();
156 test_listener2_.ClearResults();
157
158 // Click the button, a notification should happen since a text field has
159 // lost focus.
160 EXPECT_TRUE(SimulateElementClick("button"));
161 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_);
162 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_);
163 test_listener1_.ClearResults();
164 test_listener2_.ClearResults();
165
166 // Click on a text field and ensure no lost focus notification is sent.
Ilya Sherman 2011/10/25 20:06:16 nit: "Click on a text field while the button has f
csharp1 2011/10/27 15:20:58 Done.
167 EXPECT_TRUE(SimulateElementClick("text_1"));
168 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_);
169 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_);
170
171 // Make the first listener stop the event propagation, then a button and
172 // make sure only the first listener is notified.
Ilya Sherman 2011/10/25 20:06:16 nit: "then a button" -> "then click a button"
csharp1 2011/10/27 15:20:58 Done.
173 test_listener1_.notification_response_ = true;
174 EXPECT_TRUE(SimulateElementClick("button"));
175 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_);
176 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_);
177 test_listener1_.ClearResults();
178
179 // Make sure removing a listener work.
180 page_click_tracker_->RemoveListener(&test_listener1_);
181 EXPECT_TRUE(SimulateElementClick("text_1"));
182 EXPECT_TRUE(SimulateElementClick("button"));
183 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_);
184 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_);
185 test_listener2_.ClearResults();
186
187 // Make sure we don't choke when no listeners are registered.
188 page_click_tracker_->RemoveListener(&test_listener2_);
189 EXPECT_TRUE(SimulateElementClick("text_1"));
190 EXPECT_TRUE(SimulateElementClick("button"));
191 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_);
192 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_);
193 }
OLDNEW
« chrome/renderer/page_click_tracker.cc ('K') | « chrome/renderer/page_click_tracker.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698