blob: fc7127ecb8b4d5470811c6bee16c128a3f87846c [file] [log] [blame]
[email protected]5f3be2b6a2014-04-18 22:27:391// 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.
4
5#include "chrome/browser/ui/settings_window_manager.h"
6
avi655876a2015-12-25 07:18:157#include <stddef.h>
8
[email protected]5f3be2b6a2014-04-18 22:27:399#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:0410#include "base/files/file_util.h"
[email protected]5f3be2b6a2014-04-18 22:27:3911#include "base/files/scoped_temp_dir.h"
avi655876a2015-12-25 07:18:1512#include "base/macros.h"
13#include "build/build_config.h"
[email protected]5f3be2b6a2014-04-18 22:27:3914#include "chrome/browser/browser_process.h"
[email protected]5f3be2b6a2014-04-18 22:27:3915#include "chrome/browser/profiles/profile_manager.h"
16#include "chrome/browser/ui/browser.h"
[email protected]a59bcc5d2014-05-02 00:36:1417#include "chrome/browser/ui/browser_finder.h"
scottmg8abbff832016-01-28 22:57:3718#include "chrome/browser/ui/browser_list.h"
[email protected]5f3be2b6a2014-04-18 22:27:3919#include "chrome/browser/ui/browser_window.h"
[email protected]a59bcc5d2014-05-02 00:36:1420#include "chrome/browser/ui/chrome_pages.h"
[email protected]5f3be2b6a2014-04-18 22:27:3921#include "chrome/browser/ui/settings_window_manager_observer.h"
22#include "chrome/common/chrome_switches.h"
[email protected]a59bcc5d2014-05-02 00:36:1423#include "chrome/common/url_constants.h"
[email protected]5f3be2b6a2014-04-18 22:27:3924#include "chrome/test/base/in_process_browser_test.h"
25#include "content/public/browser/notification_service.h"
26#include "content/public/test/test_utils.h"
[email protected]a59bcc5d2014-05-02 00:36:1427#include "url/gurl.h"
[email protected]5f3be2b6a2014-04-18 22:27:3928
29namespace {
30
31class SettingsWindowTestObserver
32 : public chrome::SettingsWindowManagerObserver {
33 public:
34 SettingsWindowTestObserver() : browser_(NULL), new_settings_count_(0) {}
dcheng5dd5ff62014-10-21 12:42:3835 ~SettingsWindowTestObserver() override {}
[email protected]5f3be2b6a2014-04-18 22:27:3936
dcheng5dd5ff62014-10-21 12:42:3837 void OnNewSettingsWindow(Browser* settings_browser) override {
[email protected]5f3be2b6a2014-04-18 22:27:3938 browser_ = settings_browser;
39 ++new_settings_count_;
40 }
41
42 Browser* browser() { return browser_; }
43 size_t new_settings_count() const { return new_settings_count_; }
44
45 private:
46 Browser* browser_;
47 size_t new_settings_count_;
48
49 DISALLOW_COPY_AND_ASSIGN(SettingsWindowTestObserver);
50};
51
52} // namespace
53
54class SettingsWindowManagerTest : public InProcessBrowserTest {
55 public:
[email protected]a59bcc5d2014-05-02 00:36:1456 SettingsWindowManagerTest()
57 : settings_manager_(chrome::SettingsWindowManager::GetInstance()),
58 test_profile_(NULL) {
59 settings_manager_->AddObserver(&observer_);
[email protected]5f3be2b6a2014-04-18 22:27:3960 }
dcheng131bb7e62014-10-28 00:16:3861 ~SettingsWindowManagerTest() override {
[email protected]a59bcc5d2014-05-02 00:36:1462 settings_manager_->RemoveObserver(&observer_);
[email protected]5f3be2b6a2014-04-18 22:27:3963 }
64
avi556c05022014-12-22 23:31:4365 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]a59bcc5d2014-05-02 00:36:1466 command_line->AppendSwitch(::switches::kEnableSettingsWindow);
[email protected]5f3be2b6a2014-04-18 22:27:3967 }
68
69 Profile* CreateTestProfile() {
70 CHECK(!test_profile_);
71
72 ProfileManager* profile_manager = g_browser_process->profile_manager();
73 base::RunLoop run_loop;
74 profile_manager->CreateProfileAsync(
75 profile_manager->GenerateNextProfileDirectoryPath(),
76 base::Bind(&SettingsWindowManagerTest::ProfileInitialized,
77 base::Unretained(this),
78 run_loop.QuitClosure()),
79 base::string16(),
lwchkg1f62d242015-10-29 00:50:4380 std::string(),
[email protected]5f3be2b6a2014-04-18 22:27:3981 std::string());
82 run_loop.Run();
83
84 return test_profile_;
85 }
86
87 void ProfileInitialized(const base::Closure& closure,
88 Profile* profile,
89 Profile::CreateStatus status) {
90 if (status == Profile::CREATE_STATUS_INITIALIZED) {
91 test_profile_ = profile;
92 closure.Run();
93 }
94 }
95
[email protected]a59bcc5d2014-05-02 00:36:1496 void ShowSettingsForProfile(Profile* profile) {
97 settings_manager_->ShowChromePageForProfile(
98 profile, GURL(chrome::kChromeUISettingsURL));
99 }
100
[email protected]a59bcc5d2014-05-02 00:36:14101 void CloseNonDefaultBrowsers() {
102 std::list<Browser*> browsers_to_close;
scottmg8abbff832016-01-28 22:57:37103 for (auto* b : *BrowserList::GetInstance()) {
104 if (b != browser())
105 browsers_to_close.push_back(b);
[email protected]a59bcc5d2014-05-02 00:36:14106 }
107 for (std::list<Browser*>::iterator iter = browsers_to_close.begin();
108 iter != browsers_to_close.end(); ++iter) {
109 CloseBrowserSynchronously(*iter);
110 }
111 }
112
[email protected]5f3be2b6a2014-04-18 22:27:39113 protected:
[email protected]a59bcc5d2014-05-02 00:36:14114 chrome::SettingsWindowManager* settings_manager_;
[email protected]5f3be2b6a2014-04-18 22:27:39115 SettingsWindowTestObserver observer_;
116 base::ScopedTempDir temp_profile_dir_;
117 Profile* test_profile_; // Owned by g_browser_process->profile_manager()
118
119 DISALLOW_COPY_AND_ASSIGN(SettingsWindowManagerTest);
120};
121
122
123IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest, OpenSettingsWindow) {
[email protected]5f3be2b6a2014-04-18 22:27:39124 // Open a settings window.
[email protected]a59bcc5d2014-05-02 00:36:14125 ShowSettingsForProfile(browser()->profile());
[email protected]5f3be2b6a2014-04-18 22:27:39126 Browser* settings_browser =
[email protected]a59bcc5d2014-05-02 00:36:14127 settings_manager_->FindBrowserForProfile(browser()->profile());
[email protected]5f3be2b6a2014-04-18 22:27:39128 ASSERT_TRUE(settings_browser);
129 // Ensure the observer fired correctly.
130 EXPECT_EQ(1u, observer_.new_settings_count());
131 EXPECT_EQ(settings_browser, observer_.browser());
132
133 // Open the settings again: no new window.
[email protected]a59bcc5d2014-05-02 00:36:14134 ShowSettingsForProfile(browser()->profile());
[email protected]5f3be2b6a2014-04-18 22:27:39135 EXPECT_EQ(settings_browser,
[email protected]a59bcc5d2014-05-02 00:36:14136 settings_manager_->FindBrowserForProfile(browser()->profile()));
[email protected]5f3be2b6a2014-04-18 22:27:39137 EXPECT_EQ(1u, observer_.new_settings_count());
138
139 // Close the settings window.
140 CloseBrowserSynchronously(settings_browser);
[email protected]a59bcc5d2014-05-02 00:36:14141 EXPECT_FALSE(settings_manager_->FindBrowserForProfile(browser()->profile()));
[email protected]5f3be2b6a2014-04-18 22:27:39142
143 // Open a new settings window.
[email protected]a59bcc5d2014-05-02 00:36:14144 ShowSettingsForProfile(browser()->profile());
[email protected]5f3be2b6a2014-04-18 22:27:39145 Browser* settings_browser2 =
[email protected]a59bcc5d2014-05-02 00:36:14146 settings_manager_->FindBrowserForProfile(browser()->profile());
[email protected]5f3be2b6a2014-04-18 22:27:39147 ASSERT_TRUE(settings_browser2);
148 EXPECT_EQ(2u, observer_.new_settings_count());
149
150 CloseBrowserSynchronously(settings_browser2);
151}
152
153#if !defined(OS_CHROMEOS)
154IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest, SettingsWindowMultiProfile) {
[email protected]5f3be2b6a2014-04-18 22:27:39155 Profile* test_profile = CreateTestProfile();
156 ASSERT_TRUE(test_profile);
157
158 // Open a settings window.
[email protected]a59bcc5d2014-05-02 00:36:14159 ShowSettingsForProfile(browser()->profile());
[email protected]5f3be2b6a2014-04-18 22:27:39160 Browser* settings_browser =
[email protected]a59bcc5d2014-05-02 00:36:14161 settings_manager_->FindBrowserForProfile(browser()->profile());
[email protected]5f3be2b6a2014-04-18 22:27:39162 ASSERT_TRUE(settings_browser);
163 // Ensure the observer fired correctly.
164 EXPECT_EQ(1u, observer_.new_settings_count());
165 EXPECT_EQ(settings_browser, observer_.browser());
166
167 // Open a settings window for a new profile.
[email protected]a59bcc5d2014-05-02 00:36:14168 ShowSettingsForProfile(test_profile);
[email protected]5f3be2b6a2014-04-18 22:27:39169 Browser* settings_browser2 =
[email protected]a59bcc5d2014-05-02 00:36:14170 settings_manager_->FindBrowserForProfile(test_profile);
[email protected]5f3be2b6a2014-04-18 22:27:39171 ASSERT_TRUE(settings_browser2);
172 // Ensure the observer fired correctly.
173 EXPECT_EQ(2u, observer_.new_settings_count());
174 EXPECT_EQ(settings_browser2, observer_.browser());
175
176 CloseBrowserSynchronously(settings_browser);
177 CloseBrowserSynchronously(settings_browser2);
178}
179#endif
[email protected]a59bcc5d2014-05-02 00:36:14180
[email protected]4e6b6c792014-06-11 10:00:06181IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest, OpenChromePages) {
182 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
183
184 // History should open in the existing browser window.
185 chrome::ShowHistory(browser());
[email protected]a59bcc5d2014-05-02 00:36:14186 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
187
188 // Settings should open a new browser window.
189 chrome::ShowSettings(browser());
190 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
191
[email protected]e53c4412014-06-18 10:55:56192 // About should reuse the existing Settings window.
193 chrome::ShowAboutChrome(browser());
194 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
195
[email protected]4e6b6c792014-06-11 10:00:06196 // Extensions should open in an existing browser window.
[email protected]a59bcc5d2014-05-02 00:36:14197 CloseNonDefaultBrowsers();
198 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
199 std::string extension_to_highlight; // none
200 chrome::ShowExtensions(browser(), extension_to_highlight);
[email protected]a59bcc5d2014-05-02 00:36:14201 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
[email protected]4e6b6c792014-06-11 10:00:06202
203 // Downloads should open in an existing browser window.
[email protected]a59bcc5d2014-05-02 00:36:14204 chrome::ShowDownloads(browser());
205 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
[email protected]e53c4412014-06-18 10:55:56206
207 // About should open a new browser window.
208 chrome::ShowAboutChrome(browser());
209 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
[email protected]a59bcc5d2014-05-02 00:36:14210}