OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/strings/string_util.h" |
| 6 #include "chrome/browser/engagement/site_engagement_service.h" |
| 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 9 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" |
| 10 #include "chrome/common/url_constants.h" |
| 11 #include "chrome/test/base/in_process_browser_test.h" |
| 12 #include "chrome/test/base/ui_test_utils.h" |
| 13 #include "content/public/browser/child_process_security_policy.h" |
| 14 #include "content/public/browser/render_process_host.h" |
| 15 #include "content/public/common/content_switches.h" |
| 16 #include "content/public/test/browser_test_utils.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 const GURL kExampleUrl = GURL("https://www.example.com/"); |
| 21 |
| 22 } // namespace |
| 23 |
| 24 class SiteEngagementUiBrowserTest : public InProcessBrowserTest { |
| 25 protected: |
| 26 // Returns the SiteEngagementService for the test browser profile. |
| 27 SiteEngagementService* engagement_service() { |
| 28 return SiteEngagementService::Get(browser()->profile()); |
| 29 } |
| 30 |
| 31 // (Re)sets the base score for a URL's site engagement. |
| 32 void ResetBaseScore(const GURL& url, double score) { |
| 33 engagement_service()->ResetBaseScoreForURL(url, score); |
| 34 } |
| 35 void ResetBaseScoreToMax(const GURL& url) { |
| 36 ResetBaseScore(url, engagement_service()->GetMaxPoints()); |
| 37 } |
| 38 |
| 39 // Navigates the tab to the site engagement WebUI. |
| 40 void NavigateToWebUi() { |
| 41 content::WebContents* web_contents = |
| 42 browser()->tab_strip_model()->GetActiveWebContents(); |
| 43 |
| 44 const GURL web_ui_url(base::JoinString( |
| 45 {content::kChromeUIScheme, "://", chrome::kChromeUISiteEngagementHost}, |
| 46 "")); |
| 47 EXPECT_TRUE(ChromeWebUIControllerFactory::GetInstance()->UseWebUIForURL( |
| 48 web_contents->GetBrowserContext(), web_ui_url)); |
| 49 ui_test_utils::NavigateToURL(browser(), web_ui_url); |
| 50 EXPECT_TRUE( |
| 51 content::ChildProcessSecurityPolicy::GetInstance()->HasWebUIBindings( |
| 52 web_contents->GetRenderProcessHost()->GetID())); |
| 53 |
| 54 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); |
| 55 } |
| 56 |
| 57 // Waits for the tab to be populated with site engagement data. |
| 58 void WaitUntilPagePopulated() { |
| 59 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 60 browser()->tab_strip_model()->GetActiveWebContents(), |
| 61 "window.whenPageIsPopulatedForTest().then(() => {" |
| 62 " window.domAutomationController.send(true);" |
| 63 "});", |
| 64 &page_is_populated_)); |
| 65 |
| 66 ASSERT_TRUE(page_is_populated_); |
| 67 } |
| 68 |
| 69 // Verifies that a row exists for the specified site URL. |
| 70 void ExpectPageContainsUrl(const GURL& url) { |
| 71 ASSERT_TRUE(page_is_populated_); |
| 72 |
| 73 bool found_url = false; |
| 74 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 75 browser()->tab_strip_model()->GetActiveWebContents(), |
| 76 base::JoinString( |
| 77 {"var origin_cells = " |
| 78 " Array.from(document.getElementsByClassName('origin-cell'));" |
| 79 "window.domAutomationController.send(origin_cells.reduce(" |
| 80 " (found, element) => {" |
| 81 " return found || (element.innerHTML == '", |
| 82 url.spec(), |
| 83 "');" |
| 84 " }, false));"}, |
| 85 ""), |
| 86 &found_url)); |
| 87 EXPECT_TRUE(found_url); |
| 88 } |
| 89 |
| 90 private: |
| 91 // True if the page contains site engagement data. |
| 92 bool page_is_populated_ = false; |
| 93 }; |
| 94 |
| 95 IN_PROC_BROWSER_TEST_F(SiteEngagementUiBrowserTest, Basic) { |
| 96 ResetBaseScoreToMax(kExampleUrl); |
| 97 |
| 98 NavigateToWebUi(); |
| 99 WaitUntilPagePopulated(); |
| 100 |
| 101 ExpectPageContainsUrl(kExampleUrl); |
| 102 } |
OLD | NEW |