Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "base/memory/ptr_util.h" | |
| 6 #include "base/run_loop.h" | |
| 7 #include "base/scoped_observer.h" | |
| 8 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/browsing_data/browsing_data_remover_impl.h" | |
| 10 #include "chrome/browser/chrome_notification_types.h" | |
| 11 #include "chrome/browser/profiles/profile_attributes_storage.h" | |
| 12 #include "chrome/browser/profiles/profile_manager.h" | |
| 13 #include "chrome/browser/ui/browser.h" | |
| 14 #include "chrome/browser/ui/browser_list.h" | |
| 15 #include "chrome/browser/ui/browser_list_observer.h" | |
| 16 #include "chrome/browser/ui/webui/profile_helper.h" | |
| 17 #include "chrome/test/base/in_process_browser_test.h" | |
| 18 #include "content/public/browser/notification_service.h" | |
| 19 #include "content/public/test/test_utils.h" | |
| 20 #include "content/public/test/test_web_ui.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // An observer that returns back to test code after a new profile is | |
| 25 // initialized. | |
| 26 void UnblockOnProfileCreation(base::RunLoop* run_loop, | |
| 27 Profile* profile, | |
| 28 Profile::CreateStatus status) { | |
| 29 if (status == Profile::CREATE_STATUS_INITIALIZED) | |
| 30 run_loop->Quit(); | |
| 31 } | |
| 32 | |
| 33 Profile* CreateProfile() { | |
| 34 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 35 base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath(); | |
| 36 base::RunLoop run_loop; | |
| 37 profile_manager->CreateProfileAsync( | |
| 38 new_path, base::Bind(&UnblockOnProfileCreation, &run_loop), | |
| 39 base::string16(), std::string(), std::string()); | |
| 40 run_loop.Run(); | |
| 41 return profile_manager->GetProfileByPath(new_path); | |
| 42 } | |
| 43 | |
| 44 // An observer returns back to test code after brower window associated with | |
| 45 // the profile is activated. | |
| 46 class ExpectBrowserActivationForProfile : public chrome::BrowserListObserver { | |
| 47 public: | |
| 48 explicit ExpectBrowserActivationForProfile(Profile* profile) | |
| 49 : profile_(profile), scoped_observer_(this) { | |
| 50 scoped_observer_.Add(BrowserList::GetInstance()); | |
| 51 } | |
| 52 | |
| 53 void Wait() { | |
| 54 loop_.Run(); | |
| 55 } | |
| 56 | |
| 57 protected: | |
| 58 void OnBrowserSetLastActive(Browser* browser) override { | |
| 59 if (browser->profile() == profile_) | |
| 60 loop_.Quit(); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 Profile* profile_; | |
| 65 base::RunLoop loop_; | |
| 66 ScopedObserver<BrowserList, chrome::BrowserListObserver> scoped_observer_; | |
| 67 }; | |
| 68 | |
| 69 // TODO(bug 704601): remove it when bug is fixed. | |
| 70 class BrowsingDataRemoverObserver | |
|
Bernhard Bauer
2017/04/03 10:57:39
There is an actual BrowsingDataRemover::Observer c
| |
| 71 : public BrowsingDataRemoverImpl::CompletionInhibitor { | |
| 72 public: | |
| 73 explicit BrowsingDataRemoverObserver(const base::Closure& callback) | |
| 74 : callback_(callback) { | |
| 75 BrowsingDataRemoverImpl::set_completion_inhibitor_for_testing(this); | |
| 76 } | |
| 77 ~BrowsingDataRemoverObserver() { | |
| 78 BrowsingDataRemoverImpl::set_completion_inhibitor_for_testing(nullptr); | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 void OnBrowsingDataRemoverWouldComplete( | |
| 83 BrowsingDataRemoverImpl* remover, | |
| 84 const base::Closure& continue_to_completion) override { | |
| 85 continue_to_completion.Run(); | |
| 86 callback_.Run(); | |
| 87 } | |
| 88 | |
| 89 const base::Closure callback_; | |
| 90 }; | |
| 91 | |
| 92 } // namespace | |
| 93 | |
| 94 using ProfileHelperTest = InProcessBrowserTest; | |
| 95 | |
| 96 IN_PROC_BROWSER_TEST_F(ProfileHelperTest, OpenNewWindowForProfile) { | |
| 97 BrowserList* browser_list = BrowserList::GetInstance(); | |
| 98 | |
| 99 Browser* original_browser = browser(); | |
| 100 Profile* original_profile = original_browser->profile(); | |
| 101 std::unique_ptr<ExpectBrowserActivationForProfile> activation_observer; | |
| 102 | |
| 103 // Sanity checks. | |
| 104 EXPECT_EQ(1u, browser_list->size()); | |
| 105 EXPECT_TRUE(base::ContainsValue(*browser_list, original_browser)); | |
| 106 | |
| 107 // Opening existing browser profile shouldn't open additional browser windows. | |
| 108 webui::OpenNewWindowForProfile(original_profile); | |
| 109 EXPECT_EQ(1u, browser_list->size()); | |
| 110 EXPECT_EQ(original_browser, browser_list->GetLastActive()); | |
| 111 | |
| 112 // Open additional browser will add new window and activates it. | |
| 113 Profile* additional_profile = CreateProfile(); | |
| 114 activation_observer = | |
| 115 base::MakeUnique<ExpectBrowserActivationForProfile>(additional_profile); | |
| 116 webui::OpenNewWindowForProfile(additional_profile); | |
| 117 EXPECT_EQ(2u, browser_list->size()); | |
| 118 activation_observer->Wait(); | |
| 119 EXPECT_EQ(additional_profile, browser_list->GetLastActive()->profile()); | |
| 120 | |
| 121 // On Macs OpenNewWindowForProfile does not activate existing browser | |
| 122 // while non of the browser windows have focus. BrowserWindowCocoa::Show() got | |
| 123 // the same issue as BrowserWindowCocoa::Activate(), and execute call | |
| 124 // BrowserList::SetLastActive() directly. Not sure if it is a bug or desired | |
| 125 // behaviour. | |
| 126 #if !defined(OS_MACOSX) | |
| 127 // Switch to original browser. Only LastActive should change. | |
| 128 activation_observer = | |
| 129 base::MakeUnique<ExpectBrowserActivationForProfile>(original_profile); | |
| 130 webui::OpenNewWindowForProfile(original_profile); | |
| 131 EXPECT_EQ(2u, browser_list->size()); | |
| 132 activation_observer->Wait(); | |
| 133 EXPECT_EQ(original_profile, browser_list->GetLastActive()->profile()); | |
| 134 #endif | |
| 135 } | |
| 136 | |
| 137 IN_PROC_BROWSER_TEST_F(ProfileHelperTest, DeleteSoleProfile) { | |
| 138 content::TestWebUI web_ui; | |
| 139 Browser* original_browser = browser(); | |
| 140 ProfileAttributesStorage& storage = | |
| 141 g_browser_process->profile_manager()->GetProfileAttributesStorage(); | |
| 142 | |
| 143 BrowserList* browser_list = BrowserList::GetInstance(); | |
| 144 EXPECT_EQ(1u, browser_list->size()); | |
| 145 EXPECT_TRUE(base::ContainsValue(*browser_list, original_browser)); | |
| 146 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); | |
| 147 | |
| 148 // Original browser will be closed, and browser with the new profile created. | |
| 149 content::WindowedNotificationObserver open_observer( | |
| 150 chrome::NOTIFICATION_BROWSER_OPENED, | |
| 151 content::NotificationService::AllSources()); | |
| 152 content::WindowedNotificationObserver close_observer( | |
| 153 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(browser())); | |
| 154 webui::DeleteProfileAtPath(original_browser->profile()->GetPath(), &web_ui, | |
| 155 ProfileMetrics::DELETE_PROFILE_SETTINGS); | |
| 156 open_observer.Wait(); | |
| 157 close_observer.Wait(); | |
| 158 | |
| 159 EXPECT_EQ(1u, browser_list->size()); | |
| 160 EXPECT_FALSE(base::ContainsValue(*browser_list, original_browser)); | |
| 161 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); | |
| 162 } | |
| 163 | |
| 164 IN_PROC_BROWSER_TEST_F(ProfileHelperTest, DeleteActiveProfile) { | |
| 165 content::TestWebUI web_ui; | |
| 166 Browser* original_browser = browser(); | |
| 167 ProfileAttributesStorage& storage = | |
| 168 g_browser_process->profile_manager()->GetProfileAttributesStorage(); | |
| 169 | |
| 170 BrowserList* browser_list = BrowserList::GetInstance(); | |
| 171 EXPECT_EQ(1u, browser_list->size()); | |
| 172 EXPECT_TRUE(base::ContainsValue(*browser_list, original_browser)); | |
| 173 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); | |
| 174 | |
| 175 Profile* additional_profile = CreateProfile(); | |
| 176 EXPECT_EQ(2u, storage.GetNumberOfProfiles()); | |
| 177 | |
| 178 // Original browser will be closed, and browser with the new profile created. | |
| 179 content::WindowedNotificationObserver open_observer( | |
| 180 chrome::NOTIFICATION_BROWSER_OPENED, | |
| 181 content::NotificationService::AllSources()); | |
| 182 content::WindowedNotificationObserver close_observer( | |
| 183 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(browser())); | |
| 184 webui::DeleteProfileAtPath(original_browser->profile()->GetPath(), &web_ui, | |
| 185 ProfileMetrics::DELETE_PROFILE_SETTINGS); | |
| 186 open_observer.Wait(); | |
| 187 close_observer.Wait(); | |
| 188 | |
| 189 EXPECT_EQ(1u, browser_list->size()); | |
| 190 EXPECT_EQ(additional_profile, browser_list->get(0)->profile()); | |
| 191 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); | |
| 192 } | |
| 193 | |
| 194 IN_PROC_BROWSER_TEST_F(ProfileHelperTest, DeleteInactiveProfile) { | |
| 195 content::TestWebUI web_ui; | |
| 196 Browser* original_browser = browser(); | |
| 197 ProfileAttributesStorage& storage = | |
| 198 g_browser_process->profile_manager()->GetProfileAttributesStorage(); | |
| 199 | |
| 200 BrowserList* browser_list = BrowserList::GetInstance(); | |
| 201 EXPECT_EQ(1u, browser_list->size()); | |
| 202 EXPECT_TRUE(base::ContainsValue(*browser_list, original_browser)); | |
| 203 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); | |
| 204 | |
| 205 Profile* additional_profile = CreateProfile(); | |
| 206 EXPECT_EQ(2u, storage.GetNumberOfProfiles()); | |
| 207 | |
| 208 base::RunLoop loop; | |
| 209 BrowsingDataRemoverObserver observer(loop.QuitClosure()); | |
| 210 webui::DeleteProfileAtPath(additional_profile->GetPath(), &web_ui, | |
| 211 ProfileMetrics::DELETE_PROFILE_SETTINGS); | |
| 212 loop.Run(); | |
| 213 | |
| 214 EXPECT_EQ(1u, browser_list->size()); | |
| 215 EXPECT_TRUE(base::ContainsValue(*browser_list, original_browser)); | |
| 216 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); | |
| 217 } | |
| OLD | NEW |