blob: 36ce51f47d94b2b6516b0a273966d3b7f2664d26 [file] [log] [blame]
[email protected]871dc682012-06-11 19:35:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]3a4f7032009-07-08 20:43:532// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettwf1958912015-10-07 19:43:125#include "base/base_switches.h"
[email protected]9c29c4652014-05-22 17:33:526#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:527#include "base/files/file_path.h"
[email protected]a8ff8ed2014-01-09 07:32:268#include "base/strings/stringprintf.h"
[email protected]440348f2010-11-16 00:30:199#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1010#include "chrome/browser/ui/browser_commands.h"
[email protected]cc872372013-01-28 21:57:0711#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]76543b92009-08-31 17:27:4512#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3213#include "chrome/test/base/in_process_browser_test.h"
14#include "chrome/test/base/ui_test_utils.h"
[email protected]ad50def52011-10-19 23:17:0715#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1616#include "content/public/browser/notification_types.h"
[email protected]83ff91c2012-01-05 20:54:1317#include "content/public/browser/web_contents.h"
[email protected]9a001bc2014-03-12 07:59:2118#include "content/public/test/browser_test_utils.h"
[email protected]a8ff8ed2014-01-09 07:32:2619#include "net/test/embedded_test_server/embedded_test_server.h"
20#include "net/test/embedded_test_server/http_request.h"
21#include "net/test/embedded_test_server/http_response.h"
[email protected]3a4f7032009-07-08 20:43:5322#include "testing/gtest/include/gtest/gtest.h"
Sylvain Defresnec6ccc77d2014-09-19 10:19:3523#include "ui/base/page_transition_types.h"
[email protected]3a4f7032009-07-08 20:43:5324
[email protected]e5d549d2011-12-28 01:29:2025using content::OpenURLParams;
26using content::Referrer;
[email protected]9a001bc2014-03-12 07:59:2127using content::WebContents;
[email protected]e5d549d2011-12-28 01:29:2028
[email protected]bc947b82014-03-18 07:29:1829// TODO(jam): http://crbug.com/350550
30#if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
31
[email protected]3a4f7032009-07-08 20:43:5332namespace {
33
[email protected]3a4f7032009-07-08 20:43:5334void SimulateRendererCrash(Browser* browser) {
[email protected]a7fe9112012-07-20 02:34:4535 content::WindowedNotificationObserver observer(
[email protected]fbc5e5f92012-01-02 06:08:3236 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
[email protected]ad50def52011-10-19 23:17:0737 content::NotificationService::AllSources());
[email protected]e5d549d2011-12-28 01:29:2038 browser->OpenURL(OpenURLParams(
[email protected]f8a6d732013-03-02 22:46:0339 GURL(content::kChromeUICrashURL), Referrer(), CURRENT_TAB,
Sylvain Defresnec6ccc77d2014-09-19 10:19:3540 ui::PAGE_TRANSITION_TYPED, false));
[email protected]6b4e8e42011-08-17 19:36:0641 observer.Wait();
[email protected]3a4f7032009-07-08 20:43:5342}
43
[email protected]a8ff8ed2014-01-09 07:32:2644// A request handler which returns a different result each time but stays fresh
45// into the far future.
46class CacheMaxAgeHandler {
47 public:
48 explicit CacheMaxAgeHandler(const std::string& path)
49 : path_(path), request_count_(0) { }
50
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>();
55
56 request_count_++;
57 scoped_ptr<net::test_server::BasicHttpResponse> response(
58 new net::test_server::BasicHttpResponse);
59 response->set_content(base::StringPrintf("<title>%d</title>",
60 request_count_));
61 response->set_content_type("text/html");
62 response->AddCustomHeader("Cache-Control", "max-age=99999");
dcheng383ba8a2014-10-16 23:55:1963 return response.Pass();
[email protected]a8ff8ed2014-01-09 07:32:2664 }
65 private:
66 std::string path_;
67 int request_count_;
[email protected]a8ff8ed2014-01-09 07:32:2668
[email protected]9c29c4652014-05-22 17:33:5269 DISALLOW_COPY_AND_ASSIGN(CacheMaxAgeHandler);
70};
[email protected]3a4f7032009-07-08 20:43:5371
72class CrashRecoveryBrowserTest : public InProcessBrowserTest {
[email protected]9a001bc2014-03-12 07:59:2173 protected:
74 WebContents* GetActiveWebContents() {
75 return browser()->tab_strip_model()->GetActiveWebContents();
76 }
[email protected]9c29c4652014-05-22 17:33:5277
78 private:
Daniel Chenga542fca2014-10-21 09:51:2979 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]9c29c4652014-05-22 17:33:5280 command_line->AppendSwitch(switches::kDisableBreakpad);
81 }
[email protected]3a4f7032009-07-08 20:43:5382};
83
84// Test that reload works after a crash.
[email protected]4a63e242011-12-12 15:23:0885// Disabled, http://crbug.com/29331 , http://crbug.com/69637 .
[email protected]afbfc9a2011-01-18 18:37:4086IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, Reload) {
[email protected]3a4f7032009-07-08 20:43:5387 // The title of the active tab should change each time this URL is loaded.
88 GURL url(
89 "data:text/html,<script>document.title=new Date().valueOf()</script>");
90 ui_test_utils::NavigateToURL(browser(), url);
91
[email protected]96920152013-12-04 21:00:1692 base::string16 title_before_crash;
93 base::string16 title_after_crash;
[email protected]3a4f7032009-07-08 20:43:5394
95 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
96 &title_before_crash));
97 SimulateRendererCrash(browser());
[email protected]a37d4b02012-06-25 21:56:1098 chrome::Reload(browser(), CURRENT_TAB);
[email protected]9a001bc2014-03-12 07:59:2199 content::WaitForLoadStop(GetActiveWebContents());
[email protected]3a4f7032009-07-08 20:43:53100 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
101 &title_after_crash));
102 EXPECT_NE(title_before_crash, title_after_crash);
103}
104
[email protected]a8ff8ed2014-01-09 07:32:26105// Test that reload after a crash forces a cache revalidation.
106IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, ReloadCacheRevalidate) {
107 const char kTestPath[] = "/test";
108
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));
117
118 base::string16 title_before_crash;
119 base::string16 title_after_crash;
120
121 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
122 &title_before_crash));
123 SimulateRendererCrash(browser());
[email protected]a8ff8ed2014-01-09 07:32:26124 chrome::Reload(browser(), CURRENT_TAB);
[email protected]9a001bc2014-03-12 07:59:21125 content::WaitForLoadStop(GetActiveWebContents());
[email protected]a8ff8ed2014-01-09 07:32:26126 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
127 &title_after_crash));
128 EXPECT_NE(title_before_crash, title_after_crash);
129}
130
[email protected]3a4f7032009-07-08 20:43:53131// 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.
[email protected]82853322012-11-15 18:02:27135IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, LoadInNewTab) {
[email protected]9c29c4652014-05-22 17:33:52136 const base::FilePath::CharType kTitle2File[] =
[email protected]650b2d52013-02-10 03:41:45137 FILE_PATH_LITERAL("title2.html");
[email protected]5a1454362010-03-31 08:56:47138
[email protected]650b2d52013-02-10 03:41:45139 ui_test_utils::NavigateToURL(
140 browser(), ui_test_utils::GetTestUrl(
141 base::FilePath(base::FilePath::kCurrentDirectory),
142 base::FilePath(kTitle2File)));
[email protected]3a4f7032009-07-08 20:43:53143
[email protected]96920152013-12-04 21:00:16144 base::string16 title_before_crash;
145 base::string16 title_after_crash;
[email protected]3a4f7032009-07-08 20:43:53146
147 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
148 &title_before_crash));
149 SimulateRendererCrash(browser());
[email protected]a37d4b02012-06-25 21:56:10150 chrome::Reload(browser(), CURRENT_TAB);
[email protected]9a001bc2014-03-12 07:59:21151 content::WaitForLoadStop(GetActiveWebContents());
[email protected]3a4f7032009-07-08 20:43:53152 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
153 &title_after_crash));
154 EXPECT_EQ(title_before_crash, title_after_crash);
155}
[email protected]9a001bc2014-03-12 07:59:21156
157// Tests that reloads of navigation errors behave correctly after a crash.
158// Regression test for http://crbug.com/348918
159IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, DoubleReloadWithError) {
160 GURL url("chrome://bogus");
161 ui_test_utils::NavigateToURL(browser(), url);
162 ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
163
164 SimulateRendererCrash(browser());
165
166 chrome::Reload(browser(), CURRENT_TAB);
167 content::WaitForLoadStop(GetActiveWebContents());
168 ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
169
170 chrome::Reload(browser(), CURRENT_TAB);
171 content::WaitForLoadStop(GetActiveWebContents());
172 ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
173}
[email protected]bc947b82014-03-18 07:29:18174
[email protected]9c29c4652014-05-22 17:33:52175} // namespace
176
[email protected]bc947b82014-03-18 07:29:18177#endif