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(std::string(content::kChromeUIScheme) + "://" + | |
Bernhard Bauer
2017/03/31 13:05:00
Can you use JoinString here as well? Just in terms
Wez
2017/04/05 20:49:37
Done.
| |
45 std::string(chrome::kChromeUISiteEngagementHost)); | |
46 EXPECT_TRUE(ChromeWebUIControllerFactory::GetInstance()->UseWebUIForURL( | |
47 web_contents->GetBrowserContext(), web_ui_url)); | |
48 ui_test_utils::NavigateToURL(browser(), web_ui_url); | |
49 EXPECT_TRUE( | |
50 content::ChildProcessSecurityPolicy::GetInstance()->HasWebUIBindings( | |
51 web_contents->GetRenderProcessHost()->GetID())); | |
52 | |
53 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); | |
54 } | |
55 | |
56 // Waits for the tab to be populated with site engagement data. | |
57 void WaitUntilPagePopulated() { | |
58 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
59 browser()->tab_strip_model()->GetActiveWebContents(), | |
60 "window.setPagePopulatedCallbackForTest(function() {" | |
61 " window.domAutomationController.send(true);" | |
calamity
2017/04/03 04:38:52
And then here:
window.waitForPagePopulated().then
Wez
2017/04/05 20:49:37
Done.
| |
62 "});", | |
63 &page_is_populated_)); | |
64 | |
65 ASSERT_TRUE(page_is_populated_); | |
66 } | |
67 | |
68 // Verifies that a row exists for the specified site URL. | |
69 void ExpectPageContainsUrl(const GURL& url) { | |
70 ASSERT_TRUE(page_is_populated_); | |
71 | |
72 bool found_url = false; | |
73 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
74 browser()->tab_strip_model()->GetActiveWebContents(), | |
75 base::JoinString( | |
76 {"var origin_cells = " | |
77 " Array.from(document.getElementsByClassName('origin-cell'));" | |
78 "window.domAutomationController.send(origin_cells.reduce(" | |
79 " function(found, element) {" | |
80 " return found || (element.innerHTML == '", | |
81 url.spec(), | |
82 "');" | |
83 " }, false));"}, | |
84 ""), | |
85 &found_url)); | |
86 EXPECT_TRUE(found_url); | |
87 } | |
88 | |
89 private: | |
90 // True if the page contains site engagement data. | |
91 bool page_is_populated_ = false; | |
92 }; | |
93 | |
94 IN_PROC_BROWSER_TEST_F(SiteEngagementUiBrowserTest, Basic) { | |
95 ResetBaseScoreToMax(kExampleUrl); | |
96 | |
97 NavigateToWebUi(); | |
98 WaitUntilPagePopulated(); | |
99 | |
100 ExpectPageContainsUrl(kExampleUrl); | |
101 } | |
OLD | NEW |