[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 | |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 8 | #include "base/metrics/field_trial.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 9 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 10 | #include "chrome/browser/ui/browser.h" |
| 11 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 | #include "chrome/test/base/browser_with_test_window_test.h" |
| 13 | #include "chrome/test/base/ui_test_utils.h" |
timvolodine | a47291a | 2015-01-23 14:04:43 | [diff] [blame] | 14 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 15 | #include "components/search_engines/template_url_service.h" |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 16 | #include "components/variations/entropy_provider.h" |
| 17 | #include "content/public/browser/navigation_controller.h" |
| 18 | #include "content/public/browser/navigation_entry.h" |
| 19 | #include "content/public/browser/web_contents.h" |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 20 | #include "content/public/common/content_switches.h" |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 5f5ef80 | 2013-07-04 16:11:13 | [diff] [blame] | 22 | #include "url/gurl.h" |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 23 | |
| 24 | namespace chrome { |
| 25 | |
| 26 | typedef testing::Test ChromeContentBrowserClientTest; |
| 27 | |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 28 | TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) { |
| 29 | ChromeContentBrowserClient client; |
| 30 | EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test"))); |
| 31 | EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com"))); |
| 32 | EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com"))); |
| 33 | } |
| 34 | |
brettw | a68ea2b | 2015-02-01 02:54:07 | [diff] [blame^] | 35 | #if defined(ENABLE_WEBRTC) |
| 36 | |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 37 | // NOTE: Any updates to the expectations in these tests should also be done in |
| 38 | // the browser test WebRtcDisableEncryptionFlagBrowserTest. |
| 39 | class DisableWebRtcEncryptionFlagTest : public testing::Test { |
| 40 | public: |
| 41 | DisableWebRtcEncryptionFlagTest() |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 42 | : from_command_line_(base::CommandLine::NO_PROGRAM), |
| 43 | to_command_line_(base::CommandLine::NO_PROGRAM) {} |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 44 | |
| 45 | protected: |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 46 | void SetUp() override { |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 47 | from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption); |
| 48 | } |
| 49 | |
| 50 | void MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::Channel channel) { |
| 51 | ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch( |
| 52 | &to_command_line_, |
| 53 | from_command_line_, |
| 54 | channel); |
| 55 | } |
| 56 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 57 | base::CommandLine from_command_line_; |
| 58 | base::CommandLine to_command_line_; |
[email protected] | 30335fdf0 | 2014-02-26 19:51:27 | [diff] [blame] | 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest); |
| 61 | }; |
| 62 | |
| 63 | TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) { |
| 64 | MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_UNKNOWN); |
| 65 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 66 | } |
| 67 | |
| 68 | TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) { |
| 69 | MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_CANARY); |
| 70 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 71 | } |
| 72 | |
| 73 | TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) { |
| 74 | MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_DEV); |
| 75 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 76 | } |
| 77 | |
| 78 | TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) { |
| 79 | MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_BETA); |
| 80 | #if defined(OS_ANDROID) |
| 81 | EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 82 | #else |
| 83 | EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 84 | #endif |
| 85 | } |
| 86 | |
| 87 | TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) { |
| 88 | MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_STABLE); |
| 89 | EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption)); |
| 90 | } |
| 91 | |
brettw | a68ea2b | 2015-02-01 02:54:07 | [diff] [blame^] | 92 | #endif // ENABLE_WEBRTC |
| 93 | |
[email protected] | 3d83199 | 2013-07-04 01:13:29 | [diff] [blame] | 94 | } // namespace chrome |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 95 | |
| 96 | #if !defined(OS_IOS) && !defined(OS_ANDROID) |
| 97 | namespace content { |
| 98 | |
| 99 | class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest { |
| 100 | protected: |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 101 | void SetUp() override { |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 102 | BrowserWithTestWindowTest::SetUp(); |
| 103 | field_trial_list_.reset(new base::FieldTrialList( |
| 104 | new metrics::SHA1EntropyProvider("42"))); |
| 105 | } |
| 106 | |
| 107 | void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) { |
| 108 | TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 109 | profile(), &TemplateURLServiceFactory::BuildInstanceFor); |
| 110 | TemplateURLService* template_url_service = |
| 111 | TemplateURLServiceFactory::GetForProfile(browser()->profile()); |
| 112 | ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); |
| 113 | |
| 114 | TemplateURLData data; |
| 115 | data.SetURL("http://foo.com/url?bar={searchTerms}"); |
| 116 | data.new_tab_url = new_tab_page_url.spec(); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 117 | TemplateURL* template_url = new TemplateURL(data); |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 118 | // Takes ownership. |
| 119 | template_url_service->Add(template_url); |
[email protected] | f1cb558 | 2014-04-25 07:35:26 | [diff] [blame] | 120 | template_url_service->SetUserSelectedDefaultSearchProvider(template_url); |
[email protected] | 10a9bf9 | 2013-11-13 23:34:48 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 124 | }; |
| 125 | |
| 126 | TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) { |
| 127 | const GURL url_original("chrome://newtab"); |
| 128 | const GURL url_rewritten("https://www.example.com/newtab"); |
| 129 | InstallTemplateURLWithNewTabPage(url_rewritten); |
| 130 | ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended", |
| 131 | "Group1 use_cacheable_ntp:1")); |
| 132 | |
| 133 | AddTab(browser(), GURL("chrome://blank")); |
| 134 | NavigateAndCommitActiveTab(url_original); |
| 135 | |
| 136 | NavigationEntry* entry = browser()->tab_strip_model()-> |
| 137 | GetActiveWebContents()->GetController().GetLastCommittedEntry(); |
| 138 | ASSERT_TRUE(entry != NULL); |
| 139 | EXPECT_EQ(url_rewritten, entry->GetURL()); |
| 140 | EXPECT_EQ(url_original, entry->GetVirtualURL()); |
| 141 | } |
| 142 | |
| 143 | } // namespace content |
| 144 | #endif // !defined(OS_IOS) && !defined(OS_ANDROID) |
timvolodine | a47291a | 2015-01-23 14:04:43 | [diff] [blame] | 145 | |
| 146 | namespace chrome { |
| 147 | |
| 148 | // For testing permissions related functionality. |
| 149 | class PermissionBrowserClientTest : public testing::Test { |
| 150 | public: |
| 151 | PermissionBrowserClientTest() : url_("https://www.google.com") {} |
| 152 | |
| 153 | void CheckPermissionStatus(content::PermissionType type, |
| 154 | content::PermissionStatus expected) { |
| 155 | EXPECT_EQ(expected, client_.GetPermissionStatus(type, &profile_, |
| 156 | url_.GetOrigin(), |
| 157 | url_.GetOrigin())); |
| 158 | } |
| 159 | |
| 160 | void SetPermission(ContentSettingsType type, ContentSetting value) { |
| 161 | profile_.GetHostContentSettingsMap()->SetContentSetting( |
| 162 | ContentSettingsPattern::FromURLNoWildcard(url_), |
| 163 | ContentSettingsPattern::FromURLNoWildcard(url_), |
| 164 | type, std::string(), value); |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | content::TestBrowserThreadBundle thread_bundle_; |
| 169 | ChromeContentBrowserClient client_; |
| 170 | TestingProfile profile_; |
| 171 | GURL url_; |
| 172 | }; |
| 173 | |
| 174 | TEST_F(PermissionBrowserClientTest, GetPermissionStatusDefault) { |
| 175 | using namespace content; |
| 176 | CheckPermissionStatus(PERMISSION_MIDI_SYSEX, PERMISSION_STATUS_ASK); |
| 177 | CheckPermissionStatus(PERMISSION_PUSH_MESSAGING, PERMISSION_STATUS_ASK); |
| 178 | CheckPermissionStatus(PERMISSION_NOTIFICATIONS, PERMISSION_STATUS_ASK); |
| 179 | CheckPermissionStatus(PERMISSION_GEOLOCATION, PERMISSION_STATUS_ASK); |
| 180 | #if defined(OS_ANDROID) |
xhwang | 8624637f | 2015-01-28 21:09:59 | [diff] [blame] | 181 | CheckPermissionStatus(PERMISSION_PROTECTED_MEDIA_IDENTIFIER, |
| 182 | PERMISSION_STATUS_ASK); |
timvolodine | a47291a | 2015-01-23 14:04:43 | [diff] [blame] | 183 | #endif |
| 184 | } |
| 185 | |
| 186 | TEST_F(PermissionBrowserClientTest, GetPermissionStatusAfterSet) { |
| 187 | using namespace content; |
| 188 | SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ALLOW); |
| 189 | CheckPermissionStatus(PERMISSION_GEOLOCATION, PERMISSION_STATUS_GRANTED); |
| 190 | |
| 191 | SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); |
| 192 | CheckPermissionStatus(PERMISSION_NOTIFICATIONS, PERMISSION_STATUS_GRANTED); |
| 193 | |
| 194 | SetPermission(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, CONTENT_SETTING_ALLOW); |
| 195 | CheckPermissionStatus(PERMISSION_MIDI_SYSEX, PERMISSION_STATUS_GRANTED); |
| 196 | |
| 197 | SetPermission(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ALLOW); |
| 198 | CheckPermissionStatus(PERMISSION_PUSH_MESSAGING, PERMISSION_STATUS_GRANTED); |
| 199 | |
| 200 | #if defined(OS_ANDROID) |
| 201 | SetPermission(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, |
| 202 | CONTENT_SETTING_ALLOW); |
xhwang | 8624637f | 2015-01-28 21:09:59 | [diff] [blame] | 203 | CheckPermissionStatus(PERMISSION_PROTECTED_MEDIA_IDENTIFIER, |
| 204 | PERMISSION_STATUS_GRANTED); |
timvolodine | a47291a | 2015-01-23 14:04:43 | [diff] [blame] | 205 | #endif |
| 206 | } |
| 207 | |
| 208 | } // namespace chrome |