blob: e97b573bab09e25f1c307c097d4ab02272da9472 [file] [log] [blame]
[email protected]3a4f7032009-07-08 20:43:531// Copyright (c) 2009 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.
4
5#include "chrome/browser/browser.h"
6#include "chrome/browser/tab_contents/navigation_entry.h"
7#include "chrome/browser/tab_contents/tab_contents.h"
8#include "chrome/common/notification_registrar.h"
9#include "chrome/common/notification_service.h"
10#include "chrome/common/page_transition_types.h"
[email protected]76543b92009-08-31 17:27:4511#include "chrome/common/url_constants.h"
[email protected]3a4f7032009-07-08 20:43:5312#include "chrome/test/in_process_browser_test.h"
13#include "chrome/test/ui_test_utils.h"
14#include "testing/gtest/include/gtest/gtest.h"
15#include "webkit/glue/window_open_disposition.h"
16
17namespace {
18
19// Used to block until a navigation completes.
20class RendererCrashObserver : public NotificationObserver {
21 public:
22 RendererCrashObserver() {}
23
24 void WaitForRendererCrash() {
25 registrar_.Add(this, NotificationType::TAB_CONTENTS_DISCONNECTED,
26 NotificationService::AllSources());
27 ui_test_utils::RunMessageLoop();
28 }
29
30 virtual void Observe(NotificationType type,
31 const NotificationSource& source,
32 const NotificationDetails& details) {
33 if (type == NotificationType::TAB_CONTENTS_DISCONNECTED) {
34 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DISCONNECTED,
35 NotificationService::AllSources());
36 MessageLoopForUI::current()->Quit();
37 } else {
38 NOTREACHED();
39 }
40 }
41
42 private:
43 NotificationRegistrar registrar_;
44
45 DISALLOW_COPY_AND_ASSIGN(RendererCrashObserver);
46};
47
48void SimulateRendererCrash(Browser* browser) {
[email protected]76543b92009-08-31 17:27:4549 browser->OpenURL(GURL(chrome::kAboutCrashURL), GURL(), CURRENT_TAB,
[email protected]3a4f7032009-07-08 20:43:5350 PageTransition::TYPED);
51 RendererCrashObserver crash_observer;
52 crash_observer.WaitForRendererCrash();
[email protected]3a4f7032009-07-08 20:43:5353}
54
55} // namespace
56
57class CrashRecoveryBrowserTest : public InProcessBrowserTest {
58};
59
60// Test that reload works after a crash.
61IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, Reload) {
62 // The title of the active tab should change each time this URL is loaded.
63 GURL url(
64 "data:text/html,<script>document.title=new Date().valueOf()</script>");
65 ui_test_utils::NavigateToURL(browser(), url);
66
67 string16 title_before_crash;
68 string16 title_after_crash;
69
70 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
71 &title_before_crash));
72 SimulateRendererCrash(browser());
[email protected]b7a20d32009-08-15 00:02:4073 browser()->Reload();
74 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]3a4f7032009-07-08 20:43:5375 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
76 &title_after_crash));
77 EXPECT_NE(title_before_crash, title_after_crash);
78}
79
80// Tests that loading a crashed page in a new tab correctly updates the title.
81// There was an earlier bug (1270510) in process-per-site in which the max page
82// ID of the RenderProcessHost was stale, so the NavigationEntry in the new tab
83// was not committed. This prevents regression of that bug.
84IN_PROC_BROWSER_TEST_F(CrashRecoveryBrowserTest, LoadInNewTab) {
85 ui_test_utils::NavigateToURL(browser(),
86 ui_test_utils::GetTestUrl(L".", L"title2.html"));
87
88 string16 title_before_crash;
89 string16 title_after_crash;
90
91 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
92 &title_before_crash));
93 SimulateRendererCrash(browser());
[email protected]b7a20d32009-08-15 00:02:4094 browser()->Reload();
95 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]3a4f7032009-07-08 20:43:5396 ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(),
97 &title_after_crash));
98 EXPECT_EQ(title_before_crash, title_after_crash);
99}