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/logging.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/test/browser_test_utils.h"
17 #include "content/public/test/test_navigation_observer.h"
20 using content::OpenURLParams
;
21 using content::Referrer
;
25 static bool had_console_errors
= false;
27 bool HandleMessage(int severity
,
31 const std::string
& str
) {
32 if (severity
== logging::LOG_ERROR
&& file
&& file
== std::string("CONSOLE"))
33 had_console_errors
= true;
39 class NewTabUIBrowserTest
: public InProcessBrowserTest
{
41 NewTabUIBrowserTest() {
42 logging::SetLogMessageHandler(&HandleMessage
);
45 ~NewTabUIBrowserTest() override
{ logging::SetLogMessageHandler(NULL
); }
47 void TearDown() override
{
48 InProcessBrowserTest::TearDown();
49 ASSERT_FALSE(had_console_errors
);
53 // TODO(samarth): delete along with rest of NTP4 code.
54 // #if defined(OS_WIN)
55 // // Flaky on Windows (http://crbug.com/174819)
56 // #define MAYBE_LoadNTPInExistingProcess DISABLED_LoadNTPInExistingProcess
58 // #define MAYBE_LoadNTPInExistingProcess LoadNTPInExistingProcess
61 // Ensure loading a NTP with an existing SiteInstance in a reused process
62 // doesn't cause us to kill the process. See http://crbug.com/104258.
63 IN_PROC_BROWSER_TEST_F(NewTabUIBrowserTest
, DISABLED_LoadNTPInExistingProcess
) {
64 // Set max renderers to 1 to force running out of processes.
65 content::RenderProcessHost::SetMaxRendererProcessCount(1);
67 // Start server for simple page.
68 ASSERT_TRUE(test_server()->Start());
70 // Load a NTP in a new tab.
71 ui_test_utils::NavigateToURLWithDisposition(
72 browser(), GURL(chrome::kChromeUINewTabURL
), NEW_FOREGROUND_TAB
,
73 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
75 browser()->tab_strip_model()->GetWebContentsAt(1)->GetMaxPageID());
77 // Navigate that tab to another site. This allows the NTP process to exit,
78 // but it keeps the NTP SiteInstance (and its max_page_id) alive in history.
80 // Wait not just for the navigation to finish, but for the NTP process to
82 content::RenderProcessHostWatcher
process_exited_observer(
83 browser()->tab_strip_model()->GetActiveWebContents(),
84 content::RenderProcessHostWatcher::WATCH_FOR_HOST_DESTRUCTION
);
85 browser()->OpenURL(OpenURLParams(
86 test_server()->GetURL("files/title1.html"), Referrer(), CURRENT_TAB
,
87 ui::PAGE_TRANSITION_TYPED
, false));
88 process_exited_observer
.Wait();
91 // Creating a NTP in another tab should not be affected, since page IDs
92 // are now specific to a tab.
93 ui_test_utils::NavigateToURLWithDisposition(
94 browser(), GURL(chrome::kChromeUINewTabURL
), NEW_FOREGROUND_TAB
,
95 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
97 browser()->tab_strip_model()->GetWebContentsAt(2)->GetMaxPageID());
98 chrome::CloseTab(browser());
100 // Open another Web UI page in a new tab.
101 ui_test_utils::NavigateToURLWithDisposition(
102 browser(), GURL(chrome::kChromeUISettingsURL
), NEW_FOREGROUND_TAB
,
103 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
105 browser()->tab_strip_model()->GetWebContentsAt(2)->GetMaxPageID());
107 // At this point, opening another NTP will use the existing WebUI process
108 // but its own SiteInstance, so the page IDs shouldn't affect each other.
109 ui_test_utils::NavigateToURLWithDisposition(
110 browser(), GURL(chrome::kChromeUINewTabURL
), NEW_FOREGROUND_TAB
,
111 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
113 browser()->tab_strip_model()->GetWebContentsAt(3)->GetMaxPageID());
115 // Navigating to the NTP in the original tab causes a BrowsingInstance
116 // swap, so it gets a new SiteInstance starting with page ID 1 again.
117 browser()->tab_strip_model()->ActivateTabAt(1, true);
118 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL
));
120 browser()->tab_strip_model()->GetWebContentsAt(1)->GetMaxPageID());
123 // TODO(samarth): delete along with rest of NTP4 code.
124 // Loads chrome://hang/ into two NTP tabs, ensuring we don't crash.
125 // See http://crbug.com/59859.
126 // If this flakes, use http://crbug.com/87200.
127 IN_PROC_BROWSER_TEST_F(NewTabUIBrowserTest
, DISABLED_ChromeHangInNTP
) {
128 // Bring up a new tab page.
129 ui_test_utils::NavigateToURLWithDisposition(
130 browser(), GURL(chrome::kChromeUINewTabURL
), NEW_FOREGROUND_TAB
,
131 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
133 // Navigate to chrome://hang/ to stall the process.
134 ui_test_utils::NavigateToURLWithDisposition(
135 browser(), GURL(content::kChromeUIHangURL
), CURRENT_TAB
, 0);
137 // Visit chrome://hang/ again in another NTP. Don't bother waiting for the
138 // NTP to load, because it's hung.
139 chrome::NewTab(browser());
140 browser()->OpenURL(OpenURLParams(
141 GURL(content::kChromeUIHangURL
), Referrer(), CURRENT_TAB
,
142 ui::PAGE_TRANSITION_TYPED
, false));
145 // Navigate to incognito NTP. Fails if there are console errors.
146 IN_PROC_BROWSER_TEST_F(NewTabUIBrowserTest
, ShowIncognito
) {
147 ui_test_utils::NavigateToURL(CreateIncognitoBrowser(),
148 GURL(chrome::kChromeUINewTabURL
));
151 class NewTabUIProcessPerTabTest
: public NewTabUIBrowserTest
{
153 NewTabUIProcessPerTabTest() {}
155 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
156 command_line
->AppendSwitch(switches::kProcessPerTab
);
160 // Navigates away from NTP before it commits, in process-per-tab mode.
161 // Ensures that we don't load the normal page in the NTP process (and thus
162 // crash), as in http://crbug.com/69224.
163 // If this flakes, use http://crbug.com/87200
164 IN_PROC_BROWSER_TEST_F(NewTabUIProcessPerTabTest
, NavBeforeNTPCommits
) {
165 // Bring up a new tab page.
166 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL
));
168 // Navigate to chrome://hang/ to stall the process.
169 ui_test_utils::NavigateToURLWithDisposition(
170 browser(), GURL(content::kChromeUIHangURL
), CURRENT_TAB
, 0);
172 // Visit a normal URL in another NTP that hasn't committed.
173 ui_test_utils::NavigateToURLWithDisposition(
174 browser(), GURL(chrome::kChromeUINewTabURL
), NEW_FOREGROUND_TAB
, 0);
176 // We don't use ui_test_utils::NavigateToURLWithDisposition because that waits
177 // for current loading to stop.
178 content::TestNavigationObserver
observer(
179 browser()->tab_strip_model()->GetActiveWebContents());
180 browser()->OpenURL(OpenURLParams(
181 GURL("data:text/html,hello world"), Referrer(), CURRENT_TAB
,
182 ui::PAGE_TRANSITION_TYPED
, false));