Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / startup / session_crashed_infobar_delegate_unittest.cc
blobd43d42576b07c005cb6c053e07a979ba79c4ebe1
1 // Copyright (c) 2013 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.
5 #include "chrome/browser/ui/startup/session_crashed_infobar_delegate.h"
7 #include "base/prefs/pref_registry_simple.h"
8 #include "base/prefs/testing_pref_service.h"
9 #include "base/run_loop.h"
10 #include "chrome/browser/infobars/infobar.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/sessions/session_service_factory.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/common/url_constants.h"
16 #include "chrome/test/base/browser_with_test_window_test.h"
17 #include "chrome/test/base/scoped_testing_local_state.h"
18 #include "chrome/test/base/testing_browser_process.h"
20 class SessionCrashedInfoBarDelegateUnitTest : public BrowserWithTestWindowTest {
21 public:
22 virtual void SetUp() OVERRIDE {
23 pref_service.registry()
24 ->RegisterBooleanPref(prefs::kAllowFileSelectionDialogs, true);
25 pref_service.registry()->RegisterBooleanPref(prefs::kWasRestarted, false);
26 static_cast<TestingBrowserProcess*>(g_browser_process)
27 ->SetLocalState(&pref_service);
28 // This needs to be called after the local state is set, because it will
29 // create a browser which will try to read from the local state.
30 BrowserWithTestWindowTest::SetUp();
33 virtual void TearDown() OVERRIDE {
34 static_cast<TestingBrowserProcess*>(g_browser_process)->SetLocalState(NULL);
35 BrowserWithTestWindowTest::TearDown();
38 private:
39 TestingPrefServiceSimple pref_service;
42 TEST_F(SessionCrashedInfoBarDelegateUnitTest, DetachingTabWithCrashedInfoBar) {
43 SessionServiceFactory::SetForTestProfile(
44 browser()->profile(),
45 static_cast<SessionService*>(
46 SessionServiceFactory::GetInstance()->BuildServiceInstanceFor(
47 browser()->profile())));
49 // Create a browser which we can close during the test.
50 Browser::CreateParams params(browser()->profile(),
51 browser()->host_desktop_type());
52 scoped_ptr<Browser> first_browser(
53 chrome::CreateBrowserWithTestWindowForParams(&params));
54 AddTab(first_browser.get(), GURL(chrome::kChromeUINewTabURL));
56 // Attach the crashed infobar to it.
57 SessionCrashedInfoBarDelegate::Create(first_browser.get());
59 TabStripModel* tab_strip = first_browser->tab_strip_model();
60 ASSERT_EQ(1, tab_strip->count());
61 content::WebContents* web_contents = tab_strip->GetWebContentsAt(0);
62 InfoBarService* infobar_service =
63 InfoBarService::FromWebContents(web_contents);
64 EXPECT_EQ(1U, infobar_service->infobar_count());
65 ConfirmInfoBarDelegate* infobar =
66 infobar_service->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
67 ASSERT_TRUE(infobar);
69 // Open another browser.
70 scoped_ptr<Browser> opened_browser(
71 chrome::CreateBrowserWithTestWindowForParams(&params));
73 // Move the tab which is destroying the crash info bar to the new browser.
74 tab_strip->DetachWebContentsAt(0);
75 tab_strip = opened_browser->tab_strip_model();
76 tab_strip->AppendWebContents(web_contents, true);
78 // Close the original browser.
79 first_browser->window()->Close();
80 first_browser.reset();
82 ASSERT_EQ(1, tab_strip->count());
83 web_contents = tab_strip->GetWebContentsAt(0);
84 infobar_service = InfoBarService::FromWebContents(web_contents);
85 EXPECT_EQ(1U, infobar_service->infobar_count());
87 // This used to crash.
88 infobar->Accept();
90 // Ramp down the test.
91 tab_strip->CloseWebContentsAt(0, TabStripModel::CLOSE_NONE);