Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_manager_browsertest.cc
blobdef46bc0713846813a108a145fc3aec175b5a23d
1 // Copyright (c) 2012 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.
5 #include "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/profiles/profile_info_cache.h"
10 #include "chrome/browser/profiles/profile_info_cache_observer.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/profiles/profile_window.h"
13 #include "chrome/browser/profiles/profiles_state.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/host_desktop.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/test_switches.h"
22 #include "chrome/test/base/testing_browser_process.h"
23 #include "chrome/test/base/ui_test_utils.h"
25 #if defined(OS_CHROMEOS)
26 #include "base/path_service.h"
27 #include "chrome/common/chrome_constants.h"
28 #include "chrome/common/chrome_paths.h"
29 #include "chromeos/chromeos_switches.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #endif
33 namespace {
35 const profiles::ProfileSwitchingDoneCallback kOnProfileSwitchDoNothing;
37 // An observer that returns back to test code after a new profile is
38 // initialized.
39 void OnUnblockOnProfileCreation(Profile* profile,
40 Profile::CreateStatus status) {
41 if (status == Profile::CREATE_STATUS_INITIALIZED)
42 base::MessageLoop::current()->Quit();
45 void ProfileCreationComplete(Profile* profile, Profile::CreateStatus status) {
46 ASSERT_NE(status, Profile::CREATE_STATUS_LOCAL_FAIL);
47 ASSERT_NE(status, Profile::CREATE_STATUS_REMOTE_FAIL);
48 // No browser should have been created for this profile yet.
49 EXPECT_EQ(chrome::GetTotalBrowserCountForProfile(profile), 0U);
50 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U);
51 if (status == Profile::CREATE_STATUS_INITIALIZED)
52 base::MessageLoop::current()->Quit();
55 void EphemeralProfileCreationComplete(Profile* profile,
56 Profile::CreateStatus status) {
57 if (status == Profile::CREATE_STATUS_INITIALIZED)
58 profile->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, true);
59 ProfileCreationComplete(profile, status);
62 class ProfileRemovalObserver : public ProfileInfoCacheObserver {
63 public:
64 ProfileRemovalObserver() {
65 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(
66 this);
69 virtual ~ProfileRemovalObserver() {
70 g_browser_process->profile_manager()->GetProfileInfoCache().RemoveObserver(
71 this);
74 std::string last_used_profile_name() { return last_used_profile_name_; }
76 // ProfileInfoCacheObserver overrides:
77 virtual void OnProfileWillBeRemoved(
78 const base::FilePath& profile_path) OVERRIDE {
79 last_used_profile_name_ = g_browser_process->local_state()->GetString(
80 prefs::kProfileLastUsed);
83 private:
84 std::string last_used_profile_name_;
86 DISALLOW_COPY_AND_ASSIGN(ProfileRemovalObserver);
89 } // namespace
91 // This file contains tests for the ProfileManager that require a heavyweight
92 // InProcessBrowserTest. These include tests involving profile deletion.
94 // TODO(jeremy): crbug.com/103355 - These tests should be enabled on all
95 // platforms.
96 class ProfileManagerBrowserTest : public InProcessBrowserTest {
99 #if defined(OS_MACOSX)
101 // Delete single profile and make sure a new one is created.
102 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DeleteSingletonProfile) {
103 ProfileManager* profile_manager = g_browser_process->profile_manager();
104 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
105 ProfileRemovalObserver observer;
107 // We should start out with 1 profile.
108 ASSERT_EQ(cache.GetNumberOfProfiles(), 1U);
110 // Delete singleton profile.
111 base::FilePath singleton_profile_path = cache.GetPathOfProfileAtIndex(0);
112 EXPECT_FALSE(singleton_profile_path.empty());
113 profile_manager->ScheduleProfileForDeletion(singleton_profile_path,
114 ProfileManager::CreateCallback());
116 // Spin things till profile is actually deleted.
117 content::RunAllPendingInMessageLoop();
119 // Make sure a new profile was created automatically.
120 EXPECT_EQ(cache.GetNumberOfProfiles(), 1U);
121 base::FilePath new_profile_path = cache.GetPathOfProfileAtIndex(0);
122 EXPECT_NE(new_profile_path, singleton_profile_path);
124 // Make sure that last used profile preference is set correctly.
125 Profile* last_used = ProfileManager::GetLastUsedProfile();
126 EXPECT_EQ(new_profile_path, last_used->GetPath());
128 // Make sure the last used profile was set correctly before the notification
129 // was sent.
130 std::string last_used_profile_name =
131 last_used->GetPath().BaseName().MaybeAsASCII();
132 EXPECT_EQ(last_used_profile_name, observer.last_used_profile_name());
135 // Delete all profiles in a multi profile setup and make sure a new one is
136 // created.
137 // Crashes/CHECKs. See crbug.com/104851
138 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, DISABLED_DeleteAllProfiles) {
139 ProfileManager* profile_manager = g_browser_process->profile_manager();
140 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
142 // Create an additional profile.
143 base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath();
144 profile_manager->CreateProfileAsync(new_path,
145 base::Bind(&OnUnblockOnProfileCreation),
146 base::string16(), base::string16(),
147 std::string());
149 // Spin to allow profile creation to take place, loop is terminated
150 // by OnUnblockOnProfileCreation when the profile is created.
151 content::RunMessageLoop();
153 ASSERT_EQ(cache.GetNumberOfProfiles(), 2U);
155 // Delete all profiles.
156 base::FilePath profile_path1 = cache.GetPathOfProfileAtIndex(0);
157 base::FilePath profile_path2 = cache.GetPathOfProfileAtIndex(1);
158 EXPECT_FALSE(profile_path1.empty());
159 EXPECT_FALSE(profile_path2.empty());
160 profile_manager->ScheduleProfileForDeletion(profile_path1,
161 ProfileManager::CreateCallback());
162 profile_manager->ScheduleProfileForDeletion(profile_path2,
163 ProfileManager::CreateCallback());
165 // Spin things so deletion can take place.
166 content::RunAllPendingInMessageLoop();
168 // Make sure a new profile was created automatically.
169 EXPECT_EQ(cache.GetNumberOfProfiles(), 1U);
170 base::FilePath new_profile_path = cache.GetPathOfProfileAtIndex(0);
171 EXPECT_NE(new_profile_path, profile_path1);
172 EXPECT_NE(new_profile_path, profile_path2);
174 // Make sure that last used profile preference is set correctly.
175 Profile* last_used = ProfileManager::GetLastUsedProfile();
176 EXPECT_EQ(new_profile_path, last_used->GetPath());
178 #endif // OS_MACOSX
180 #if defined(OS_CHROMEOS)
182 class ProfileManagerCrOSBrowserTest : public ProfileManagerBrowserTest,
183 public testing::WithParamInterface<bool> {
184 protected:
185 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
186 if (GetParam()) {
187 command_line->AppendSwitch(::switches::kMultiProfiles);
188 // Use a user hash other than the default chrome::kTestUserProfileDir
189 // so that the prefix case is tested.
190 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile,
191 "test-user-hash");
196 IN_PROC_BROWSER_TEST_P(ProfileManagerCrOSBrowserTest, GetLastUsedProfile) {
197 // Make sure that last used profile is correct.
198 Profile* last_used_profile = ProfileManager::GetLastUsedProfile();
199 EXPECT_TRUE(last_used_profile != NULL);
201 base::FilePath profile_path;
202 PathService::Get(chrome::DIR_USER_DATA, &profile_path);
204 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
205 if (command_line.HasSwitch(switches::kMultiProfiles)) {
206 profile_path = profile_path.AppendASCII(
207 std::string(chrome::kProfileDirPrefix) + "test-user-hash");
208 } else {
209 profile_path = profile_path.AppendASCII(chrome::kTestUserProfileDir);
211 EXPECT_EQ(profile_path.value(), last_used_profile->GetPath().value());
214 INSTANTIATE_TEST_CASE_P(ProfileManagerCrOSBrowserTestInstantiation,
215 ProfileManagerCrOSBrowserTest,
216 testing::Bool());
218 #endif // OS_CHROMEOS
220 // Times out (http://crbug.com/159002)
221 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest,
222 DISABLED_CreateProfileWithCallback) {
223 ProfileManager* profile_manager = g_browser_process->profile_manager();
225 ASSERT_EQ(profile_manager->GetNumberOfProfiles(), 1U);
226 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U);
228 // Create a profile, make sure callback is invoked before any callbacks are
229 // invoked (so they can do things like sign in the profile, etc).
230 ProfileManager::CreateMultiProfileAsync(
231 base::string16(), // name
232 base::string16(), // icon url
233 base::Bind(ProfileCreationComplete),
234 std::string());
235 // Wait for profile to finish loading.
236 content::RunMessageLoop();
237 EXPECT_EQ(profile_manager->GetNumberOfProfiles(), 2U);
238 EXPECT_EQ(chrome::GetTotalBrowserCount(), 2U);
240 // Now close all browser windows.
241 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles();
242 for (std::vector<Profile*>::const_iterator it = profiles.begin();
243 it != profiles.end(); ++it) {
244 BrowserList::CloseAllBrowsersWithProfile(*it);
248 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest,
249 SwitchToProfile) {
250 #if defined(OS_WIN) && defined(USE_ASH)
251 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
252 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
253 return;
254 #endif
256 // If multiprofile mode is not enabled, you can't switch between profiles.
257 if (!profiles::IsMultipleProfilesEnabled())
258 return;
260 ProfileManager* profile_manager = g_browser_process->profile_manager();
261 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
262 base::FilePath path_profile1 = cache.GetPathOfProfileAtIndex(0);
264 ASSERT_EQ(profile_manager->GetNumberOfProfiles(), 1U);
265 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U);
267 // Create an additional profile.
268 base::FilePath path_profile2 =
269 profile_manager->GenerateNextProfileDirectoryPath();
270 profile_manager->CreateProfileAsync(path_profile2,
271 base::Bind(&OnUnblockOnProfileCreation),
272 base::string16(), base::string16(),
273 std::string());
275 // Spin to allow profile creation to take place, loop is terminated
276 // by OnUnblockOnProfileCreation when the profile is created.
277 content::RunMessageLoop();
279 chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop();
280 BrowserList* browser_list = BrowserList::GetInstance(desktop_type);
281 ASSERT_EQ(cache.GetNumberOfProfiles(), 2U);
282 EXPECT_EQ(1U, browser_list->size());
284 // Open a browser window for the first profile.
285 profiles::SwitchToProfile(path_profile1, desktop_type, false,
286 kOnProfileSwitchDoNothing,
287 ProfileMetrics::SWITCH_PROFILE_ICON);
288 EXPECT_EQ(chrome::GetTotalBrowserCount(), 1U);
289 EXPECT_EQ(1U, browser_list->size());
290 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
292 // Open a browser window for the second profile.
293 profiles::SwitchToProfile(path_profile2, desktop_type, false,
294 kOnProfileSwitchDoNothing,
295 ProfileMetrics::SWITCH_PROFILE_ICON);
296 EXPECT_EQ(chrome::GetTotalBrowserCount(), 2U);
297 EXPECT_EQ(2U, browser_list->size());
298 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath());
300 // Switch to the first profile without opening a new window.
301 profiles::SwitchToProfile(path_profile1, desktop_type, false,
302 kOnProfileSwitchDoNothing,
303 ProfileMetrics::SWITCH_PROFILE_ICON);
304 EXPECT_EQ(chrome::GetTotalBrowserCount(), 2U);
305 EXPECT_EQ(2U, browser_list->size());
307 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
308 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath());
311 // This test used to be flakily timing out on Windows: http://crbug.com/314905.
312 // If this happens again please make it a MAYBE_ test and reopen that bug.
313 IN_PROC_BROWSER_TEST_F(ProfileManagerBrowserTest, EphemeralProfile) {
314 #if defined(OS_WIN) && defined(USE_ASH)
315 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
316 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
317 return;
318 #endif
320 // If multiprofile mode is not enabled, you can't switch between profiles.
321 if (!profiles::IsMultipleProfilesEnabled())
322 return;
324 ProfileManager* profile_manager = g_browser_process->profile_manager();
325 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
326 base::FilePath path_profile1 = cache.GetPathOfProfileAtIndex(0);
328 ASSERT_EQ(1U, profile_manager->GetNumberOfProfiles());
329 EXPECT_EQ(1U, chrome::GetTotalBrowserCount());
331 // Create an ephemeral profile.
332 base::FilePath path_profile2 =
333 profile_manager->GenerateNextProfileDirectoryPath();
334 profile_manager->CreateProfileAsync(
335 path_profile2,
336 base::Bind(&EphemeralProfileCreationComplete),
337 base::string16(), base::string16(), std::string());
339 // Spin to allow profile creation to take place.
340 content::RunMessageLoop();
342 chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop();
343 BrowserList* browser_list = BrowserList::GetInstance(desktop_type);
344 ASSERT_EQ(2U, cache.GetNumberOfProfiles());
345 EXPECT_EQ(1U, browser_list->size());
347 // Open a browser window for the second profile.
348 profiles::SwitchToProfile(path_profile2, desktop_type, false,
349 kOnProfileSwitchDoNothing,
350 ProfileMetrics::SWITCH_PROFILE_ICON);
351 EXPECT_EQ(2U, chrome::GetTotalBrowserCount());
352 EXPECT_EQ(2U, browser_list->size());
353 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath());
355 // Create a second window for the ephemeral profile.
356 profiles::SwitchToProfile(path_profile2, desktop_type, true,
357 kOnProfileSwitchDoNothing,
358 ProfileMetrics::SWITCH_PROFILE_ICON);
359 EXPECT_EQ(3U, chrome::GetTotalBrowserCount());
360 EXPECT_EQ(3U, browser_list->size());
362 EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
363 EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath());
364 EXPECT_EQ(path_profile2, browser_list->get(2)->profile()->GetPath());
366 // Closing the first window of the ephemeral profile should not delete it.
367 browser_list->get(2)->window()->Close();
368 content::RunAllPendingInMessageLoop();
369 EXPECT_EQ(2U, browser_list->size());
370 ASSERT_EQ(2U, cache.GetNumberOfProfiles());
372 // The second should though.
373 browser_list->get(1)->window()->Close();
374 content::RunAllPendingInMessageLoop();
375 EXPECT_EQ(1U, browser_list->size());
376 ASSERT_EQ(1U, cache.GetNumberOfProfiles());