Refactor SharedMemory::Create and fix a rare file leak.
[chromium-blink-merge.git] / chrome / browser / chrome_content_browser_client_unittest.cc
blob6b35cb94ddaa24ed638a134ecdc332b3825819e0
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 "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/search_engines/template_url_service_factory.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/browser_with_test_window_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "components/search_engines/template_url_service.h"
17 #include "components/variations/entropy_provider.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/content_switches.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "url/gurl.h"
25 namespace chrome {
27 using ChromeContentBrowserClientTest = testing::Test;
29 TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
30 ChromeContentBrowserClient client;
31 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
32 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
33 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
36 // BrowserWithTestWindowTest doesn't work on iOS and Android.
37 #if !defined(OS_ANDROID) && !defined(OS_IOS)
39 using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest;
41 static void DidOpenURLForWindowTest(content::WebContents** target_contents,
42 content::WebContents* opened_contents) {
43 DCHECK(target_contents);
45 *target_contents = opened_contents;
48 // This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
49 // URLs to be opened in new tabs and activated, changing the active tabs after
50 // each call and increasing the tab count by 2.
51 TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) {
52 ChromeContentBrowserClient client;
54 int previous_count = browser()->tab_strip_model()->count();
56 GURL urls[] = { GURL("https://www.google.com"),
57 GURL("https://www.chromium.org") };
59 for (const GURL& url : urls) {
60 content::OpenURLParams params(url,
61 content::Referrer(),
62 NEW_FOREGROUND_TAB,
63 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
64 false);
65 // TODO(peter): We should have more in-depth browser tests for the window
66 // opening functionality, which also covers Android. This test can currently
67 // only be ran on platforms where OpenURL is implemented synchronously.
68 // See https://crbug.com/457667.
69 content::WebContents* web_contents = nullptr;
70 client.OpenURL(browser()->profile(),
71 params,
72 base::Bind(&DidOpenURLForWindowTest, &web_contents));
74 EXPECT_TRUE(web_contents);
76 content::WebContents* active_contents = browser()->tab_strip_model()->
77 GetActiveWebContents();
78 EXPECT_EQ(web_contents, active_contents);
79 EXPECT_EQ(url, active_contents->GetVisibleURL());
82 EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count());
85 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
87 #if defined(ENABLE_WEBRTC)
89 // NOTE: Any updates to the expectations in these tests should also be done in
90 // the browser test WebRtcDisableEncryptionFlagBrowserTest.
91 class DisableWebRtcEncryptionFlagTest : public testing::Test {
92 public:
93 DisableWebRtcEncryptionFlagTest()
94 : from_command_line_(base::CommandLine::NO_PROGRAM),
95 to_command_line_(base::CommandLine::NO_PROGRAM) {}
97 protected:
98 void SetUp() override {
99 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
102 void MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::Channel channel) {
103 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
104 &to_command_line_,
105 from_command_line_,
106 channel);
109 base::CommandLine from_command_line_;
110 base::CommandLine to_command_line_;
112 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
115 TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
116 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_UNKNOWN);
117 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
120 TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
121 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_CANARY);
122 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
125 TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
126 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_DEV);
127 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
130 TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
131 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_BETA);
132 #if defined(OS_ANDROID)
133 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
134 #else
135 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
136 #endif
139 TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
140 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_STABLE);
141 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
144 #endif // ENABLE_WEBRTC
146 } // namespace chrome
148 #if !defined(OS_IOS) && !defined(OS_ANDROID)
149 namespace content {
151 class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
152 protected:
153 void SetUp() override {
154 BrowserWithTestWindowTest::SetUp();
155 field_trial_list_.reset(new base::FieldTrialList(
156 new metrics::SHA1EntropyProvider("42")));
159 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
160 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
161 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
162 TemplateURLService* template_url_service =
163 TemplateURLServiceFactory::GetForProfile(browser()->profile());
164 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
166 TemplateURLData data;
167 data.SetShortName(base::ASCIIToUTF16("foo.com"));
168 data.SetURL("http://foo.com/url?bar={searchTerms}");
169 data.new_tab_url = new_tab_page_url.spec();
170 TemplateURL* template_url = new TemplateURL(data);
171 // Takes ownership.
172 template_url_service->Add(template_url);
173 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
176 scoped_ptr<base::FieldTrialList> field_trial_list_;
179 TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
180 const GURL url_original("chrome://newtab");
181 const GURL url_rewritten("https://www.example.com/newtab");
182 InstallTemplateURLWithNewTabPage(url_rewritten);
183 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
184 "Group1 use_cacheable_ntp:1"));
186 AddTab(browser(), GURL("chrome://blank"));
187 NavigateAndCommitActiveTab(url_original);
189 NavigationEntry* entry = browser()->tab_strip_model()->
190 GetActiveWebContents()->GetController().GetLastCommittedEntry();
191 ASSERT_TRUE(entry != NULL);
192 EXPECT_EQ(url_rewritten, entry->GetURL());
193 EXPECT_EQ(url_original, entry->GetVirtualURL());
196 } // namespace content
197 #endif // !defined(OS_IOS) && !defined(OS_ANDROID)