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
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/search_engines/template_url_service.h"
15#include "components/variations/entropy_provider.h"
16#include "content/public/browser/navigation_controller.h"
17#include "content/public/browser/navigation_entry.h"
18#include "content/public/browser/web_contents.h"
19#include "content/public/common/content_switches.h"
20#include "testing/gtest/include/gtest/gtest.h"
21#include "url/gurl.h"
22
23namespace chrome {
24
25typedef testing::Test ChromeContentBrowserClientTest;
26
27TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
28  ChromeContentBrowserClient client;
29  EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
30  EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
31  EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
32}
33
34// NOTE: Any updates to the expectations in these tests should also be done in
35// the browser test WebRtcDisableEncryptionFlagBrowserTest.
36class DisableWebRtcEncryptionFlagTest : public testing::Test {
37 public:
38  DisableWebRtcEncryptionFlagTest()
39      : from_command_line_(CommandLine::NO_PROGRAM),
40        to_command_line_(CommandLine::NO_PROGRAM) {}
41
42 protected:
43  virtual void SetUp() {
44    from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
45  }
46
47  void MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::Channel channel) {
48    ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
49        &to_command_line_,
50        from_command_line_,
51        channel);
52  }
53
54  CommandLine from_command_line_;
55  CommandLine to_command_line_;
56
57  DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
58};
59
60TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
61  MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_UNKNOWN);
62  EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
63}
64
65TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
66  MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_CANARY);
67  EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
68}
69
70TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
71  MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_DEV);
72  EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
73}
74
75TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
76  MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_BETA);
77#if defined(OS_ANDROID)
78  EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
79#else
80  EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
81#endif
82}
83
84TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
85  MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_STABLE);
86  EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
87}
88
89}  // namespace chrome
90
91#if !defined(OS_IOS) && !defined(OS_ANDROID)
92namespace content {
93
94class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
95 protected:
96  virtual void SetUp() OVERRIDE {
97    BrowserWithTestWindowTest::SetUp();
98    field_trial_list_.reset(new base::FieldTrialList(
99        new metrics::SHA1EntropyProvider("42")));
100  }
101
102  void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
103    TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
104        profile(), &TemplateURLServiceFactory::BuildInstanceFor);
105    TemplateURLService* template_url_service =
106        TemplateURLServiceFactory::GetForProfile(browser()->profile());
107    ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
108
109    TemplateURLData data;
110    data.SetURL("http://foo.com/url?bar={searchTerms}");
111    data.new_tab_url = new_tab_page_url.spec();
112    TemplateURL* template_url = new TemplateURL(data);
113    // Takes ownership.
114    template_url_service->Add(template_url);
115    template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
116  }
117
118  scoped_ptr<base::FieldTrialList> field_trial_list_;
119};
120
121TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
122  const GURL url_original("chrome://newtab");
123  const GURL url_rewritten("https://www.example.com/newtab");
124  InstallTemplateURLWithNewTabPage(url_rewritten);
125  ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
126      "Group1 use_cacheable_ntp:1"));
127
128  AddTab(browser(), GURL("chrome://blank"));
129  NavigateAndCommitActiveTab(url_original);
130
131  NavigationEntry* entry = browser()->tab_strip_model()->
132      GetActiveWebContents()->GetController().GetLastCommittedEntry();
133  ASSERT_TRUE(entry != NULL);
134  EXPECT_EQ(url_rewritten, entry->GetURL());
135  EXPECT_EQ(url_original, entry->GetVirtualURL());
136}
137
138}  // namespace content
139#endif  // !defined(OS_IOS) && !defined(OS_ANDROID)
140