blob: 1ae59f3d9806e120f19f23f96504a0e0e26b189c [file] [log] [blame]
[email protected]ebbbb9f2011-03-09 13:16:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d99bcaa2010-12-05 20:15:565#include <string>
6
[email protected]943aed5542010-06-11 15:57:087#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/file_util.h"
[email protected]ab820df2008-08-26 05:55:109#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/path_service.h"
[email protected]81ad7f4a2011-03-16 01:33:2911#include "chrome/browser/prefs/browser_prefs.h"
[email protected]8ecad5e2010-12-02 21:18:3312#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/profiles/profile_manager.h"
[email protected]943aed5542010-06-11 15:57:0814#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2915#include "chrome/common/chrome_paths.h"
[email protected]943aed5542010-06-11 15:57:0816#include "chrome/common/chrome_switches.h"
[email protected]81ad7f4a2011-03-16 01:33:2917#include "chrome/test/testing_browser_process.h"
18#include "chrome/test/testing_pref_service.h"
[email protected]1bda97552011-03-01 20:11:5219#include "content/browser/browser_thread.h"
[email protected]ebbbb9f2011-03-09 13:16:1420#include "content/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2921#include "testing/gtest/include/gtest/gtest.h"
[email protected]4ae61df2011-01-19 15:29:4722#include "ui/base/system_monitor/system_monitor.h"
initial.commit09911bf2008-07-26 23:55:2923
initial.commit09911bf2008-07-26 23:55:2924class ProfileManagerTest : public testing::Test {
[email protected]ee5e3792009-10-13 23:23:4725 protected:
[email protected]6cad5bf2011-03-10 21:21:5526 ProfileManagerTest()
27 : ui_thread_(BrowserThread::UI, &message_loop_),
28 file_thread_(BrowserThread::FILE, &message_loop_) {
[email protected]cb0e4f12009-12-03 00:09:0929 }
30
initial.commit09911bf2008-07-26 23:55:2931 virtual void SetUp() {
32 // Name a subdirectory of the temp directory.
33 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
[email protected]f7011fcb2009-01-28 21:54:3234 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("ProfileManagerTest"));
initial.commit09911bf2008-07-26 23:55:2935
36 // Create a fresh, empty copy of this directory.
37 file_util::Delete(test_dir_, true);
[email protected]f7011fcb2009-01-28 21:54:3238 file_util::CreateDirectory(test_dir_);
[email protected]81ad7f4a2011-03-16 01:33:2939
40 // Create a local_state PrefService.
41 browser::RegisterLocalState(&test_local_state_);
42 TestingBrowserProcess* testing_browser_process =
43 static_cast<TestingBrowserProcess*>(g_browser_process);
44 testing_browser_process->SetPrefService(&test_local_state_);
initial.commit09911bf2008-07-26 23:55:2945 }
[email protected]81ad7f4a2011-03-16 01:33:2946
initial.commit09911bf2008-07-26 23:55:2947 virtual void TearDown() {
48 // Clean up test directory
49 ASSERT_TRUE(file_util::Delete(test_dir_, true));
50 ASSERT_FALSE(file_util::PathExists(test_dir_));
[email protected]81ad7f4a2011-03-16 01:33:2951
52 TestingBrowserProcess* testing_browser_process =
53 static_cast<TestingBrowserProcess*>(g_browser_process);
54 testing_browser_process->SetPrefService(NULL);
initial.commit09911bf2008-07-26 23:55:2955 }
[email protected]f0a51fb52009-03-05 12:46:3856
[email protected]ab820df2008-08-26 05:55:1057 MessageLoopForUI message_loop_;
[email protected]f8b3ef82010-10-11 02:45:5258 BrowserThread ui_thread_;
[email protected]6cad5bf2011-03-10 21:21:5559 BrowserThread file_thread_;
initial.commit09911bf2008-07-26 23:55:2960
61 // the path to temporary directory used to contain the test operations
[email protected]f7011fcb2009-01-28 21:54:3262 FilePath test_dir_;
[email protected]81ad7f4a2011-03-16 01:33:2963
64 TestingPrefService test_local_state_;
initial.commit09911bf2008-07-26 23:55:2965};
66
initial.commit09911bf2008-07-26 23:55:2967TEST_F(ProfileManagerTest, CreateProfile) {
[email protected]f7011fcb2009-01-28 21:54:3268 FilePath source_path;
initial.commit09911bf2008-07-26 23:55:2969 PathService::Get(chrome::DIR_TEST_DATA, &source_path);
[email protected]f7011fcb2009-01-28 21:54:3270 source_path = source_path.Append(FILE_PATH_LITERAL("profiles"));
71 source_path = source_path.Append(FILE_PATH_LITERAL("sample"));
initial.commit09911bf2008-07-26 23:55:2972
[email protected]f7011fcb2009-01-28 21:54:3273 FilePath dest_path = test_dir_;
74 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
initial.commit09911bf2008-07-26 23:55:2975
76 scoped_ptr<Profile> profile;
77
78 // Successfully create a profile.
[email protected]9acc48602009-11-30 21:18:5179 profile.reset(ProfileManager::CreateProfile(dest_path));
initial.commit09911bf2008-07-26 23:55:2980 ASSERT_TRUE(profile.get());
81
initial.commit09911bf2008-07-26 23:55:2982 profile.reset();
83
84#ifdef NDEBUG
85 // In Release mode, we always try to always return a profile. In debug,
86 // these cases would trigger DCHECKs.
87
88 // The profile already exists when we call CreateProfile. Just load it.
[email protected]9acc48602009-11-30 21:18:5189 profile.reset(ProfileManager::CreateProfile(dest_path));
initial.commit09911bf2008-07-26 23:55:2990 ASSERT_TRUE(profile.get());
initial.commit09911bf2008-07-26 23:55:2991#endif
92}
[email protected]55474b572009-04-14 22:05:3393
[email protected]943aed5542010-06-11 15:57:0894TEST_F(ProfileManagerTest, DefaultProfileDir) {
95 CommandLine *cl = CommandLine::ForCurrentProcess();
[email protected]4ae61df2011-01-19 15:29:4796 ui::SystemMonitor dummy;
[email protected]943aed5542010-06-11 15:57:0897 ProfileManager profile_manager;
98 std::string profile_dir("my_user");
99
100 cl->AppendSwitch(switches::kTestType);
101
102 FilePath expected_default =
[email protected]162b5992011-03-15 19:40:48103 FilePath().AppendASCII(chrome::kNotSignedInProfile);
[email protected]943aed5542010-06-11 15:57:08104 EXPECT_EQ(expected_default.value(),
105 profile_manager.GetCurrentProfileDir().value());
106}
107
108#if defined(OS_CHROMEOS)
109// This functionality only exists on Chrome OS.
110TEST_F(ProfileManagerTest, LoggedInProfileDir) {
111 CommandLine *cl = CommandLine::ForCurrentProcess();
[email protected]4ae61df2011-01-19 15:29:47112 ui::SystemMonitor dummy;
[email protected]943aed5542010-06-11 15:57:08113 ProfileManager profile_manager;
114 std::string profile_dir("my_user");
115
[email protected]05076ba22010-07-30 05:59:57116 cl->AppendSwitchASCII(switches::kLoginProfile, profile_dir);
[email protected]943aed5542010-06-11 15:57:08117 cl->AppendSwitch(switches::kTestType);
118
119 FilePath expected_default =
[email protected]162b5992011-03-15 19:40:48120 FilePath().AppendASCII(chrome::kNotSignedInProfile);
[email protected]943aed5542010-06-11 15:57:08121 EXPECT_EQ(expected_default.value(),
122 profile_manager.GetCurrentProfileDir().value());
123
124 profile_manager.Observe(NotificationType::LOGIN_USER_CHANGED,
125 NotificationService::AllSources(),
126 NotificationService::NoDetails());
127 FilePath expected_logged_in(profile_dir);
128 EXPECT_EQ(expected_logged_in.value(),
129 profile_manager.GetCurrentProfileDir().value());
[email protected]8e96e502010-10-21 20:57:12130 VLOG(1) << test_dir_.Append(profile_manager.GetCurrentProfileDir()).value();
[email protected]943aed5542010-06-11 15:57:08131}
132
133#endif
134
[email protected]52d69b882010-06-17 17:35:51135TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
[email protected]55474b572009-04-14 22:05:33136 FilePath source_path;
137 PathService::Get(chrome::DIR_TEST_DATA, &source_path);
138 source_path = source_path.Append(FILE_PATH_LITERAL("profiles"));
139 source_path = source_path.Append(FILE_PATH_LITERAL("sample"));
140
141 FilePath dest_path1 = test_dir_;
142 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]ee5e3792009-10-13 23:23:47143
[email protected]55474b572009-04-14 22:05:33144 FilePath dest_path2 = test_dir_;
145 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
146
147 scoped_ptr<Profile> profile1;
148 scoped_ptr<Profile> profile2;
149
150 // Successfully create the profiles.
[email protected]9acc48602009-11-30 21:18:51151 profile1.reset(ProfileManager::CreateProfile(dest_path1));
[email protected]55474b572009-04-14 22:05:33152 ASSERT_TRUE(profile1.get());
153
[email protected]9acc48602009-11-30 21:18:51154 profile2.reset(ProfileManager::CreateProfile(dest_path2));
[email protected]55474b572009-04-14 22:05:33155 ASSERT_TRUE(profile2.get());
156
157 // Force lazy-init of some profile services to simulate use.
158 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS));
159 EXPECT_TRUE(profile1->GetBookmarkModel());
160 EXPECT_TRUE(profile2->GetBookmarkModel());
161 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS));
[email protected]6cad5bf2011-03-10 21:21:55162
163 // Make sure any pending tasks run before we destroy the profiles.
164 message_loop_.RunAllPending();
165
[email protected]55474b572009-04-14 22:05:33166 profile1.reset();
167 profile2.reset();
[email protected]6cad5bf2011-03-10 21:21:55168
[email protected]237e6d02010-11-08 21:45:42169 // Make sure history cleans up correctly.
170 message_loop_.RunAllPending();
[email protected]55474b572009-04-14 22:05:33171}