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