Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/values.h" | |
| 12 #include "chrome/browser/engagement/site_engagement_helper.h" | |
| 13 #include "chrome/browser/engagement/site_engagement_service.h" | |
| 14 #include "chrome/browser/engagement/site_engagement_service_factory.h" | |
| 15 #include "chrome/browser/ui/browser.h" | |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 17 #include "chrome/common/chrome_switches.h" | |
| 18 #include "chrome/test/base/browser_with_test_window_test.h" | |
| 19 #include "chrome/test/base/in_process_browser_test.h" | |
| 20 #include "chrome/test/base/ui_test_utils.h" | |
| 21 #include "content/public/test/browser_test_utils.h" | |
| 22 #include "content/public/test/test_navigation_observer.h" | |
| 23 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 #include "ui/events/keycodes/keyboard_codes.h" | |
| 26 | |
| 27 class SiteEngagementServiceBrowserTest : public InProcessBrowserTest { | |
| 28 public: | |
| 29 void SetUpOnMainThread() override { | |
| 30 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 31 switches::kDisableSiteEngagementService); | |
|
calamity
2015/09/22 02:57:21
Why is this necessary? It probably needs a comment
dominickn
2015/09/23 00:06:44
Done.
| |
| 32 SiteEngagementHelper::SetSecondsBetweenUserInputCheck(10); | |
|
calamity
2015/09/22 02:57:21
This time change doesn't affect anything at the mo
dominickn
2015/09/23 00:06:44
With the mock timer in for the test, not any more.
| |
| 33 SiteEngagementHelper::DisableCallbackRegistrationForTesting(); | |
|
calamity
2015/09/22 02:57:21
// This prevents other browser tests from generati
dominickn
2015/09/23 00:06:44
It was primarily to address the case where I'm run
calamity
2015/09/24 03:50:53
Acknowledged.
| |
| 34 | |
| 35 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 36 InProcessBrowserTest::SetUpOnMainThread(); | |
| 37 } | |
| 38 | |
| 39 void NavigateToURL(content::WebContents* web_contents, GURL url) { | |
| 40 content::TestNavigationObserver observer(web_contents, 1); | |
| 41 ui_test_utils::NavigateToURL(browser(), url); | |
| 42 observer.Wait(); | |
| 43 } | |
| 44 | |
| 45 SiteEngagementHelper* CreateHelper(content::WebContents* web_contents) { | |
| 46 SiteEngagementHelper::CreateForWebContents(web_contents); | |
| 47 SiteEngagementHelper* helper = | |
| 48 SiteEngagementHelper::FromWebContents(web_contents); | |
| 49 DCHECK(helper); | |
| 50 return helper; | |
| 51 } | |
| 52 | |
| 53 void HandleKeyPress(SiteEngagementHelper* helper, ui::KeyboardCode key) { | |
| 54 content::NativeWebKeyboardEvent event; | |
| 55 event.windowsKeyCode = key; | |
| 56 event.type = blink::WebKeyboardEvent::RawKeyDown; | |
| 57 helper->tracker_->HandleKeyPressEvent(event); | |
| 58 } | |
| 59 | |
| 60 void HandleMouseEvent(SiteEngagementHelper* helper, | |
| 61 blink::WebMouseEvent::Button button, | |
| 62 blink::WebInputEvent::Type type) { | |
| 63 blink::WebMouseEvent event; | |
| 64 event.button = button; | |
| 65 event.type = type; | |
| 66 helper->tracker_->HandleMouseEvent(event); | |
| 67 } | |
| 68 }; | |
| 69 | |
| 70 IN_PROC_BROWSER_TEST_F(SiteEngagementServiceBrowserTest, | |
| 71 KeyPressEngagementAccumulation) { | |
| 72 GURL url("https://www.google.com/"); | |
| 73 GURL url2("http://www.google.com/"); | |
| 74 content::WebContents* web_contents = | |
| 75 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 76 | |
| 77 SiteEngagementHelper* helper = CreateHelper(web_contents); | |
| 78 SiteEngagementService* service = | |
| 79 SiteEngagementServiceFactory::GetForProfile(browser()->profile()); | |
| 80 DCHECK(service); | |
| 81 | |
| 82 NavigateToURL(web_contents, url); | |
| 83 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url)); | |
| 84 EXPECT_EQ(0, service->GetScore(url2)); | |
| 85 | |
| 86 HandleKeyPress(helper, ui::VKEY_DOWN); | |
| 87 | |
| 88 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url)); | |
| 89 EXPECT_EQ(0, service->GetScore(url2)); | |
| 90 | |
| 91 HandleKeyPress(helper, ui::VKEY_UP); | |
| 92 HandleKeyPress(helper, ui::VKEY_RETURN); | |
| 93 HandleKeyPress(helper, ui::VKEY_J); | |
|
calamity
2015/09/22 02:57:21
There needs to be some test for the timer accumula
dominickn
2015/09/23 00:06:44
Done.
| |
| 94 | |
| 95 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url)); | |
| 96 EXPECT_EQ(0, service->GetScore(url2)); | |
| 97 | |
| 98 NavigateToURL(web_contents, url2); | |
| 99 | |
| 100 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url)); | |
| 101 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url2)); | |
| 102 EXPECT_DOUBLE_EQ(1.2, service->GetTotalEngagementPoints()); | |
| 103 | |
| 104 HandleKeyPress(helper, ui::VKEY_K); | |
| 105 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url)); | |
| 106 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url2)); | |
| 107 EXPECT_DOUBLE_EQ(1.25, service->GetTotalEngagementPoints()); | |
| 108 } | |
| 109 | |
| 110 IN_PROC_BROWSER_TEST_F(SiteEngagementServiceBrowserTest, | |
| 111 MouseEventEngagementAccumulation) { | |
| 112 GURL url("https://www.google.com/"); | |
| 113 GURL url2("http://www.google.com/"); | |
| 114 content::WebContents* web_contents = | |
| 115 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 116 | |
| 117 SiteEngagementHelper* helper = CreateHelper(web_contents); | |
| 118 SiteEngagementService* service = | |
| 119 SiteEngagementServiceFactory::GetForProfile(browser()->profile()); | |
| 120 DCHECK(service); | |
| 121 | |
| 122 NavigateToURL(web_contents, url); | |
| 123 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url)); | |
| 124 EXPECT_EQ(0, service->GetScore(url2)); | |
| 125 | |
| 126 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonLeft, | |
| 127 blink::WebInputEvent::MouseDown); | |
| 128 | |
| 129 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url)); | |
| 130 EXPECT_EQ(0, service->GetScore(url2)); | |
| 131 | |
| 132 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonRight, | |
| 133 blink::WebInputEvent::ContextMenu); | |
| 134 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonMiddle, | |
| 135 blink::WebInputEvent::MouseDown); | |
| 136 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonNone, | |
| 137 blink::WebInputEvent::MouseMove); | |
| 138 | |
| 139 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url)); | |
| 140 EXPECT_EQ(0, service->GetScore(url2)); | |
| 141 | |
| 142 NavigateToURL(web_contents, url2); | |
| 143 | |
| 144 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url)); | |
| 145 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url2)); | |
| 146 EXPECT_DOUBLE_EQ(1.2, service->GetTotalEngagementPoints()); | |
| 147 | |
| 148 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonNone, | |
| 149 blink::WebInputEvent::GestureScrollUpdate); | |
| 150 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url)); | |
| 151 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url2)); | |
| 152 EXPECT_DOUBLE_EQ(1.25, service->GetTotalEngagementPoints()); | |
| 153 } | |
| 154 | |
| 155 IN_PROC_BROWSER_TEST_F(SiteEngagementServiceBrowserTest, | |
| 156 MixedInputEngagementAccumulation) { | |
| 157 GURL url("https://www.google.com/"); | |
| 158 GURL url2("http://www.google.com/"); | |
| 159 content::WebContents* web_contents = | |
| 160 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 161 | |
| 162 SiteEngagementHelper* helper = CreateHelper(web_contents); | |
| 163 SiteEngagementService* service = | |
| 164 SiteEngagementServiceFactory::GetForProfile(browser()->profile()); | |
| 165 DCHECK(service); | |
| 166 | |
| 167 NavigateToURL(web_contents, url); | |
| 168 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url)); | |
| 169 EXPECT_EQ(0, service->GetScore(url2)); | |
| 170 | |
| 171 HandleKeyPress(helper, ui::VKEY_UP); | |
| 172 HandleKeyPress(helper, ui::VKEY_RETURN); | |
| 173 HandleKeyPress(helper, ui::VKEY_J); | |
| 174 | |
| 175 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonLeft, | |
| 176 blink::WebInputEvent::MouseDown); | |
| 177 | |
| 178 EXPECT_DOUBLE_EQ(0.7, service->GetScore(url)); | |
| 179 EXPECT_EQ(0, service->GetScore(url2)); | |
| 180 | |
| 181 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonRight, | |
| 182 blink::WebInputEvent::ContextMenu); | |
| 183 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonMiddle, | |
| 184 blink::WebInputEvent::MouseDown); | |
| 185 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonNone, | |
| 186 blink::WebInputEvent::MouseMove); | |
| 187 | |
| 188 EXPECT_DOUBLE_EQ(0.85, service->GetScore(url)); | |
| 189 EXPECT_EQ(0, service->GetScore(url2)); | |
| 190 | |
| 191 NavigateToURL(web_contents, url2); | |
| 192 | |
| 193 EXPECT_DOUBLE_EQ(0.85, service->GetScore(url)); | |
| 194 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url2)); | |
| 195 EXPECT_DOUBLE_EQ(1.35, service->GetTotalEngagementPoints()); | |
| 196 | |
| 197 HandleMouseEvent(helper, blink::WebMouseEvent::ButtonNone, | |
| 198 blink::WebInputEvent::GestureScrollUpdate); | |
| 199 HandleKeyPress(helper, ui::VKEY_DOWN); | |
| 200 | |
| 201 EXPECT_DOUBLE_EQ(0.85, service->GetScore(url)); | |
| 202 EXPECT_DOUBLE_EQ(0.6, service->GetScore(url2)); | |
| 203 EXPECT_DOUBLE_EQ(1.45, service->GetTotalEngagementPoints()); | |
| 204 } | |
| OLD | NEW |