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/files/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 ~SettingsWindowTestObserver() override
{}
34 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 ~SettingsWindowManagerTest() override
{
59 settings_manager_
->RemoveObserver(&observer_
);
62 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
63 command_line
->AppendSwitch(::switches::kEnableSettingsWindow
);
66 Profile
* CreateTestProfile() {
67 CHECK(!test_profile_
);
69 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
70 base::RunLoop run_loop
;
71 profile_manager
->CreateProfileAsync(
72 profile_manager
->GenerateNextProfileDirectoryPath(),
73 base::Bind(&SettingsWindowManagerTest::ProfileInitialized
,
74 base::Unretained(this),
75 run_loop
.QuitClosure()),
84 void ProfileInitialized(const base::Closure
& closure
,
86 Profile::CreateStatus status
) {
87 if (status
== Profile::CREATE_STATUS_INITIALIZED
) {
88 test_profile_
= profile
;
93 void ShowSettingsForProfile(Profile
* profile
) {
94 settings_manager_
->ShowChromePageForProfile(
95 profile
, GURL(chrome::kChromeUISettingsURL
));
98 void CloseBrowserSynchronously(Browser
* browser
) {
99 content::WindowedNotificationObserver
observer(
100 chrome::NOTIFICATION_BROWSER_CLOSED
,
101 content::NotificationService::AllSources());
102 browser
->window()->Close();
106 void CloseNonDefaultBrowsers() {
107 std::list
<Browser
*> browsers_to_close
;
108 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
109 if (*it
!= browser())
110 browsers_to_close
.push_back(*it
);
112 for (std::list
<Browser
*>::iterator iter
= browsers_to_close
.begin();
113 iter
!= browsers_to_close
.end(); ++iter
) {
114 CloseBrowserSynchronously(*iter
);
119 chrome::SettingsWindowManager
* settings_manager_
;
120 SettingsWindowTestObserver observer_
;
121 base::ScopedTempDir temp_profile_dir_
;
122 Profile
* test_profile_
; // Owned by g_browser_process->profile_manager()
124 DISALLOW_COPY_AND_ASSIGN(SettingsWindowManagerTest
);
128 IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest
, OpenSettingsWindow
) {
129 // Open a settings window.
130 ShowSettingsForProfile(browser()->profile());
131 Browser
* settings_browser
=
132 settings_manager_
->FindBrowserForProfile(browser()->profile());
133 ASSERT_TRUE(settings_browser
);
134 // Ensure the observer fired correctly.
135 EXPECT_EQ(1u, observer_
.new_settings_count());
136 EXPECT_EQ(settings_browser
, observer_
.browser());
138 // Open the settings again: no new window.
139 ShowSettingsForProfile(browser()->profile());
140 EXPECT_EQ(settings_browser
,
141 settings_manager_
->FindBrowserForProfile(browser()->profile()));
142 EXPECT_EQ(1u, observer_
.new_settings_count());
144 // Close the settings window.
145 CloseBrowserSynchronously(settings_browser
);
146 EXPECT_FALSE(settings_manager_
->FindBrowserForProfile(browser()->profile()));
148 // Open a new settings window.
149 ShowSettingsForProfile(browser()->profile());
150 Browser
* settings_browser2
=
151 settings_manager_
->FindBrowserForProfile(browser()->profile());
152 ASSERT_TRUE(settings_browser2
);
153 EXPECT_EQ(2u, observer_
.new_settings_count());
155 CloseBrowserSynchronously(settings_browser2
);
158 #if !defined(OS_CHROMEOS)
159 IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest
, SettingsWindowMultiProfile
) {
160 Profile
* test_profile
= CreateTestProfile();
161 ASSERT_TRUE(test_profile
);
163 // Open a settings window.
164 ShowSettingsForProfile(browser()->profile());
165 Browser
* settings_browser
=
166 settings_manager_
->FindBrowserForProfile(browser()->profile());
167 ASSERT_TRUE(settings_browser
);
168 // Ensure the observer fired correctly.
169 EXPECT_EQ(1u, observer_
.new_settings_count());
170 EXPECT_EQ(settings_browser
, observer_
.browser());
172 // Open a settings window for a new profile.
173 ShowSettingsForProfile(test_profile
);
174 Browser
* settings_browser2
=
175 settings_manager_
->FindBrowserForProfile(test_profile
);
176 ASSERT_TRUE(settings_browser2
);
177 // Ensure the observer fired correctly.
178 EXPECT_EQ(2u, observer_
.new_settings_count());
179 EXPECT_EQ(settings_browser2
, observer_
.browser());
181 CloseBrowserSynchronously(settings_browser
);
182 CloseBrowserSynchronously(settings_browser2
);
186 IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest
, OpenChromePages
) {
187 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
189 // History should open in the existing browser window.
190 chrome::ShowHistory(browser());
191 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
193 // Settings should open a new browser window.
194 chrome::ShowSettings(browser());
195 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
197 // About should reuse the existing Settings window.
198 chrome::ShowAboutChrome(browser());
199 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
201 // Extensions should open in an existing browser window.
202 CloseNonDefaultBrowsers();
203 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
204 std::string extension_to_highlight
; // none
205 chrome::ShowExtensions(browser(), extension_to_highlight
);
206 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
208 // Downloads should open in an existing browser window.
209 chrome::ShowDownloads(browser());
210 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
212 // About should open a new browser window.
213 chrome::ShowAboutChrome(browser());
214 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());