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/files/file_path.h"
6 #include "base/strings/stringprintf.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/url_constants.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/notification_types.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/page_transition_types.h"
17 #include "content/public/test/browser_test_utils.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
19 #include "net/test/embedded_test_server/http_request.h"
20 #include "net/test/embedded_test_server/http_response.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using content::NavigationController
;
24 using content::OpenURLParams
;
25 using content::Referrer
;
26 using content::WebContents
;
28 // TODO(jam): http://crbug.com/350550
29 #if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
33 void SimulateRendererCrash(Browser
* browser
) {
34 content::WindowedNotificationObserver
observer(
35 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED
,
36 content::NotificationService::AllSources());
37 browser
->OpenURL(OpenURLParams(
38 GURL(content::kChromeUICrashURL
), Referrer(), CURRENT_TAB
,
39 content::PAGE_TRANSITION_TYPED
, false));
43 // A request handler which returns a different result each time but stays fresh
44 // into the far future.
45 class CacheMaxAgeHandler
{
47 explicit CacheMaxAgeHandler(const std::string
& path
)
48 : path_(path
), request_count_(0) { }
50 scoped_ptr
<net::test_server::HttpResponse
> HandleRequest(
51 const net::test_server::HttpRequest
& request
) {
52 if (request
.relative_url
!= path_
)
53 return scoped_ptr
<net::test_server::HttpResponse
>();
56 scoped_ptr
<net::test_server::BasicHttpResponse
> response(
57 new net::test_server::BasicHttpResponse
);
58 response
->set_content(base::StringPrintf("<title>%d</title>",
60 response
->set_content_type("text/html");
61 response
->AddCustomHeader("Cache-Control", "max-age=99999");
62 return response
.PassAs
<net::test_server::HttpResponse
>();
71 class CrashRecoveryBrowserTest
: public InProcessBrowserTest
{
73 WebContents
* GetActiveWebContents() {
74 return browser()->tab_strip_model()->GetActiveWebContents();
78 // Test that reload works after a crash.
79 // Disabled, http://crbug.com/29331 , http://crbug.com/69637 .
80 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, Reload
) {
81 // The title of the active tab should change each time this URL is loaded.
83 "data:text/html,<script>document.title=new Date().valueOf()</script>");
84 ui_test_utils::NavigateToURL(browser(), url
);
86 base::string16 title_before_crash
;
87 base::string16 title_after_crash
;
89 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
90 &title_before_crash
));
91 SimulateRendererCrash(browser());
92 chrome::Reload(browser(), CURRENT_TAB
);
93 content::WaitForLoadStop(GetActiveWebContents());
94 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
96 EXPECT_NE(title_before_crash
, title_after_crash
);
99 // Test that reload after a crash forces a cache revalidation.
100 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, ReloadCacheRevalidate
) {
101 const char kTestPath
[] = "/test";
103 // Use the test server so as not to bypass cache behavior. The title of the
104 // active tab should change only when this URL is reloaded.
105 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
106 embedded_test_server()->RegisterRequestHandler(
107 base::Bind(&CacheMaxAgeHandler::HandleRequest
,
108 base::Owned(new CacheMaxAgeHandler(kTestPath
))));
109 ui_test_utils::NavigateToURL(browser(),
110 embedded_test_server()->GetURL(kTestPath
));
112 base::string16 title_before_crash
;
113 base::string16 title_after_crash
;
115 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
116 &title_before_crash
));
117 SimulateRendererCrash(browser());
118 chrome::Reload(browser(), CURRENT_TAB
);
119 content::WaitForLoadStop(GetActiveWebContents());
120 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
121 &title_after_crash
));
122 EXPECT_NE(title_before_crash
, title_after_crash
);
125 // Tests that loading a crashed page in a new tab correctly updates the title.
126 // There was an earlier bug (1270510) in process-per-site in which the max page
127 // ID of the RenderProcessHost was stale, so the NavigationEntry in the new tab
128 // was not committed. This prevents regression of that bug.
129 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, LoadInNewTab
) {
130 const base::FilePath::CharType
* kTitle2File
=
131 FILE_PATH_LITERAL("title2.html");
133 ui_test_utils::NavigateToURL(
134 browser(), ui_test_utils::GetTestUrl(
135 base::FilePath(base::FilePath::kCurrentDirectory
),
136 base::FilePath(kTitle2File
)));
138 base::string16 title_before_crash
;
139 base::string16 title_after_crash
;
141 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
142 &title_before_crash
));
143 SimulateRendererCrash(browser());
144 chrome::Reload(browser(), CURRENT_TAB
);
145 content::WaitForLoadStop(GetActiveWebContents());
146 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
147 &title_after_crash
));
148 EXPECT_EQ(title_before_crash
, title_after_crash
);
151 // Tests that reloads of navigation errors behave correctly after a crash.
152 // Regression test for http://crbug.com/348918
153 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, DoubleReloadWithError
) {
154 GURL
url("chrome://bogus");
155 ui_test_utils::NavigateToURL(browser(), url
);
156 ASSERT_EQ(url
, GetActiveWebContents()->GetVisibleURL());
158 SimulateRendererCrash(browser());
160 chrome::Reload(browser(), CURRENT_TAB
);
161 content::WaitForLoadStop(GetActiveWebContents());
162 ASSERT_EQ(url
, GetActiveWebContents()->GetVisibleURL());
164 chrome::Reload(browser(), CURRENT_TAB
);
165 content::WaitForLoadStop(GetActiveWebContents());
166 ASSERT_EQ(url
, GetActiveWebContents()->GetVisibleURL());