session_crashed_infobar_delegate_unittest.cc revision e5d81f57cb97b3b6b7fccc9c5610d21eb81db09d
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 "chrome/browser/ui/startup/session_crashed_infobar_delegate.h" 6 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_manager.h" 12#include "chrome/browser/infobars/infobar_service.h" 13#include "chrome/browser/prefs/browser_prefs.h" 14#include "chrome/browser/sessions/session_service_factory.h" 15#include "chrome/browser/ui/tabs/tab_strip_model.h" 16#include "chrome/common/pref_names.h" 17#include "chrome/common/url_constants.h" 18#include "chrome/test/base/browser_with_test_window_test.h" 19#include "chrome/test/base/scoped_testing_local_state.h" 20#include "chrome/test/base/testing_browser_process.h" 21 22#if defined(OS_CHROMEOS) 23#include "base/command_line.h" 24#include "chrome/common/chrome_switches.h" 25#endif 26 27#if defined(OS_CHROMEOS) 28// TODO(nkostylev): Cleanup this code once multi-profiles are enabled by 29// default on CrOS. http://crbug.com/351655 30class SessionCrashedInfoBarDelegateUnitTest : 31 public BrowserWithTestWindowTest, 32 public testing::WithParamInterface<bool> { 33#else 34class SessionCrashedInfoBarDelegateUnitTest : public BrowserWithTestWindowTest { 35#endif 36 public: 37 virtual void SetUp() OVERRIDE { 38 static_cast<TestingBrowserProcess*>(g_browser_process) 39 ->SetLocalState(&pref_service); 40 chrome::RegisterLocalState(pref_service.registry()); 41 42#if defined(OS_CHROMEOS) 43 if (GetParam()) 44 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kMultiProfiles); 45#endif 46 47 // This needs to be called after the local state is set, because it will 48 // create a browser which will try to read from the local state. 49 BrowserWithTestWindowTest::SetUp(); 50 } 51 52 virtual void TearDown() OVERRIDE { 53 static_cast<TestingBrowserProcess*>(g_browser_process)->SetLocalState(NULL); 54 BrowserWithTestWindowTest::TearDown(); 55 } 56 57 private: 58 TestingPrefServiceSimple pref_service; 59}; 60 61#if defined(OS_CHROMEOS) 62TEST_P(SessionCrashedInfoBarDelegateUnitTest, DetachingTabWithCrashedInfoBar) { 63#else 64TEST_F(SessionCrashedInfoBarDelegateUnitTest, DetachingTabWithCrashedInfoBar) { 65#endif 66 SessionServiceFactory::SetForTestProfile( 67 browser()->profile(), 68 static_cast<SessionService*>( 69 SessionServiceFactory::GetInstance()->BuildServiceInstanceFor( 70 browser()->profile()))); 71 72 // Create a browser which we can close during the test. 73 Browser::CreateParams params(browser()->profile(), 74 browser()->host_desktop_type()); 75 scoped_ptr<Browser> first_browser( 76 chrome::CreateBrowserWithTestWindowForParams(¶ms)); 77 AddTab(first_browser.get(), GURL(chrome::kChromeUINewTabURL)); 78 79 // Attach the crashed infobar to it. 80 SessionCrashedInfoBarDelegate::Create(first_browser.get()); 81 82 TabStripModel* tab_strip = first_browser->tab_strip_model(); 83 ASSERT_EQ(1, tab_strip->count()); 84 content::WebContents* web_contents = tab_strip->GetWebContentsAt(0); 85 InfoBarManager* infobar_manager = 86 InfoBarService::FromWebContents(web_contents)->infobar_manager(); 87 EXPECT_EQ(1U, infobar_manager->infobar_count()); 88 ConfirmInfoBarDelegate* infobar = 89 infobar_manager->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); 90 ASSERT_TRUE(infobar); 91 92 // Open another browser. 93 scoped_ptr<Browser> opened_browser( 94 chrome::CreateBrowserWithTestWindowForParams(¶ms)); 95 96 // Move the tab which is destroying the crash info bar to the new browser. 97 tab_strip->DetachWebContentsAt(0); 98 tab_strip = opened_browser->tab_strip_model(); 99 tab_strip->AppendWebContents(web_contents, true); 100 101 // Close the original browser. 102 first_browser->window()->Close(); 103 first_browser.reset(); 104 105 ASSERT_EQ(1, tab_strip->count()); 106 InfoBarService* infobar_service = 107 InfoBarService::FromWebContents(tab_strip->GetWebContentsAt(0)); 108 EXPECT_EQ(1U, infobar_service->infobar_manager()->infobar_count()); 109 110 // This used to crash. 111 infobar->Accept(); 112 113 // Ramp down the test. 114 tab_strip->CloseWebContentsAt(0, TabStripModel::CLOSE_NONE); 115} 116 117#if defined(OS_CHROMEOS) 118INSTANTIATE_TEST_CASE_P(SessionCrashedInfoBarDelegateUnitTestInstantiation, 119 SessionCrashedInfoBarDelegateUnitTest, 120 testing::Bool()); 121#endif 122