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