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

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

Issue 2811643002: [SiteEngagement WebUI] Replace C++ browser test with JS version. (Closed)
Patch Set: rebase, address comments Created 3 years, 7 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 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 // Expects that there will be the specified number of rows.
70 int NumberOfRows() {
71 EXPECT_TRUE(page_is_populated_);
72
73 int number_of_rows = -1;
74 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
75 browser()->tab_strip_model()->GetActiveWebContents(),
76 "window.domAutomationController.send("
77 " document.getElementsByClassName('origin-cell').length);",
78 &number_of_rows));
79
80 return number_of_rows;
81 }
82
83 // Returns the origin URL at the specified zero-based row index.
84 std::string OriginUrlAtRow(int index) {
85 EXPECT_TRUE(page_is_populated_);
86
87 std::string origin_url;
88 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
89 browser()->tab_strip_model()->GetActiveWebContents(),
90 base::JoinString({"window.domAutomationController.send("
91 " document.getElementsByClassName('origin-cell')[",
92 base::IntToString(index), "].innerHTML);"},
93 ""),
94 &origin_url));
95
96 return origin_url;
97 }
98
99 // Returns the stringified base score at the specified zero-based row index.
100 std::string BaseScoreAtRow(int index) {
101 EXPECT_TRUE(page_is_populated_);
102
103 std::string score_string;
104 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
105 browser()->tab_strip_model()->GetActiveWebContents(),
106 base::JoinString(
107 {"window.domAutomationController.send("
108 " document.getElementsByClassName('base-score-input')[",
109 base::IntToString(index), "].value);"},
110 ""),
111 &score_string));
112
113 return score_string;
114 }
115
116 std::string BonusScoreAtRow(int index) {
117 EXPECT_TRUE(page_is_populated_);
118
119 std::string score_string;
120 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
121 browser()->tab_strip_model()->GetActiveWebContents(),
122 base::JoinString(
123 {"window.domAutomationController.send("
124 " document.getElementsByClassName('bonus-score-cell')[",
125 base::IntToString(index), "].innerHTML);"},
126 ""),
127 &score_string));
128
129 return score_string;
130 }
131
132 std::string TotalScoreAtRow(int index) {
133 EXPECT_TRUE(page_is_populated_);
134
135 std::string score_string;
136 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
137 browser()->tab_strip_model()->GetActiveWebContents(),
138 base::JoinString(
139 {"window.domAutomationController.send("
140 " document.getElementsByClassName('total-score-cell')[",
141 base::IntToString(index), "].innerHTML);"},
142 ""),
143 &score_string));
144
145 return score_string;
146 }
147
148 private:
149 // True if the page contains site engagement data.
150 bool page_is_populated_ = false;
151 };
152
153 IN_PROC_BROWSER_TEST_F(SiteEngagementUiBrowserTest, Basic) {
154 ResetBaseScoreToMax(kExampleUrl);
155
156 NavigateToWebUi();
157 WaitUntilPagePopulated();
158
159 EXPECT_EQ(1, NumberOfRows());
160 EXPECT_EQ(kExampleUrl, OriginUrlAtRow(0));
161 }
162
163 IN_PROC_BROWSER_TEST_F(SiteEngagementUiBrowserTest,
164 ScoresHaveTwoDecimalPlaces) {
165 ResetBaseScore(kExampleUrl, 3.14159);
166
167 NavigateToWebUi();
168 WaitUntilPagePopulated();
169
170 EXPECT_EQ(1, NumberOfRows());
171 EXPECT_EQ("3.14", BaseScoreAtRow(0));
172 EXPECT_EQ("0", BonusScoreAtRow(0));
173 EXPECT_EQ("3.14", TotalScoreAtRow(0));
174 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698