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.
5 #include "chrome/browser/chrome_content_browser_client.h"
7 #include "base/command_line.h"
8 #include "base/metrics/field_trial.h"
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"
14 #include "components/content_settings/core/browser/host_content_settings_map.h"
15 #include "components/search_engines/template_url_service.h"
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"
20 #include "content/public/common/content_switches.h"
21 #include "testing/gtest/include/gtest/gtest.h"
26 using ChromeContentBrowserClientTest
= testing::Test
;
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")));
35 // BrowserWithTestWindowTest doesn't work on iOS and Android.
36 #if !defined(OS_ANDROID) && !defined(OS_IOS)
38 using ChromeContentBrowserClientWindowTest
= BrowserWithTestWindowTest
;
40 static void DidOpenURLForWindowTest(content::WebContents
** target_contents
,
41 content::WebContents
* opened_contents
) {
42 DCHECK(target_contents
);
44 *target_contents
= opened_contents
;
47 // This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
48 // URLs to be opened in new tabs and activated, changing the active tabs after
49 // each call and increasing the tab count by 2.
50 TEST_F(ChromeContentBrowserClientWindowTest
, OpenURL
) {
51 ChromeContentBrowserClient client
;
53 int previous_count
= browser()->tab_strip_model()->count();
55 GURL urls
[] = { GURL("https://www.google.com"),
56 GURL("https://www.chromium.org") };
58 for (const GURL
& url
: urls
) {
59 content::OpenURLParams
params(url
,
62 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
,
64 // TODO(peter): We should have more in-depth browser tests for the window
65 // opening functionality, which also covers Android. This test can currently
66 // only be ran on platforms where OpenURL is implemented synchronously.
67 // See https://crbug.com/457667.
68 content::WebContents
* web_contents
= nullptr;
69 client
.OpenURL(browser()->profile(),
71 base::Bind(&DidOpenURLForWindowTest
, &web_contents
));
73 EXPECT_TRUE(web_contents
);
75 content::WebContents
* active_contents
= browser()->tab_strip_model()->
76 GetActiveWebContents();
77 EXPECT_EQ(web_contents
, active_contents
);
78 EXPECT_EQ(url
, active_contents
->GetVisibleURL());
81 EXPECT_EQ(previous_count
+ 2, browser()->tab_strip_model()->count());
84 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
86 #if defined(ENABLE_WEBRTC)
88 // NOTE: Any updates to the expectations in these tests should also be done in
89 // the browser test WebRtcDisableEncryptionFlagBrowserTest.
90 class DisableWebRtcEncryptionFlagTest
: public testing::Test
{
92 DisableWebRtcEncryptionFlagTest()
93 : from_command_line_(base::CommandLine::NO_PROGRAM
),
94 to_command_line_(base::CommandLine::NO_PROGRAM
) {}
97 void SetUp() override
{
98 from_command_line_
.AppendSwitch(switches::kDisableWebRtcEncryption
);
101 void MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::Channel channel
) {
102 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
108 base::CommandLine from_command_line_
;
109 base::CommandLine to_command_line_
;
111 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest
);
114 TEST_F(DisableWebRtcEncryptionFlagTest
, UnknownChannel
) {
115 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_UNKNOWN
);
116 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
119 TEST_F(DisableWebRtcEncryptionFlagTest
, CanaryChannel
) {
120 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_CANARY
);
121 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
124 TEST_F(DisableWebRtcEncryptionFlagTest
, DevChannel
) {
125 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_DEV
);
126 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
129 TEST_F(DisableWebRtcEncryptionFlagTest
, BetaChannel
) {
130 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_BETA
);
131 #if defined(OS_ANDROID)
132 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
134 EXPECT_FALSE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
138 TEST_F(DisableWebRtcEncryptionFlagTest
, StableChannel
) {
139 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_STABLE
);
140 EXPECT_FALSE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
143 #endif // ENABLE_WEBRTC
145 } // namespace chrome
147 #if !defined(OS_IOS) && !defined(OS_ANDROID)
150 class InstantNTPURLRewriteTest
: public BrowserWithTestWindowTest
{
152 void SetUp() override
{
153 BrowserWithTestWindowTest::SetUp();
154 field_trial_list_
.reset(new base::FieldTrialList(
155 new metrics::SHA1EntropyProvider("42")));
158 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url
) {
159 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
160 profile(), &TemplateURLServiceFactory::BuildInstanceFor
);
161 TemplateURLService
* template_url_service
=
162 TemplateURLServiceFactory::GetForProfile(browser()->profile());
163 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service
);
165 TemplateURLData data
;
166 data
.SetURL("http://foo.com/url?bar={searchTerms}");
167 data
.new_tab_url
= new_tab_page_url
.spec();
168 TemplateURL
* template_url
= new TemplateURL(data
);
170 template_url_service
->Add(template_url
);
171 template_url_service
->SetUserSelectedDefaultSearchProvider(template_url
);
174 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
177 TEST_F(InstantNTPURLRewriteTest
, UberURLHandler_InstantExtendedNewTabPage
) {
178 const GURL
url_original("chrome://newtab");
179 const GURL
url_rewritten("https://www.example.com/newtab");
180 InstallTemplateURLWithNewTabPage(url_rewritten
);
181 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
182 "Group1 use_cacheable_ntp:1"));
184 AddTab(browser(), GURL("chrome://blank"));
185 NavigateAndCommitActiveTab(url_original
);
187 NavigationEntry
* entry
= browser()->tab_strip_model()->
188 GetActiveWebContents()->GetController().GetLastCommittedEntry();
189 ASSERT_TRUE(entry
!= NULL
);
190 EXPECT_EQ(url_rewritten
, entry
->GetURL());
191 EXPECT_EQ(url_original
, entry
->GetVirtualURL());
194 } // namespace content
195 #endif // !defined(OS_IOS) && !defined(OS_ANDROID)