| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 "app/system_monitor.h" | |
| 6 #include "base/command_line.h" | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/path_service.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/browser_thread.h" | |
| 12 #include "chrome/browser/prefs/pref_service.h" | |
| 13 #include "chrome/browser/profile.h" | |
| 14 #include "chrome/browser/profile_manager.h" | |
| 15 #include "chrome/common/chrome_constants.h" | |
| 16 #include "chrome/common/chrome_paths.h" | |
| 17 #include "chrome/common/chrome_switches.h" | |
| 18 #include "chrome/common/notification_service.h" | |
| 19 #include "chrome/common/pref_names.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 class ProfileManagerTest : public testing::Test { | |
| 23 protected: | |
| 24 ProfileManagerTest() : ui_thread_(BrowserThread::UI, &message_loop_) { | |
| 25 } | |
| 26 | |
| 27 virtual void SetUp() { | |
| 28 // Name a subdirectory of the temp directory. | |
| 29 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | |
| 30 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("ProfileManagerTest")); | |
| 31 | |
| 32 // Create a fresh, empty copy of this directory. | |
| 33 file_util::Delete(test_dir_, true); | |
| 34 file_util::CreateDirectory(test_dir_); | |
| 35 } | |
| 36 virtual void TearDown() { | |
| 37 // Clean up test directory | |
| 38 ASSERT_TRUE(file_util::Delete(test_dir_, true)); | |
| 39 ASSERT_FALSE(file_util::PathExists(test_dir_)); | |
| 40 } | |
| 41 | |
| 42 MessageLoopForUI message_loop_; | |
| 43 BrowserThread ui_thread_; | |
| 44 | |
| 45 // the path to temporary directory used to contain the test operations | |
| 46 FilePath test_dir_; | |
| 47 }; | |
| 48 | |
| 49 TEST_F(ProfileManagerTest, CreateProfile) { | |
| 50 FilePath source_path; | |
| 51 PathService::Get(chrome::DIR_TEST_DATA, &source_path); | |
| 52 source_path = source_path.Append(FILE_PATH_LITERAL("profiles")); | |
| 53 source_path = source_path.Append(FILE_PATH_LITERAL("sample")); | |
| 54 | |
| 55 FilePath dest_path = test_dir_; | |
| 56 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); | |
| 57 | |
| 58 scoped_ptr<Profile> profile; | |
| 59 | |
| 60 // Successfully create a profile. | |
| 61 profile.reset(ProfileManager::CreateProfile(dest_path)); | |
| 62 ASSERT_TRUE(profile.get()); | |
| 63 | |
| 64 profile.reset(); | |
| 65 | |
| 66 #ifdef NDEBUG | |
| 67 // In Release mode, we always try to always return a profile. In debug, | |
| 68 // these cases would trigger DCHECKs. | |
| 69 | |
| 70 // The profile already exists when we call CreateProfile. Just load it. | |
| 71 profile.reset(ProfileManager::CreateProfile(dest_path)); | |
| 72 ASSERT_TRUE(profile.get()); | |
| 73 #endif | |
| 74 } | |
| 75 | |
| 76 TEST_F(ProfileManagerTest, DefaultProfileDir) { | |
| 77 CommandLine *cl = CommandLine::ForCurrentProcess(); | |
| 78 SystemMonitor dummy; | |
| 79 ProfileManager profile_manager; | |
| 80 std::string profile_dir("my_user"); | |
| 81 | |
| 82 cl->AppendSwitch(switches::kTestType); | |
| 83 | |
| 84 FilePath expected_default = | |
| 85 FilePath::FromWStringHack(chrome::kNotSignedInProfile); | |
| 86 EXPECT_EQ(expected_default.value(), | |
| 87 profile_manager.GetCurrentProfileDir().value()); | |
| 88 } | |
| 89 | |
| 90 #if defined(OS_CHROMEOS) | |
| 91 // This functionality only exists on Chrome OS. | |
| 92 TEST_F(ProfileManagerTest, LoggedInProfileDir) { | |
| 93 CommandLine *cl = CommandLine::ForCurrentProcess(); | |
| 94 SystemMonitor dummy; | |
| 95 ProfileManager profile_manager; | |
| 96 std::string profile_dir("my_user"); | |
| 97 | |
| 98 cl->AppendSwitchASCII(switches::kLoginProfile, profile_dir); | |
| 99 cl->AppendSwitch(switches::kTestType); | |
| 100 | |
| 101 FilePath expected_default = | |
| 102 FilePath::FromWStringHack(chrome::kNotSignedInProfile); | |
| 103 EXPECT_EQ(expected_default.value(), | |
| 104 profile_manager.GetCurrentProfileDir().value()); | |
| 105 | |
| 106 profile_manager.Observe(NotificationType::LOGIN_USER_CHANGED, | |
| 107 NotificationService::AllSources(), | |
| 108 NotificationService::NoDetails()); | |
| 109 FilePath expected_logged_in(profile_dir); | |
| 110 EXPECT_EQ(expected_logged_in.value(), | |
| 111 profile_manager.GetCurrentProfileDir().value()); | |
| 112 VLOG(1) << test_dir_.Append(profile_manager.GetCurrentProfileDir()).value(); | |
| 113 } | |
| 114 | |
| 115 #endif | |
| 116 | |
| 117 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { | |
| 118 FilePath source_path; | |
| 119 PathService::Get(chrome::DIR_TEST_DATA, &source_path); | |
| 120 source_path = source_path.Append(FILE_PATH_LITERAL("profiles")); | |
| 121 source_path = source_path.Append(FILE_PATH_LITERAL("sample")); | |
| 122 | |
| 123 FilePath dest_path1 = test_dir_; | |
| 124 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); | |
| 125 | |
| 126 FilePath dest_path2 = test_dir_; | |
| 127 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); | |
| 128 | |
| 129 scoped_ptr<Profile> profile1; | |
| 130 scoped_ptr<Profile> profile2; | |
| 131 | |
| 132 // Successfully create the profiles. | |
| 133 profile1.reset(ProfileManager::CreateProfile(dest_path1)); | |
| 134 ASSERT_TRUE(profile1.get()); | |
| 135 | |
| 136 profile2.reset(ProfileManager::CreateProfile(dest_path2)); | |
| 137 ASSERT_TRUE(profile2.get()); | |
| 138 | |
| 139 // Force lazy-init of some profile services to simulate use. | |
| 140 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); | |
| 141 EXPECT_TRUE(profile1->GetBookmarkModel()); | |
| 142 EXPECT_TRUE(profile2->GetBookmarkModel()); | |
| 143 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); | |
| 144 profile1.reset(); | |
| 145 profile2.reset(); | |
| 146 // Make sure history cleans up correctly. | |
| 147 message_loop_.RunAllPending(); | |
| 148 } | |
| OLD | NEW |