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/files/file_path.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_commands.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/url_constants.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/notification_service.h"
16 #include "content/public/browser/notification_types.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/page_transition_types.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
21 #include "net/test/embedded_test_server/http_request.h"
22 #include "net/test/embedded_test_server/http_response.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using content::OpenURLParams
;
26 using content::Referrer
;
27 using content::WebContents
;
29 // TODO(jam): http://crbug.com/350550
30 #if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
34 void SimulateRendererCrash(Browser
* browser
) {
35 content::WindowedNotificationObserver
observer(
36 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED
,
37 content::NotificationService::AllSources());
38 browser
->OpenURL(OpenURLParams(
39 GURL(content::kChromeUICrashURL
), Referrer(), CURRENT_TAB
,
40 content::PAGE_TRANSITION_TYPED
, false));
44 // A request handler which returns a different result each time but stays fresh
45 // into the far future.
46 class CacheMaxAgeHandler
{
48 explicit CacheMaxAgeHandler(const std::string
& path
)
49 : path_(path
), request_count_(0) { }
51 scoped_ptr
<net::test_server::HttpResponse
> HandleRequest(
52 const net::test_server::HttpRequest
& request
) {
53 if (request
.relative_url
!= path_
)
54 return scoped_ptr
<net::test_server::HttpResponse
>();
57 scoped_ptr
<net::test_server::BasicHttpResponse
> response(
58 new net::test_server::BasicHttpResponse
);
59 response
->set_content(base::StringPrintf("<title>%d</title>",
61 response
->set_content_type("text/html");
62 response
->AddCustomHeader("Cache-Control", "max-age=99999");
63 return response
.PassAs
<net::test_server::HttpResponse
>();
69 DISALLOW_COPY_AND_ASSIGN(CacheMaxAgeHandler
);
72 class CrashRecoveryBrowserTest
: public InProcessBrowserTest
{
74 WebContents
* GetActiveWebContents() {
75 return browser()->tab_strip_model()->GetActiveWebContents();
79 virtual void SetUpCommandLine(base::CommandLine
* command_line
) OVERRIDE
{
80 command_line
->AppendSwitch(switches::kDisableBreakpad
);
84 // Test that reload works after a crash.
85 // Disabled, http://crbug.com/29331 , http://crbug.com/69637 .
86 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, Reload
) {
87 // The title of the active tab should change each time this URL is loaded.
89 "data:text/html,<script>document.title=new Date().valueOf()</script>");
90 ui_test_utils::NavigateToURL(browser(), url
);
92 base::string16 title_before_crash
;
93 base::string16 title_after_crash
;
95 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
96 &title_before_crash
));
97 SimulateRendererCrash(browser());
98 chrome::Reload(browser(), CURRENT_TAB
);
99 content::WaitForLoadStop(GetActiveWebContents());
100 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
101 &title_after_crash
));
102 EXPECT_NE(title_before_crash
, title_after_crash
);
105 // Test that reload after a crash forces a cache revalidation.
106 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, ReloadCacheRevalidate
) {
107 const char kTestPath
[] = "/test";
109 // Use the test server so as not to bypass cache behavior. The title of the
110 // active tab should change only when this URL is reloaded.
111 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
112 embedded_test_server()->RegisterRequestHandler(
113 base::Bind(&CacheMaxAgeHandler::HandleRequest
,
114 base::Owned(new CacheMaxAgeHandler(kTestPath
))));
115 ui_test_utils::NavigateToURL(browser(),
116 embedded_test_server()->GetURL(kTestPath
));
118 base::string16 title_before_crash
;
119 base::string16 title_after_crash
;
121 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
122 &title_before_crash
));
123 SimulateRendererCrash(browser());
124 chrome::Reload(browser(), CURRENT_TAB
);
125 content::WaitForLoadStop(GetActiveWebContents());
126 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
127 &title_after_crash
));
128 EXPECT_NE(title_before_crash
, title_after_crash
);
131 // Tests that loading a crashed page in a new tab correctly updates the title.
132 // There was an earlier bug (1270510) in process-per-site in which the max page
133 // ID of the RenderProcessHost was stale, so the NavigationEntry in the new tab
134 // was not committed. This prevents regression of that bug.
135 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, LoadInNewTab
) {
136 const base::FilePath::CharType kTitle2File
[] =
137 FILE_PATH_LITERAL("title2.html");
139 ui_test_utils::NavigateToURL(
140 browser(), ui_test_utils::GetTestUrl(
141 base::FilePath(base::FilePath::kCurrentDirectory
),
142 base::FilePath(kTitle2File
)));
144 base::string16 title_before_crash
;
145 base::string16 title_after_crash
;
147 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
148 &title_before_crash
));
149 SimulateRendererCrash(browser());
150 chrome::Reload(browser(), CURRENT_TAB
);
151 content::WaitForLoadStop(GetActiveWebContents());
152 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
153 &title_after_crash
));
154 EXPECT_EQ(title_before_crash
, title_after_crash
);
157 // Tests that reloads of navigation errors behave correctly after a crash.
158 // Regression test for http://crbug.com/348918
159 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, DoubleReloadWithError
) {
160 GURL
url("chrome://bogus");
161 ui_test_utils::NavigateToURL(browser(), url
);
162 ASSERT_EQ(url
, GetActiveWebContents()->GetVisibleURL());
164 SimulateRendererCrash(browser());
166 chrome::Reload(browser(), CURRENT_TAB
);
167 content::WaitForLoadStop(GetActiveWebContents());
168 ASSERT_EQ(url
, GetActiveWebContents()->GetVisibleURL());
170 chrome::Reload(browser(), CURRENT_TAB
);
171 content::WaitForLoadStop(GetActiveWebContents());
172 ASSERT_EQ(url
, GetActiveWebContents()->GetVisibleURL());