blob: d4e68931d9528f8ceaf4cdb9387f14d09b9d1185 [file] [log] [blame]
[email protected]3d831992013-07-04 01:13:291// Copyright 2013 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/chrome_content_browser_client.h"
6
msramek1f4746d2016-08-19 21:37:077#include <list>
bmcquade5d2d9cf32015-06-19 17:42:288#include <map>
msramek1f4746d2016-08-19 21:37:079#include <memory>
bmcquade5d2d9cf32015-06-19 17:42:2810
msramek1f4746d2016-08-19 21:37:0711#include "base/bind.h"
[email protected]30335fdf02014-02-26 19:51:2712#include "base/command_line.h"
avie4d7b6f2015-12-26 00:59:1813#include "base/macros.h"
[email protected]10a9bf92013-11-13 23:34:4814#include "base/metrics/field_trial.h"
msramek22328112017-03-23 16:50:2915#include "base/run_loop.h"
msramek1f4746d2016-08-19 21:37:0716#include "base/strings/stringprintf.h"
mpearson3c6d7af2015-05-13 23:59:5317#include "base/strings/utf_string_conversions.h"
Yutaka Hirano968d6252018-12-04 05:09:3118#include "base/test/scoped_command_line.h"
John Delaneyac24e572019-04-30 19:47:0219#include "base/test/scoped_feature_list.h"
avie4d7b6f2015-12-26 00:59:1820#include "build/build_config.h"
msramek1f4746d2016-08-19 21:37:0721#include "chrome/browser/browsing_data/browsing_data_helper.h"
msramek1c2b3ca2017-03-14 17:57:5622#include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"
[email protected]10a9bf92013-11-13 23:34:4823#include "chrome/browser/search_engines/template_url_service_factory.h"
Matt Siembor8db9de02019-05-23 04:26:5824#include "chrome/common/webui_url_constants.h"
msramek1f4746d2016-08-19 21:37:0725#include "chrome/test/base/testing_profile.h"
timvolodinea47291a2015-01-23 14:04:4326#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]bf5c532d2014-07-05 00:29:5327#include "components/search_engines/template_url_service.h"
[email protected]10a9bf92013-11-13 23:34:4828#include "components/variations/entropy_provider.h"
bmcquade5d2d9cf32015-06-19 17:42:2829#include "components/variations/variations_associated_data.h"
sdefresne9fb67692015-08-03 18:48:2230#include "components/version_info/version_info.h"
msrameka3c7cfd2017-02-03 13:34:1331#include "content/public/browser/browsing_data_filter_builder.h"
msrameke169ccb2017-04-26 05:21:4132#include "content/public/browser/browsing_data_remover.h"
[email protected]10a9bf92013-11-13 23:34:4833#include "content/public/browser/navigation_controller.h"
34#include "content/public/browser/navigation_entry.h"
Matt Falkenhagen18c5c5d2018-11-26 02:55:3735#include "content/public/browser/site_instance.h"
msramek22328112017-03-23 16:50:2936#include "content/public/browser/storage_partition.h"
[email protected]10a9bf92013-11-13 23:34:4837#include "content/public/browser/web_contents.h"
[email protected]30335fdf02014-02-26 19:51:2738#include "content/public/common/content_switches.h"
John Delaneyac24e572019-04-30 19:47:0239#include "content/public/common/user_agent.h"
bmcquade5d2d9cf32015-06-19 17:42:2840#include "content/public/test/test_browser_thread_bundle.h"
Scott Violeta35f9a42018-03-22 22:00:4441#include "media/media_buildflags.h"
msramek1f4746d2016-08-19 21:37:0742#include "testing/gmock/include/gmock/gmock.h"
[email protected]3d831992013-07-04 01:13:2943#include "testing/gtest/include/gtest/gtest.h"
John Delaneyac24e572019-04-30 19:47:0244#include "third_party/blink/public/common/features.h"
Mike West6e4cbb32019-02-13 09:40:1745#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
[email protected]5f5ef802013-07-04 16:11:1346#include "url/gurl.h"
[email protected]3d831992013-07-04 01:13:2947
jam1c5a91492016-02-24 20:47:5348#if !defined(OS_ANDROID)
thestigf80564462015-09-29 23:12:0849#include "chrome/browser/ui/browser.h"
50#include "chrome/browser/ui/tabs/tab_strip_model.h"
bengre8a146f2016-03-10 01:20:2251#include "chrome/common/pref_names.h"
thestigf80564462015-09-29 23:12:0852#include "chrome/test/base/browser_with_test_window_test.h"
53#include "chrome/test/base/search_test_utils.h"
54#endif
55
msrameka3c7cfd2017-02-03 13:34:1356using content::BrowsingDataFilterBuilder;
msramek1f4746d2016-08-19 21:37:0757using testing::_;
mlamourieb40d552015-02-05 00:57:0858using ChromeContentBrowserClientTest = testing::Test;
[email protected]3d831992013-07-04 01:13:2959
Yutaka Hirano968d6252018-12-04 05:09:3160namespace {
61
62void CheckUserAgentStringOrdering(bool mobile_device) {
63 std::vector<std::string> pieces;
64
65 // Check if the pieces of the user agent string come in the correct order.
66 ChromeContentBrowserClient content_browser_client;
67 std::string buffer = content_browser_client.GetUserAgent();
68
69 pieces = base::SplitStringUsingSubstr(
70 buffer, "Mozilla/5.0 (", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
71 ASSERT_EQ(2u, pieces.size());
72 buffer = pieces[1];
73 EXPECT_EQ("", pieces[0]);
74
75 pieces = base::SplitStringUsingSubstr(
76 buffer, ") AppleWebKit/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
77 ASSERT_EQ(2u, pieces.size());
78 buffer = pieces[1];
79 std::string os_str = pieces[0];
80
81 pieces =
82 base::SplitStringUsingSubstr(buffer, " (KHTML, like Gecko) ",
83 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
84 ASSERT_EQ(2u, pieces.size());
85 buffer = pieces[1];
86 std::string webkit_version_str = pieces[0];
87
88 pieces = base::SplitStringUsingSubstr(
89 buffer, " Safari/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
90 ASSERT_EQ(2u, pieces.size());
91 std::string product_str = pieces[0];
92 std::string safari_version_str = pieces[1];
93
94 // Not sure what can be done to better check the OS string, since it's highly
95 // platform-dependent.
96 EXPECT_FALSE(os_str.empty());
97
98 // Check that the version numbers match.
99 EXPECT_FALSE(webkit_version_str.empty());
100 EXPECT_FALSE(safari_version_str.empty());
101 EXPECT_EQ(webkit_version_str, safari_version_str);
102
103 EXPECT_TRUE(
104 base::StartsWith(product_str, "Chrome/", base::CompareCase::SENSITIVE));
105 if (mobile_device) {
106 // "Mobile" gets tacked on to the end for mobile devices, like phones.
107 EXPECT_TRUE(
108 base::EndsWith(product_str, " Mobile", base::CompareCase::SENSITIVE));
109 }
110}
111
112} // namespace
113
[email protected]3d831992013-07-04 01:13:29114TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
115 ChromeContentBrowserClient client;
116 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
117 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
118 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
119}
120
jam1c5a91492016-02-24 20:47:53121// BrowserWithTestWindowTest doesn't work on Android.
122#if !defined(OS_ANDROID)
mlamourieb40d552015-02-05 00:57:08123
peterbbcccc12015-02-11 22:23:33124using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest;
125
126static void DidOpenURLForWindowTest(content::WebContents** target_contents,
127 content::WebContents* opened_contents) {
128 DCHECK(target_contents);
129
130 *target_contents = opened_contents;
131}
132
mlamourieb40d552015-02-05 00:57:08133// This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
134// URLs to be opened in new tabs and activated, changing the active tabs after
135// each call and increasing the tab count by 2.
136TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) {
137 ChromeContentBrowserClient client;
138
139 int previous_count = browser()->tab_strip_model()->count();
140
141 GURL urls[] = { GURL("https://www.google.com"),
142 GURL("https://www.chromium.org") };
143
144 for (const GURL& url : urls) {
nick3b04f322016-08-31 19:29:19145 content::OpenURLParams params(url, content::Referrer(),
146 WindowOpenDisposition::NEW_FOREGROUND_TAB,
147 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false);
peterbbcccc12015-02-11 22:23:33148 // TODO(peter): We should have more in-depth browser tests for the window
149 // opening functionality, which also covers Android. This test can currently
150 // only be ran on platforms where OpenURL is implemented synchronously.
151 // See https://crbug.com/457667.
152 content::WebContents* web_contents = nullptr;
Matt Falkenhagen18c5c5d2018-11-26 02:55:37153 scoped_refptr<content::SiteInstance> site_instance =
154 content::SiteInstance::Create(browser()->profile());
155 client.OpenURL(site_instance.get(), params,
peterbbcccc12015-02-11 22:23:33156 base::Bind(&DidOpenURLForWindowTest, &web_contents));
157
158 EXPECT_TRUE(web_contents);
mlamourieb40d552015-02-05 00:57:08159
160 content::WebContents* active_contents = browser()->tab_strip_model()->
161 GetActiveWebContents();
peterbbcccc12015-02-11 22:23:33162 EXPECT_EQ(web_contents, active_contents);
mlamourieb40d552015-02-05 00:57:08163 EXPECT_EQ(url, active_contents->GetVisibleURL());
164 }
165
166 EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count());
167}
168
jam1c5a91492016-02-24 20:47:53169#endif // !defined(OS_ANDROID)
mlamourieb40d552015-02-05 00:57:08170
[email protected]30335fdf02014-02-26 19:51:27171// NOTE: Any updates to the expectations in these tests should also be done in
172// the browser test WebRtcDisableEncryptionFlagBrowserTest.
173class DisableWebRtcEncryptionFlagTest : public testing::Test {
174 public:
175 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:17176 : from_command_line_(base::CommandLine::NO_PROGRAM),
177 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:27178
179 protected:
dchenge1bc7982014-10-30 00:32:40180 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:27181 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
182 }
183
sdefresne6e883e42015-07-30 08:05:54184 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel) {
[email protected]30335fdf02014-02-26 19:51:27185 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
186 &to_command_line_,
187 from_command_line_,
188 channel);
189 }
190
avi3ef9ec9e2014-12-22 22:50:17191 base::CommandLine from_command_line_;
192 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:27193
thestigf80564462015-09-29 23:12:08194 private:
[email protected]30335fdf02014-02-26 19:51:27195 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
196};
197
198TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
sdefresne6e883e42015-07-30 08:05:54199 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN);
[email protected]30335fdf02014-02-26 19:51:27200 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
201}
202
203TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
sdefresne6e883e42015-07-30 08:05:54204 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY);
[email protected]30335fdf02014-02-26 19:51:27205 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
206}
207
208TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
sdefresne6e883e42015-07-30 08:05:54209 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV);
[email protected]30335fdf02014-02-26 19:51:27210 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
211}
212
213TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
sdefresne6e883e42015-07-30 08:05:54214 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA);
[email protected]30335fdf02014-02-26 19:51:27215#if defined(OS_ANDROID)
216 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
217#else
218 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
219#endif
220}
221
222TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
sdefresne6e883e42015-07-30 08:05:54223 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE);
[email protected]30335fdf02014-02-26 19:51:27224 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
225}
226
bmcquade5d2d9cf32015-06-19 17:42:28227class BlinkSettingsFieldTrialTest : public testing::Test {
228 public:
Gayane Petrosyand6477ee2018-01-10 16:29:46229 static const char kDisallowFetchFieldTrialName[];
pmeenan9ac669682015-08-17 14:57:03230 static const char kFakeGroupName[];
bmcquade9dd54cc2015-06-22 16:56:52231
bmcquade5d2d9cf32015-06-19 17:42:28232 BlinkSettingsFieldTrialTest()
233 : trial_list_(NULL),
234 command_line_(base::CommandLine::NO_PROGRAM) {}
235
236 void SetUp() override {
237 command_line_.AppendSwitchASCII(
238 switches::kProcessType, switches::kRendererProcess);
239 }
240
241 void TearDown() override {
242 variations::testing::ClearAllVariationParams();
243 }
244
pmeenan9ac669682015-08-17 14:57:03245 void CreateFieldTrial(const char* trial_name, const char* group_name) {
246 base::FieldTrialList::CreateFieldTrial(trial_name, group_name);
bmcquade5d2d9cf32015-06-19 17:42:28247 }
248
bmcquade9dd54cc2015-06-22 16:56:52249 void CreateFieldTrialWithParams(
250 const char* trial_name,
pmeenan9ac669682015-08-17 14:57:03251 const char* group_name,
bmcquade9dd54cc2015-06-22 16:56:52252 const char* key1, const char* value1,
253 const char* key2, const char* value2) {
bmcquade5d2d9cf32015-06-19 17:42:28254 std::map<std::string, std::string> params;
bmcquade9dd54cc2015-06-22 16:56:52255 params.insert(std::make_pair(key1, value1));
256 params.insert(std::make_pair(key2, value2));
pmeenan9ac669682015-08-17 14:57:03257 CreateFieldTrial(trial_name, kFakeGroupName);
258 variations::AssociateVariationParams(trial_name, kFakeGroupName, params);
bmcquade5d2d9cf32015-06-19 17:42:28259 }
260
261 void AppendContentBrowserClientSwitches() {
262 client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId);
263 }
264
265 const base::CommandLine& command_line() const {
266 return command_line_;
267 }
268
269 void AppendBlinkSettingsSwitch(const char* value) {
270 command_line_.AppendSwitchASCII(switches::kBlinkSettings, value);
271 }
272
273 private:
274 static const int kFakeChildProcessId = 1;
bmcquade5d2d9cf32015-06-19 17:42:28275
276 ChromeContentBrowserClient client_;
277 base::FieldTrialList trial_list_;
278 base::CommandLine command_line_;
279
280 content::TestBrowserThreadBundle thread_bundle_;
281};
282
Gayane Petrosyand6477ee2018-01-10 16:29:46283const char BlinkSettingsFieldTrialTest::kDisallowFetchFieldTrialName[] =
284 "DisallowFetchForDocWrittenScriptsInMainFrame";
pmeenan9ac669682015-08-17 14:57:03285const char BlinkSettingsFieldTrialTest::kFakeGroupName[] = "FakeGroup";
bmcquade5d2d9cf32015-06-19 17:42:28286
287TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) {
288 AppendContentBrowserClientSwitches();
289 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
290}
291
292TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) {
Gayane Petrosyand6477ee2018-01-10 16:29:46293 CreateFieldTrial(kDisallowFetchFieldTrialName, kFakeGroupName);
bmcquade5d2d9cf32015-06-19 17:42:28294 AppendContentBrowserClientSwitches();
295 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
296}
297
298TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) {
299 AppendBlinkSettingsSwitch("foo");
Gayane Petrosyand6477ee2018-01-10 16:29:46300 CreateFieldTrialWithParams(kDisallowFetchFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52301 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28302 AppendContentBrowserClientSwitches();
303 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
304 EXPECT_EQ("foo",
305 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
306}
307
308TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) {
Gayane Petrosyand6477ee2018-01-10 16:29:46309 CreateFieldTrialWithParams(kDisallowFetchFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52310 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28311 AppendContentBrowserClientSwitches();
312 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
313 EXPECT_EQ("key1=value1,key2=value2",
314 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
315}
316
jam1c5a91492016-02-24 20:47:53317#if !defined(OS_ANDROID)
[email protected]10a9bf92013-11-13 23:34:48318namespace content {
319
320class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
321 protected:
dchenge1bc7982014-10-30 00:32:40322 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48323 BrowserWithTestWindowTest::SetUp();
324 field_trial_list_.reset(new base::FieldTrialList(
Jeremy Romanec48d7a2018-03-01 17:35:09325 std::make_unique<variations::SHA1EntropyProvider>("42")));
[email protected]10a9bf92013-11-13 23:34:48326 }
327
328 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
329 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
Sylvain Defresne9ac699e2018-10-17 09:52:10330 profile(),
331 base::BindRepeating(&TemplateURLServiceFactory::BuildInstanceFor));
[email protected]10a9bf92013-11-13 23:34:48332 TemplateURLService* template_url_service =
333 TemplateURLServiceFactory::GetForProfile(browser()->profile());
thestigf80564462015-09-29 23:12:08334 search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
[email protected]10a9bf92013-11-13 23:34:48335
336 TemplateURLData data;
mpearson3c6d7af2015-05-13 23:59:53337 data.SetShortName(base::ASCIIToUTF16("foo.com"));
[email protected]10a9bf92013-11-13 23:34:48338 data.SetURL("http://foo.com/url?bar={searchTerms}");
339 data.new_tab_url = new_tab_page_url.spec();
avi8a64b715b2016-09-02 17:30:04340 TemplateURL* template_url =
Jeremy Romanec48d7a2018-03-01 17:35:09341 template_url_service->Add(std::make_unique<TemplateURL>(data));
[email protected]f1cb5582014-04-25 07:35:26342 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48343 }
344
dcheng4af48582016-04-19 00:29:35345 std::unique_ptr<base::FieldTrialList> field_trial_list_;
[email protected]10a9bf92013-11-13 23:34:48346};
347
348TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
Matt Siembor8db9de02019-05-23 04:26:58349 const GURL url_original(chrome::kChromeUINewTabURL);
[email protected]10a9bf92013-11-13 23:34:48350 const GURL url_rewritten("https://www.example.com/newtab");
351 InstallTemplateURLWithNewTabPage(url_rewritten);
352 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
353 "Group1 use_cacheable_ntp:1"));
354
Matt Siembor8db9de02019-05-23 04:26:58355 AddTab(browser(), GURL(url::kAboutBlankURL));
[email protected]10a9bf92013-11-13 23:34:48356 NavigateAndCommitActiveTab(url_original);
357
358 NavigationEntry* entry = browser()->tab_strip_model()->
359 GetActiveWebContents()->GetController().GetLastCommittedEntry();
360 ASSERT_TRUE(entry != NULL);
361 EXPECT_EQ(url_rewritten, entry->GetURL());
362 EXPECT_EQ(url_original, entry->GetVirtualURL());
363}
364
365} // namespace content
jam1c5a91492016-02-24 20:47:53366#endif // !defined(OS_ANDROID)
kerrnela67fad52017-06-14 20:18:16367
368class ChromeContentBrowserClientGetLoggingFileTest : public testing::Test {};
369
370TEST_F(ChromeContentBrowserClientGetLoggingFileTest, GetLoggingFile) {
Michael Giuffrida6802ffe2017-09-29 03:40:25371 base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM);
kerrnela67fad52017-06-14 20:18:16372 ChromeContentBrowserClient client;
373 base::FilePath log_file_name;
Michael Giuffrida6802ffe2017-09-29 03:40:25374 EXPECT_FALSE(client.GetLoggingFileName(cmd_line).empty());
375}
376
377TEST_F(ChromeContentBrowserClientGetLoggingFileTest,
378 GetLoggingFileFromCommandLine) {
379 base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM);
380 cmd_line.AppendSwitchASCII(switches::kLogFile, "test_log.txt");
381 ChromeContentBrowserClient client;
382 base::FilePath log_file_name;
383 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("test_log.txt")).value(),
384 client.GetLoggingFileName(cmd_line).value());
kerrnela67fad52017-06-14 20:18:16385}
dbeam25472e0c2017-06-23 19:02:31386
387class TestChromeContentBrowserClient : public ChromeContentBrowserClient {
388 public:
389 using ChromeContentBrowserClient::HandleWebUI;
390 using ChromeContentBrowserClient::HandleWebUIReverse;
391};
392
393TEST(ChromeContentBrowserClientTest, HandleWebUI) {
394 TestChromeContentBrowserClient test_content_browser_client;
395 const GURL http_help("http://help/");
396 GURL should_not_redirect = http_help;
397 test_content_browser_client.HandleWebUI(&should_not_redirect, nullptr);
398 EXPECT_EQ(http_help, should_not_redirect);
399
Matt Siembor8db9de02019-05-23 04:26:58400 const GURL chrome_help(chrome::kChromeUIHelpURL);
dbeam25472e0c2017-06-23 19:02:31401 GURL should_redirect = chrome_help;
402 test_content_browser_client.HandleWebUI(&should_redirect, nullptr);
403 EXPECT_NE(chrome_help, should_redirect);
404}
405
406TEST(ChromeContentBrowserClientTest, HandleWebUIReverse) {
407 TestChromeContentBrowserClient test_content_browser_client;
408 GURL http_settings("http://settings/");
409 EXPECT_FALSE(
410 test_content_browser_client.HandleWebUIReverse(&http_settings, nullptr));
Matt Siembor8db9de02019-05-23 04:26:58411 GURL chrome_settings(chrome::kChromeUISettingsURL);
dbeam25472e0c2017-06-23 19:02:31412 EXPECT_TRUE(test_content_browser_client.HandleWebUIReverse(&chrome_settings,
413 nullptr));
414}
Matt Falkenhagen8479b202017-07-14 04:35:31415
416TEST(ChromeContentBrowserClientTest, GetMetricSuffixForURL) {
417 ChromeContentBrowserClient client;
418 // Search is detected.
419 EXPECT_EQ("search", client.GetMetricSuffixForURL(GURL(
420 "https://www.google.co.jp/search?q=whatsgoingon")));
421 // Not a Search host.
422 EXPECT_EQ("", client.GetMetricSuffixForURL(GURL(
423 "https://www.google.example.com/search?q=whatsgoingon")));
424 // For now, non-https is considered a Search host.
425 EXPECT_EQ("search", client.GetMetricSuffixForURL(
426 GURL("http://www.google.com/search?q=whatsgoingon")));
427 // Not a Search result page (no query).
428 EXPECT_EQ("", client.GetMetricSuffixForURL(
429 GURL("https://www.google.com/search?notaquery=nope")));
430}
James Cooked46d842017-10-13 19:41:17431
Dan Beamf0a7e112019-06-07 18:40:58432TEST(ChromeContentBrowserClientTest, UserAgentStringFrozen) {
John Delaneyac24e572019-04-30 19:47:02433 base::test::ScopedFeatureList scoped_feature_list;
434 scoped_feature_list.InitAndEnableFeature(blink::features::kFreezeUserAgent);
435
436#if defined(OS_ANDROID)
437 // Verify the correct user agent is returned when the UseMobileUserAgent
438 // command line flag is present.
439 const char* const kArguments[] = {"chrome"};
440 base::test::ScopedCommandLine scoped_command_line;
441 base::CommandLine* command_line = scoped_command_line.GetProcessCommandLine();
442 command_line->InitFromArgv(1, kArguments);
443
444 // Verify the mobile user agent string is not returned when not using a mobile
445 // user agent.
446 ASSERT_FALSE(command_line->HasSwitch(switches::kUseMobileUserAgent));
447 {
448 ChromeContentBrowserClient content_browser_client;
449 std::string buffer = content_browser_client.GetUserAgent();
450 EXPECT_EQ(buffer, content::frozen_user_agent_strings::kAndroid);
451 }
452
453 // Verify the mobile user agent string is returned when using a mobile user
454 // agent.
455 command_line->AppendSwitch(switches::kUseMobileUserAgent);
456 ASSERT_TRUE(command_line->HasSwitch(switches::kUseMobileUserAgent));
457 {
458 ChromeContentBrowserClient content_browser_client;
459 std::string buffer = content_browser_client.GetUserAgent();
460 EXPECT_EQ(buffer, content::frozen_user_agent_strings::kAndroidMobile);
461 }
462#else
463 {
464 ChromeContentBrowserClient content_browser_client;
465 std::string buffer = content_browser_client.GetUserAgent();
466 EXPECT_EQ(buffer, content::frozen_user_agent_strings::kDesktop);
467 }
468#endif
469}
470
Dan Beamf0a7e112019-06-07 18:40:58471TEST(ChromeContentBrowserClientTest, UserAgentStringOrdering) {
Yutaka Hirano968d6252018-12-04 05:09:31472#if defined(OS_ANDROID)
473 const char* const kArguments[] = {"chrome"};
474 base::test::ScopedCommandLine scoped_command_line;
475 base::CommandLine* command_line = scoped_command_line.GetProcessCommandLine();
476 command_line->InitFromArgv(1, kArguments);
477
478 // Do it for regular devices.
479 ASSERT_FALSE(command_line->HasSwitch(switches::kUseMobileUserAgent));
480 CheckUserAgentStringOrdering(false);
481
482 // Do it for mobile devices.
483 command_line->AppendSwitch(switches::kUseMobileUserAgent);
484 ASSERT_TRUE(command_line->HasSwitch(switches::kUseMobileUserAgent));
485 CheckUserAgentStringOrdering(true);
486#else
487 CheckUserAgentStringOrdering(false);
488#endif
489}
490
Dan Beamf0a7e112019-06-07 18:40:58491TEST(ChromeContentBrowserClientTest, UserAgentMetadata) {
Mike West6e4cbb32019-02-13 09:40:17492 ChromeContentBrowserClient content_browser_client;
493 auto metadata = content_browser_client.GetUserAgentMetadata();
494
495 EXPECT_EQ(metadata.brand, version_info::GetProductName());
Mike Weste555be862019-02-20 16:17:30496 EXPECT_EQ(metadata.full_version, version_info::GetVersionNumber());
497 EXPECT_EQ(metadata.major_version, version_info::GetMajorVersionNumber());
Mike West6e4cbb32019-02-13 09:40:17498 EXPECT_EQ(metadata.platform, version_info::GetOSType());
499 EXPECT_EQ(metadata.architecture, "");
500 EXPECT_EQ(metadata.model, "");
501}