license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "base/file_util.h" |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 6 | #include "base/message_loop.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | class ProfileManagerTest : public testing::Test { |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame^] | 16 | protected: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | virtual void SetUp() { |
| 18 | // Name a subdirectory of the temp directory. |
| 19 | ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 20 | test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("ProfileManagerTest")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | |
| 22 | // Create a fresh, empty copy of this directory. |
| 23 | file_util::Delete(test_dir_, true); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 24 | file_util::CreateDirectory(test_dir_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | } |
| 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] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 31 | |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 32 | MessageLoopForUI message_loop_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | |
| 34 | // the path to temporary directory used to contain the test operations |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 35 | FilePath test_dir_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | TEST_F(ProfileManagerTest, CopyProfileData) { |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 39 | FilePath source_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | PathService::Get(chrome::DIR_TEST_DATA, &source_path); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 41 | source_path = source_path.Append(FILE_PATH_LITERAL("profiles")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | |
| 43 | ASSERT_FALSE(ProfileManager::IsProfile(source_path)); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 44 | source_path = source_path.Append(FILE_PATH_LITERAL("sample")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | ASSERT_TRUE(ProfileManager::IsProfile(source_path)); |
| 46 | |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 47 | FilePath dest_path = test_dir_; |
| 48 | dest_path = dest_path.Append(FILE_PATH_LITERAL("profile_copy")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | ASSERT_FALSE(ProfileManager::IsProfile(dest_path)); |
| 50 | ASSERT_TRUE(ProfileManager::CopyProfileData(source_path, dest_path)); |
| 51 | ASSERT_TRUE(ProfileManager::IsProfile(dest_path)); |
| 52 | } |
| 53 | |
| 54 | TEST_F(ProfileManagerTest, CreateProfile) { |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 55 | FilePath source_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | PathService::Get(chrome::DIR_TEST_DATA, &source_path); |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 57 | source_path = source_path.Append(FILE_PATH_LITERAL("profiles")); |
| 58 | source_path = source_path.Append(FILE_PATH_LITERAL("sample")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 60 | FilePath dest_path = test_dir_; |
| 61 | dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | |
| 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] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 88 | |
[email protected] | 3bacc9d | 2009-04-15 17:15:41 | [diff] [blame] | 89 | // TODO(timsteele): This is disabled while I try to track down a purify |
| 90 | // regression (http://crbug.com/10553). |
| 91 | TEST_F(ProfileManagerTest, DISABLED_CreateAndUseTwoProfiles) { |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 92 | 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] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame^] | 99 | |
[email protected] | 55474b57 | 2009-04-14 22:05:33 | [diff] [blame] | 100 | 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 | } |