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