[email protected] | 871dc68 | 2012-06-11 19:35:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 5 | #include <utility> |
| 6 | |
brettw | f195891 | 2015-10-07 19:43:12 | [diff] [blame] | 7 | #include "base/base_switches.h" |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 8 | #include "base/bind.h" |
[email protected] | 9c29c465 | 2014-05-22 17:33:52 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | 440348f | 2010-11-16 00:30:19 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser_commands.h" |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 16 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 17 | #include "chrome/common/url_constants.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 18 | #include "chrome/test/base/in_process_browser_test.h" |
| 19 | #include "chrome/test/base/ui_test_utils.h" |
jam | 0b18f83 | 2016-10-14 19:08:10 | [diff] [blame] | 20 | #include "content/public/browser/navigation_controller.h" |
| 21 | #include "content/public/browser/navigation_entry.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 22 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 23 | #include "content/public/browser/notification_types.h" |
jam | 8e07fc9 | 2017-03-17 00:34:14 | [diff] [blame] | 24 | #include "content/public/browser/render_frame_host.h" |
| 25 | #include "content/public/browser/render_process_host.h" |
| 26 | #include "content/public/browser/render_widget_host_view.h" |
[email protected] | 83ff91c | 2012-01-05 20:54:13 | [diff] [blame] | 27 | #include "content/public/browser/web_contents.h" |
[email protected] | 9a001bc | 2014-03-12 07:59:21 | [diff] [blame] | 28 | #include "content/public/test/browser_test_utils.h" |
Lukasz Anforowicz | 7ea1dbd | 2019-06-12 12:26:24 | [diff] [blame] | 29 | #include "content/public/test/no_renderer_crashes_assertion.h" |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 30 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 31 | #include "net/test/embedded_test_server/http_request.h" |
| 32 | #include "net/test/embedded_test_server/http_response.h" |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 33 | #include "testing/gtest/include/gtest/gtest.h" |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 34 | #include "ui/base/page_transition_types.h" |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 35 | |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 36 | using content::OpenURLParams; |
| 37 | using content::Referrer; |
[email protected] | 9a001bc | 2014-03-12 07:59:21 | [diff] [blame] | 38 | using content::WebContents; |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 39 | |
[email protected] | bc947b8 | 2014-03-18 07:29:18 | [diff] [blame] | 40 | // TODO(jam): http://crbug.com/350550 |
| 41 | #if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER)) |
| 42 | |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 43 | namespace { |
| 44 | |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 45 | void SimulateRendererCrash(Browser* browser) { |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 46 | content::WindowedNotificationObserver observer( |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame] | 47 | content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 48 | content::NotificationService::AllSources()); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 49 | browser->OpenURL(OpenURLParams(GURL(content::kChromeUICrashURL), Referrer(), |
| 50 | WindowOpenDisposition::CURRENT_TAB, |
| 51 | ui::PAGE_TRANSITION_TYPED, false)); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 52 | observer.Wait(); |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 55 | // A request handler which returns a different result each time but stays fresh |
| 56 | // into the far future. |
| 57 | class CacheMaxAgeHandler { |
| 58 | public: |
| 59 | explicit CacheMaxAgeHandler(const std::string& path) |
| 60 | : path_(path), request_count_(0) { } |
| 61 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 62 | std::unique_ptr<net::test_server::HttpResponse> HandleRequest( |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 63 | const net::test_server::HttpRequest& request) { |
| 64 | if (request.relative_url != path_) |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 65 | return std::unique_ptr<net::test_server::HttpResponse>(); |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 66 | |
| 67 | request_count_++; |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 68 | std::unique_ptr<net::test_server::BasicHttpResponse> response( |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 69 | new net::test_server::BasicHttpResponse); |
| 70 | response->set_content(base::StringPrintf("<title>%d</title>", |
| 71 | request_count_)); |
| 72 | response->set_content_type("text/html"); |
| 73 | response->AddCustomHeader("Cache-Control", "max-age=99999"); |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 74 | return std::move(response); |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 75 | } |
| 76 | private: |
| 77 | std::string path_; |
| 78 | int request_count_; |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 79 | |
[email protected] | 9c29c465 | 2014-05-22 17:33:52 | [diff] [blame] | 80 | DISALLOW_COPY_AND_ASSIGN(CacheMaxAgeHandler); |
| 81 | }; |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 82 | |
| 83 | class CrashRecoveryBrowserTest : public InProcessBrowserTest { |
[email protected] | 9a001bc | 2014-03-12 07:59:21 | [diff] [blame] | 84 | protected: |
| 85 | WebContents* GetActiveWebContents() { |
| 86 | return browser()->tab_strip_model()->GetActiveWebContents(); |
| 87 | } |
[email protected] | 9c29c465 | 2014-05-22 17:33:52 | [diff] [blame] | 88 | |
| 89 | private: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 90 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 9c29c465 | 2014-05-22 17:33:52 | [diff] [blame] | 91 | command_line->AppendSwitch(switches::kDisableBreakpad); |
| 92 | } |
Lukasz Anforowicz | 7ea1dbd | 2019-06-12 12:26:24 | [diff] [blame] | 93 | |
| 94 | content::ScopedAllowRendererCrashes scoped_allow_renderer_crashes_; |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | // Test that reload works after a crash. |
Greg Thompson | ce8736b | 2019-07-18 10:52:31 | [diff] [blame] | 98 | // Flaky timeouts on Win7 Tests (dbg)(1); see https://crbug.com/985255. |
| 99 | #if defined(OS_WIN) && !defined(NDEBUG) |
| 100 | #define MAYBE_Reload DISABLED_Reload |
| 101 | #else |
| 102 | #define MAYBE_Reload Reload |
| 103 | #endif |
| 104 | IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, MAYBE_Reload) { |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 105 | // The title of the active tab should change each time this URL is loaded. |
| 106 | GURL url( |
| 107 | "data:text/html,<script>document.title=new Date().valueOf()</script>"); |
| 108 | ui_test_utils::NavigateToURL(browser(), url); |
| 109 | |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 110 | base::string16 title_before_crash; |
| 111 | base::string16 title_after_crash; |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 112 | |
| 113 | ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), |
| 114 | &title_before_crash)); |
| 115 | SimulateRendererCrash(browser()); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 116 | chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB); |
[email protected] | 9a001bc | 2014-03-12 07:59:21 | [diff] [blame] | 117 | content::WaitForLoadStop(GetActiveWebContents()); |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 118 | ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), |
| 119 | &title_after_crash)); |
| 120 | EXPECT_NE(title_before_crash, title_after_crash); |
jam | 8e07fc9 | 2017-03-17 00:34:14 | [diff] [blame] | 121 | ASSERT_TRUE(GetActiveWebContents()->GetMainFrame()->GetView()->IsShowing()); |
Lukasz Anforowicz | 57ade7c | 2017-09-28 20:26:33 | [diff] [blame] | 122 | ASSERT_FALSE(GetActiveWebContents() |
| 123 | ->GetMainFrame() |
| 124 | ->GetProcess() |
| 125 | ->IsProcessBackgrounded()); |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 126 | } |
| 127 | |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 128 | // Test that reload after a crash forces a cache revalidation. |
| 129 | IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, ReloadCacheRevalidate) { |
| 130 | const char kTestPath[] = "/test"; |
| 131 | |
| 132 | // Use the test server so as not to bypass cache behavior. The title of the |
| 133 | // active tab should change only when this URL is reloaded. |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 134 | embedded_test_server()->RegisterRequestHandler( |
| 135 | base::Bind(&CacheMaxAgeHandler::HandleRequest, |
| 136 | base::Owned(new CacheMaxAgeHandler(kTestPath)))); |
martijn | bb41e73 | 2016-11-23 16:55:34 | [diff] [blame] | 137 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 138 | ui_test_utils::NavigateToURL(browser(), |
| 139 | embedded_test_server()->GetURL(kTestPath)); |
| 140 | |
| 141 | base::string16 title_before_crash; |
| 142 | base::string16 title_after_crash; |
| 143 | |
| 144 | ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), |
| 145 | &title_before_crash)); |
| 146 | SimulateRendererCrash(browser()); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 147 | chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB); |
[email protected] | 9a001bc | 2014-03-12 07:59:21 | [diff] [blame] | 148 | content::WaitForLoadStop(GetActiveWebContents()); |
[email protected] | a8ff8ed | 2014-01-09 07:32:26 | [diff] [blame] | 149 | ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), |
| 150 | &title_after_crash)); |
| 151 | EXPECT_NE(title_before_crash, title_after_crash); |
| 152 | } |
| 153 | |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 154 | // Tests that loading a crashed page in a new tab correctly updates the title. |
| 155 | // There was an earlier bug (1270510) in process-per-site in which the max page |
| 156 | // ID of the RenderProcessHost was stale, so the NavigationEntry in the new tab |
| 157 | // was not committed. This prevents regression of that bug. |
Greg Thompson | ce8736b | 2019-07-18 10:52:31 | [diff] [blame] | 158 | // Flaky timeouts on Win7 Tests (dbg)(1); see https://crbug.com/985255. |
| 159 | #if defined(OS_WIN) && !defined(NDEBUG) |
| 160 | #define MAYBE_LoadInNewTab DISABLED_LoadInNewTab |
| 161 | #else |
| 162 | #define MAYBE_LoadInNewTab LoadInNewTab |
| 163 | #endif |
| 164 | IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, MAYBE_LoadInNewTab) { |
[email protected] | 9c29c465 | 2014-05-22 17:33:52 | [diff] [blame] | 165 | const base::FilePath::CharType kTitle2File[] = |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 166 | FILE_PATH_LITERAL("title2.html"); |
[email protected] | 5a145436 | 2010-03-31 08:56:47 | [diff] [blame] | 167 | |
jam | 0b18f83 | 2016-10-14 19:08:10 | [diff] [blame] | 168 | GURL url(ui_test_utils::GetTestUrl( |
| 169 | base::FilePath(base::FilePath::kCurrentDirectory), |
| 170 | base::FilePath(kTitle2File))); |
| 171 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 172 | |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 173 | base::string16 title_before_crash; |
| 174 | base::string16 title_after_crash; |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 175 | |
| 176 | ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), |
| 177 | &title_before_crash)); |
| 178 | SimulateRendererCrash(browser()); |
jam | 0b18f83 | 2016-10-14 19:08:10 | [diff] [blame] | 179 | ASSERT_EQ(GURL(content::kChromeUICrashURL), |
| 180 | GetActiveWebContents()->GetController().GetVisibleEntry()-> |
| 181 | GetVirtualURL()); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 182 | chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB); |
[email protected] | 9a001bc | 2014-03-12 07:59:21 | [diff] [blame] | 183 | content::WaitForLoadStop(GetActiveWebContents()); |
[email protected] | 3a4f703 | 2009-07-08 20:43:53 | [diff] [blame] | 184 | ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), |
| 185 | &title_after_crash)); |
| 186 | EXPECT_EQ(title_before_crash, title_after_crash); |
| 187 | } |
[email protected] | 9a001bc | 2014-03-12 07:59:21 | [diff] [blame] | 188 | |
| 189 | // Tests that reloads of navigation errors behave correctly after a crash. |
| 190 | // Regression test for http://crbug.com/348918 |
| 191 | IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, DoubleReloadWithError) { |
Matt Siembor | 39462b3 | 2019-05-07 20:20:44 | [diff] [blame] | 192 | GURL url(content::GetWebUIURL("bogus")); |
[email protected] | 9a001bc | 2014-03-12 07:59:21 | [diff] [blame] | 193 | ui_test_utils::NavigateToURL(browser(), url); |
| 194 | ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL()); |
| 195 | |
| 196 | SimulateRendererCrash(browser()); |
| 197 | |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 198 | chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB); |
[email protected] | 9a001bc | 2014-03-12 07:59:21 | [diff] [blame] | 199 | content::WaitForLoadStop(GetActiveWebContents()); |
| 200 | ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL()); |
| 201 | |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 202 | chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB); |
[email protected] | 9a001bc | 2014-03-12 07:59:21 | [diff] [blame] | 203 | content::WaitForLoadStop(GetActiveWebContents()); |
| 204 | ASSERT_EQ(url, GetActiveWebContents()->GetVisibleURL()); |
| 205 | } |
[email protected] | bc947b8 | 2014-03-18 07:29:18 | [diff] [blame] | 206 | |
jam | b618e0bb2 | 2016-10-01 05:28:22 | [diff] [blame] | 207 | // Tests that a beforeunload handler doesn't run if user navigates to |
| 208 | // chrome::crash. |
| 209 | IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, BeforeUnloadNotRun) { |
| 210 | const char* kBeforeUnloadHTML = |
| 211 | "<html><body>" |
| 212 | "<script>window.onbeforeunload=function(e){return 'foo'}</script>" |
| 213 | "</body></html>"; |
| 214 | GURL url(std::string("data:text/html,") + kBeforeUnloadHTML); |
| 215 | ui_test_utils::NavigateToURL(browser(), url); |
| 216 | SimulateRendererCrash(browser()); |
| 217 | } |
| 218 | |
[email protected] | 9c29c465 | 2014-05-22 17:33:52 | [diff] [blame] | 219 | } // namespace |
| 220 | |
[email protected] | bc947b8 | 2014-03-18 07:29:18 | [diff] [blame] | 221 | #endif |