blob: b4f328ed325a119d04dcd3ecbe1a6321875d5b3a [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
5#include "base/file_util.h"
[email protected]ab820df2008-08-26 05:55:106#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:297#include "base/path_service.h"
8#include "chrome/browser/profile.h"
9#include "chrome/browser/profile_manager.h"
10#include "chrome/common/chrome_paths.h"
11#include "chrome/common/pref_names.h"
12#include "chrome/common/pref_service.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
initial.commit09911bf2008-07-26 23:55:2915class ProfileManagerTest : public testing::Test {
[email protected]ee5e3792009-10-13 23:23:4716 protected:
initial.commit09911bf2008-07-26 23:55:2917 virtual void SetUp() {
18 // Name a subdirectory of the temp directory.
19 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
[email protected]f7011fcb2009-01-28 21:54:3220 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("ProfileManagerTest"));
initial.commit09911bf2008-07-26 23:55:2921
22 // Create a fresh, empty copy of this directory.
23 file_util::Delete(test_dir_, true);
[email protected]f7011fcb2009-01-28 21:54:3224 file_util::CreateDirectory(test_dir_);
initial.commit09911bf2008-07-26 23:55:2925 }
26 virtual void TearDown() {
27 // Clean up test directory
28 ASSERT_TRUE(file_util::Delete(test_dir_, true));
29 ASSERT_FALSE(file_util::PathExists(test_dir_));
30 }
[email protected]f0a51fb52009-03-05 12:46:3831
[email protected]ab820df2008-08-26 05:55:1032 MessageLoopForUI message_loop_;
initial.commit09911bf2008-07-26 23:55:2933
34 // the path to temporary directory used to contain the test operations
[email protected]f7011fcb2009-01-28 21:54:3235 FilePath test_dir_;
initial.commit09911bf2008-07-26 23:55:2936};
37
38TEST_F(ProfileManagerTest, CopyProfileData) {
[email protected]f7011fcb2009-01-28 21:54:3239 FilePath source_path;
initial.commit09911bf2008-07-26 23:55:2940 PathService::Get(chrome::DIR_TEST_DATA, &source_path);
[email protected]f7011fcb2009-01-28 21:54:3241 source_path = source_path.Append(FILE_PATH_LITERAL("profiles"));
initial.commit09911bf2008-07-26 23:55:2942
43 ASSERT_FALSE(ProfileManager::IsProfile(source_path));
[email protected]f7011fcb2009-01-28 21:54:3244 source_path = source_path.Append(FILE_PATH_LITERAL("sample"));
initial.commit09911bf2008-07-26 23:55:2945 ASSERT_TRUE(ProfileManager::IsProfile(source_path));
46
[email protected]f7011fcb2009-01-28 21:54:3247 FilePath dest_path = test_dir_;
48 dest_path = dest_path.Append(FILE_PATH_LITERAL("profile_copy"));
initial.commit09911bf2008-07-26 23:55:2949 ASSERT_FALSE(ProfileManager::IsProfile(dest_path));
50 ASSERT_TRUE(ProfileManager::CopyProfileData(source_path, dest_path));
51 ASSERT_TRUE(ProfileManager::IsProfile(dest_path));
52}
53
54TEST_F(ProfileManagerTest, CreateProfile) {
[email protected]f7011fcb2009-01-28 21:54:3255 FilePath source_path;
initial.commit09911bf2008-07-26 23:55:2956 PathService::Get(chrome::DIR_TEST_DATA, &source_path);
[email protected]f7011fcb2009-01-28 21:54:3257 source_path = source_path.Append(FILE_PATH_LITERAL("profiles"));
58 source_path = source_path.Append(FILE_PATH_LITERAL("sample"));
initial.commit09911bf2008-07-26 23:55:2959
[email protected]f7011fcb2009-01-28 21:54:3260 FilePath dest_path = test_dir_;
61 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
initial.commit09911bf2008-07-26 23:55:2962
63 scoped_ptr<Profile> profile;
64
65 // Successfully create a profile.
66 profile.reset(ProfileManager::CreateProfile(dest_path, L"New Profile", L"",
67 L"new-profile"));
68 ASSERT_TRUE(profile.get());
69
70 PrefService* prefs = profile->GetPrefs();
71 ASSERT_EQ(L"New Profile", prefs->GetString(prefs::kProfileName));
72 ASSERT_EQ(L"new-profile", prefs->GetString(prefs::kProfileID));
73 profile.reset();
74
75#ifdef NDEBUG
76 // In Release mode, we always try to always return a profile. In debug,
77 // these cases would trigger DCHECKs.
78
79 // The profile already exists when we call CreateProfile. Just load it.
80 profile.reset(ProfileManager::CreateProfile(dest_path, L"New Profile", L"",
81 L"new-profile"));
82 ASSERT_TRUE(profile.get());
83 prefs = profile->GetPrefs();
84 ASSERT_EQ(L"New Profile", prefs->GetString(prefs::kProfileName));
85 ASSERT_EQ(L"new-profile", prefs->GetString(prefs::kProfileID));
86#endif
87}
[email protected]55474b572009-04-14 22:05:3388
[email protected]3bacc9d2009-04-15 17:15:4189// TODO(timsteele): This is disabled while I try to track down a purify
90// regression (http://crbug.com/10553).
91TEST_F(ProfileManagerTest, DISABLED_CreateAndUseTwoProfiles) {
[email protected]55474b572009-04-14 22:05:3392 FilePath source_path;
93 PathService::Get(chrome::DIR_TEST_DATA, &source_path);
94 source_path = source_path.Append(FILE_PATH_LITERAL("profiles"));
95 source_path = source_path.Append(FILE_PATH_LITERAL("sample"));
96
97 FilePath dest_path1 = test_dir_;
98 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
[email protected]ee5e3792009-10-13 23:23:4799
[email protected]55474b572009-04-14 22:05:33100 FilePath dest_path2 = test_dir_;
101 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
102
103 scoped_ptr<Profile> profile1;
104 scoped_ptr<Profile> profile2;
105
106 // Successfully create the profiles.
107 profile1.reset(ProfileManager::CreateProfile(dest_path1, L"New Profile 1",
108 L"", L"new-profile-1"));
109 ASSERT_TRUE(profile1.get());
110
111 profile2.reset(ProfileManager::CreateProfile(dest_path2, L"New Profile 2",
112 L"", L"new-profile-2"));
113 ASSERT_TRUE(profile2.get());
114
115 // Force lazy-init of some profile services to simulate use.
116 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS));
117 EXPECT_TRUE(profile1->GetBookmarkModel());
118 EXPECT_TRUE(profile2->GetBookmarkModel());
119 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS));
120 profile1.reset();
121 profile2.reset();
122}