blob: 9ea8f8c972c27be3904709c2d82192bed24813bc [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
[email protected]30335fdf02014-02-26 19:51:277#include "base/command_line.h"
[email protected]10a9bf92013-11-13 23:34:488#include "base/metrics/field_trial.h"
[email protected]10a9bf92013-11-13 23:34:489#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"
timvolodinea47291a2015-01-23 14:04:4314#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]bf5c532d2014-07-05 00:29:5315#include "components/search_engines/template_url_service.h"
[email protected]10a9bf92013-11-13 23:34:4816#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]30335fdf02014-02-26 19:51:2720#include "content/public/common/content_switches.h"
[email protected]3d831992013-07-04 01:13:2921#include "testing/gtest/include/gtest/gtest.h"
[email protected]5f5ef802013-07-04 16:11:1322#include "url/gurl.h"
[email protected]3d831992013-07-04 01:13:2923
24namespace chrome {
25
26typedef testing::Test ChromeContentBrowserClientTest;
27
[email protected]3d831992013-07-04 01:13:2928TEST_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]30335fdf02014-02-26 19:51:2735// NOTE: Any updates to the expectations in these tests should also be done in
36// the browser test WebRtcDisableEncryptionFlagBrowserTest.
37class DisableWebRtcEncryptionFlagTest : public testing::Test {
38 public:
39 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:1740 : from_command_line_(base::CommandLine::NO_PROGRAM),
41 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:2742
43 protected:
dchenge1bc7982014-10-30 00:32:4044 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:2745 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
avi3ef9ec9e2014-12-22 22:50:1755 base::CommandLine from_command_line_;
56 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:2757
58 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
59};
60
61TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
62 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_UNKNOWN);
63 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
64}
65
66TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
67 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_CANARY);
68 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
69}
70
71TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
72 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_DEV);
73 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
74}
75
76TEST_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
85TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
86 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_STABLE);
87 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
88}
89
[email protected]3d831992013-07-04 01:13:2990} // namespace chrome
[email protected]10a9bf92013-11-13 23:34:4891
92#if !defined(OS_IOS) && !defined(OS_ANDROID)
93namespace content {
94
95class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
96 protected:
dchenge1bc7982014-10-30 00:32:4097 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:4898 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]168d08722014-06-18 07:13:28113 TemplateURL* template_url = new TemplateURL(data);
[email protected]10a9bf92013-11-13 23:34:48114 // Takes ownership.
115 template_url_service->Add(template_url);
[email protected]f1cb5582014-04-25 07:35:26116 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48117 }
118
119 scoped_ptr<base::FieldTrialList> field_trial_list_;
120};
121
122TEST_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)
timvolodinea47291a2015-01-23 14:04:43141
142namespace chrome {
143
144// For testing permissions related functionality.
145class 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
170TEST_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
181TEST_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