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 "net/test/embedded_test_server/embedded_test_server.h"
18 #include "net/test/embedded_test_server/http_request.h"
19 #include "net/test/embedded_test_server/http_response.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using content::NavigationController
;
23 using content::OpenURLParams
;
24 using content::Referrer
;
28 void SimulateRendererCrash(Browser
* browser
) {
29 content::WindowedNotificationObserver
observer(
30 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED
,
31 content::NotificationService::AllSources());
32 browser
->OpenURL(OpenURLParams(
33 GURL(content::kChromeUICrashURL
), Referrer(), CURRENT_TAB
,
34 content::PAGE_TRANSITION_TYPED
, false));
38 // A request handler which returns a different result each time but stays fresh
39 // into the far future.
40 class CacheMaxAgeHandler
{
42 explicit CacheMaxAgeHandler(const std::string
& path
)
43 : path_(path
), request_count_(0) { }
45 scoped_ptr
<net::test_server::HttpResponse
> HandleRequest(
46 const net::test_server::HttpRequest
& request
) {
47 if (request
.relative_url
!= path_
)
48 return scoped_ptr
<net::test_server::HttpResponse
>();
51 scoped_ptr
<net::test_server::BasicHttpResponse
> response(
52 new net::test_server::BasicHttpResponse
);
53 response
->set_content(base::StringPrintf("<title>%d</title>",
55 response
->set_content_type("text/html");
56 response
->AddCustomHeader("Cache-Control", "max-age=99999");
57 return response
.PassAs
<net::test_server::HttpResponse
>();
66 class CrashRecoveryBrowserTest
: public InProcessBrowserTest
{
69 // Test that reload works after a crash.
70 // Disabled, http://crbug.com/29331 , http://crbug.com/69637 .
71 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, Reload
) {
72 // The title of the active tab should change each time this URL is loaded.
74 "data:text/html,<script>document.title=new Date().valueOf()</script>");
75 ui_test_utils::NavigateToURL(browser(), url
);
77 base::string16 title_before_crash
;
78 base::string16 title_after_crash
;
80 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
81 &title_before_crash
));
82 SimulateRendererCrash(browser());
83 content::WindowedNotificationObserver
observer(
84 content::NOTIFICATION_LOAD_STOP
,
85 content::Source
<NavigationController
>(
86 &browser()->tab_strip_model()->GetActiveWebContents()->
88 chrome::Reload(browser(), CURRENT_TAB
);
90 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
92 EXPECT_NE(title_before_crash
, title_after_crash
);
95 // Test that reload after a crash forces a cache revalidation.
96 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, ReloadCacheRevalidate
) {
97 const char kTestPath
[] = "/test";
99 // Use the test server so as not to bypass cache behavior. The title of the
100 // active tab should change only when this URL is reloaded.
101 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
102 embedded_test_server()->RegisterRequestHandler(
103 base::Bind(&CacheMaxAgeHandler::HandleRequest
,
104 base::Owned(new CacheMaxAgeHandler(kTestPath
))));
105 ui_test_utils::NavigateToURL(browser(),
106 embedded_test_server()->GetURL(kTestPath
));
108 base::string16 title_before_crash
;
109 base::string16 title_after_crash
;
111 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
112 &title_before_crash
));
113 SimulateRendererCrash(browser());
114 content::WindowedNotificationObserver
observer(
115 content::NOTIFICATION_LOAD_STOP
,
116 content::Source
<NavigationController
>(
117 &browser()->tab_strip_model()->GetActiveWebContents()->
119 chrome::Reload(browser(), CURRENT_TAB
);
121 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
122 &title_after_crash
));
123 EXPECT_NE(title_before_crash
, title_after_crash
);
126 // Tests that loading a crashed page in a new tab correctly updates the title.
127 // There was an earlier bug (1270510) in process-per-site in which the max page
128 // ID of the RenderProcessHost was stale, so the NavigationEntry in the new tab
129 // was not committed. This prevents regression of that bug.
130 IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest
, LoadInNewTab
) {
131 const base::FilePath::CharType
* kTitle2File
=
132 FILE_PATH_LITERAL("title2.html");
134 ui_test_utils::NavigateToURL(
135 browser(), ui_test_utils::GetTestUrl(
136 base::FilePath(base::FilePath::kCurrentDirectory
),
137 base::FilePath(kTitle2File
)));
139 base::string16 title_before_crash
;
140 base::string16 title_after_crash
;
142 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
143 &title_before_crash
));
144 SimulateRendererCrash(browser());
145 content::WindowedNotificationObserver
observer(
146 content::NOTIFICATION_LOAD_STOP
,
147 content::Source
<NavigationController
>(
148 &browser()->tab_strip_model()->GetActiveWebContents()->
150 chrome::Reload(browser(), CURRENT_TAB
);
152 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
153 &title_after_crash
));
154 EXPECT_EQ(title_before_crash
, title_after_crash
);