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

Unified 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: Address review comments Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/engagement/site_engagement_ui_browsertest.cc
diff --git a/chrome/browser/ui/webui/engagement/site_engagement_ui_browsertest.cc b/chrome/browser/ui/webui/engagement/site_engagement_ui_browsertest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..33c37f63c583c7f16464d53372a599875a3ffa7b
--- /dev/null
+++ b/chrome/browser/ui/webui/engagement/site_engagement_ui_browsertest.cc
@@ -0,0 +1,101 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/strings/string_util.h"
+#include "chrome/browser/engagement/site_engagement_service.h"
+#include "chrome/browser/ui/browser.h"
+#include "chrome/browser/ui/tabs/tab_strip_model.h"
+#include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
+#include "chrome/common/url_constants.h"
+#include "chrome/test/base/in_process_browser_test.h"
+#include "chrome/test/base/ui_test_utils.h"
+#include "content/public/browser/child_process_security_policy.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/common/content_switches.h"
+#include "content/public/test/browser_test_utils.h"
+
+namespace {
+
+const GURL kExampleUrl = GURL("https://www.example.com/");
+
+} // namespace
+
+class SiteEngagementUiBrowserTest : public InProcessBrowserTest {
+ protected:
+ // Returns the SiteEngagementService for the test browser profile.
+ SiteEngagementService* engagement_service() {
+ return SiteEngagementService::Get(browser()->profile());
+ }
+
+ // (Re)sets the base score for a URL's site engagement.
+ void ResetBaseScore(const GURL& url, double score) {
+ engagement_service()->ResetBaseScoreForURL(url, score);
+ }
+ void ResetBaseScoreToMax(const GURL& url) {
+ ResetBaseScore(url, engagement_service()->GetMaxPoints());
+ }
+
+ // Navigates the tab to the site engagement WebUI.
+ void NavigateToWebUi() {
+ content::WebContents* web_contents =
+ browser()->tab_strip_model()->GetActiveWebContents();
+
+ 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.
+ std::string(chrome::kChromeUISiteEngagementHost));
+ EXPECT_TRUE(ChromeWebUIControllerFactory::GetInstance()->UseWebUIForURL(
+ web_contents->GetBrowserContext(), web_ui_url));
+ ui_test_utils::NavigateToURL(browser(), web_ui_url);
+ EXPECT_TRUE(
+ content::ChildProcessSecurityPolicy::GetInstance()->HasWebUIBindings(
+ web_contents->GetRenderProcessHost()->GetID()));
+
+ EXPECT_TRUE(content::WaitForLoadStop(web_contents));
+ }
+
+ // Waits for the tab to be populated with site engagement data.
+ void WaitUntilPagePopulated() {
+ ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
+ browser()->tab_strip_model()->GetActiveWebContents(),
+ "window.setPagePopulatedCallbackForTest(function() {"
+ " 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.
+ "});",
+ &page_is_populated_));
+
+ ASSERT_TRUE(page_is_populated_);
+ }
+
+ // Verifies that a row exists for the specified site URL.
+ void ExpectPageContainsUrl(const GURL& url) {
+ ASSERT_TRUE(page_is_populated_);
+
+ bool found_url = false;
+ ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
+ browser()->tab_strip_model()->GetActiveWebContents(),
+ base::JoinString(
+ {"var origin_cells = "
+ " Array.from(document.getElementsByClassName('origin-cell'));"
+ "window.domAutomationController.send(origin_cells.reduce("
+ " function(found, element) {"
+ " return found || (element.innerHTML == '",
+ url.spec(),
+ "');"
+ " }, false));"},
+ ""),
+ &found_url));
+ EXPECT_TRUE(found_url);
+ }
+
+ private:
+ // True if the page contains site engagement data.
+ bool page_is_populated_ = false;
+};
+
+IN_PROC_BROWSER_TEST_F(SiteEngagementUiBrowserTest, Basic) {
+ ResetBaseScoreToMax(kExampleUrl);
+
+ NavigateToWebUi();
+ WaitUntilPagePopulated();
+
+ ExpectPageContainsUrl(kExampleUrl);
+}

Powered by Google App Engine
This is Rietveld 408576698