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

Side by Side Diff: chrome/browser/ui/webui/engagement/site_engagement_ui_browsertest.cc

Issue 2780873003: Add a browser test for the chrome://site-engagement WebUI. (Closed)
Patch Set: Switch to Promise Created 3 years, 8 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
« no previous file with comments | « chrome/browser/resources/engagement/site_engagement.js ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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.pageIsPopulatedPromiseForTest().then(function() {"
Bernhard Bauer 2017/04/06 15:20:47 Nit: I realize that technically the function does
Wez 2017/04/06 23:48:06 Ah, I see your point; you want the call site to re
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 " function(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 }
OLDNEW
« no previous file with comments | « chrome/browser/resources/engagement/site_engagement.js ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698