[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 | |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 7 | #include <map> |
| 8 | |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 10 | #include "base/metrics/field_trial.h" |
mpearson | 3c6d7af | 2015-05-13 23:59:53 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 12 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 13 | #include "chrome/browser/ui/browser.h" |
| 14 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 | #include "chrome/test/base/browser_with_test_window_test.h" |
| 16 | #include "chrome/test/base/ui_test_utils.h" |
timvolodine | a47291a | 2015-01-23 14:04:43 | [diff] [blame] | 17 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 18 | #include "components/search_engines/template_url_service.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 19 | #include "components/variations/entropy_provider.h" |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 20 | #include "components/variations/variations_associated_data.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 21 | #include "content/public/browser/navigation_controller.h" |
| 22 | #include "content/public/browser/navigation_entry.h" |
| 23 | #include "content/public/browser/web_contents.h" |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 24 | #include "content/public/common/content_switches.h" |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 25 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 26 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 5f5ef80 | 2013-07-04 16:11:13 | [diff] [blame] | 27 | #include "url/gurl.h" |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 28 | |
| 29 | namespace chrome { |
| 30 | |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 31 | using ChromeContentBrowserClientTest = testing::Test; |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 32 | |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 33 | TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) { |
| 34 | ChromeContentBrowserClient client; |
| 35 | EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test"))); |
| 36 | EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com"))); |
| 37 | EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com"))); |
| 38 | } |
| 39 | |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 40 | // BrowserWithTestWindowTest doesn't work on iOS and Android. |
| 41 | #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 42 | |
peter | bbcccc1 | 2015-02-11 22:23:33 | [diff] [blame] | 43 | using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest; |
| 44 | |
| 45 | static void DidOpenURLForWindowTest(content::WebContents** target_contents, |
| 46 | content::WebContents* opened_contents) { |
| 47 | DCHECK(target_contents); |
| 48 | |
| 49 | *target_contents = opened_contents; |
| 50 | } |
| 51 | |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 52 | // This test opens two URLs using ContentBrowserClient::OpenURL. It expects the |
| 53 | // URLs to be opened in new tabs and activated, changing the active tabs after |
| 54 | // each call and increasing the tab count by 2. |
| 55 | TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) { |
| 56 | ChromeContentBrowserClient client; |
| 57 | |
| 58 | int previous_count = browser()->tab_strip_model()->count(); |
| 59 | |
| 60 | GURL urls[] = { GURL("https://www.google.com"), |
| 61 | GURL("https://www.chromium.org") }; |
| 62 | |
| 63 | for (const GURL& url : urls) { |
| 64 | content::OpenURLParams params(url, |
| 65 | content::Referrer(), |
| 66 | NEW_FOREGROUND_TAB, |
| 67 | ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 68 | false); |
peter | bbcccc1 | 2015-02-11 22:23:33 | [diff] [blame] | 69 | // TODO(peter): We should have more in-depth browser tests for the window |
| 70 | // opening functionality, which also covers Android. This test can currently |
| 71 | // only be ran on platforms where OpenURL is implemented synchronously. |
| 72 | // See https://crbug.com/457667. |
| 73 | content::WebContents* web_contents = nullptr; |
| 74 | client.OpenURL(browser()->profile(), |
| 75 | params, |
| 76 | base::Bind(&DidOpenURLForWindowTest, &web_contents)); |
| 77 | |
| 78 | EXPECT_TRUE(web_contents); |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 79 | |
| 80 | content::WebContents* active_contents = browser()->tab_strip_model()-> |
| 81 | GetActiveWebContents(); |
peter | bbcccc1 | 2015-02-11 22:23:33 | [diff] [blame] | 82 | EXPECT_EQ(web_contents, active_contents); |
mlamouri | eb40d55 | 2015-02-05 00:57:08 | [diff] [blame] | 83 | EXPECT_EQ(url, active_contents->GetVisibleURL()); |
| 84 | } |
| 85 | |
| 86 | EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count()); |
| 87 | } |
| 88 | |
| 89 | #endif // !defined(OS_ANDROID) && !defined(OS_IOS) |
| 90 | |
brettw | a68ea2b | 2015-02-01 02:54:07 | [diff] [blame] | 91 | #if defined(ENABLE_WEBRTC) |
| 92 | |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 93 | // NOTE: Any updates to the expectations in these tests should also be done in |
| 94 | // the browser test WebRtcDisableEncryptionFlagBrowserTest. |
| 95 | class DisableWebRtcEncryptionFlagTest : public testing::Test { |
| 96 | public: |
| 97 | DisableWebRtcEncryptionFlagTest() |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 98 | : from_command_line_(base::CommandLine::NO_PROGRAM), |
| 99 | to_command_line_(base::CommandLine::NO_PROGRAM) {} |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 100 | |
| 101 | protected: |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 102 | void SetUp() override { |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 103 | from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption); |
| 104 | } |
| 105 | |
| 106 | void MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::Channel channel) { |
| 107 | ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch( |
| 108 | &to_command_line_, |
| 109 | from_command_line_, |
| 110 | channel); |
| 111 | } |
| 112 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 113 | base::CommandLine from_command_line_; |
| 114 | base::CommandLine to_command_line_; |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 115 | |
| 116 | DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest); |
| 117 | }; |
| 118 | |
| 119 | TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) { |
| 120 | MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_UNKNOWN); |
| 121 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 122 | } |
| 123 | |
| 124 | TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) { |
| 125 | MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_CANARY); |
| 126 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 127 | } |
| 128 | |
| 129 | TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) { |
| 130 | MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_DEV); |
| 131 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 132 | } |
| 133 | |
| 134 | TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) { |
| 135 | MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_BETA); |
| 136 | #if defined(OS_ANDROID) |
| 137 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 138 | #else |
| 139 | EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) { |
| 144 | MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_STABLE); |
| 145 | EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 146 | } |
| 147 | |
brettw | a68ea2b | 2015-02-01 02:54:07 | [diff] [blame] | 148 | #endif // ENABLE_WEBRTC |
| 149 | |
bmcquade | 5d2d9cf3 | 2015-06-19 17:42:28 | [diff] [blame] | 150 | class BlinkSettingsFieldTrialTest : public testing::Test { |
| 151 | public: |
| 152 | BlinkSettingsFieldTrialTest() |
| 153 | : trial_list_(NULL), |
| 154 | command_line_(base::CommandLine::NO_PROGRAM) {} |
| 155 | |
| 156 | void SetUp() override { |
| 157 | command_line_.AppendSwitchASCII( |
| 158 | switches::kProcessType, switches::kRendererProcess); |
| 159 | } |
| 160 | |
| 161 | void TearDown() override { |
| 162 | variations::testing::ClearAllVariationParams(); |
| 163 | } |
| 164 | |
| 165 | void CreateFieldTrial() { |
| 166 | base::FieldTrialList::CreateFieldTrial(kFieldTrialName, kGroupName); |
| 167 | } |
| 168 | |
| 169 | void CreateFieldTrialWithParams() { |
| 170 | CreateFieldTrial(); |
| 171 | std::map<std::string, std::string> params; |
| 172 | params.insert(std::make_pair("key1", "value1")); |
| 173 | params.insert(std::make_pair("key2", "value2")); |
| 174 | variations::AssociateVariationParams(kFieldTrialName, kGroupName, params); |
| 175 | } |
| 176 | |
| 177 | void AppendContentBrowserClientSwitches() { |
| 178 | client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId); |
| 179 | } |
| 180 | |
| 181 | const base::CommandLine& command_line() const { |
| 182 | return command_line_; |
| 183 | } |
| 184 | |
| 185 | void AppendBlinkSettingsSwitch(const char* value) { |
| 186 | command_line_.AppendSwitchASCII(switches::kBlinkSettings, value); |
| 187 | } |
| 188 | |
| 189 | private: |
| 190 | static const int kFakeChildProcessId = 1; |
| 191 | static const char kFieldTrialName[]; |
| 192 | static const char kGroupName[]; |
| 193 | |
| 194 | ChromeContentBrowserClient client_; |
| 195 | base::FieldTrialList trial_list_; |
| 196 | base::CommandLine command_line_; |
| 197 | |
| 198 | content::TestBrowserThreadBundle thread_bundle_; |
| 199 | }; |
| 200 | |
| 201 | const char BlinkSettingsFieldTrialTest::kFieldTrialName[] = |
| 202 | "BackgroundHtmlParserTokenLimits"; |
| 203 | const char BlinkSettingsFieldTrialTest::kGroupName[] = "FakeGroup"; |
| 204 | |
| 205 | TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) { |
| 206 | AppendContentBrowserClientSwitches(); |
| 207 | EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings)); |
| 208 | } |
| 209 | |
| 210 | TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) { |
| 211 | CreateFieldTrial(); |
| 212 | AppendContentBrowserClientSwitches(); |
| 213 | EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings)); |
| 214 | } |
| 215 | |
| 216 | TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) { |
| 217 | AppendBlinkSettingsSwitch("foo"); |
| 218 | CreateFieldTrialWithParams(); |
| 219 | AppendContentBrowserClientSwitches(); |
| 220 | EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings)); |
| 221 | EXPECT_EQ("foo", |
| 222 | command_line().GetSwitchValueASCII(switches::kBlinkSettings)); |
| 223 | } |
| 224 | |
| 225 | TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) { |
| 226 | CreateFieldTrialWithParams(); |
| 227 | AppendContentBrowserClientSwitches(); |
| 228 | EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings)); |
| 229 | EXPECT_EQ("key1=value1,key2=value2", |
| 230 | command_line().GetSwitchValueASCII(switches::kBlinkSettings)); |
| 231 | } |
| 232 | |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 233 | } // namespace chrome |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 234 | |
| 235 | #if !defined(OS_IOS) && !defined(OS_ANDROID) |
| 236 | namespace content { |
| 237 | |
| 238 | class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest { |
| 239 | protected: |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 240 | void SetUp() override { |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 241 | BrowserWithTestWindowTest::SetUp(); |
| 242 | field_trial_list_.reset(new base::FieldTrialList( |
| 243 | new metrics::SHA1EntropyProvider("42"))); |
| 244 | } |
| 245 | |
| 246 | void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) { |
| 247 | TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 248 | profile(), &TemplateURLServiceFactory::BuildInstanceFor); |
| 249 | TemplateURLService* template_url_service = |
| 250 | TemplateURLServiceFactory::GetForProfile(browser()->profile()); |
| 251 | ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); |
| 252 | |
| 253 | TemplateURLData data; |
mpearson | 3c6d7af | 2015-05-13 23:59:53 | [diff] [blame] | 254 | data.SetShortName(base::ASCIIToUTF16("foo.com")); |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 255 | data.SetURL("http://foo.com/url?bar={searchTerms}"); |
| 256 | data.new_tab_url = new_tab_page_url.spec(); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 257 | TemplateURL* template_url = new TemplateURL(data); |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 258 | // Takes ownership. |
| 259 | template_url_service->Add(template_url); |
[email protected] | f1cb558 | 2014-04-25 07:35:26 | [diff] [blame] | 260 | template_url_service->SetUserSelectedDefaultSearchProvider(template_url); |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 264 | }; |
| 265 | |
| 266 | TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) { |
| 267 | const GURL url_original("chrome://newtab"); |
| 268 | const GURL url_rewritten("https://www.example.com/newtab"); |
| 269 | InstallTemplateURLWithNewTabPage(url_rewritten); |
| 270 | ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended", |
| 271 | "Group1 use_cacheable_ntp:1")); |
| 272 | |
| 273 | AddTab(browser(), GURL("chrome://blank")); |
| 274 | NavigateAndCommitActiveTab(url_original); |
| 275 | |
| 276 | NavigationEntry* entry = browser()->tab_strip_model()-> |
| 277 | GetActiveWebContents()->GetController().GetLastCommittedEntry(); |
| 278 | ASSERT_TRUE(entry != NULL); |
| 279 | EXPECT_EQ(url_rewritten, entry->GetURL()); |
| 280 | EXPECT_EQ(url_original, entry->GetVirtualURL()); |
| 281 | } |
| 282 | |
| 283 | } // namespace content |
| 284 | #endif // !defined(OS_IOS) && !defined(OS_ANDROID) |