[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 1 | // 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 | |
msramek | 1f4746d | 2016-08-19 21:37:07 | [diff] [blame] | 7 | #include <list> |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 8 | #include <map> |
msramek | 1f4746d | 2016-08-19 21:37:07 | [diff] [blame] | 9 | #include <memory> |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 10 | |
msramek | 1f4746d | 2016-08-19 21:37:07 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 12 | #include "base/command_line.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 14 | #include "base/metrics/field_trial.h" |
msramek | 2232811 | 2017-03-23 16:50:29 | [diff] [blame] | 15 | #include "base/run_loop.h" |
msramek | 1f4746d | 2016-08-19 21:37:07 | [diff] [blame] | 16 | #include "base/strings/stringprintf.h" |
mpearson | 3c6d7af | 2015-05-13 23:59:53 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 18 | #include "build/build_config.h" |
msramek | 1f4746d | 2016-08-19 21:37:07 | [diff] [blame] | 19 | #include "chrome/browser/browsing_data/browsing_data_helper.h" |
msramek | 1c2b3ca | 2017-03-14 17:57:56 | [diff] [blame] | 20 | #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 21 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
msramek | 1f4746d | 2016-08-19 21:37:07 | [diff] [blame] | 22 | #include "chrome/test/base/testing_profile.h" |
timvolodine | a47291a | 2015-01-23 14:04:43 | [diff] [blame] | 23 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 24 | #include "components/search_engines/template_url_service.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 25 | #include "components/variations/entropy_provider.h" |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 26 | #include "components/variations/variations_associated_data.h" |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 27 | #include "components/version_info/version_info.h" |
msramek | a3c7cfd | 2017-02-03 13:34:13 | [diff] [blame] | 28 | #include "content/public/browser/browsing_data_filter_builder.h" |
msramek | e169ccb | 2017-04-26 05:21:41 | [diff] [blame] | 29 | #include "content/public/browser/browsing_data_remover.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 30 | #include "content/public/browser/navigation_controller.h" |
| 31 | #include "content/public/browser/navigation_entry.h" |
msramek | 2232811 | 2017-03-23 16:50:29 | [diff] [blame] | 32 | #include "content/public/browser/storage_partition.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 33 | #include "content/public/browser/web_contents.h" |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 34 | #include "content/public/common/content_switches.h" |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 35 | #include "content/public/test/test_browser_thread_bundle.h" |
Scott Violet | a35f9a4 | 2018-03-22 22:00:44 | [diff] [blame] | 36 | #include "media/media_buildflags.h" |
msramek | 1f4746d | 2016-08-19 21:37:07 | [diff] [blame] | 37 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 38 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 5f5ef80 | 2013-07-04 16:11:13 | [diff] [blame] | 39 | #include "url/gurl.h" |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 40 | |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 41 | #if !defined(OS_ANDROID) |
thestig | f8056446 | 2015-09-29 23:12:08 | [diff] [blame] | 42 | #include "chrome/browser/ui/browser.h" |
| 43 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
bengr | e8a146f | 2016-03-10 01:20:22 | [diff] [blame] | 44 | #include "chrome/common/pref_names.h" |
thestig | f8056446 | 2015-09-29 23:12:08 | [diff] [blame] | 45 | #include "chrome/test/base/browser_with_test_window_test.h" |
| 46 | #include "chrome/test/base/search_test_utils.h" |
| 47 | #endif |
| 48 | |
James Cook | ed46d84 | 2017-10-13 19:41:17 | [diff] [blame] | 49 | #if defined(OS_CHROMEOS) |
| 50 | #include "ash/public/interfaces/constants.mojom.h" |
| 51 | #include "content/public/common/service_names.mojom.h" |
| 52 | #include "services/ui/public/interfaces/constants.mojom.h" |
| 53 | #endif |
| 54 | |
msramek | a3c7cfd | 2017-02-03 13:34:13 | [diff] [blame] | 55 | using content::BrowsingDataFilterBuilder; |
msramek | 1f4746d | 2016-08-19 21:37:07 | [diff] [blame] | 56 | using testing::_; |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 57 | using ChromeContentBrowserClientTest = testing::Test; |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 58 | |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 59 | TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) { |
| 60 | ChromeContentBrowserClient client; |
| 61 | EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test"))); |
| 62 | EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com"))); |
| 63 | EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com"))); |
| 64 | } |
| 65 | |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 66 | // BrowserWithTestWindowTest doesn't work on Android. |
| 67 | #if !defined(OS_ANDROID) |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 68 | |
peter | bbcccc1 | 2015-02-11 22:23:33 | [diff] [blame] | 69 | using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest; |
| 70 | |
| 71 | static void DidOpenURLForWindowTest(content::WebContents** target_contents, |
| 72 | content::WebContents* opened_contents) { |
| 73 | DCHECK(target_contents); |
| 74 | |
| 75 | *target_contents = opened_contents; |
| 76 | } |
| 77 | |
bengr | e8a146f | 2016-03-10 01:20:22 | [diff] [blame] | 78 | TEST_F(ChromeContentBrowserClientWindowTest, IsDataSaverEnabled) { |
| 79 | ChromeContentBrowserClient client; |
| 80 | content::BrowserContext* context = browser()->profile(); |
| 81 | EXPECT_FALSE(client.IsDataSaverEnabled(context)); |
| 82 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kDataSaverEnabled, true); |
| 83 | EXPECT_TRUE(client.IsDataSaverEnabled(context)); |
| 84 | } |
| 85 | |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 86 | // This test opens two URLs using ContentBrowserClient::OpenURL. It expects the |
| 87 | // URLs to be opened in new tabs and activated, changing the active tabs after |
| 88 | // each call and increasing the tab count by 2. |
| 89 | TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) { |
| 90 | ChromeContentBrowserClient client; |
| 91 | |
| 92 | int previous_count = browser()->tab_strip_model()->count(); |
| 93 | |
| 94 | GURL urls[] = { GURL("https://www.google.com"), |
| 95 | GURL("https://www.chromium.org") }; |
| 96 | |
| 97 | for (const GURL& url : urls) { |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 98 | content::OpenURLParams params(url, content::Referrer(), |
| 99 | WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 100 | ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false); |
peter | bbcccc1 | 2015-02-11 22:23:33 | [diff] [blame] | 101 | // TODO(peter): We should have more in-depth browser tests for the window |
| 102 | // opening functionality, which also covers Android. This test can currently |
| 103 | // only be ran on platforms where OpenURL is implemented synchronously. |
| 104 | // See https://crbug.com/457667. |
| 105 | content::WebContents* web_contents = nullptr; |
| 106 | client.OpenURL(browser()->profile(), |
| 107 | params, |
| 108 | base::Bind(&DidOpenURLForWindowTest, &web_contents)); |
| 109 | |
| 110 | EXPECT_TRUE(web_contents); |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 111 | |
| 112 | content::WebContents* active_contents = browser()->tab_strip_model()-> |
| 113 | GetActiveWebContents(); |
peter | bbcccc1 | 2015-02-11 22:23:33 | [diff] [blame] | 114 | EXPECT_EQ(web_contents, active_contents); |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 115 | EXPECT_EQ(url, active_contents->GetVisibleURL()); |
| 116 | } |
| 117 | |
| 118 | EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count()); |
| 119 | } |
| 120 | |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 121 | #endif // !defined(OS_ANDROID) |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 122 | |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 123 | // NOTE: Any updates to the expectations in these tests should also be done in |
| 124 | // the browser test WebRtcDisableEncryptionFlagBrowserTest. |
| 125 | class DisableWebRtcEncryptionFlagTest : public testing::Test { |
| 126 | public: |
| 127 | DisableWebRtcEncryptionFlagTest() |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 128 | : from_command_line_(base::CommandLine::NO_PROGRAM), |
| 129 | to_command_line_(base::CommandLine::NO_PROGRAM) {} |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 130 | |
| 131 | protected: |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 132 | void SetUp() override { |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 133 | from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption); |
| 134 | } |
| 135 | |
sdefresne | 6e883e4 | 2015-07-30 08:05:54 | [diff] [blame] | 136 | void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel) { |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 137 | ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch( |
| 138 | &to_command_line_, |
| 139 | from_command_line_, |
| 140 | channel); |
| 141 | } |
| 142 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 143 | base::CommandLine from_command_line_; |
| 144 | base::CommandLine to_command_line_; |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 145 | |
thestig | f8056446 | 2015-09-29 23:12:08 | [diff] [blame] | 146 | private: |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 147 | DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest); |
| 148 | }; |
| 149 | |
| 150 | TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) { |
sdefresne | 6e883e4 | 2015-07-30 08:05:54 | [diff] [blame] | 151 | MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN); |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 152 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 153 | } |
| 154 | |
| 155 | TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) { |
sdefresne | 6e883e4 | 2015-07-30 08:05:54 | [diff] [blame] | 156 | MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY); |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 157 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 158 | } |
| 159 | |
| 160 | TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) { |
sdefresne | 6e883e4 | 2015-07-30 08:05:54 | [diff] [blame] | 161 | MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV); |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 162 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 163 | } |
| 164 | |
| 165 | TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) { |
sdefresne | 6e883e4 | 2015-07-30 08:05:54 | [diff] [blame] | 166 | MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA); |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 167 | #if defined(OS_ANDROID) |
| 168 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 169 | #else |
| 170 | EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) { |
sdefresne | 6e883e4 | 2015-07-30 08:05:54 | [diff] [blame] | 175 | MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE); |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 176 | EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 177 | } |
| 178 | |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 179 | class BlinkSettingsFieldTrialTest : public testing::Test { |
| 180 | public: |
Gayane Petrosyan | d6477ee | 2018-01-10 16:29:46 | [diff] [blame] | 181 | static const char kDisallowFetchFieldTrialName[]; |
| 182 | static const char kCSSExternalScannerFieldTrialName[]; |
pmeenan | 9ac66968 | 2015-08-17 14:57:03 | [diff] [blame] | 183 | static const char kFakeGroupName[]; |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 184 | |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 185 | BlinkSettingsFieldTrialTest() |
| 186 | : trial_list_(NULL), |
| 187 | command_line_(base::CommandLine::NO_PROGRAM) {} |
| 188 | |
| 189 | void SetUp() override { |
| 190 | command_line_.AppendSwitchASCII( |
| 191 | switches::kProcessType, switches::kRendererProcess); |
| 192 | } |
| 193 | |
| 194 | void TearDown() override { |
| 195 | variations::testing::ClearAllVariationParams(); |
| 196 | } |
| 197 | |
pmeenan | 9ac66968 | 2015-08-17 14:57:03 | [diff] [blame] | 198 | void CreateFieldTrial(const char* trial_name, const char* group_name) { |
| 199 | base::FieldTrialList::CreateFieldTrial(trial_name, group_name); |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 200 | } |
| 201 | |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 202 | void CreateFieldTrialWithParams( |
| 203 | const char* trial_name, |
pmeenan | 9ac66968 | 2015-08-17 14:57:03 | [diff] [blame] | 204 | const char* group_name, |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 205 | const char* key1, const char* value1, |
| 206 | const char* key2, const char* value2) { |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 207 | std::map<std::string, std::string> params; |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 208 | params.insert(std::make_pair(key1, value1)); |
| 209 | params.insert(std::make_pair(key2, value2)); |
pmeenan | 9ac66968 | 2015-08-17 14:57:03 | [diff] [blame] | 210 | CreateFieldTrial(trial_name, kFakeGroupName); |
| 211 | variations::AssociateVariationParams(trial_name, kFakeGroupName, params); |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void AppendContentBrowserClientSwitches() { |
| 215 | client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId); |
| 216 | } |
| 217 | |
| 218 | const base::CommandLine& command_line() const { |
| 219 | return command_line_; |
| 220 | } |
| 221 | |
| 222 | void AppendBlinkSettingsSwitch(const char* value) { |
| 223 | command_line_.AppendSwitchASCII(switches::kBlinkSettings, value); |
| 224 | } |
| 225 | |
| 226 | private: |
| 227 | static const int kFakeChildProcessId = 1; |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 228 | |
| 229 | ChromeContentBrowserClient client_; |
| 230 | base::FieldTrialList trial_list_; |
| 231 | base::CommandLine command_line_; |
| 232 | |
| 233 | content::TestBrowserThreadBundle thread_bundle_; |
| 234 | }; |
| 235 | |
Gayane Petrosyan | d6477ee | 2018-01-10 16:29:46 | [diff] [blame] | 236 | const char BlinkSettingsFieldTrialTest::kDisallowFetchFieldTrialName[] = |
| 237 | "DisallowFetchForDocWrittenScriptsInMainFrame"; |
| 238 | const char BlinkSettingsFieldTrialTest::kCSSExternalScannerFieldTrialName[] = |
| 239 | "CSSExternalScanner"; |
pmeenan | 9ac66968 | 2015-08-17 14:57:03 | [diff] [blame] | 240 | const char BlinkSettingsFieldTrialTest::kFakeGroupName[] = "FakeGroup"; |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 241 | |
| 242 | TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) { |
| 243 | AppendContentBrowserClientSwitches(); |
| 244 | EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings)); |
| 245 | } |
| 246 | |
| 247 | TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) { |
Gayane Petrosyan | d6477ee | 2018-01-10 16:29:46 | [diff] [blame] | 248 | CreateFieldTrial(kDisallowFetchFieldTrialName, kFakeGroupName); |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 249 | AppendContentBrowserClientSwitches(); |
| 250 | EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings)); |
| 251 | } |
| 252 | |
| 253 | TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) { |
| 254 | AppendBlinkSettingsSwitch("foo"); |
Gayane Petrosyan | d6477ee | 2018-01-10 16:29:46 | [diff] [blame] | 255 | CreateFieldTrialWithParams(kDisallowFetchFieldTrialName, kFakeGroupName, |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 256 | "key1", "value1", "key2", "value2"); |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 257 | AppendContentBrowserClientSwitches(); |
| 258 | EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings)); |
| 259 | EXPECT_EQ("foo", |
| 260 | command_line().GetSwitchValueASCII(switches::kBlinkSettings)); |
| 261 | } |
| 262 | |
| 263 | TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) { |
Gayane Petrosyan | d6477ee | 2018-01-10 16:29:46 | [diff] [blame] | 264 | CreateFieldTrialWithParams(kDisallowFetchFieldTrialName, kFakeGroupName, |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 265 | "key1", "value1", "key2", "value2"); |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 266 | AppendContentBrowserClientSwitches(); |
| 267 | EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings)); |
| 268 | EXPECT_EQ("key1=value1,key2=value2", |
| 269 | command_line().GetSwitchValueASCII(switches::kBlinkSettings)); |
| 270 | } |
| 271 | |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 272 | TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsEnabled) { |
Gayane Petrosyan | d6477ee | 2018-01-10 16:29:46 | [diff] [blame] | 273 | CreateFieldTrialWithParams(kDisallowFetchFieldTrialName, kFakeGroupName, |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 274 | "key1", "value1", "key2", "value2"); |
Gayane Petrosyan | d6477ee | 2018-01-10 16:29:46 | [diff] [blame] | 275 | CreateFieldTrialWithParams(kCSSExternalScannerFieldTrialName, kFakeGroupName, |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 276 | "keyA", "valueA", "keyB", "valueB"); |
| 277 | AppendContentBrowserClientSwitches(); |
| 278 | EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings)); |
| 279 | EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB", |
| 280 | command_line().GetSwitchValueASCII(switches::kBlinkSettings)); |
| 281 | } |
| 282 | |
| 283 | TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsDuplicateKeys) { |
Gayane Petrosyan | d6477ee | 2018-01-10 16:29:46 | [diff] [blame] | 284 | CreateFieldTrialWithParams(kDisallowFetchFieldTrialName, kFakeGroupName, |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 285 | "key1", "value1", "key2", "value2"); |
Gayane Petrosyan | d6477ee | 2018-01-10 16:29:46 | [diff] [blame] | 286 | CreateFieldTrialWithParams(kCSSExternalScannerFieldTrialName, kFakeGroupName, |
bmcquade | 9dd54cc | 2015-06-22 16:56:52 | [diff] [blame] | 287 | "key2", "duplicate", "key3", "value3"); |
| 288 | AppendContentBrowserClientSwitches(); |
| 289 | EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings)); |
| 290 | EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3", |
| 291 | command_line().GetSwitchValueASCII(switches::kBlinkSettings)); |
| 292 | } |
| 293 | |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 294 | #if !defined(OS_ANDROID) |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 295 | namespace content { |
| 296 | |
| 297 | class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest { |
| 298 | protected: |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 299 | void SetUp() override { |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 300 | BrowserWithTestWindowTest::SetUp(); |
| 301 | field_trial_list_.reset(new base::FieldTrialList( |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 302 | std::make_unique<variations::SHA1EntropyProvider>("42"))); |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) { |
| 306 | TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 307 | profile(), &TemplateURLServiceFactory::BuildInstanceFor); |
| 308 | TemplateURLService* template_url_service = |
| 309 | TemplateURLServiceFactory::GetForProfile(browser()->profile()); |
thestig | f8056446 | 2015-09-29 23:12:08 | [diff] [blame] | 310 | search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 311 | |
| 312 | TemplateURLData data; |
mpearson | 3c6d7af | 2015-05-13 23:59:53 | [diff] [blame] | 313 | data.SetShortName(base::ASCIIToUTF16("foo.com")); |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 314 | data.SetURL("http://foo.com/url?bar={searchTerms}"); |
| 315 | data.new_tab_url = new_tab_page_url.spec(); |
avi | 8a64b715b | 2016-09-02 17:30:04 | [diff] [blame] | 316 | TemplateURL* template_url = |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 317 | template_url_service->Add(std::make_unique<TemplateURL>(data)); |
[email protected] | f1cb558 | 2014-04-25 07:35:26 | [diff] [blame] | 318 | template_url_service->SetUserSelectedDefaultSearchProvider(template_url); |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 319 | } |
| 320 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 321 | std::unique_ptr<base::FieldTrialList> field_trial_list_; |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) { |
| 325 | const GURL url_original("chrome://newtab"); |
| 326 | const GURL url_rewritten("https://www.example.com/newtab"); |
| 327 | InstallTemplateURLWithNewTabPage(url_rewritten); |
| 328 | ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended", |
| 329 | "Group1 use_cacheable_ntp:1")); |
| 330 | |
| 331 | AddTab(browser(), GURL("chrome://blank")); |
| 332 | NavigateAndCommitActiveTab(url_original); |
| 333 | |
| 334 | NavigationEntry* entry = browser()->tab_strip_model()-> |
| 335 | GetActiveWebContents()->GetController().GetLastCommittedEntry(); |
| 336 | ASSERT_TRUE(entry != NULL); |
| 337 | EXPECT_EQ(url_rewritten, entry->GetURL()); |
| 338 | EXPECT_EQ(url_original, entry->GetVirtualURL()); |
| 339 | } |
| 340 | |
| 341 | } // namespace content |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 342 | #endif // !defined(OS_ANDROID) |
kerrnel | a67fad5 | 2017-06-14 20:18:16 | [diff] [blame] | 343 | |
| 344 | class ChromeContentBrowserClientGetLoggingFileTest : public testing::Test {}; |
| 345 | |
| 346 | TEST_F(ChromeContentBrowserClientGetLoggingFileTest, GetLoggingFile) { |
Michael Giuffrida | 6802ffe | 2017-09-29 03:40:25 | [diff] [blame] | 347 | base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM); |
kerrnel | a67fad5 | 2017-06-14 20:18:16 | [diff] [blame] | 348 | ChromeContentBrowserClient client; |
| 349 | base::FilePath log_file_name; |
Michael Giuffrida | 6802ffe | 2017-09-29 03:40:25 | [diff] [blame] | 350 | EXPECT_FALSE(client.GetLoggingFileName(cmd_line).empty()); |
| 351 | } |
| 352 | |
| 353 | TEST_F(ChromeContentBrowserClientGetLoggingFileTest, |
| 354 | GetLoggingFileFromCommandLine) { |
| 355 | base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM); |
| 356 | cmd_line.AppendSwitchASCII(switches::kLogFile, "test_log.txt"); |
| 357 | ChromeContentBrowserClient client; |
| 358 | base::FilePath log_file_name; |
| 359 | EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("test_log.txt")).value(), |
| 360 | client.GetLoggingFileName(cmd_line).value()); |
kerrnel | a67fad5 | 2017-06-14 20:18:16 | [diff] [blame] | 361 | } |
dbeam | 25472e0c | 2017-06-23 19:02:31 | [diff] [blame] | 362 | |
| 363 | class TestChromeContentBrowserClient : public ChromeContentBrowserClient { |
| 364 | public: |
| 365 | using ChromeContentBrowserClient::HandleWebUI; |
| 366 | using ChromeContentBrowserClient::HandleWebUIReverse; |
| 367 | }; |
| 368 | |
| 369 | TEST(ChromeContentBrowserClientTest, HandleWebUI) { |
| 370 | TestChromeContentBrowserClient test_content_browser_client; |
| 371 | const GURL http_help("http://help/"); |
| 372 | GURL should_not_redirect = http_help; |
| 373 | test_content_browser_client.HandleWebUI(&should_not_redirect, nullptr); |
| 374 | EXPECT_EQ(http_help, should_not_redirect); |
| 375 | |
| 376 | const GURL chrome_help("chrome://help/"); |
| 377 | GURL should_redirect = chrome_help; |
| 378 | test_content_browser_client.HandleWebUI(&should_redirect, nullptr); |
| 379 | EXPECT_NE(chrome_help, should_redirect); |
| 380 | } |
| 381 | |
| 382 | TEST(ChromeContentBrowserClientTest, HandleWebUIReverse) { |
| 383 | TestChromeContentBrowserClient test_content_browser_client; |
| 384 | GURL http_settings("http://settings/"); |
| 385 | EXPECT_FALSE( |
| 386 | test_content_browser_client.HandleWebUIReverse(&http_settings, nullptr)); |
| 387 | GURL chrome_settings("chrome://settings/"); |
| 388 | EXPECT_TRUE(test_content_browser_client.HandleWebUIReverse(&chrome_settings, |
| 389 | nullptr)); |
| 390 | } |
Matt Falkenhagen | 8479b20 | 2017-07-14 04:35:31 | [diff] [blame] | 391 | |
| 392 | TEST(ChromeContentBrowserClientTest, GetMetricSuffixForURL) { |
| 393 | ChromeContentBrowserClient client; |
| 394 | // Search is detected. |
| 395 | EXPECT_EQ("search", client.GetMetricSuffixForURL(GURL( |
| 396 | "https://www.google.co.jp/search?q=whatsgoingon"))); |
| 397 | // Not a Search host. |
| 398 | EXPECT_EQ("", client.GetMetricSuffixForURL(GURL( |
| 399 | "https://www.google.example.com/search?q=whatsgoingon"))); |
| 400 | // For now, non-https is considered a Search host. |
| 401 | EXPECT_EQ("search", client.GetMetricSuffixForURL( |
| 402 | GURL("http://www.google.com/search?q=whatsgoingon"))); |
| 403 | // Not a Search result page (no query). |
| 404 | EXPECT_EQ("", client.GetMetricSuffixForURL( |
| 405 | GURL("https://www.google.com/search?notaquery=nope"))); |
| 406 | } |
James Cook | ed46d84 | 2017-10-13 19:41:17 | [diff] [blame] | 407 | |
| 408 | #if defined(OS_CHROMEOS) |
| 409 | |
James Cook | ed46d84 | 2017-10-13 19:41:17 | [diff] [blame] | 410 | TEST(ChromeContentBrowserClientTest, ShouldTerminateOnServiceQuit) { |
| 411 | const struct { |
| 412 | std::string service_name; |
| 413 | bool expect_terminate; |
| 414 | } kTestCases[] = { |
| 415 | // Don't terminate for invalid service names. |
| 416 | {"", false}, |
| 417 | {"unknown-name", false}, |
| 418 | // Don't terminate for some well-known browser services. |
| 419 | {content::mojom::kBrowserServiceName, false}, |
| 420 | {content::mojom::kGpuServiceName, false}, |
| 421 | {content::mojom::kRendererServiceName, false}, |
| 422 | // Do terminate for some mash-specific cases. |
| 423 | {ui::mojom::kServiceName, true}, |
| 424 | {ash::mojom::kServiceName, true}, |
| 425 | }; |
| 426 | ChromeContentBrowserClient client; |
| 427 | for (const auto& test : kTestCases) { |
| 428 | service_manager::Identity id(test.service_name); |
| 429 | EXPECT_EQ(test.expect_terminate, client.ShouldTerminateOnServiceQuit(id)) |
| 430 | << "for service name " << test.service_name; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | #endif // defined(OS_CHROMEOS) |