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.
7 #include "base/message_loop/message_loop.h"
8 #include "chrome/browser/renderer_host/web_cache_manager.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/result_codes.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using content::WebContents
;
21 class WebCacheManagerBrowserTest
: public InProcessBrowserTest
{
24 // Regression test for http://crbug.com/12362. If a renderer crashes and the
25 // user navigates to another tab and back, the browser doesn't crash.
26 IN_PROC_BROWSER_TEST_F(WebCacheManagerBrowserTest
, CrashOnceOnly
) {
27 const base::FilePath
kTestDir(FILE_PATH_LITERAL("google"));
28 const base::FilePath
kTestFile(FILE_PATH_LITERAL("google.html"));
29 GURL
url(ui_test_utils::GetTestUrl(kTestDir
, kTestFile
));
31 ui_test_utils::NavigateToURL(browser(), url
);
33 chrome::NewTab(browser());
34 ui_test_utils::NavigateToURL(browser(), url
);
36 WebContents
* tab
= browser()->tab_strip_model()->GetWebContentsAt(0);
37 ASSERT_TRUE(tab
!= NULL
);
38 base::KillProcess(tab
->GetRenderProcessHost()->GetHandle(),
39 content::RESULT_CODE_KILLED
, true);
41 browser()->tab_strip_model()->ActivateTabAt(0, true);
42 chrome::NewTab(browser());
43 ui_test_utils::NavigateToURL(browser(), url
);
45 browser()->tab_strip_model()->ActivateTabAt(0, true);
46 chrome::NewTab(browser());
47 ui_test_utils::NavigateToURL(browser(), url
);
49 // We would have crashed at the above line with the bug.
51 browser()->tab_strip_model()->ActivateTabAt(0, true);
52 chrome::CloseTab(browser());
53 browser()->tab_strip_model()->ActivateTabAt(0, true);
54 chrome::CloseTab(browser());
55 browser()->tab_strip_model()->ActivateTabAt(0, true);
56 chrome::CloseTab(browser());
58 ui_test_utils::NavigateToURL(browser(), url
);
60 // Depending on the speed of execution of the unload event, we may have one or
61 // two active renderers at that point (one executing the unload event in
63 EXPECT_GE(WebCacheManager::GetInstance()->active_renderers_
.size(), 1U);
64 EXPECT_LE(WebCacheManager::GetInstance()->active_renderers_
.size(), 2U);
66 WebCacheManager::GetInstance()->inactive_renderers_
.size(), 0U);
67 EXPECT_GE(WebCacheManager::GetInstance()->stats_
.size(), 1U);
68 EXPECT_LE(WebCacheManager::GetInstance()->stats_
.size(), 2U);