1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/log_web_ui_url.h"
#include <stdint.h>
#include <vector>
#include "base/hash/hash.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/webui/top_chrome/webui_contents_preload_manager_test_api.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
using base::Bucket;
using testing::ElementsAre;
namespace webui {
class LogWebUIUrlTest : public InProcessBrowserTest {
public:
LogWebUIUrlTest() = default;
LogWebUIUrlTest(const LogWebUIUrlTest&) = delete;
LogWebUIUrlTest& operator=(const LogWebUIUrlTest&) = delete;
~LogWebUIUrlTest() override = default;
base::HistogramTester& histogram_tester() { return histogram_tester_; }
void RunTest(std::u16string title, const GURL& url) {
EXPECT_THAT(histogram_tester_.GetAllSamples(webui::kWebUICreatedForUrl),
::testing::IsEmpty());
auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
content::TitleWatcher title_watcher(tab, title);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_EQ(title, title_watcher.WaitAndGetTitle());
uint32_t origin_hash = base::Hash(url.DeprecatedGetOriginAsURL().spec());
EXPECT_THAT(histogram_tester_.GetAllSamples(webui::kWebUICreatedForUrl),
ElementsAre(Bucket(origin_hash, 1)));
}
private:
base::HistogramTester histogram_tester_;
};
IN_PROC_BROWSER_TEST_F(LogWebUIUrlTest, TestExtensionsPage) {
RunTest(
l10n_util::GetStringUTF16(IDS_MANAGE_EXTENSIONS_SETTING_WINDOWS_TITLE),
GURL(chrome::kChromeUIExtensionsURL));
}
IN_PROC_BROWSER_TEST_F(LogWebUIUrlTest, TestHistoryPage) {
RunTest(l10n_util::GetStringUTF16(IDS_HISTORY_TITLE),
GURL(chrome::kChromeUIHistoryURL));
}
IN_PROC_BROWSER_TEST_F(LogWebUIUrlTest, TestSettingsPage) {
RunTest(l10n_util::GetStringUTF16(IDS_SETTINGS_SETTINGS),
GURL(chrome::kChromeUISettingsURL));
}
IN_PROC_BROWSER_TEST_F(LogWebUIUrlTest, TestDinoPage) {
GURL url = content::GetWebUIURL(content::kChromeUIDinoHost);
// When a page does not have a dedicated title the URL with a trailing slash
// is displayed as the title.
RunTest(base::UTF8ToUTF16(url.GetWithEmptyPath().spec()), url);
}
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(LogWebUIUrlTest, TestChromeUntrustedPage) {
RunTest(u"", GURL(base::StrCat(
{chrome::kChromeUIUntrustedPrintURL, "1/1/print.pdf"})));
}
#endif
// Tests that WebUI.ShownURL is logged after showing a WebUI.
IN_PROC_BROWSER_TEST_F(LogWebUIUrlTest, ShownWebUI) {
const GURL url(chrome::kChromeUIHistoryURL);
EXPECT_THAT(histogram_tester().GetAllSamples(webui::kWebUICreatedForUrl),
::testing::IsEmpty());
EXPECT_THAT(histogram_tester().GetAllSamples(webui::kWebUIShownUrl),
::testing::IsEmpty());
std::unique_ptr<content::WebContents> web_contents =
content::WebContents::Create(
content::WebContents::CreateParams(browser()->profile()));
ASSERT_TRUE(content::NavigateToURL(web_contents.get(), url));
ASSERT_TRUE(
content::WaitForRenderFrameReady(web_contents->GetPrimaryMainFrame()));
uint32_t origin_hash = base::Hash(url.DeprecatedGetOriginAsURL().spec());
EXPECT_THAT(histogram_tester().GetAllSamples(webui::kWebUICreatedForUrl),
ElementsAre(Bucket(origin_hash, 1)));
// The content is not visible before attaching it to the browser.
EXPECT_THAT(
histogram_tester().GetBucketCount(webui::kWebUIShownUrl, origin_hash), 1);
}
// Tests that WebUI.ShownURL is logged after showing a preloaded WebUI.
IN_PROC_BROWSER_TEST_F(LogWebUIUrlTest, ShownWebUIForPreloadedPage) {
// Only top-chrome WebUIs are preloaded. Use Tab Search as an example.
const GURL url(chrome::kChromeUITabSearchURL);
uint32_t origin_hash = base::Hash(url.DeprecatedGetOriginAsURL().spec());
EXPECT_THAT(histogram_tester().GetAllSamples(webui::kWebUICreatedForUrl),
::testing::IsEmpty());
EXPECT_THAT(histogram_tester().GetAllSamples(webui::kWebUIShownUrl),
::testing::IsEmpty());
// Preload the WebUI. The WebUI is created but not shown.
WebUIContentsPreloadManagerTestAPI preload_test_api;
preload_test_api.PreloadUrl(browser()->profile(), url);
EXPECT_TRUE(
content::WaitForLoadStop(preload_test_api.GetPreloadedWebContents()));
EXPECT_THAT(histogram_tester().GetBucketCount(webui::kWebUICreatedForUrl,
origin_hash),
1);
EXPECT_THAT(
histogram_tester().GetBucketCount(webui::kWebUIShownUrl, origin_hash), 0);
// Show the WebUI.
std::unique_ptr<content::WebContents> web_contents =
std::move(preload_test_api.preload_manager()
->Request(url, browser()->profile())
.web_contents);
EXPECT_THAT(
histogram_tester().GetBucketCount(webui::kWebUIShownUrl, origin_hash), 1);
}
} // namespace webui
|