Chromium Code Reviews| 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 "chrome/browser/engagement/site_engagement_service.h" | |
| 6 #include "chrome/browser/ui/browser.h" | |
| 7 #include "chrome/browser/ui/browser_commands.h" | |
|
dominickn
2017/03/30 06:05:24
Not sure you need this one?
Wez
2017/03/31 05:21:04
You're right, I think; I'd left this because of th
| |
| 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/"); | |
|
dominickn
2017/03/30 06:05:24
I'm surprised this doesn't fail some sort of stati
Wez
2017/03/31 05:21:02
Static initializers are only checked on the perf b
| |
| 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(GURL url, double score) { | |
|
dominickn
2017/03/30 06:05:24
Nit: const GURL&
Wez
2017/03/31 05:21:03
Done.
| |
| 33 engagement_service()->ResetBaseScoreForURL(url, score); | |
| 34 } | |
| 35 void ResetBaseScoreToMax(GURL url) { | |
|
dominickn
2017/03/30 06:05:24
Nit: const GURL&
Wez
2017/03/31 05:21:03
Done.
| |
| 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) + "://" + | |
| 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);" | |
| 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(GURL url) { | |
|
dominickn
2017/03/30 06:05:24
Nit: const GURL&
Wez
2017/03/31 05:21:02
Done.
Wez
2017/03/31 05:21:03
Done.
| |
| 70 ASSERT_TRUE(page_is_populated_); | |
| 71 | |
| 72 bool found_url = false; | |
| 73 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
| 74 browser()->tab_strip_model()->GetActiveWebContents(), | |
| 75 "var origin_cells = " | |
| 76 " Array.from(document.getElementsByClassName('origin-cell'));" | |
| 77 "window.domAutomationController.send(origin_cells.reduce(" | |
| 78 " function(found, element) {" | |
| 79 " return found || (element.innerHTML == '" + | |
| 80 url.spec() + | |
| 81 "');" | |
| 82 " }, false));", | |
| 83 &found_url)); | |
| 84 EXPECT_TRUE(found_url); | |
| 85 } | |
| 86 | |
| 87 private: | |
| 88 // True if the page contains site engagement data. | |
| 89 bool page_is_populated_ = false; | |
| 90 }; | |
| 91 | |
| 92 IN_PROC_BROWSER_TEST_F(SiteEngagementUiBrowserTest, Basic) { | |
| 93 ResetBaseScoreToMax(kExampleUrl); | |
| 94 | |
| 95 NavigateToWebUi(); | |
| 96 WaitUntilPagePopulated(); | |
| 97 | |
| 98 ExpectPageContainsUrl(kExampleUrl); | |
| 99 } | |
| OLD | NEW |