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 #if defined(OS_WIN) | 100 #if defined(OS_WIN) |
70 // Force the ProfileInfoCache to be created immediately, so we can | 101 // Force the ProfileInfoCache to be created immediately, so we can |
71 // remove the shortcut manager for testing. | 102 // remove the shortcut manager for testing. |
72 profile_manager_->GetProfileInfoCache(); | 103 profile_manager_->GetProfileInfoCache(); |
73 profile_manager_->RemoveProfileShortcutManagerForTesting(); | 104 profile_manager_->RemoveProfileShortcutManagerForTesting(); |
74 #endif | 105 #endif |
75 } | 106 } |
76 | 107 |
77 virtual void TearDown() { | 108 virtual void TearDown() { |
78 profile_manager_.reset(); | 109 profile_manager_.reset(); |
(...skipping 15 matching lines...) Expand all Loading... |
94 // The path to temporary directory used to contain the test operations. | 125 // The path to temporary directory used to contain the test operations. |
95 ScopedTempDir temp_dir_; | 126 ScopedTempDir temp_dir_; |
96 ScopedTestingLocalState local_state_; | 127 ScopedTestingLocalState local_state_; |
97 scoped_refptr<ExtensionEventRouterForwarder> | 128 scoped_refptr<ExtensionEventRouterForwarder> |
98 extension_event_router_forwarder_; | 129 extension_event_router_forwarder_; |
99 | 130 |
100 MessageLoopForUI message_loop_; | 131 MessageLoopForUI message_loop_; |
101 content::TestBrowserThread ui_thread_; | 132 content::TestBrowserThread ui_thread_; |
102 content::TestBrowserThread db_thread_; | 133 content::TestBrowserThread db_thread_; |
103 content::TestBrowserThread file_thread_; | 134 content::TestBrowserThread file_thread_; |
| 135 // IOThread is necessary for the creation of some services below. |
104 IOThread io_thread_; | 136 IOThread io_thread_; |
105 | 137 |
106 scoped_ptr<base::SystemMonitor> system_monitor_dummy_; | 138 scoped_ptr<base::SystemMonitor> system_monitor_dummy_; |
107 | 139 |
108 // Also will test profile deletion. | 140 // Also will test profile deletion. |
109 scoped_ptr<ProfileManager> profile_manager_; | 141 scoped_ptr<ProfileManager> profile_manager_; |
110 }; | 142 }; |
111 | 143 |
112 TEST_F(ProfileManagerTest, GetProfile) { | 144 TEST_F(ProfileManagerTest, GetProfile) { |
113 FilePath dest_path = temp_dir_.path(); | 145 FilePath dest_path = temp_dir_.path(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 | 193 |
162 #endif | 194 #endif |
163 | 195 |
164 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { | 196 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { |
165 FilePath dest_path1 = temp_dir_.path(); | 197 FilePath dest_path1 = temp_dir_.path(); |
166 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); | 198 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); |
167 | 199 |
168 FilePath dest_path2 = temp_dir_.path(); | 200 FilePath dest_path2 = temp_dir_.path(); |
169 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); | 201 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); |
170 | 202 |
171 Profile* profile1; | |
172 Profile* profile2; | |
173 | |
174 // Successfully create the profiles. | 203 // Successfully create the profiles. |
175 profile1 = profile_manager_->GetProfile(dest_path1); | 204 TestingProfile* profile1 = |
| 205 static_cast<TestingProfile*>(profile_manager_->GetProfile(dest_path1)); |
176 ASSERT_TRUE(profile1); | 206 ASSERT_TRUE(profile1); |
177 | 207 |
178 profile2 = profile_manager_->GetProfile(dest_path2); | 208 TestingProfile* profile2 = |
| 209 static_cast<TestingProfile*>(profile_manager_->GetProfile(dest_path2)); |
179 ASSERT_TRUE(profile2); | 210 ASSERT_TRUE(profile2); |
180 | 211 |
181 // Force lazy-init of some profile services to simulate use. | 212 // Force lazy-init of some profile services to simulate use. |
| 213 profile1->CreateHistoryService(true, false); |
182 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); | 214 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); |
| 215 profile1->CreateBookmarkModel(true); |
183 EXPECT_TRUE(profile1->GetBookmarkModel()); | 216 EXPECT_TRUE(profile1->GetBookmarkModel()); |
| 217 profile2->CreateBookmarkModel(true); |
184 EXPECT_TRUE(profile2->GetBookmarkModel()); | 218 EXPECT_TRUE(profile2->GetBookmarkModel()); |
| 219 profile2->CreateHistoryService(true, false); |
185 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); | 220 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); |
186 | 221 |
187 // Make sure any pending tasks run before we destroy the profiles. | 222 // Make sure any pending tasks run before we destroy the profiles. |
188 message_loop_.RunAllPending(); | 223 message_loop_.RunAllPending(); |
189 | 224 |
190 profile_manager_.reset(); | 225 profile_manager_.reset(); |
191 | 226 |
192 // Make sure history cleans up correctly. | 227 // Make sure history cleans up correctly. |
193 message_loop_.RunAllPending(); | 228 message_loop_.RunAllPending(); |
194 } | 229 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), | 303 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), |
269 ASCIIToUTF16("name_3"), string16(), 0); | 304 ASCIIToUTF16("name_3"), string16(), 0); |
270 cache.SetBackgroundStatusOfProfileAtIndex(0, true); | 305 cache.SetBackgroundStatusOfProfileAtIndex(0, true); |
271 cache.SetBackgroundStatusOfProfileAtIndex(2, true); | 306 cache.SetBackgroundStatusOfProfileAtIndex(2, true); |
272 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); | 307 EXPECT_EQ(3u, cache.GetNumberOfProfiles()); |
273 | 308 |
274 profile_manager_->AutoloadProfiles(); | 309 profile_manager_->AutoloadProfiles(); |
275 | 310 |
276 EXPECT_EQ(2u, profile_manager_->GetLoadedProfiles().size()); | 311 EXPECT_EQ(2u, profile_manager_->GetLoadedProfiles().size()); |
277 } | 312 } |
OLD | NEW |