1 // Copyright (c) 2012 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 "base/command_line.h"
6 #include "base/metrics/field_trial.h"
7 #include "chrome/browser/search/search.h"
8 #include "chrome/browser/search_engines/template_url_service.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/common/chrome_switches.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/content_switches.h"
23 class ChromeContentBrowserClientBrowserTest : public InProcessBrowserTest {
25 // Returns the last committed navigation entry of the first tab. May be NULL
26 // if there is no such entry.
27 NavigationEntry* GetLastCommittedEntry() {
28 return browser()->tab_strip_model()->GetWebContentsAt(0)->
29 GetController().GetLastCommittedEntry();
32 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
33 TemplateURLService* template_url_service =
34 TemplateURLServiceFactory::GetForProfile(browser()->profile());
35 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
38 data.SetURL("http://foo.com/url?bar={searchTerms}");
39 data.new_tab_url = new_tab_page_url.spec();
40 TemplateURL* template_url = new TemplateURL(browser()->profile(), data);
42 template_url_service->Add(template_url);
43 template_url_service->SetDefaultSearchProvider(template_url);
47 IN_PROC_BROWSER_TEST_F(ChromeContentBrowserClientBrowserTest,
48 UberURLHandler_SettingsPage) {
49 const GURL url_short("chrome://settings/");
50 const GURL url_long("chrome://chrome/settings/");
52 ui_test_utils::NavigateToURL(browser(), url_short);
53 NavigationEntry* entry = GetLastCommittedEntry();
55 ASSERT_TRUE(entry != NULL);
56 EXPECT_EQ(url_long, entry->GetURL());
57 EXPECT_EQ(url_short, entry->GetVirtualURL());
60 IN_PROC_BROWSER_TEST_F(ChromeContentBrowserClientBrowserTest,
61 UberURLHandler_ContentSettingsPage) {
62 const GURL url_short("chrome://settings/content");
63 const GURL url_long("chrome://chrome/settings/content");
65 ui_test_utils::NavigateToURL(browser(), url_short);
66 NavigationEntry* entry = GetLastCommittedEntry();
68 ASSERT_TRUE(entry != NULL);
69 EXPECT_EQ(url_long, entry->GetURL());
70 EXPECT_EQ(url_short, entry->GetVirtualURL());
73 IN_PROC_BROWSER_TEST_F(ChromeContentBrowserClientBrowserTest,
74 UberURLHandler_AboutPage) {
75 const GURL url("chrome://chrome/");
77 ui_test_utils::NavigateToURL(browser(), url);
78 NavigationEntry* entry = GetLastCommittedEntry();
80 ASSERT_TRUE(entry != NULL);
81 EXPECT_EQ(url, entry->GetURL());
82 EXPECT_EQ(url, entry->GetVirtualURL());
85 IN_PROC_BROWSER_TEST_F(ChromeContentBrowserClientBrowserTest,
86 UberURLHandler_EmptyHost) {
87 const GURL url("chrome://chrome//foo");
89 ui_test_utils::NavigateToURL(browser(), url);
90 NavigationEntry* entry = GetLastCommittedEntry();
92 ASSERT_TRUE(entry != NULL);
93 EXPECT_TRUE(entry->GetVirtualURL().is_valid());
94 EXPECT_EQ(url, entry->GetVirtualURL());
97 class InstantNTPURLRewriteTest : public ChromeContentBrowserClientBrowserTest {
99 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
100 // browsertest initially spins up a non-Instant renderer for about:blank.
101 // In a real browser, navigating this renderer to the Instant new tab page
102 // forks a new privileged Instant render process, but that warps
103 // browsertest's fragile little mind; it just never navigates. We aren't
104 // trying to test the process model here, so turn it off.
105 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kSingleProcess);
109 IN_PROC_BROWSER_TEST_F(InstantNTPURLRewriteTest,
110 UberURLHandler_InstantExtendedNewTabPage) {
111 const GURL url_original("chrome://newtab");
112 const GURL url_rewritten("https://example.com/newtab");
113 InstallTemplateURLWithNewTabPage(url_rewritten);
114 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
115 "Group1 use_cacheable_ntp:1"));
116 chrome::EnableInstantExtendedAPIForTesting();
118 ui_test_utils::NavigateToURL(browser(), url_original);
119 NavigationEntry* entry = GetLastCommittedEntry();
121 ASSERT_TRUE(entry != NULL);
122 EXPECT_EQ(url_rewritten, entry->GetURL());
123 EXPECT_EQ(url_original, entry->GetVirtualURL());
126 IN_PROC_BROWSER_TEST_F(ChromeContentBrowserClientBrowserTest,
127 UberURLHandler_InstantExtendedNewTabPageDisabled) {
128 // Don't do the kSingleProcess shenanigans here (see the dual test) because
129 // otherwise RenderViewImpl crashes in a paranoid fit on startup.
130 const GURL url_original("chrome://newtab");
131 const GURL url_rewritten("https://example.com/newtab");
132 InstallTemplateURLWithNewTabPage(url_rewritten);
133 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
134 "Group1 use_cacheable_ntp:1"));
135 chrome::DisableInstantExtendedAPIForTesting();
137 ui_test_utils::NavigateToURL(browser(), url_original);
138 NavigationEntry* entry = GetLastCommittedEntry();
140 ASSERT_TRUE(entry != NULL);
141 EXPECT_EQ(url_original, entry->GetURL());
142 EXPECT_EQ(url_original, entry->GetVirtualURL());
145 // Test that a basic navigation works in --site-per-process mode. This prevents
146 // regressions when that mode calls out into the ChromeContentBrowserClient,
147 // such as http://crbug.com/164223.
148 IN_PROC_BROWSER_TEST_F(ChromeContentBrowserClientBrowserTest,
149 SitePerProcessNavigation) {
150 CommandLine::ForCurrentProcess()->AppendSwitch(
151 switches::kSitePerProcess);
152 ASSERT_TRUE(test_server()->Start());
153 const GURL url(test_server()->GetURL("files/title1.html"));
155 ui_test_utils::NavigateToURL(browser(), url);
156 NavigationEntry* entry = GetLastCommittedEntry();
158 ASSERT_TRUE(entry != NULL);
159 EXPECT_EQ(url, entry->GetURL());
160 EXPECT_EQ(url, entry->GetVirtualURL());
163 } // namespace content