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

Side by Side Diff: chrome/browser/engagement/site_engagement_service_browsertest.cc

Issue 1338603002: Implement a site engagement score based on time-on-site. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update types used in browser test Created 5 years, 2 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/command_line.h"
6 #include "base/run_loop.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/task_runner.h"
9 #include "base/test/simple_test_clock.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/timer/mock_timer.h"
12 #include "base/values.h"
13 #include "chrome/browser/engagement/site_engagement_helper.h"
14 #include "chrome/browser/engagement/site_engagement_service.h"
15 #include "chrome/browser/engagement/site_engagement_service_factory.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/browser_with_test_window_test.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "content/public/test/browser_test_utils.h"
23 #include "content/public/test/test_navigation_observer.h"
24 #include "net/test/embedded_test_server/embedded_test_server.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/events/keycodes/keyboard_codes.h"
27
28 class TestObserver : public SiteEngagementHelper::Observer {
29 public:
30 TestObserver(SiteEngagementHelper* helper, base::Closure quit_closure)
31 : helper_(helper), quit_closure_(quit_closure) { }
32 ~TestObserver() {
33 helper_->RemoveObserverForTesting(this);
34 }
35
36 void OnInputRecorded(SiteEngagementHelper* helper) override {
37 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_);
38 }
39
40 private:
41 SiteEngagementHelper* helper_;
42 base::Closure quit_closure_;
43 };
44
45 class SiteEngagementServiceBrowserTest : public InProcessBrowserTest {
46 public:
47 void SetUpOnMainThread() override {
48 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
49 InProcessBrowserTest::SetUpOnMainThread();
50 }
51
52 // Create a SiteEngagementHelper. Called here as friend class methods cannot
53 // be called in tests.
54 SiteEngagementHelper* CreateHelper(content::WebContents* web_contents,
55 bool enable_callbacks) {
56 SiteEngagementHelper::CreateForWebContents(web_contents);
57 SiteEngagementHelper* helper =
58 SiteEngagementHelper::FromWebContents(web_contents);
59 DCHECK(helper);
60
61 // Selectively enable or disable the input listening callbacks to avoid
62 // interference in tests.
63 if (enable_callbacks)
64 SiteEngagementHelper::EnableCallbackRegistrationForTesting();
65 else
66 SiteEngagementHelper::DisableCallbackRegistrationForTesting();
67 return helper;
68 }
69
70 // Simulate a key press event and handle it.
71 void HandleKeyPress(SiteEngagementHelper* helper, ui::KeyboardCode key) {
72 content::NativeWebKeyboardEvent event;
73 event.windowsKeyCode = key;
74 event.type = blink::WebKeyboardEvent::RawKeyDown;
75 helper->tracker_->HandleKeyPressEvent(event);
76 }
77
78 // Simulate a mouse event and handle it.
79 void HandleMouseEvent(SiteEngagementHelper* helper,
80 blink::WebMouseEvent::Button button,
81 blink::WebInputEvent::Type type) {
82 blink::WebMouseEvent event;
83 event.button = button;
84 event.type = type;
85 helper->tracker_->HandleMouseEvent(event);
86 }
87
88 // Set a timer object for test purposes.
89 void SetHelperTimer(SiteEngagementHelper* helper,
90 scoped_ptr<base::Timer> timer) {
91 helper->tracker_->SetTimerForTesting(timer.Pass());
92 }
93 };
94
95 IN_PROC_BROWSER_TEST_F(SiteEngagementServiceBrowserTest,
96 KeyPressEngagementAccumulation) {
97 GURL url("https://www.google.com/");
98 GURL url2("http://www.google.com/");
99 content::WebContents* web_contents =
100 browser()->tab_strip_model()->GetActiveWebContents();
101
102 SiteEngagementHelper* helper = CreateHelper(web_contents, false);
103 SiteEngagementService* service =
104 SiteEngagementServiceFactory::GetForProfile(browser()->profile());
105 DCHECK(service);
106
107 // Check that navigation triggers engagement.
108 ui_test_utils::NavigateToURL(browser(), url);
109 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url));
110 EXPECT_EQ(0, service->GetScore(url2));
111
112 // Simulate a key press trigger and ensure it is treated correctly.
113 HandleKeyPress(helper, ui::VKEY_DOWN);
114
115 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url));
116 EXPECT_EQ(0, service->GetScore(url2));
117
118 // Simulate three key presses, and ensure they are treated correctly.
119 HandleKeyPress(helper, ui::VKEY_UP);
120 HandleKeyPress(helper, ui::VKEY_RETURN);
121 HandleKeyPress(helper, ui::VKEY_J);
122
123 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url));
124 EXPECT_EQ(0, service->GetScore(url2));
125
126 // Simulate key presses for a different link.
127 ui_test_utils::NavigateToURL(browser(), url2);
128
129 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url));
130 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url2));
131 EXPECT_DOUBLE_EQ(1.2, service->GetTotalEngagementPoints());
132
133 HandleKeyPress(helper, ui::VKEY_K);
134 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url));
135 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url2));
136 EXPECT_DOUBLE_EQ(1.25, service->GetTotalEngagementPoints());
137 }
138
139 IN_PROC_BROWSER_TEST_F(SiteEngagementServiceBrowserTest,
140 MouseEventEngagementAccumulation) {
141 GURL url("https://www.google.com/");
142 GURL url2("http://www.google.com/");
143 content::WebContents* web_contents =
144 browser()->tab_strip_model()->GetActiveWebContents();
145
146 SiteEngagementHelper* helper = CreateHelper(web_contents, false);
147 SiteEngagementService* service =
148 SiteEngagementServiceFactory::GetForProfile(browser()->profile());
149 DCHECK(service);
150
151 ui_test_utils::NavigateToURL(browser(), url);
152 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url));
153 EXPECT_EQ(0, service->GetScore(url2));
154
155 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonLeft,
156 blink::WebInputEvent::MouseDown);
157
158 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url));
159 EXPECT_EQ(0, service->GetScore(url2));
160
161 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonRight,
162 blink::WebInputEvent::MouseWheel);
163 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonMiddle,
164 blink::WebInputEvent::MouseDown);
165 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonNone,
166 blink::WebInputEvent::MouseMove);
167
168 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url));
169 EXPECT_EQ(0, service->GetScore(url2));
170
171 ui_test_utils::NavigateToURL(browser(), url2);
172
173 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url));
174 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url2));
175 EXPECT_DOUBLE_EQ(1.2, service->GetTotalEngagementPoints());
176
177 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonLeft,
178 blink::WebInputEvent::MouseDown);
179 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url));
180 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url2));
181 EXPECT_DOUBLE_EQ(1.25, service->GetTotalEngagementPoints());
182 }
183
184 IN_PROC_BROWSER_TEST_F(SiteEngagementServiceBrowserTest,
185 MixedInputEngagementAccumulation) {
186 GURL url("https://www.google.com/");
187 GURL url2("http://www.google.com/");
188 content::WebContents* web_contents =
189 browser()->tab_strip_model()->GetActiveWebContents();
190
191 SiteEngagementHelper* helper = CreateHelper(web_contents, false);
192 SiteEngagementService* service =
193 SiteEngagementServiceFactory::GetForProfile(browser()->profile());
194 DCHECK(service);
195
196 ui_test_utils::NavigateToURL(browser(), url);
197 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url));
198 EXPECT_EQ(0, service->GetScore(url2));
199
200 HandleKeyPress(helper, ui::VKEY_UP);
201 HandleKeyPress(helper, ui::VKEY_RETURN);
202 HandleKeyPress(helper, ui::VKEY_J);
203
204 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonLeft,
205 blink::WebInputEvent::MouseDown);
206
207 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url));
208 EXPECT_EQ(0, service->GetScore(url2));
209
210 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonRight,
211 blink::WebInputEvent::MouseMove);
212 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonMiddle,
213 blink::WebInputEvent::MouseDown);
214 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonNone,
215 blink::WebInputEvent::MouseMove);
216
217 EXPECT_DOUBLE_EQ(0.85, service->GetScore(url));
218 EXPECT_EQ(0, service->GetScore(url2));
219
220 ui_test_utils::NavigateToURL(browser(), url2);
221
222 EXPECT_DOUBLE_EQ(0.85, service->GetScore(url));
223 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url2));
224 EXPECT_DOUBLE_EQ(1.35, service->GetTotalEngagementPoints());
225
226 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonNone,
227 blink::WebInputEvent::MouseWheel);
228 HandleKeyPress(helper, ui::VKEY_DOWN);
229
230 EXPECT_DOUBLE_EQ(0.85, service->GetScore(url));
231 EXPECT_DOUBLE_EQ(0.6, service->GetScore(url2));
232 EXPECT_DOUBLE_EQ(1.45, service->GetTotalEngagementPoints());
233 }
234
235 IN_PROC_BROWSER_TEST_F(SiteEngagementServiceBrowserTest,
236 SimulateInput) {
237 GURL url("https://www.google.com/");
238 GURL url2("http://www.google.com/");
239 content::WebContents* web_contents =
240 browser()->tab_strip_model()->GetActiveWebContents();
241
242 scoped_ptr<base::MockTimer> mock_timer(new base::MockTimer(true, false));
243 base::MockTimer* timer = mock_timer.get();
244 SiteEngagementHelper* helper = CreateHelper(web_contents, true);
245 SetHelperTimer(helper, mock_timer.Pass());
246
247 SiteEngagementService* service =
248 SiteEngagementServiceFactory::GetForProfile(browser()->profile());
249 DCHECK(service);
250 EXPECT_FALSE(timer->IsRunning());
251
252 // Navigate and click. Ensure that the timer is running as expected.
253 {
254 base::RunLoop run_loop;
255 TestObserver observer(helper, run_loop.QuitClosure());
256 helper->AddObserverForTesting(&observer);
257
258 ui_test_utils::NavigateToURL(browser(), url);
259 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url));
260 EXPECT_EQ(0, service->GetScore(url2));
261 EXPECT_FALSE(timer->IsRunning());
262
263 // Click and wait until the observer exits.
264 content::SimulateMouseClick(web_contents, 0,
265 blink::WebMouseEvent::ButtonLeft);
266 run_loop.Run();
267
268 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url));
269 EXPECT_EQ(0, service->GetScore(url2));
270 EXPECT_TRUE(timer->IsRunning());
271 timer->Fire();
272 }
273
274 // Navigate and click.
275 {
276 base::RunLoop run_loop;
277 TestObserver observer(helper, run_loop.QuitClosure());
278 helper->AddObserverForTesting(&observer);
279
280 EXPECT_FALSE(timer->IsRunning());
281 ui_test_utils::NavigateToURL(browser(), url);
282 EXPECT_DOUBLE_EQ(1.05, service->GetScore(url));
283 EXPECT_EQ(0, service->GetScore(url2));
284 EXPECT_FALSE(timer->IsRunning());
285
286 content::SimulateMouseClick(web_contents, 0,
287 blink::WebMouseEvent::ButtonLeft);
288 run_loop.Run();
289
290 EXPECT_DOUBLE_EQ(1.1, service->GetScore(url));
291 EXPECT_EQ(0, service->GetScore(url2));
292 EXPECT_TRUE(timer->IsRunning());
293 timer->Fire();
294 }
295
296 // Click only.
297 {
298 base::RunLoop run_loop;
299 TestObserver observer(helper, run_loop.QuitClosure());
300 helper->AddObserverForTesting(&observer);
301
302 EXPECT_FALSE(timer->IsRunning());
303 content::SimulateMouseClick(web_contents, 0,
304 blink::WebMouseEvent::ButtonMiddle);
305 run_loop.Run();
306
307 EXPECT_DOUBLE_EQ(1.15, service->GetScore(url));
308 EXPECT_EQ(0, service->GetScore(url2));
309 EXPECT_TRUE(timer->IsRunning());
310 timer->Fire();
311 }
312
313 // Navigate and click.
314 {
315 base::RunLoop run_loop;
316 TestObserver observer(helper, run_loop.QuitClosure());
317 helper->AddObserverForTesting(&observer);
318
319 EXPECT_FALSE(timer->IsRunning());
320 ui_test_utils::NavigateToURL(browser(), url);
321 EXPECT_DOUBLE_EQ(1.65, service->GetScore(url));
322 EXPECT_EQ(0, service->GetScore(url2));
323 content::SimulateMouseClick(web_contents, 0,
324 blink::WebMouseEvent::ButtonRight);
325 run_loop.Run();
326
327 EXPECT_DOUBLE_EQ(1.7, service->GetScore(url));
328 EXPECT_EQ(0, service->GetScore(url2));
329 EXPECT_TRUE(timer->IsRunning());
330 timer->Fire();
calamity 2015/09/24 03:50:53 Simulate an extra event before the timer fires to
dominickn 2015/09/24 06:52:07 Done, though I'm not sure we can properly wait and
calamity 2015/09/24 07:36:21 Hmm. True. Can you just add a test accessor to cal
331 }
332 }
333
334 IN_PROC_BROWSER_TEST_F(SiteEngagementServiceBrowserTest,
335 ShowAndHide) {
336 GURL url("https://www.google.com/");
calamity 2015/09/24 03:50:53 nit: url1
dominickn 2015/09/24 06:52:07 Done.
337 GURL url2("http://www.google.com/");
338 content::WebContents* web_contents =
339 browser()->tab_strip_model()->GetActiveWebContents();
340
341 scoped_ptr<base::MockTimer> mock_timer(new base::MockTimer(true, false));
342 base::MockTimer* timer = mock_timer.get();
343 SiteEngagementHelper* helper = CreateHelper(web_contents, true);
calamity 2015/09/24 03:50:53 Wait, doesn't this reintroduce the flakiness you w
dominickn 2015/09/24 06:52:07 I've added another test which does just call Handl
calamity 2015/09/24 07:36:21 Ah, right. Because the callbacks are only called b
344 SetHelperTimer(helper, mock_timer.Pass());
345
346 SiteEngagementService* service =
347 SiteEngagementServiceFactory::GetForProfile(browser()->profile());
348 DCHECK(service);
349 EXPECT_FALSE(timer->IsRunning());
350
351 // Navigate and hide.
352 {
353 base::RunLoop run_loop;
354 TestObserver observer(helper, run_loop.QuitClosure());
355 helper->AddObserverForTesting(&observer);
356
357 ui_test_utils::NavigateToURL(browser(), url);
358 content::SimulateMouseClick(web_contents, 0,
359 blink::WebMouseEvent::ButtonRight);
360 run_loop.Run();
361
362 // Timer runs after input is recorded.
363 EXPECT_TRUE(timer->IsRunning());
364
365 ui_test_utils::NavigateToURLWithDisposition(
366 browser(), url2, NEW_FOREGROUND_TAB,
367 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
368
369 // Hiding the original tab should stop the timer.
370 EXPECT_FALSE(timer->IsRunning());
371 EXPECT_EQ(2, browser()->tab_strip_model()->count());
372
373 // Timer should still be stopped on re-focus.
calamity 2015/09/24 03:50:53 Hmm. So does this mean that showing a popup in a n
dominickn 2015/09/24 06:52:07 This case will be handled by adding a 1-shot timer
calamity 2015/09/24 07:36:21 Acknowledged.
374 browser()->tab_strip_model()->GetWebContentsAt(0)->WasShown();
calamity 2015/09/24 03:50:53 May as well call TasbStripModel::ActivateTabAt(0).
dominickn 2015/09/24 06:52:07 Done.
375 EXPECT_FALSE(timer->IsRunning());
376 }
377
378 // Click. Timer should be started.
379 {
380 base::RunLoop run_loop;
381 TestObserver observer(helper, run_loop.QuitClosure());
382 helper->AddObserverForTesting(&observer);
383
384 content::SimulateMouseClick(web_contents, 0,
385 blink::WebMouseEvent::ButtonRight);
386 run_loop.Run();
387
388 // Timer runs after input is recorded.
389 EXPECT_TRUE(timer->IsRunning());
390 timer->Fire();
391 EXPECT_FALSE(timer->IsRunning());
392 }
393 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698