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.
7 #include "base/base_switches.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/macros.h"
12 #include "base/strings/stringprintf.h"
13 #include "build/build_config.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/url_constants.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/navigation_controller.h"
21 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_types.h"
24 #include "content/public/browser/render_frame_host.h"
25 #include "content/public/browser/render_process_host.h"
26 #include "content/public/browser/render_widget_host_view.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/test/browser_test.h"
29 #include "content/public/test/browser_test_utils.h"
30 #include "content/public/test/no_renderer_crashes_assertion.h"
31 #include "net/test/embedded_test_server/embedded_test_server.h"
32 #include "net/test/embedded_test_server/http_request.h"
33 #include "net/test/embedded_test_server/http_response.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "ui/base/page_transition_types.h"
37 using content::OpenURLParams;
38 using content::Referrer;
39 using content::WebContents;
41 // TODO(jam): http://crbug.com/350550
42 #if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
46 void SimulateRendererCrash(Browser* browser) {
47 content::WindowedNotificationObserver observer(
48 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
49 content::NotificationService::AllSources());
50 browser->OpenURL(OpenURLParams(GURL(content::kChromeUICrashURL), Referrer(),
51 WindowOpenDisposition::CURRENT_TAB,
52 ui::PAGE_TRANSITION_TYPED, false));
56 // A request handler which returns a different result each time but stays fresh
57 // into the far future.
58 class CacheMaxAgeHandler {
60 explicit CacheMaxAgeHandler(const std::string& path)
61 : path_(path), request_count_(0) { }
63 std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
64 const net::test_server::HttpRequest& request) {
65 if (request.relative_url != path_)
66 return std::unique_ptr<net::test_server::HttpResponse>();
69 std::unique_ptr<net::test_server::BasicHttpResponse> response(
70 new net::test_server::BasicHttpResponse);
71 response->set_content(base::StringPrintf("<title>%d</title>",
73 response->set_content_type("text/html");
74 response->AddCustomHeader("Cache-Control", "max-age=99999");
75 return std::move(response);
81 DISALLOW_COPY_AND_ASSIGN(CacheMaxAgeHandler);
84 class CrashRecoveryBrowserTest : public InProcessBrowserTest {
86 WebContents* GetActiveWebContents() {
87 return browser()->tab_strip_model()->GetActiveWebContents();
91 void SetUpCommandLine(base::CommandLine* command_line) override {
92 command_line->AppendSwitch(switches::kDisableBreakpad);
95 content::ScopedAllowRendererCrashes scoped_allow_renderer_crashes_;
98 // Test that reload works after a crash.
99 // Flaky timeouts on Win7 Tests (dbg)(1); see https://crbug.com/985255.
100 #if defined(OS_WIN) && !defined(NDEBUG)
101 #define MAYBE_Reload DISABLED_Reload
103 #define MAYBE_Reload Reload
105 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, MAYBE_Reload) {
106 // The title of the active tab should change each time this URL is loaded.
108 "data:text/html,<script>document.title=new Date().valueOf()</script>");
109 ui_test_utils::NavigateToURL(browser(), url);
111 base::string16 title_before_crash;
112 base::string16 title_after_crash;
114 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
115 &title_before_crash));
116 SimulateRendererCrash(browser());
117 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
118 EXPECT_TRUE(content::WaitForLoadStop(GetActiveWebContents()));
119 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
120 &title_after_crash));
121 EXPECT_NE(title_before_crash, title_after_crash);
122 ASSERT_TRUE(GetActiveWebContents()->GetMainFrame()->GetView()->IsShowing());
123 ASSERT_FALSE(GetActiveWebContents()
126 ->IsProcessBackgrounded());
129 // Test that reload after a crash forces a cache revalidation.
131 // Flaky timeouts on Win7 Tests (dbg)(1); see https://crbug.com/985255.
132 #if defined(OS_WIN) && !defined(NDEBUG)
133 #define MAYBE_ReloadCacheRevalidate DISABLED_ReloadCacheRevalidate
135 #define MAYBE_ReloadCacheRevalidate ReloadCacheRevalidate
137 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, MAYBE_ReloadCacheRevalidate) {
138 const char kTestPath[] = "/test";
140 // Use the test server so as not to bypass cache behavior. The title of the
141 // active tab should change only when this URL is reloaded.
142 embedded_test_server()->RegisterRequestHandler(
143 base::Bind(&CacheMaxAgeHandler::HandleRequest,
144 base::Owned(new CacheMaxAgeHandler(kTestPath))));
145 ASSERT_TRUE(embedded_test_server()->Start());
146 ui_test_utils::NavigateToURL(browser(),
147 embedded_test_server()->GetURL(kTestPath));
149 base::string16 title_before_crash;
150 base::string16 title_after_crash;
152 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
153 &title_before_crash));
154 SimulateRendererCrash(browser());
155 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
156 EXPECT_TRUE(content::WaitForLoadStop(GetActiveWebContents()));
157 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
158 &title_after_crash));
159 EXPECT_NE(title_before_crash, title_after_crash);
162 // Tests that loading a crashed page in a new tab correctly updates the title.
163 // There was an earlier bug (1270510) in process-per-site in which the max page
164 // ID of the RenderProcessHost was stale, so the NavigationEntry in the new tab
165 // was not committed. This prevents regression of that bug.
166 // Flaky timeouts on Win7 Tests (dbg)(1); see https://crbug.com/985255.
167 #if defined(OS_WIN) && !defined(NDEBUG)
168 #define MAYBE_LoadInNewTab DISABLED_LoadInNewTab
170 #define MAYBE_LoadInNewTab LoadInNewTab
172 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, MAYBE_LoadInNewTab) {
173 const base::FilePath::CharType kTitle2File[] =
174 FILE_PATH_LITERAL("title2.html");
176 GURL url(ui_test_utils::GetTestUrl(
177 base::FilePath(base::FilePath::kCurrentDirectory),
178 base::FilePath(kTitle2File)));
179 ui_test_utils::NavigateToURL(browser(), url);
181 base::string16 title_before_crash;
182 base::string16 title_after_crash;
184 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
185 &title_before_crash));
186 SimulateRendererCrash(browser());
187 ASSERT_EQ(GURL(content::kChromeUICrashURL),
188 GetActiveWebContents()->GetController().GetVisibleEntry()->
190 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
191 EXPECT_TRUE(content::WaitForLoadStop(GetActiveWebContents()));
192 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
193 &title_after_crash));
194 EXPECT_EQ(title_before_crash, title_after_crash);
197 // Tests that reloads of navigation errors behave correctly after a crash.
198 // Regression test for http://crbug.com/348918
200 // Flaky timeouts on Win7 Tests (dbg)(1); see https://crbug.com/985255.
201 #if defined(OS_WIN) && !defined(NDEBUG)
202 #define MAYBE_DoubleReloadWithError DISABLED_DoubleReloadWithError
204 #define MAYBE_DoubleReloadWithError DoubleReloadWithError
206 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, MAYBE_DoubleReloadWithError) {
207 GURL url(content::GetWebUIURL("bogus"));
208 ui_test_utils::NavigateToURL(browser(), url);
209 ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
211 SimulateRendererCrash(browser());
213 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
214 EXPECT_FALSE(content::WaitForLoadStop(GetActiveWebContents()));
215 ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
217 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
218 EXPECT_FALSE(content::WaitForLoadStop(GetActiveWebContents()));
219 ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
222 // Tests that a beforeunload handler doesn't run if user navigates to
225 // Flaky timeouts on Win7 Tests (dbg)(1); see https://crbug.com/985255.
226 #if defined(OS_WIN) && !defined(NDEBUG)
227 #define MAYBE_BeforeUnloadNotRun DISABLED_BeforeUnloadNotRun
229 #define MAYBE_BeforeUnloadNotRun BeforeUnloadNotRun
231 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, MAYBE_BeforeUnloadNotRun) {
232 const char* kBeforeUnloadHTML =
234 "<script>window.onbeforeunload=function(e){return 'foo'}</script>"
236 GURL url(std::string("data:text/html,") + kBeforeUnloadHTML);
237 ui_test_utils::NavigateToURL(browser(), url);
238 SimulateRendererCrash(browser());