blob: 2f96e82b66f26c50c754d4ae84e4978e64c3ba78 [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
dchenge73d8520c2015-12-27 01:19:095#include <utility>
6
brettwf1958912015-10-07 19:43:127#include "base/base_switches.h"
[email protected]9c29c4652014-05-22 17:33:528#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
avie4d7b6f2015-12-26 00:59:1810#include "base/macros.h"
[email protected]a8ff8ed2014-01-09 07:32:2611#include "base/strings/stringprintf.h"
avie4d7b6f2015-12-26 00:59:1812#include "build/build_config.h"
[email protected]440348f2010-11-16 00:30:1913#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1014#include "chrome/browser/ui/browser_commands.h"
[email protected]cc872372013-01-28 21:57:0715#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]76543b92009-08-31 17:27:4516#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3217#include "chrome/test/base/in_process_browser_test.h"
18#include "chrome/test/base/ui_test_utils.h"
jam0b18f832016-10-14 19:08:1019#include "content/public/browser/navigation_controller.h"
20#include "content/public/browser/navigation_entry.h"
[email protected]ad50def52011-10-19 23:17:0721#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1622#include "content/public/browser/notification_types.h"
[email protected]83ff91c2012-01-05 20:54:1323#include "content/public/browser/web_contents.h"
[email protected]9a001bc2014-03-12 07:59:2124#include "content/public/test/browser_test_utils.h"
[email protected]a8ff8ed2014-01-09 07:32:2625#include "net/test/embedded_test_server/embedded_test_server.h"
26#include "net/test/embedded_test_server/http_request.h"
27#include "net/test/embedded_test_server/http_response.h"
[email protected]3a4f7032009-07-08 20:43:5328#include "testing/gtest/include/gtest/gtest.h"
Sylvain Defresnec6ccc77d2014-09-19 10:19:3529#include "ui/base/page_transition_types.h"
[email protected]3a4f7032009-07-08 20:43:5330
[email protected]e5d549d2011-12-28 01:29:2031using content::OpenURLParams;
32using content::Referrer;
[email protected]9a001bc2014-03-12 07:59:2133using content::WebContents;
[email protected]e5d549d2011-12-28 01:29:2034
[email protected]bc947b82014-03-18 07:29:1835// TODO(jam): http://crbug.com/350550
36#if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
37
[email protected]3a4f7032009-07-08 20:43:5338namespace {
39
[email protected]3a4f7032009-07-08 20:43:5340void SimulateRendererCrash(Browser* browser) {
[email protected]a7fe9112012-07-20 02:34:4541 content::WindowedNotificationObserver observer(
[email protected]fbc5e5f92012-01-02 06:08:3242 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
[email protected]ad50def52011-10-19 23:17:0743 content::NotificationService::AllSources());
nick3b04f322016-08-31 19:29:1944 browser->OpenURL(OpenURLParams(GURL(content::kChromeUICrashURL), Referrer(),
45 WindowOpenDisposition::CURRENT_TAB,
46 ui::PAGE_TRANSITION_TYPED, false));
[email protected]6b4e8e42011-08-17 19:36:0647 observer.Wait();
[email protected]3a4f7032009-07-08 20:43:5348}
49
[email protected]a8ff8ed2014-01-09 07:32:2650// A request handler which returns a different result each time but stays fresh
51// into the far future.
52class CacheMaxAgeHandler {
53 public:
54 explicit CacheMaxAgeHandler(const std::string& path)
55 : path_(path), request_count_(0) { }
56
dcheng4af48582016-04-19 00:29:3557 std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
[email protected]a8ff8ed2014-01-09 07:32:2658 const net::test_server::HttpRequest& request) {
59 if (request.relative_url != path_)
dcheng4af48582016-04-19 00:29:3560 return std::unique_ptr<net::test_server::HttpResponse>();
[email protected]a8ff8ed2014-01-09 07:32:2661
62 request_count_++;
dcheng4af48582016-04-19 00:29:3563 std::unique_ptr<net::test_server::BasicHttpResponse> response(
[email protected]a8ff8ed2014-01-09 07:32:2664 new net::test_server::BasicHttpResponse);
65 response->set_content(base::StringPrintf("<title>%d</title>",
66 request_count_));
67 response->set_content_type("text/html");
68 response->AddCustomHeader("Cache-Control", "max-age=99999");
dchenge73d8520c2015-12-27 01:19:0969 return std::move(response);
[email protected]a8ff8ed2014-01-09 07:32:2670 }
71 private:
72 std::string path_;
73 int request_count_;
[email protected]a8ff8ed2014-01-09 07:32:2674
[email protected]9c29c4652014-05-22 17:33:5275 DISALLOW_COPY_AND_ASSIGN(CacheMaxAgeHandler);
76};
[email protected]3a4f7032009-07-08 20:43:5377
78class CrashRecoveryBrowserTest : public InProcessBrowserTest {
[email protected]9a001bc2014-03-12 07:59:2179 protected:
80 WebContents* GetActiveWebContents() {
81 return browser()->tab_strip_model()->GetActiveWebContents();
82 }
[email protected]9c29c4652014-05-22 17:33:5283
84 private:
Daniel Chenga542fca2014-10-21 09:51:2985 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]9c29c4652014-05-22 17:33:5286 command_line->AppendSwitch(switches::kDisableBreakpad);
87 }
[email protected]3a4f7032009-07-08 20:43:5388};
89
90// Test that reload works after a crash.
[email protected]4a63e242011-12-12 15:23:0891// Disabled, http://crbug.com/29331 , http://crbug.com/69637 .
[email protected]afbfc9a2011-01-18 18:37:4092IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, Reload) {
[email protected]3a4f7032009-07-08 20:43:5393 // The title of the active tab should change each time this URL is loaded.
94 GURL url(
95 "data:text/html,<script>document.title=new Date().valueOf()</script>");
96 ui_test_utils::NavigateToURL(browser(), url);
97
[email protected]96920152013-12-04 21:00:1698 base::string16 title_before_crash;
99 base::string16 title_after_crash;
[email protected]3a4f7032009-07-08 20:43:53100
101 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
102 &title_before_crash));
103 SimulateRendererCrash(browser());
nick3b04f322016-08-31 19:29:19104 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
[email protected]9a001bc2014-03-12 07:59:21105 content::WaitForLoadStop(GetActiveWebContents());
[email protected]3a4f7032009-07-08 20:43:53106 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
107 &title_after_crash));
108 EXPECT_NE(title_before_crash, title_after_crash);
109}
110
[email protected]a8ff8ed2014-01-09 07:32:26111// Test that reload after a crash forces a cache revalidation.
112IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, ReloadCacheRevalidate) {
113 const char kTestPath[] = "/test";
114
115 // Use the test server so as not to bypass cache behavior. The title of the
116 // active tab should change only when this URL is reloaded.
svaldeza01f7d92015-11-18 17:47:56117 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]a8ff8ed2014-01-09 07:32:26118 embedded_test_server()->RegisterRequestHandler(
119 base::Bind(&CacheMaxAgeHandler::HandleRequest,
120 base::Owned(new CacheMaxAgeHandler(kTestPath))));
121 ui_test_utils::NavigateToURL(browser(),
122 embedded_test_server()->GetURL(kTestPath));
123
124 base::string16 title_before_crash;
125 base::string16 title_after_crash;
126
127 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
128 &title_before_crash));
129 SimulateRendererCrash(browser());
nick3b04f322016-08-31 19:29:19130 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
[email protected]9a001bc2014-03-12 07:59:21131 content::WaitForLoadStop(GetActiveWebContents());
[email protected]a8ff8ed2014-01-09 07:32:26132 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
133 &title_after_crash));
134 EXPECT_NE(title_before_crash, title_after_crash);
135}
136
[email protected]3a4f7032009-07-08 20:43:53137// Tests that loading a crashed page in a new tab correctly updates the title.
138// There was an earlier bug (1270510) in process-per-site in which the max page
139// ID of the RenderProcessHost was stale, so the NavigationEntry in the new tab
140// was not committed. This prevents regression of that bug.
[email protected]82853322012-11-15 18:02:27141IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, LoadInNewTab) {
[email protected]9c29c4652014-05-22 17:33:52142 const base::FilePath::CharType kTitle2File[] =
[email protected]650b2d52013-02-10 03:41:45143 FILE_PATH_LITERAL("title2.html");
[email protected]5a1454362010-03-31 08:56:47144
jam0b18f832016-10-14 19:08:10145 GURL url(ui_test_utils::GetTestUrl(
146 base::FilePath(base::FilePath::kCurrentDirectory),
147 base::FilePath(kTitle2File)));
148 ui_test_utils::NavigateToURL(browser(), url);
[email protected]3a4f7032009-07-08 20:43:53149
[email protected]96920152013-12-04 21:00:16150 base::string16 title_before_crash;
151 base::string16 title_after_crash;
[email protected]3a4f7032009-07-08 20:43:53152
153 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
154 &title_before_crash));
155 SimulateRendererCrash(browser());
jam0b18f832016-10-14 19:08:10156 ASSERT_EQ(GURL(content::kChromeUICrashURL),
157 GetActiveWebContents()->GetController().GetVisibleEntry()->
158 GetVirtualURL());
nick3b04f322016-08-31 19:29:19159 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
[email protected]9a001bc2014-03-12 07:59:21160 content::WaitForLoadStop(GetActiveWebContents());
[email protected]3a4f7032009-07-08 20:43:53161 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
162 &title_after_crash));
163 EXPECT_EQ(title_before_crash, title_after_crash);
164}
[email protected]9a001bc2014-03-12 07:59:21165
166// Tests that reloads of navigation errors behave correctly after a crash.
167// Regression test for http://crbug.com/348918
168IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, DoubleReloadWithError) {
169 GURL url("chrome://bogus");
170 ui_test_utils::NavigateToURL(browser(), url);
171 ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
172
173 SimulateRendererCrash(browser());
174
nick3b04f322016-08-31 19:29:19175 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
[email protected]9a001bc2014-03-12 07:59:21176 content::WaitForLoadStop(GetActiveWebContents());
177 ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
178
nick3b04f322016-08-31 19:29:19179 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
[email protected]9a001bc2014-03-12 07:59:21180 content::WaitForLoadStop(GetActiveWebContents());
181 ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL());
182}
[email protected]bc947b82014-03-18 07:29:18183
jamb618e0bb22016-10-01 05:28:22184// Tests that a beforeunload handler doesn't run if user navigates to
185// chrome::crash.
186IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, BeforeUnloadNotRun) {
187 const char* kBeforeUnloadHTML =
188 "<html><body>"
189 "<script>window.onbeforeunload=function(e){return 'foo'}</script>"
190 "</body></html>";
191 GURL url(std::string("data:text/html,") + kBeforeUnloadHTML);
192 ui_test_utils::NavigateToURL(browser(), url);
193 SimulateRendererCrash(browser());
194}
195
[email protected]9c29c4652014-05-22 17:33:52196} // namespace
197
[email protected]bc947b82014-03-18 07:29:18198#endif