1 // Copyright 2014 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/settings_window_manager.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/browser_iterator.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/chrome_pages.h"
18 #include "chrome/browser/ui/settings_window_manager_observer.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/test/base/in_process_browser_test.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/test/test_utils.h"
28 class SettingsWindowTestObserver
29 : public chrome::SettingsWindowManagerObserver
{
31 SettingsWindowTestObserver() : browser_(NULL
), new_settings_count_(0) {}
32 virtual ~SettingsWindowTestObserver() {}
34 virtual void OnNewSettingsWindow(Browser
* settings_browser
) OVERRIDE
{
35 browser_
= settings_browser
;
36 ++new_settings_count_
;
39 Browser
* browser() { return browser_
; }
40 size_t new_settings_count() const { return new_settings_count_
; }
44 size_t new_settings_count_
;
46 DISALLOW_COPY_AND_ASSIGN(SettingsWindowTestObserver
);
51 class SettingsWindowManagerTest
: public InProcessBrowserTest
{
53 SettingsWindowManagerTest()
54 : settings_manager_(chrome::SettingsWindowManager::GetInstance()),
56 settings_manager_
->AddObserver(&observer_
);
58 virtual ~SettingsWindowManagerTest() {
59 settings_manager_
->RemoveObserver(&observer_
);
62 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
63 command_line
->AppendSwitch(::switches::kEnableSettingsWindow
);
64 command_line
->AppendSwitch(::switches::kMultiProfiles
);
67 Profile
* CreateTestProfile() {
68 CHECK(!test_profile_
);
70 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
71 base::RunLoop run_loop
;
72 profile_manager
->CreateProfileAsync(
73 profile_manager
->GenerateNextProfileDirectoryPath(),
74 base::Bind(&SettingsWindowManagerTest::ProfileInitialized
,
75 base::Unretained(this),
76 run_loop
.QuitClosure()),
85 void ProfileInitialized(const base::Closure
& closure
,
87 Profile::CreateStatus status
) {
88 if (status
== Profile::CREATE_STATUS_INITIALIZED
) {
89 test_profile_
= profile
;
94 void ShowSettingsForProfile(Profile
* profile
) {
95 settings_manager_
->ShowChromePageForProfile(
96 profile
, GURL(chrome::kChromeUISettingsURL
));
99 void CloseBrowserSynchronously(Browser
* browser
) {
100 content::WindowedNotificationObserver
observer(
101 chrome::NOTIFICATION_BROWSER_CLOSED
,
102 content::NotificationService::AllSources());
103 browser
->window()->Close();
107 void CloseNonDefaultBrowsers() {
108 std::list
<Browser
*> browsers_to_close
;
109 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
110 if (*it
!= browser())
111 browsers_to_close
.push_back(*it
);
113 for (std::list
<Browser
*>::iterator iter
= browsers_to_close
.begin();
114 iter
!= browsers_to_close
.end(); ++iter
) {
115 CloseBrowserSynchronously(*iter
);
120 chrome::SettingsWindowManager
* settings_manager_
;
121 SettingsWindowTestObserver observer_
;
122 base::ScopedTempDir temp_profile_dir_
;
123 Profile
* test_profile_
; // Owned by g_browser_process->profile_manager()
125 DISALLOW_COPY_AND_ASSIGN(SettingsWindowManagerTest
);
129 IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest
, OpenSettingsWindow
) {
130 // Open a settings window.
131 ShowSettingsForProfile(browser()->profile());
132 Browser
* settings_browser
=
133 settings_manager_
->FindBrowserForProfile(browser()->profile());
134 ASSERT_TRUE(settings_browser
);
135 // Ensure the observer fired correctly.
136 EXPECT_EQ(1u, observer_
.new_settings_count());
137 EXPECT_EQ(settings_browser
, observer_
.browser());
139 // Open the settings again: no new window.
140 ShowSettingsForProfile(browser()->profile());
141 EXPECT_EQ(settings_browser
,
142 settings_manager_
->FindBrowserForProfile(browser()->profile()));
143 EXPECT_EQ(1u, observer_
.new_settings_count());
145 // Close the settings window.
146 CloseBrowserSynchronously(settings_browser
);
147 EXPECT_FALSE(settings_manager_
->FindBrowserForProfile(browser()->profile()));
149 // Open a new settings window.
150 ShowSettingsForProfile(browser()->profile());
151 Browser
* settings_browser2
=
152 settings_manager_
->FindBrowserForProfile(browser()->profile());
153 ASSERT_TRUE(settings_browser2
);
154 EXPECT_EQ(2u, observer_
.new_settings_count());
156 CloseBrowserSynchronously(settings_browser2
);
159 #if !defined(OS_CHROMEOS)
160 IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest
, SettingsWindowMultiProfile
) {
161 Profile
* test_profile
= CreateTestProfile();
162 ASSERT_TRUE(test_profile
);
164 // Open a settings window.
165 ShowSettingsForProfile(browser()->profile());
166 Browser
* settings_browser
=
167 settings_manager_
->FindBrowserForProfile(browser()->profile());
168 ASSERT_TRUE(settings_browser
);
169 // Ensure the observer fired correctly.
170 EXPECT_EQ(1u, observer_
.new_settings_count());
171 EXPECT_EQ(settings_browser
, observer_
.browser());
173 // Open a settings window for a new profile.
174 ShowSettingsForProfile(test_profile
);
175 Browser
* settings_browser2
=
176 settings_manager_
->FindBrowserForProfile(test_profile
);
177 ASSERT_TRUE(settings_browser2
);
178 // Ensure the observer fired correctly.
179 EXPECT_EQ(2u, observer_
.new_settings_count());
180 EXPECT_EQ(settings_browser2
, observer_
.browser());
182 CloseBrowserSynchronously(settings_browser
);
183 CloseBrowserSynchronously(settings_browser2
);
187 IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest
, OpenSettingsChromePages
) {
188 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
190 // Settings should open a new browser window.
191 chrome::ShowSettings(browser());
192 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
194 // History should open a new browser window.
195 CloseNonDefaultBrowsers();
196 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
197 chrome::ShowHistory(browser());
198 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
200 // Extensions should open a new browser window.
201 CloseNonDefaultBrowsers();
202 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
203 std::string extension_to_highlight
; // none
204 chrome::ShowExtensions(browser(), extension_to_highlight
);
205 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
207 // Downloads should NOT open a new browser window.
208 CloseNonDefaultBrowsers();
209 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
210 chrome::ShowDownloads(browser());
211 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());