| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" |
| 8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 10 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 11 #include "base/system_monitor/system_monitor.h" | 12 #include "base/system_monitor/system_monitor.h" |
| 12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/extensions/extension_event_router_forwarder.h" | 17 #include "chrome/browser/extensions/extension_event_router_forwarder.h" |
| 17 #include "chrome/browser/io_thread.h" | 18 #include "chrome/browser/io_thread.h" |
| 18 #include "chrome/browser/prefs/browser_prefs.h" | 19 #include "chrome/browser/prefs/browser_prefs.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/profiles/profile_info_cache.h" | 21 #include "chrome/browser/profiles/profile_info_cache.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | 22 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "chrome/common/chrome_constants.h" | 23 #include "chrome/common/chrome_constants.h" |
| 23 #include "chrome/common/chrome_notification_types.h" | 24 #include "chrome/common/chrome_notification_types.h" |
| 24 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 25 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
| 27 #include "chrome/test/base/testing_browser_process.h" | 28 #include "chrome/test/base/testing_browser_process.h" |
| 28 #include "chrome/test/base/testing_pref_service.h" | 29 #include "chrome/test/base/testing_pref_service.h" |
| 30 #include "chrome/test/base/testing_profile.h" |
| 29 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" |
| 30 #include "content/test/test_browser_thread.h" | 32 #include "content/test/test_browser_thread.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 35 |
| 34 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
| 35 #include "chrome/browser/chromeos/cros/cros_library.h" | 37 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 36 #endif | 38 #endif |
| 37 | 39 |
| 38 using content::BrowserThread; | 40 using content::BrowserThread; |
| 39 | 41 |
| 40 namespace { | 42 namespace { |
| 41 // This global variable is used to check that value returned to different | 43 // This global variable is used to check that value returned to different |
| 42 // observers is the same. | 44 // observers is the same. |
| 43 Profile* g_created_profile; | 45 Profile* g_created_profile; |
| 44 | 46 |
| 45 } // namespace | 47 } // namespace |
| 46 | 48 |
| 49 namespace testing { |
| 50 |
| 51 class ProfileManager : public ::ProfileManagerWithoutInit { |
| 52 public: |
| 53 explicit ProfileManager(const FilePath& user_data_dir) |
| 54 : ::ProfileManagerWithoutInit(user_data_dir) {} |
| 55 |
| 56 protected: |
| 57 virtual Profile* CreateProfileHelper(const FilePath& file_path) OVERRIDE { |
| 58 if (!file_util::PathExists(file_path)) { |
| 59 if (!file_util::CreateDirectory(file_path)) |
| 60 return NULL; |
| 61 } |
| 62 return new TestingProfile(file_path, NULL); |
| 63 } |
| 64 |
| 65 virtual Profile* CreateProfileAsyncHelper(const FilePath& path, |
| 66 Delegate* delegate) OVERRIDE { |
| 67 // This is safe while all file operations are done on the FILE thread. |
| 68 BrowserThread::PostTask( |
| 69 BrowserThread::FILE, FROM_HERE, |
| 70 base::IgnoreReturn<bool>(base::Bind(&file_util::CreateDirectory, |
| 71 path))); |
| 72 |
| 73 return new TestingProfile(path, this); |
| 74 } |
| 75 }; |
| 76 |
| 77 } // namespace testing |
| 78 |
| 47 class ProfileManagerTest : public testing::Test { | 79 class ProfileManagerTest : public testing::Test { |
| 48 protected: | 80 protected: |
| 49 ProfileManagerTest() | 81 ProfileManagerTest() |
| 50 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), | 82 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), |
| 51 extension_event_router_forwarder_(new ExtensionEventRouterForwarder), | 83 extension_event_router_forwarder_(new ExtensionEventRouterForwarder), |
| 52 ui_thread_(BrowserThread::UI, &message_loop_), | 84 ui_thread_(BrowserThread::UI, &message_loop_), |
| 53 db_thread_(BrowserThread::DB, &message_loop_), | 85 db_thread_(BrowserThread::DB, &message_loop_), |
| 54 file_thread_(BrowserThread::FILE, &message_loop_), | 86 file_thread_(BrowserThread::FILE, &message_loop_), |
| 55 io_thread_(local_state_.Get(), NULL, | 87 io_thread_(local_state_.Get(), NULL, |
| 56 extension_event_router_forwarder_) { | 88 extension_event_router_forwarder_) { |
| 57 #if defined(OS_MACOSX) | 89 #if defined(OS_MACOSX) |
| 58 base::SystemMonitor::AllocateSystemIOPorts(); | 90 base::SystemMonitor::AllocateSystemIOPorts(); |
| 59 #endif | 91 #endif |
| 60 system_monitor_dummy_.reset(new base::SystemMonitor); | 92 system_monitor_dummy_.reset(new base::SystemMonitor); |
| 61 static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread( | 93 static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread( |
| 62 &io_thread_); | 94 &io_thread_); |
| 63 } | 95 } |
| 64 | 96 |
| 65 virtual void SetUp() { | 97 virtual void SetUp() { |
| 66 // Create a new temporary directory, and store the path | 98 // Create a new temporary directory, and store the path |
| 67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 99 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 68 profile_manager_.reset(new ProfileManagerWithoutInit(temp_dir_.path())); | 100 profile_manager_.reset(new testing::ProfileManager(temp_dir_.path())); |
| 69 #if defined(OS_WIN) | 101 #if defined(OS_WIN) |
| 70 // Force the ProfileInfoCache to be created immediately, so we can | 102 // Force the ProfileInfoCache to be created immediately, so we can |
| 71 // remove the shortcut manager for testing. | 103 // remove the shortcut manager for testing. |
| 72 profile_manager_->GetProfileInfoCache(); | 104 profile_manager_->GetProfileInfoCache(); |
| 73 profile_manager_->RemoveProfileShortcutManagerForTesting(); | 105 profile_manager_->RemoveProfileShortcutManagerForTesting(); |
| 74 #endif | 106 #endif |
| 107 #if defined(OS_CHROMEOS) |
| 108 CommandLine *cl = CommandLine::ForCurrentProcess(); |
| 109 cl->AppendSwitch(switches::kTestType); |
| 110 #endif |
| 75 } | 111 } |
| 76 | 112 |
| 77 virtual void TearDown() { | 113 virtual void TearDown() { |
| 78 profile_manager_.reset(); | 114 profile_manager_.reset(); |
| 79 message_loop_.RunAllPending(); | 115 message_loop_.RunAllPending(); |
| 80 } | 116 } |
| 81 | 117 |
| 82 class MockObserver : public ProfileManagerObserver { | 118 class MockObserver : public ProfileManagerObserver { |
| 83 public: | 119 public: |
| 84 MOCK_METHOD2(OnProfileCreated, void(Profile* profile, Status status)); | 120 MOCK_METHOD2(OnProfileCreated, void(Profile* profile, Status status)); |
| 85 }; | 121 }; |
| 86 | 122 |
| 87 #if defined(OS_CHROMEOS) | 123 #if defined(OS_CHROMEOS) |
| 88 // Do not change order of stub_cros_enabler_, which needs to be constructed | 124 // Do not change order of stub_cros_enabler_, which needs to be constructed |
| 89 // before io_thread_ which requires CrosLibrary to be initialized to construct | 125 // before io_thread_ which requires CrosLibrary to be initialized to construct |
| 90 // its data member pref_proxy_config_tracker_ on ChromeOS. | 126 // its data member pref_proxy_config_tracker_ on ChromeOS. |
| 91 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; | 127 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; |
| 92 #endif | 128 #endif |
| 93 | 129 |
| 94 // The path to temporary directory used to contain the test operations. | 130 // The path to temporary directory used to contain the test operations. |
| 95 ScopedTempDir temp_dir_; | 131 ScopedTempDir temp_dir_; |
| 96 ScopedTestingLocalState local_state_; | 132 ScopedTestingLocalState local_state_; |
| 97 scoped_refptr<ExtensionEventRouterForwarder> | 133 scoped_refptr<ExtensionEventRouterForwarder> |
| 98 extension_event_router_forwarder_; | 134 extension_event_router_forwarder_; |
| 99 | 135 |
| 100 MessageLoopForUI message_loop_; | 136 MessageLoopForUI message_loop_; |
| 101 content::TestBrowserThread ui_thread_; | 137 content::TestBrowserThread ui_thread_; |
| 102 content::TestBrowserThread db_thread_; | 138 content::TestBrowserThread db_thread_; |
| 103 content::TestBrowserThread file_thread_; | 139 content::TestBrowserThread file_thread_; |
| 140 // IOThread is necessary for the creation of some services below. |
| 104 IOThread io_thread_; | 141 IOThread io_thread_; |
| 105 | 142 |
| 106 scoped_ptr<base::SystemMonitor> system_monitor_dummy_; | 143 scoped_ptr<base::SystemMonitor> system_monitor_dummy_; |
| 107 | 144 |
| 108 // Also will test profile deletion. | 145 // Also will test profile deletion. |
| 109 scoped_ptr<ProfileManager> profile_manager_; | 146 scoped_ptr<ProfileManager> profile_manager_; |
| 110 }; | 147 }; |
| 111 | 148 |
| 112 TEST_F(ProfileManagerTest, GetProfile) { | 149 TEST_F(ProfileManagerTest, GetProfile) { |
| 113 FilePath dest_path = temp_dir_.path(); | 150 FilePath dest_path = temp_dir_.path(); |
| 114 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); | 151 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); |
| 115 | 152 |
| 116 Profile* profile; | 153 Profile* profile; |
| 117 | 154 |
| 118 // Successfully create a profile. | 155 // Successfully create a profile. |
| 119 profile = profile_manager_->GetProfile(dest_path); | 156 profile = profile_manager_->GetProfile(dest_path); |
| 120 EXPECT_TRUE(profile); | 157 EXPECT_TRUE(profile); |
| 121 | 158 |
| 122 // The profile already exists when we call GetProfile. Just load it. | 159 // The profile already exists when we call GetProfile. Just load it. |
| 123 EXPECT_EQ(profile, profile_manager_->GetProfile(dest_path)); | 160 EXPECT_EQ(profile, profile_manager_->GetProfile(dest_path)); |
| 124 } | 161 } |
| 125 | 162 |
| 126 TEST_F(ProfileManagerTest, DefaultProfileDir) { | 163 TEST_F(ProfileManagerTest, DefaultProfileDir) { |
| 127 CommandLine *cl = CommandLine::ForCurrentProcess(); | |
| 128 std::string profile_dir("my_user"); | |
| 129 | |
| 130 cl->AppendSwitch(switches::kTestType); | |
| 131 | |
| 132 FilePath expected_default = | 164 FilePath expected_default = |
| 133 FilePath().AppendASCII(chrome::kInitialProfile); | 165 FilePath().AppendASCII(chrome::kInitialProfile); |
| 134 EXPECT_EQ(expected_default.value(), | 166 EXPECT_EQ(expected_default.value(), |
| 135 profile_manager_->GetInitialProfileDir().value()); | 167 profile_manager_->GetInitialProfileDir().value()); |
| 136 } | 168 } |
| 137 | 169 |
| 138 #if defined(OS_CHROMEOS) | 170 #if defined(OS_CHROMEOS) |
| 139 // This functionality only exists on Chrome OS. | 171 // This functionality only exists on Chrome OS. |
| 140 TEST_F(ProfileManagerTest, LoggedInProfileDir) { | 172 TEST_F(ProfileManagerTest, LoggedInProfileDir) { |
| 141 CommandLine *cl = CommandLine::ForCurrentProcess(); | 173 CommandLine *cl = CommandLine::ForCurrentProcess(); |
| 142 std::string profile_dir("my_user"); | 174 std::string profile_dir("my_user"); |
| 143 | 175 |
| 144 cl->AppendSwitchASCII(switches::kLoginProfile, profile_dir); | 176 cl->AppendSwitchASCII(switches::kLoginProfile, profile_dir); |
| 145 cl->AppendSwitch(switches::kTestType); | |
| 146 | 177 |
| 147 FilePath expected_default = | 178 FilePath expected_default = |
| 148 FilePath().AppendASCII(chrome::kInitialProfile); | 179 FilePath().AppendASCII(chrome::kInitialProfile); |
| 149 EXPECT_EQ(expected_default.value(), | 180 EXPECT_EQ(expected_default.value(), |
| 150 profile_manager_->GetInitialProfileDir().value()); | 181 profile_manager_->GetInitialProfileDir().value()); |
| 151 | 182 |
| 152 profile_manager_->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 183 profile_manager_->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 153 content::NotificationService::AllSources(), | 184 content::NotificationService::AllSources(), |
| 154 content::NotificationService::NoDetails()); | 185 content::NotificationService::NoDetails()); |
| 155 FilePath expected_logged_in(profile_dir); | 186 FilePath expected_logged_in(profile_dir); |
| 156 EXPECT_EQ(expected_logged_in.value(), | 187 EXPECT_EQ(expected_logged_in.value(), |
| 157 profile_manager_->GetInitialProfileDir().value()); | 188 profile_manager_->GetInitialProfileDir().value()); |
| 158 VLOG(1) << temp_dir_.path().Append( | 189 VLOG(1) << temp_dir_.path().Append( |
| 159 profile_manager_->GetInitialProfileDir()).value(); | 190 profile_manager_->GetInitialProfileDir()).value(); |
| 160 } | 191 } |
| 161 | 192 |
| 162 #endif | 193 #endif |
| 163 | 194 |
| 164 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { | 195 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
| 165 FilePath dest_path1 = temp_dir_.path(); | 196 FilePath dest_path1 = temp_dir_.path(); |
| 166 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); | 197 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 167 | 198 |
| 168 FilePath dest_path2 = temp_dir_.path(); | 199 FilePath dest_path2 = temp_dir_.path(); |
| 169 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); | 200 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 170 | 201 |
| 171 Profile* profile1; | |
| 172 Profile* profile2; | |
| 173 | |
| 174 // Successfully create the profiles. | 202 // Successfully create the profiles. |
| 175 profile1 = profile_manager_->GetProfile(dest_path1); | 203 TestingProfile* profile1 = |
| 204 static_cast<TestingProfile*>(profile_manager_->GetProfile(dest_path1)); |
| 176 ASSERT_TRUE(profile1); | 205 ASSERT_TRUE(profile1); |
| 177 | 206 |
| 178 profile2 = profile_manager_->GetProfile(dest_path2); | 207 TestingProfile* profile2 = |
| 208 static_cast<TestingProfile*>(profile_manager_->GetProfile(dest_path2)); |
| 179 ASSERT_TRUE(profile2); | 209 ASSERT_TRUE(profile2); |
| 180 | 210 |
| 181 // Force lazy-init of some profile services to simulate use. | 211 // Force lazy-init of some profile services to simulate use. |
| 212 profile1->CreateHistoryService(true, false); |
| 182 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); | 213 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); |
| 214 profile1->CreateBookmarkModel(true); |
| 183 EXPECT_TRUE(profile1->GetBookmarkModel()); | 215 EXPECT_TRUE(profile1->GetBookmarkModel()); |
| 216 profile2->CreateBookmarkModel(true); |
| 184 EXPECT_TRUE(profile2->GetBookmarkModel()); | 217 EXPECT_TRUE(profile2->GetBookmarkModel()); |
| 218 profile2->CreateHistoryService(true, false); |
| 185 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); | 219 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); |
| 186 | 220 |
| 187 // Make sure any pending tasks run before we destroy the profiles. | 221 // Make sure any pending tasks run before we destroy the profiles. |
| 188 message_loop_.RunAllPending(); | 222 message_loop_.RunAllPending(); |
| 189 | 223 |
| 190 profile_manager_.reset(); | 224 profile_manager_.reset(); |
| 191 | 225 |
| 192 // Make sure history cleans up correctly. | 226 // Make sure history cleans up correctly. |
| 193 message_loop_.RunAllPending(); | 227 message_loop_.RunAllPending(); |
| 194 } | 228 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), | 302 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), |
| 269 ASCIIToUTF16("name_3"), string16(), 0); | 303 ASCIIToUTF16("name_3"), string16(), 0); |
| 270 cache.SetBackgroundStatusOfProfileAtIndex(0, true); | 304 cache.SetBackgroundStatusOfProfileAtIndex(0, true); |
| 271 cache.SetBackgroundStatusOfProfileAtIndex(2, true); | 305 cache.SetBackgroundStatusOfProfileAtIndex(2, true); |
| 272 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); | 306 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); |
| 273 | 307 |
| 274 profile_manager_->AutoloadProfiles(); | 308 profile_manager_->AutoloadProfiles(); |
| 275 | 309 |
| 276 EXPECT_EQ(2u, profile_manager_->GetLoadedProfiles().size()); | 310 EXPECT_EQ(2u, profile_manager_->GetLoadedProfiles().size()); |
| 277 } | 311 } |
| OLD | NEW |