1 // Copyright (c) 2012 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/tab_contents/tab_contents_iterator.h"
7 #include "base/prefs/pref_registry_simple.h"
8 #include "base/prefs/testing_pref_service.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/browser_shutdown.h"
11 #include "chrome/browser/lifetime/application_lifetime.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/host_desktop.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/browser_with_test_window_test.h"
20 #include "chrome/test/base/test_browser_window.h"
21 #include "chrome/test/base/testing_browser_process.h"
23 typedef BrowserWithTestWindowTest BrowserListTest
;
27 // Helper function to iterate and count all the tabs.
28 size_t CountAllTabs() {
30 for (TabContentsIterator iterator
; !iterator
.done(); iterator
.Next())
37 TEST_F(BrowserListTest
, TabContentsIteratorVerifyCount
) {
38 // Make sure we have 1 window to start with.
40 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
)->size());
42 EXPECT_EQ(0U, CountAllTabs());
44 // Create more browsers/windows.
45 Browser::CreateParams
native_params(profile(),
46 chrome::HOST_DESKTOP_TYPE_NATIVE
);
47 scoped_ptr
<Browser
> browser2(
48 chrome::CreateBrowserWithTestWindowForParams(&native_params
));
49 // Create browser 3 and 4 on the Ash desktop (the TabContentsIterator
50 // shouldn't see the difference).
51 Browser::CreateParams
ash_params(profile(), chrome::HOST_DESKTOP_TYPE_ASH
);
52 scoped_ptr
<Browser
> browser3(
53 chrome::CreateBrowserWithTestWindowForParams(&ash_params
));
54 scoped_ptr
<Browser
> browser4(
55 chrome::CreateBrowserWithTestWindowForParams(&ash_params
));
58 #if defined(OS_CHROMEOS)
59 // The ash desktop is the native desktop on Chrome OS.
61 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
)->size());
64 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
)->size());
66 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH
)->size());
68 EXPECT_EQ(0, browser()->tab_strip_model()->count());
69 EXPECT_EQ(0, browser2
->tab_strip_model()->count());
70 EXPECT_EQ(0, browser3
->tab_strip_model()->count());
71 EXPECT_EQ(0, browser4
->tab_strip_model()->count());
73 EXPECT_EQ(0U, CountAllTabs());
76 for (size_t i
= 0; i
< 3; ++i
)
77 chrome::NewTab(browser2
.get());
78 chrome::NewTab(browser3
.get());
80 EXPECT_EQ(4U, CountAllTabs());
83 browser2
->tab_strip_model()->CloseAllTabs();
85 EXPECT_EQ(1U, CountAllTabs());
88 for (size_t i
= 0; i
< 41; ++i
)
89 chrome::NewTab(browser());
91 EXPECT_EQ(42U, CountAllTabs());
92 // Close all remaining tabs to keep all the destructors happy.
93 browser3
->tab_strip_model()->CloseAllTabs();
96 TEST_F(BrowserListTest
, TabContentsIteratorVerifyBrowser
) {
97 // Make sure we have 1 window to start with.
99 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
)->size());
101 // Create more browsers/windows.
102 Browser::CreateParams
native_params(profile(),
103 chrome::HOST_DESKTOP_TYPE_NATIVE
);
104 scoped_ptr
<Browser
> browser2(
105 chrome::CreateBrowserWithTestWindowForParams(&native_params
));
106 // Create browser 3 on the Ash desktop (the TabContentsIterator shouldn't see
108 Browser::CreateParams
ash_params(profile(), chrome::HOST_DESKTOP_TYPE_ASH
);
109 scoped_ptr
<Browser
> browser3(
110 chrome::CreateBrowserWithTestWindowForParams(&ash_params
));
113 #if defined(OS_CHROMEOS)
114 // The ash desktop is the native desktop on Chrome OS.
116 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
)->size());
119 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
)->size());
121 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH
)->size());
123 EXPECT_EQ(0, browser()->tab_strip_model()->count());
124 EXPECT_EQ(0, browser2
->tab_strip_model()->count());
125 EXPECT_EQ(0, browser3
->tab_strip_model()->count());
127 EXPECT_EQ(0U, CountAllTabs());
130 for (size_t i
= 0; i
< 3; ++i
)
131 chrome::NewTab(browser2
.get());
132 for (size_t i
= 0; i
< 2; ++i
)
133 chrome::NewTab(browser3
.get());
136 for (TabContentsIterator iterator
; !iterator
.done(); iterator
.Next(),
139 EXPECT_EQ(browser2
.get(), iterator
.browser());
141 EXPECT_EQ(browser3
.get(), iterator
.browser());
147 browser2
->tab_strip_model()->CloseAllTabs();
148 browser3
->tab_strip_model()->CloseWebContentsAt(1, TabStripModel::CLOSE_NONE
);
151 for (TabContentsIterator iterator
; !iterator
.done(); iterator
.Next(),
154 EXPECT_EQ(browser3
.get(), iterator
.browser());
159 // Now make it one tab per browser.
160 chrome::NewTab(browser());
161 chrome::NewTab(browser2
.get());
164 for (TabContentsIterator iterator
; !iterator
.done(); iterator
.Next(),
167 EXPECT_EQ(browser(), iterator
.browser());
169 EXPECT_EQ(browser2
.get(), iterator
.browser());
171 EXPECT_EQ(browser3
.get(), iterator
.browser());
176 // Close all remaining tabs to keep all the destructors happy.
177 browser2
->tab_strip_model()->CloseAllTabs();
178 browser3
->tab_strip_model()->CloseAllTabs();
181 #if defined(OS_CHROMEOS)
182 // Calling AttemptRestart on ChromeOS will exit the test.
183 #define MAYBE_AttemptRestart DISABLED_AttemptRestart
185 #define MAYBE_AttemptRestart AttemptRestart
188 TEST_F(BrowserListTest
, MAYBE_AttemptRestart
) {
189 ASSERT_TRUE(g_browser_process
);
190 TestingPrefServiceSimple testing_pref_service
;
191 testing_pref_service
.registry()->RegisterBooleanPref(
192 prefs::kWasRestarted
, false);
193 testing_pref_service
.registry()->RegisterBooleanPref(
194 prefs::kRestartLastSessionOnShutdown
,
196 testing_pref_service
.registry()->RegisterListPref(
197 prefs::kProfilesLastActive
);
199 TestingBrowserProcess
* testing_browser_process
=
200 TestingBrowserProcess::GetGlobal();
201 testing_browser_process
->SetLocalState(&testing_pref_service
);
202 ASSERT_TRUE(g_browser_process
->local_state());
203 ProfileManager
* profile_manager
= new ProfileManager(base::FilePath());
204 testing_browser_process
->SetProfileManager(profile_manager
);
206 chrome::AttemptRestart();
207 // Cancel the effects of us calling chrome::AttemptRestart. Otherwise tests
208 // ran after this one will fail.
209 browser_shutdown::SetTryingToQuit(false);
211 EXPECT_TRUE(testing_pref_service
.GetBoolean(prefs::kWasRestarted
));
212 testing_browser_process
->SetLocalState(NULL
);