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/profiles/profile_manager.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_iterator.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/chrome_pages.h"
17 #include "chrome/browser/ui/settings_window_manager_observer.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/url_constants.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/test/test_utils.h"
27 class SettingsWindowTestObserver
28 : public chrome::SettingsWindowManagerObserver
{
30 SettingsWindowTestObserver() : browser_(NULL
), new_settings_count_(0) {}
31 ~SettingsWindowTestObserver() override
{}
33 void OnNewSettingsWindow(Browser
* settings_browser
) override
{
34 browser_
= settings_browser
;
35 ++new_settings_count_
;
38 Browser
* browser() { return browser_
; }
39 size_t new_settings_count() const { return new_settings_count_
; }
43 size_t new_settings_count_
;
45 DISALLOW_COPY_AND_ASSIGN(SettingsWindowTestObserver
);
50 class SettingsWindowManagerTest
: public InProcessBrowserTest
{
52 SettingsWindowManagerTest()
53 : settings_manager_(chrome::SettingsWindowManager::GetInstance()),
55 settings_manager_
->AddObserver(&observer_
);
57 ~SettingsWindowManagerTest() override
{
58 settings_manager_
->RemoveObserver(&observer_
);
61 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
62 command_line
->AppendSwitch(::switches::kEnableSettingsWindow
);
65 Profile
* CreateTestProfile() {
66 CHECK(!test_profile_
);
68 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
69 base::RunLoop run_loop
;
70 profile_manager
->CreateProfileAsync(
71 profile_manager
->GenerateNextProfileDirectoryPath(),
72 base::Bind(&SettingsWindowManagerTest::ProfileInitialized
,
73 base::Unretained(this),
74 run_loop
.QuitClosure()),
83 void ProfileInitialized(const base::Closure
& closure
,
85 Profile::CreateStatus status
) {
86 if (status
== Profile::CREATE_STATUS_INITIALIZED
) {
87 test_profile_
= profile
;
92 void ShowSettingsForProfile(Profile
* profile
) {
93 settings_manager_
->ShowChromePageForProfile(
94 profile
, GURL(chrome::kChromeUISettingsURL
));
97 void CloseNonDefaultBrowsers() {
98 std::list
<Browser
*> browsers_to_close
;
99 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
100 if (*it
!= browser())
101 browsers_to_close
.push_back(*it
);
103 for (std::list
<Browser
*>::iterator iter
= browsers_to_close
.begin();
104 iter
!= browsers_to_close
.end(); ++iter
) {
105 CloseBrowserSynchronously(*iter
);
110 chrome::SettingsWindowManager
* settings_manager_
;
111 SettingsWindowTestObserver observer_
;
112 base::ScopedTempDir temp_profile_dir_
;
113 Profile
* test_profile_
; // Owned by g_browser_process->profile_manager()
115 DISALLOW_COPY_AND_ASSIGN(SettingsWindowManagerTest
);
119 IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest
, OpenSettingsWindow
) {
120 // Open a settings window.
121 ShowSettingsForProfile(browser()->profile());
122 Browser
* settings_browser
=
123 settings_manager_
->FindBrowserForProfile(browser()->profile());
124 ASSERT_TRUE(settings_browser
);
125 // Ensure the observer fired correctly.
126 EXPECT_EQ(1u, observer_
.new_settings_count());
127 EXPECT_EQ(settings_browser
, observer_
.browser());
129 // Open the settings again: no new window.
130 ShowSettingsForProfile(browser()->profile());
131 EXPECT_EQ(settings_browser
,
132 settings_manager_
->FindBrowserForProfile(browser()->profile()));
133 EXPECT_EQ(1u, observer_
.new_settings_count());
135 // Close the settings window.
136 CloseBrowserSynchronously(settings_browser
);
137 EXPECT_FALSE(settings_manager_
->FindBrowserForProfile(browser()->profile()));
139 // Open a new settings window.
140 ShowSettingsForProfile(browser()->profile());
141 Browser
* settings_browser2
=
142 settings_manager_
->FindBrowserForProfile(browser()->profile());
143 ASSERT_TRUE(settings_browser2
);
144 EXPECT_EQ(2u, observer_
.new_settings_count());
146 CloseBrowserSynchronously(settings_browser2
);
149 #if !defined(OS_CHROMEOS)
150 IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest
, SettingsWindowMultiProfile
) {
151 Profile
* test_profile
= CreateTestProfile();
152 ASSERT_TRUE(test_profile
);
154 // Open a settings window.
155 ShowSettingsForProfile(browser()->profile());
156 Browser
* settings_browser
=
157 settings_manager_
->FindBrowserForProfile(browser()->profile());
158 ASSERT_TRUE(settings_browser
);
159 // Ensure the observer fired correctly.
160 EXPECT_EQ(1u, observer_
.new_settings_count());
161 EXPECT_EQ(settings_browser
, observer_
.browser());
163 // Open a settings window for a new profile.
164 ShowSettingsForProfile(test_profile
);
165 Browser
* settings_browser2
=
166 settings_manager_
->FindBrowserForProfile(test_profile
);
167 ASSERT_TRUE(settings_browser2
);
168 // Ensure the observer fired correctly.
169 EXPECT_EQ(2u, observer_
.new_settings_count());
170 EXPECT_EQ(settings_browser2
, observer_
.browser());
172 CloseBrowserSynchronously(settings_browser
);
173 CloseBrowserSynchronously(settings_browser2
);
177 IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest
, OpenChromePages
) {
178 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
180 // History should open in the existing browser window.
181 chrome::ShowHistory(browser());
182 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
184 // Settings should open a new browser window.
185 chrome::ShowSettings(browser());
186 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
188 // About should reuse the existing Settings window.
189 chrome::ShowAboutChrome(browser());
190 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
192 // Extensions should open in an existing browser window.
193 CloseNonDefaultBrowsers();
194 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
195 std::string extension_to_highlight
; // none
196 chrome::ShowExtensions(browser(), extension_to_highlight
);
197 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
199 // Downloads should open in an existing browser window.
200 chrome::ShowDownloads(browser());
201 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
203 // About should open a new browser window.
204 chrome::ShowAboutChrome(browser());
205 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());