[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/ui/settings_window_manager.h" |
| 6 | |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 9 | #include "base/command_line.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 11 | #include "base/files/scoped_temp_dir.h" |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 12 | #include "base/macros.h" |
| 13 | #include "build/build_config.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 14 | #include "chrome/browser/browser_process.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 15 | #include "chrome/browser/profiles/profile_manager.h" |
| 16 | #include "chrome/browser/ui/browser.h" |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser_finder.h" |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 18 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 19 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 20 | #include "chrome/browser/ui/chrome_pages.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 21 | #include "chrome/browser/ui/settings_window_manager_observer.h" |
| 22 | #include "chrome/common/chrome_switches.h" |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 23 | #include "chrome/common/url_constants.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 24 | #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] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 27 | #include "url/gurl.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
| 31 | class SettingsWindowTestObserver |
| 32 | : public chrome::SettingsWindowManagerObserver { |
| 33 | public: |
| 34 | SettingsWindowTestObserver() : browser_(NULL), new_settings_count_(0) {} |
dcheng | 5dd5ff6 | 2014-10-21 12:42:38 | [diff] [blame] | 35 | ~SettingsWindowTestObserver() override {} |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 36 | |
dcheng | 5dd5ff6 | 2014-10-21 12:42:38 | [diff] [blame] | 37 | void OnNewSettingsWindow(Browser* settings_browser) override { |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 38 | 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 | |
| 54 | class SettingsWindowManagerTest : public InProcessBrowserTest { |
| 55 | public: |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 56 | SettingsWindowManagerTest() |
| 57 | : settings_manager_(chrome::SettingsWindowManager::GetInstance()), |
| 58 | test_profile_(NULL) { |
| 59 | settings_manager_->AddObserver(&observer_); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 60 | } |
dcheng | 131bb7e6 | 2014-10-28 00:16:38 | [diff] [blame] | 61 | ~SettingsWindowManagerTest() override { |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 62 | settings_manager_->RemoveObserver(&observer_); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 63 | } |
| 64 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 65 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 66 | command_line->AppendSwitch(::switches::kEnableSettingsWindow); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 67 | } |
| 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(), |
lwchkg | 1f62d24 | 2015-10-29 00:50:43 | [diff] [blame] | 80 | std::string(), |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 81 | 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] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 96 | void ShowSettingsForProfile(Profile* profile) { |
| 97 | settings_manager_->ShowChromePageForProfile( |
| 98 | profile, GURL(chrome::kChromeUISettingsURL)); |
| 99 | } |
| 100 | |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 101 | void CloseNonDefaultBrowsers() { |
| 102 | std::list<Browser*> browsers_to_close; |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 103 | for (auto* b : *BrowserList::GetInstance()) { |
| 104 | if (b != browser()) |
| 105 | browsers_to_close.push_back(b); |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 106 | } |
| 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] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 113 | protected: |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 114 | chrome::SettingsWindowManager* settings_manager_; |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 115 | 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 | |
| 123 | IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest, OpenSettingsWindow) { |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 124 | // Open a settings window. |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 125 | ShowSettingsForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 126 | Browser* settings_browser = |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 127 | settings_manager_->FindBrowserForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 128 | 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] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 134 | ShowSettingsForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 135 | EXPECT_EQ(settings_browser, |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 136 | settings_manager_->FindBrowserForProfile(browser()->profile())); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 137 | EXPECT_EQ(1u, observer_.new_settings_count()); |
| 138 | |
| 139 | // Close the settings window. |
| 140 | CloseBrowserSynchronously(settings_browser); |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 141 | EXPECT_FALSE(settings_manager_->FindBrowserForProfile(browser()->profile())); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 142 | |
| 143 | // Open a new settings window. |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 144 | ShowSettingsForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 145 | Browser* settings_browser2 = |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 146 | settings_manager_->FindBrowserForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 147 | ASSERT_TRUE(settings_browser2); |
| 148 | EXPECT_EQ(2u, observer_.new_settings_count()); |
| 149 | |
| 150 | CloseBrowserSynchronously(settings_browser2); |
| 151 | } |
| 152 | |
| 153 | #if !defined(OS_CHROMEOS) |
| 154 | IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest, SettingsWindowMultiProfile) { |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 155 | Profile* test_profile = CreateTestProfile(); |
| 156 | ASSERT_TRUE(test_profile); |
| 157 | |
| 158 | // Open a settings window. |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 159 | ShowSettingsForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 160 | Browser* settings_browser = |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 161 | settings_manager_->FindBrowserForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 162 | 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] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 168 | ShowSettingsForProfile(test_profile); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 169 | Browser* settings_browser2 = |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 170 | settings_manager_->FindBrowserForProfile(test_profile); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 171 | 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] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 180 | |
[email protected] | 4e6b6c79 | 2014-06-11 10:00:06 | [diff] [blame] | 181 | IN_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] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 186 | 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] | e53c441 | 2014-06-18 10:55:56 | [diff] [blame] | 192 | // About should reuse the existing Settings window. |
| 193 | chrome::ShowAboutChrome(browser()); |
| 194 | EXPECT_EQ(2u, chrome::GetTotalBrowserCount()); |
| 195 | |
[email protected] | 4e6b6c79 | 2014-06-11 10:00:06 | [diff] [blame] | 196 | // Extensions should open in an existing browser window. |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 197 | CloseNonDefaultBrowsers(); |
| 198 | EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
| 199 | std::string extension_to_highlight; // none |
| 200 | chrome::ShowExtensions(browser(), extension_to_highlight); |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 201 | EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
[email protected] | 4e6b6c79 | 2014-06-11 10:00:06 | [diff] [blame] | 202 | |
| 203 | // Downloads should open in an existing browser window. |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 204 | chrome::ShowDownloads(browser()); |
| 205 | EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
[email protected] | e53c441 | 2014-06-18 10:55:56 | [diff] [blame] | 206 | |
| 207 | // About should open a new browser window. |
| 208 | chrome::ShowAboutChrome(browser()); |
| 209 | EXPECT_EQ(2u, chrome::GetTotalBrowserCount()); |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame] | 210 | } |