| 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) { |
| 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) { |
| 67 // This is safe while all file operations are done on the FILE thread. |
| 68 BrowserThread::PostTask(BrowserThread::FILE, |
| 69 FROM_HERE, |
| 70 NewRunnableFunction(&file_util::CreateDirectory, |
| 71 path)); |
| 72 return new TestingProfile(path, this); |
| 73 } |
| 74 }; |
| 75 |
| 76 } // namespace testing |
| 77 |
| 47 class ProfileManagerTest : public testing::Test { | 78 class ProfileManagerTest : public testing::Test { |
| 48 protected: | 79 protected: |
| 49 ProfileManagerTest() | 80 ProfileManagerTest() |
| 50 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), | 81 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), |
| 51 extension_event_router_forwarder_(new ExtensionEventRouterForwarder), | 82 extension_event_router_forwarder_(new ExtensionEventRouterForwarder), |
| 52 ui_thread_(BrowserThread::UI, &message_loop_), | 83 ui_thread_(BrowserThread::UI, &message_loop_), |
| 53 db_thread_(BrowserThread::DB, &message_loop_), | 84 db_thread_(BrowserThread::DB, &message_loop_), |
| 54 file_thread_(BrowserThread::FILE, &message_loop_), | 85 file_thread_(BrowserThread::FILE, &message_loop_), |
| 55 io_thread_(local_state_.Get(), NULL, | 86 io_thread_(local_state_.Get(), NULL, |
| 56 extension_event_router_forwarder_) { | 87 extension_event_router_forwarder_) { |
| 57 #if defined(OS_MACOSX) | 88 #if defined(OS_MACOSX) |
| 58 base::SystemMonitor::AllocateSystemIOPorts(); | 89 base::SystemMonitor::AllocateSystemIOPorts(); |
| 59 #endif | 90 #endif |
| 60 system_monitor_dummy_.reset(new base::SystemMonitor); | 91 system_monitor_dummy_.reset(new base::SystemMonitor); |
| 61 static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread( | 92 static_cast<TestingBrowserProcess*>(g_browser_process)->SetIOThread( |
| 62 &io_thread_); | 93 &io_thread_); |
| 63 } | 94 } |
| 64 | 95 |
| 65 virtual void SetUp() { | 96 virtual void SetUp() { |
| 66 // Create a new temporary directory, and store the path | 97 // Create a new temporary directory, and store the path |
| 67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 98 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 68 profile_manager_.reset(new ProfileManagerWithoutInit(temp_dir_.path())); | 99 profile_manager_.reset(new testing::ProfileManager(temp_dir_.path())); |
| 69 } | 100 } |
| 70 | 101 |
| 71 virtual void TearDown() { | 102 virtual void TearDown() { |
| 72 profile_manager_.reset(); | 103 profile_manager_.reset(); |
| 73 message_loop_.RunAllPending(); | 104 message_loop_.RunAllPending(); |
| 74 } | 105 } |
| 75 | 106 |
| 76 class MockObserver : public ProfileManagerObserver { | 107 class MockObserver : public ProfileManagerObserver { |
| 77 public: | 108 public: |
| 78 MOCK_METHOD2(OnProfileCreated, void(Profile* profile, Status status)); | 109 MOCK_METHOD2(OnProfileCreated, void(Profile* profile, Status status)); |
| 79 }; | 110 }; |
| 80 | 111 |
| 81 #if defined(OS_CHROMEOS) | 112 #if defined(OS_CHROMEOS) |
| 82 // Do not change order of stub_cros_enabler_, which needs to be constructed | 113 // Do not change order of stub_cros_enabler_, which needs to be constructed |
| 83 // before io_thread_ which requires CrosLibrary to be initialized to construct | 114 // before io_thread_ which requires CrosLibrary to be initialized to construct |
| 84 // its data member pref_proxy_config_tracker_ on ChromeOS. | 115 // its data member pref_proxy_config_tracker_ on ChromeOS. |
| 85 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; | 116 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; |
| 86 #endif | 117 #endif |
| 87 | 118 |
| 88 // The path to temporary directory used to contain the test operations. | 119 // The path to temporary directory used to contain the test operations. |
| 89 ScopedTempDir temp_dir_; | 120 ScopedTempDir temp_dir_; |
| 90 ScopedTestingLocalState local_state_; | 121 ScopedTestingLocalState local_state_; |
| 91 scoped_refptr<ExtensionEventRouterForwarder> | 122 scoped_refptr<ExtensionEventRouterForwarder> |
| 92 extension_event_router_forwarder_; | 123 extension_event_router_forwarder_; |
| 93 | 124 |
| 94 MessageLoopForUI message_loop_; | 125 MessageLoopForUI message_loop_; |
| 95 content::TestBrowserThread ui_thread_; | 126 content::TestBrowserThread ui_thread_; |
| 96 content::TestBrowserThread db_thread_; | 127 content::TestBrowserThread db_thread_; |
| 97 content::TestBrowserThread file_thread_; | 128 content::TestBrowserThread file_thread_; |
| 129 // IOThread is necessary for the creation of some services below. |
| 98 IOThread io_thread_; | 130 IOThread io_thread_; |
| 99 | 131 |
| 100 scoped_ptr<base::SystemMonitor> system_monitor_dummy_; | 132 scoped_ptr<base::SystemMonitor> system_monitor_dummy_; |
| 101 | 133 |
| 102 // Also will test profile deletion. | 134 // Also will test profile deletion. |
| 103 scoped_ptr<ProfileManager> profile_manager_; | 135 scoped_ptr<ProfileManager> profile_manager_; |
| 104 }; | 136 }; |
| 105 | 137 |
| 106 TEST_F(ProfileManagerTest, GetProfile) { | 138 TEST_F(ProfileManagerTest, GetProfile) { |
| 107 FilePath dest_path = temp_dir_.path(); | 139 FilePath dest_path = temp_dir_.path(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 187 |
| 156 #endif | 188 #endif |
| 157 | 189 |
| 158 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { | 190 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
| 159 FilePath dest_path1 = temp_dir_.path(); | 191 FilePath dest_path1 = temp_dir_.path(); |
| 160 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); | 192 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
| 161 | 193 |
| 162 FilePath dest_path2 = temp_dir_.path(); | 194 FilePath dest_path2 = temp_dir_.path(); |
| 163 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); | 195 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
| 164 | 196 |
| 165 Profile* profile1; | 197 // Successfully create the profiles. |
| 166 Profile* profile2; | 198 TestingProfile* profile1 = |
| 199 static_cast<TestingProfile*>(profile_manager_->GetProfile(dest_path1)); |
| 200 ASSERT_TRUE(profile1); |
| 201 profile1->set_enable_lazy_service_initialization(true); |
| 167 | 202 |
| 168 // Successfully create the profiles. | 203 TestingProfile* profile2 = |
| 169 profile1 = profile_manager_->GetProfile(dest_path1); | 204 static_cast<TestingProfile*>(profile_manager_->GetProfile(dest_path2)); |
| 170 ASSERT_TRUE(profile1); | |
| 171 | |
| 172 profile2 = profile_manager_->GetProfile(dest_path2); | |
| 173 ASSERT_TRUE(profile2); | 205 ASSERT_TRUE(profile2); |
| 206 profile2->set_enable_lazy_service_initialization(true); |
| 174 | 207 |
| 175 // Force lazy-init of some profile services to simulate use. | 208 // Force lazy-init of some profile services to simulate use. |
| 176 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); | 209 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); |
| 177 EXPECT_TRUE(profile1->GetBookmarkModel()); | 210 EXPECT_TRUE(profile1->GetBookmarkModel()); |
| 178 EXPECT_TRUE(profile2->GetBookmarkModel()); | 211 EXPECT_TRUE(profile2->GetBookmarkModel()); |
| 179 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); | 212 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); |
| 180 | 213 |
| 181 // Make sure any pending tasks run before we destroy the profiles. | 214 // Make sure any pending tasks run before we destroy the profiles. |
| 182 message_loop_.RunAllPending(); | 215 message_loop_.RunAllPending(); |
| 183 | 216 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), | 295 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), |
| 263 ASCIIToUTF16("name_3"), string16(), 0); | 296 ASCIIToUTF16("name_3"), string16(), 0); |
| 264 cache.SetBackgroundStatusOfProfileAtIndex(0, true); | 297 cache.SetBackgroundStatusOfProfileAtIndex(0, true); |
| 265 cache.SetBackgroundStatusOfProfileAtIndex(2, true); | 298 cache.SetBackgroundStatusOfProfileAtIndex(2, true); |
| 266 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); | 299 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); |
| 267 | 300 |
| 268 profile_manager_->AutoloadProfiles(); | 301 profile_manager_->AutoloadProfiles(); |
| 269 | 302 |
| 270 EXPECT_EQ(2u, profile_manager_->GetLoadedProfiles().size()); | 303 EXPECT_EQ(2u, profile_manager_->GetLoadedProfiles().size()); |
| 271 } | 304 } |
| OLD | NEW |