- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chrome_content_browser_client_browsertest.cc
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.
4
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"
19 #include "url/gurl.h"
20
21 namespace content {
22
23 class ChromeContentBrowserClientBrowserTest : public InProcessBrowserTest {
24  public:
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();
30   }
31
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);
36
37     TemplateURLData data;
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);
41     // Takes ownership.
42     template_url_service->Add(template_url);
43     template_url_service->SetDefaultSearchProvider(template_url);
44   }
45 };
46
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/");
51
52   ui_test_utils::NavigateToURL(browser(), url_short);
53   NavigationEntry* entry = GetLastCommittedEntry();
54
55   ASSERT_TRUE(entry != NULL);
56   EXPECT_EQ(url_long, entry->GetURL());
57   EXPECT_EQ(url_short, entry->GetVirtualURL());
58 }
59
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");
64
65   ui_test_utils::NavigateToURL(browser(), url_short);
66   NavigationEntry* entry = GetLastCommittedEntry();
67
68   ASSERT_TRUE(entry != NULL);
69   EXPECT_EQ(url_long, entry->GetURL());
70   EXPECT_EQ(url_short, entry->GetVirtualURL());
71 }
72
73 IN_PROC_BROWSER_TEST_F(ChromeContentBrowserClientBrowserTest,
74                        UberURLHandler_AboutPage) {
75   const GURL url("chrome://chrome/");
76
77   ui_test_utils::NavigateToURL(browser(), url);
78   NavigationEntry* entry = GetLastCommittedEntry();
79
80   ASSERT_TRUE(entry != NULL);
81   EXPECT_EQ(url, entry->GetURL());
82   EXPECT_EQ(url, entry->GetVirtualURL());
83 }
84
85 IN_PROC_BROWSER_TEST_F(ChromeContentBrowserClientBrowserTest,
86                        UberURLHandler_EmptyHost) {
87   const GURL url("chrome://chrome//foo");
88
89   ui_test_utils::NavigateToURL(browser(), url);
90   NavigationEntry* entry = GetLastCommittedEntry();
91
92   ASSERT_TRUE(entry != NULL);
93   EXPECT_TRUE(entry->GetVirtualURL().is_valid());
94   EXPECT_EQ(url, entry->GetVirtualURL());
95 }
96
97 class InstantNTPURLRewriteTest : public ChromeContentBrowserClientBrowserTest {
98  public:
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);
106   }
107 };
108
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();
117
118   ui_test_utils::NavigateToURL(browser(), url_original);
119   NavigationEntry* entry = GetLastCommittedEntry();
120
121   ASSERT_TRUE(entry != NULL);
122   EXPECT_EQ(url_rewritten, entry->GetURL());
123   EXPECT_EQ(url_original, entry->GetVirtualURL());
124 }
125
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();
136
137   ui_test_utils::NavigateToURL(browser(), url_original);
138   NavigationEntry* entry = GetLastCommittedEntry();
139
140   ASSERT_TRUE(entry != NULL);
141   EXPECT_EQ(url_original, entry->GetURL());
142   EXPECT_EQ(url_original, entry->GetVirtualURL());
143 }
144
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"));
154
155   ui_test_utils::NavigateToURL(browser(), url);
156   NavigationEntry* entry = GetLastCommittedEntry();
157
158   ASSERT_TRUE(entry != NULL);
159   EXPECT_EQ(url, entry->GetURL());
160   EXPECT_EQ(url, entry->GetVirtualURL());
161 }
162
163 }  // namespace content