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/files/scoped_temp_dir.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/prefs/browser_prefs.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/sessions/session_service_factory.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/test/base/browser_with_test_window_test.h"
18 #include "chrome/test/base/scoped_testing_local_state.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "components/infobars/core/infobar.h"
22 class SessionCrashedInfoBarDelegateUnitTest
: public BrowserWithTestWindowTest
{
24 void SetUp() override
{
25 static_cast<TestingBrowserProcess
*>(g_browser_process
)
26 ->SetLocalState(&pref_service
);
27 chrome::RegisterLocalState(pref_service
.registry());
29 // This needs to be called after the local state is set, because it will
30 // create a browser which will try to read from the local state.
31 BrowserWithTestWindowTest::SetUp();
33 // Make sure we have a Profile Manager.
34 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
35 TestingBrowserProcess::GetGlobal()->SetProfileManager(
36 new ProfileManagerWithoutInit(temp_dir_
.path()));
39 void TearDown() override
{
40 static_cast<TestingBrowserProcess
*>(g_browser_process
)->SetLocalState(NULL
);
41 BrowserWithTestWindowTest::TearDown();
43 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL
);
47 TestingPrefServiceSimple pref_service
;
48 base::ScopedTempDir temp_dir_
;
51 TEST_F(SessionCrashedInfoBarDelegateUnitTest
, DetachingTabWithCrashedInfoBar
) {
52 SessionServiceFactory::SetForTestProfile(
54 make_scoped_ptr(static_cast<SessionService
*>(
55 SessionServiceFactory::GetInstance()->BuildServiceInstanceFor(
56 browser()->profile()))));
58 // Create a browser which we can close during the test.
59 Browser::CreateParams
params(browser()->profile(),
60 browser()->host_desktop_type());
61 scoped_ptr
<Browser
> first_browser(
62 chrome::CreateBrowserWithTestWindowForParams(¶ms
));
63 AddTab(first_browser
.get(), GURL(chrome::kChromeUINewTabURL
));
65 // Attach the crashed infobar to it.
66 SessionCrashedInfoBarDelegate::Create(first_browser
.get());
68 TabStripModel
* tab_strip
= first_browser
->tab_strip_model();
69 ASSERT_EQ(1, tab_strip
->count());
70 content::WebContents
* web_contents
= tab_strip
->GetWebContentsAt(0);
71 InfoBarService
* infobar_service
=
72 InfoBarService::FromWebContents(web_contents
);
73 EXPECT_EQ(1U, infobar_service
->infobar_count());
74 ConfirmInfoBarDelegate
* infobar
=
75 infobar_service
->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
78 // Open another browser.
79 scoped_ptr
<Browser
> opened_browser(
80 chrome::CreateBrowserWithTestWindowForParams(¶ms
));
82 // Move the tab which is destroying the crash info bar to the new browser.
83 tab_strip
->DetachWebContentsAt(0);
84 tab_strip
= opened_browser
->tab_strip_model();
85 tab_strip
->AppendWebContents(web_contents
, true);
87 // Close the original browser.
88 first_browser
->window()->Close();
89 first_browser
.reset();
91 ASSERT_EQ(1, tab_strip
->count());
93 InfoBarService::FromWebContents(tab_strip
->GetWebContentsAt(0));
94 EXPECT_EQ(1U, infobar_service
->infobar_count());
96 // This used to crash.
99 // Ramp down the test.
100 tab_strip
->CloseWebContentsAt(0, TabStripModel::CLOSE_NONE
);