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