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