[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 | |
| 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" |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 14 | #include "chrome/browser/ui/browser_finder.h" |
| 15 | #include "chrome/browser/ui/browser_iterator.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 16 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 17 | #include "chrome/browser/ui/chrome_pages.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 18 | #include "chrome/browser/ui/settings_window_manager_observer.h" |
| 19 | #include "chrome/common/chrome_switches.h" |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 20 | #include "chrome/common/url_constants.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 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" |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 24 | #include "url/gurl.h" |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
| 28 | class SettingsWindowTestObserver |
| 29 | : public chrome::SettingsWindowManagerObserver { |
| 30 | public: |
| 31 | SettingsWindowTestObserver() : browser_(NULL), new_settings_count_(0) {} |
| 32 | virtual ~SettingsWindowTestObserver() {} |
| 33 | |
| 34 | virtual void OnNewSettingsWindow(Browser* settings_browser) OVERRIDE { |
| 35 | browser_ = settings_browser; |
| 36 | ++new_settings_count_; |
| 37 | } |
| 38 | |
| 39 | Browser* browser() { return browser_; } |
| 40 | size_t new_settings_count() const { return new_settings_count_; } |
| 41 | |
| 42 | private: |
| 43 | Browser* browser_; |
| 44 | size_t new_settings_count_; |
| 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(SettingsWindowTestObserver); |
| 47 | }; |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | class SettingsWindowManagerTest : public InProcessBrowserTest { |
| 52 | public: |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 53 | SettingsWindowManagerTest() |
| 54 | : settings_manager_(chrome::SettingsWindowManager::GetInstance()), |
| 55 | test_profile_(NULL) { |
| 56 | settings_manager_->AddObserver(&observer_); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 57 | } |
| 58 | virtual ~SettingsWindowManagerTest() { |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 59 | settings_manager_->RemoveObserver(&observer_); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 63 | command_line->AppendSwitch(::switches::kEnableSettingsWindow); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 64 | command_line->AppendSwitch(::switches::kMultiProfiles); |
| 65 | } |
| 66 | |
| 67 | Profile* CreateTestProfile() { |
| 68 | CHECK(!test_profile_); |
| 69 | |
| 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()), |
| 77 | base::string16(), |
| 78 | base::string16(), |
| 79 | std::string()); |
| 80 | run_loop.Run(); |
| 81 | |
| 82 | return test_profile_; |
| 83 | } |
| 84 | |
| 85 | void ProfileInitialized(const base::Closure& closure, |
| 86 | Profile* profile, |
| 87 | Profile::CreateStatus status) { |
| 88 | if (status == Profile::CREATE_STATUS_INITIALIZED) { |
| 89 | test_profile_ = profile; |
| 90 | closure.Run(); |
| 91 | } |
| 92 | } |
| 93 | |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 94 | void ShowSettingsForProfile(Profile* profile) { |
| 95 | settings_manager_->ShowChromePageForProfile( |
| 96 | profile, GURL(chrome::kChromeUISettingsURL)); |
| 97 | } |
| 98 | |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 99 | void CloseBrowserSynchronously(Browser* browser) { |
| 100 | content::WindowedNotificationObserver observer( |
| 101 | chrome::NOTIFICATION_BROWSER_CLOSED, |
| 102 | content::NotificationService::AllSources()); |
| 103 | browser->window()->Close(); |
| 104 | observer.Wait(); |
| 105 | } |
| 106 | |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 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); |
| 112 | } |
| 113 | for (std::list<Browser*>::iterator iter = browsers_to_close.begin(); |
| 114 | iter != browsers_to_close.end(); ++iter) { |
| 115 | CloseBrowserSynchronously(*iter); |
| 116 | } |
| 117 | } |
| 118 | |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 119 | protected: |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 120 | chrome::SettingsWindowManager* settings_manager_; |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 121 | SettingsWindowTestObserver observer_; |
| 122 | base::ScopedTempDir temp_profile_dir_; |
| 123 | Profile* test_profile_; // Owned by g_browser_process->profile_manager() |
| 124 | |
| 125 | DISALLOW_COPY_AND_ASSIGN(SettingsWindowManagerTest); |
| 126 | }; |
| 127 | |
| 128 | |
| 129 | IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest, OpenSettingsWindow) { |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 130 | // Open a settings window. |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 131 | ShowSettingsForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 132 | Browser* settings_browser = |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 133 | settings_manager_->FindBrowserForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 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()); |
| 138 | |
| 139 | // Open the settings again: no new window. |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 140 | ShowSettingsForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 141 | EXPECT_EQ(settings_browser, |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 142 | settings_manager_->FindBrowserForProfile(browser()->profile())); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 143 | EXPECT_EQ(1u, observer_.new_settings_count()); |
| 144 | |
| 145 | // Close the settings window. |
| 146 | CloseBrowserSynchronously(settings_browser); |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 147 | EXPECT_FALSE(settings_manager_->FindBrowserForProfile(browser()->profile())); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 148 | |
| 149 | // Open a new settings window. |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 150 | ShowSettingsForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 151 | Browser* settings_browser2 = |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 152 | settings_manager_->FindBrowserForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 153 | ASSERT_TRUE(settings_browser2); |
| 154 | EXPECT_EQ(2u, observer_.new_settings_count()); |
| 155 | |
| 156 | CloseBrowserSynchronously(settings_browser2); |
| 157 | } |
| 158 | |
| 159 | #if !defined(OS_CHROMEOS) |
| 160 | IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest, SettingsWindowMultiProfile) { |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 161 | Profile* test_profile = CreateTestProfile(); |
| 162 | ASSERT_TRUE(test_profile); |
| 163 | |
| 164 | // Open a settings window. |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 165 | ShowSettingsForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 166 | Browser* settings_browser = |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 167 | settings_manager_->FindBrowserForProfile(browser()->profile()); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 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()); |
| 172 | |
| 173 | // Open a settings window for a new profile. |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 174 | ShowSettingsForProfile(test_profile); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 175 | Browser* settings_browser2 = |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 176 | settings_manager_->FindBrowserForProfile(test_profile); |
[email protected] | 5f3be2b6a | 2014-04-18 22:27:39 | [diff] [blame] | 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()); |
| 181 | |
| 182 | CloseBrowserSynchronously(settings_browser); |
| 183 | CloseBrowserSynchronously(settings_browser2); |
| 184 | } |
| 185 | #endif |
[email protected] | a59bcc5d | 2014-05-02 00:36:14 | [diff] [blame^] | 186 | |
| 187 | IN_PROC_BROWSER_TEST_F(SettingsWindowManagerTest, OpenSettingsChromePages) { |
| 188 | EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
| 189 | |
| 190 | // Settings should open a new browser window. |
| 191 | chrome::ShowSettings(browser()); |
| 192 | EXPECT_EQ(2u, chrome::GetTotalBrowserCount()); |
| 193 | |
| 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()); |
| 199 | |
| 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()); |
| 206 | |
| 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()); |
| 212 | } |