OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/profiles/profile.h" | 5 #include "chrome/browser/profiles/profile.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/json/json_reader.h" | |
11 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
12 #include "base/sequenced_task_runner.h" | |
13 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
14 #include "base/values.h" | |
15 #include "base/version.h" | 12 #include "base/version.h" |
16 #include "chrome/browser/browser_process.h" | |
17 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
18 #include "chrome/browser/profiles/chrome_version_service.h" | 14 #include "chrome/browser/profiles/chrome_version_service.h" |
19 #include "chrome/browser/profiles/profile_impl.h" | 15 #include "chrome/browser/profiles/profile_impl.h" |
20 #include "chrome/browser/profiles/profile_manager.h" | |
21 #include "chrome/browser/profiles/startup_task_runner_service.h" | 16 #include "chrome/browser/profiles/startup_task_runner_service.h" |
22 #include "chrome/browser/profiles/startup_task_runner_service_factory.h" | 17 #include "chrome/browser/profiles/startup_task_runner_service_factory.h" |
23 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
24 #include "chrome/common/chrome_version_info.h" | 19 #include "chrome/common/chrome_version_info.h" |
25 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
26 #include "chrome/test/base/in_process_browser_test.h" | 21 #include "chrome/test/base/in_process_browser_test.h" |
27 #include "chrome/test/base/ui_test_utils.h" | 22 #include "chrome/test/base/ui_test_utils.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
30 | 25 |
31 #if defined(OS_CHROMEOS) | 26 #if defined(OS_CHROMEOS) |
32 #include "chromeos/chromeos_switches.h" | 27 #include "chromeos/chromeos_switches.h" |
33 #endif | 28 #endif |
34 | 29 |
35 namespace { | 30 namespace { |
36 | 31 |
37 class MockProfileDelegate : public Profile::Delegate { | 32 class MockProfileDelegate : public Profile::Delegate { |
38 public: | 33 public: |
39 MOCK_METHOD1(OnPrefsLoaded, void(Profile*)); | 34 MOCK_METHOD1(OnPrefsLoaded, void(Profile*)); |
40 MOCK_METHOD3(OnProfileCreated, void(Profile*, bool, bool)); | 35 MOCK_METHOD3(OnProfileCreated, void(Profile*, bool, bool)); |
41 }; | 36 }; |
42 | 37 |
43 // Creates a prefs file in the given directory. | 38 // Creates a prefs file in the given directory. |
44 void CreatePrefsFileInDirectory(const base::FilePath& directory_path) { | 39 void CreatePrefsFileInDirectory(const base::FilePath& directory_path) { |
45 base::FilePath pref_path(directory_path.Append(chrome::kPreferencesFilename)); | 40 base::FilePath pref_path(directory_path.Append(chrome::kPreferencesFilename)); |
46 std::string data("{}"); | 41 std::string data("{}"); |
47 ASSERT_TRUE(base::WriteFile(pref_path, data.c_str(), data.size())); | 42 ASSERT_TRUE(base::WriteFile(pref_path, data.c_str(), data.size())); |
48 } | 43 } |
49 | 44 |
| 45 scoped_ptr<Profile> CreateProfile( |
| 46 const base::FilePath& path, |
| 47 Profile::Delegate* delegate, |
| 48 Profile::CreateMode create_mode) { |
| 49 scoped_ptr<Profile> profile(Profile::CreateProfile( |
| 50 path, delegate, create_mode)); |
| 51 EXPECT_TRUE(profile.get()); |
| 52 // This is necessary to avoid a memleak from BookmarkModel::Load. |
| 53 // Unfortunately, this also results in warnings during debug runs. |
| 54 StartupTaskRunnerServiceFactory::GetForProfile(profile.get())-> |
| 55 StartDeferredTaskRunners(); |
| 56 return profile.Pass(); |
| 57 } |
| 58 |
50 void CheckChromeVersion(Profile *profile, bool is_new) { | 59 void CheckChromeVersion(Profile *profile, bool is_new) { |
51 std::string created_by_version; | 60 std::string created_by_version; |
52 if (is_new) { | 61 if (is_new) { |
53 chrome::VersionInfo version_info; | 62 chrome::VersionInfo version_info; |
54 created_by_version = version_info.Version(); | 63 created_by_version = version_info.Version(); |
55 } else { | 64 } else { |
56 created_by_version = "1.0.0.0"; | 65 created_by_version = "1.0.0.0"; |
57 } | 66 } |
58 std::string pref_version = | 67 std::string pref_version = |
59 ChromeVersionService::GetVersion(profile->GetPrefs()); | 68 ChromeVersionService::GetVersion(profile->GetPrefs()); |
60 // Assert that created_by_version pref gets set to current version. | 69 // Assert that created_by_version pref gets set to current version. |
61 EXPECT_EQ(created_by_version, pref_version); | 70 EXPECT_EQ(created_by_version, pref_version); |
62 } | 71 } |
63 | 72 |
64 void BlockThread( | 73 void BlockThread( |
65 base::WaitableEvent* is_blocked, | 74 base::WaitableEvent* is_blocked, |
66 base::WaitableEvent* unblock) { | 75 base::WaitableEvent* unblock) { |
67 is_blocked->Signal(); | 76 is_blocked->Signal(); |
68 unblock->Wait(); | 77 unblock->Wait(); |
69 } | 78 } |
70 | 79 |
71 void FlushTaskRunner(base::SequencedTaskRunner* runner) { | |
72 ASSERT_TRUE(runner); | |
73 base::WaitableEvent unblock(false, false); | |
74 | |
75 runner->PostTask(FROM_HERE, | |
76 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&unblock))); | |
77 | |
78 unblock.Wait(); | |
79 } | |
80 | |
81 std::string GetExitTypePreferenceFromDisk(Profile* profile) { | |
82 base::FilePath prefs_path = | |
83 profile->GetPath().Append(chrome::kPreferencesFilename); | |
84 std::string prefs; | |
85 if (!base::ReadFileToString(prefs_path, &prefs)) | |
86 return std::string(); | |
87 | |
88 scoped_ptr<base::Value> value(base::JSONReader::Read(prefs)); | |
89 if (!value) | |
90 return std::string(); | |
91 | |
92 base::DictionaryValue* dict = NULL; | |
93 if (!value->GetAsDictionary(&dict) || !dict) | |
94 return std::string(); | |
95 | |
96 std::string exit_type; | |
97 if (!dict->GetString("profile.exit_type", &exit_type)) | |
98 return std::string(); | |
99 | |
100 return exit_type; | |
101 } | |
102 | |
103 void SpinThreads() { | 80 void SpinThreads() { |
104 // Give threads a chance to do their stuff before shutting down (i.e. | 81 // Give threads a chance to do their stuff before shutting down (i.e. |
105 // deleting scoped temp dir etc). | 82 // deleting scoped temp dir etc). |
106 // Should not be necessary anymore once Profile deletion is fixed | 83 // Should not be necessary anymore once Profile deletion is fixed |
107 // (see crbug.com/88586). | 84 // (see crbug.com/88586). |
108 content::RunAllPendingInMessageLoop(); | 85 content::RunAllPendingInMessageLoop(); |
109 content::RunAllPendingInMessageLoop(content::BrowserThread::DB); | 86 content::RunAllPendingInMessageLoop(content::BrowserThread::DB); |
110 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 87 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
111 } | 88 } |
112 | 89 |
113 } // namespace | 90 } // namespace |
114 | 91 |
115 class ProfileBrowserTest : public InProcessBrowserTest { | 92 class ProfileBrowserTest : public InProcessBrowserTest { |
116 protected: | 93 protected: |
117 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 94 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
118 #if defined(OS_CHROMEOS) | 95 #if defined(OS_CHROMEOS) |
119 command_line->AppendSwitch( | 96 command_line->AppendSwitch( |
120 chromeos::switches::kIgnoreUserProfileMappingForTests); | 97 chromeos::switches::kIgnoreUserProfileMappingForTests); |
121 #endif | 98 #endif |
122 } | 99 } |
123 | |
124 scoped_ptr<Profile> CreateProfile( | |
125 const base::FilePath& path, | |
126 Profile::Delegate* delegate, | |
127 Profile::CreateMode create_mode) { | |
128 scoped_ptr<Profile> profile(Profile::CreateProfile( | |
129 path, delegate, create_mode)); | |
130 EXPECT_TRUE(profile.get()); | |
131 | |
132 // Store the Profile's IO task runner so we can wind it down. | |
133 profile_io_task_runner_ = profile->GetIOTaskRunner(); | |
134 | |
135 return profile.Pass(); | |
136 } | |
137 | |
138 void FlushIoTaskRunnerAndSpinThreads() { | |
139 FlushTaskRunner(profile_io_task_runner_); | |
140 SpinThreads(); | |
141 } | |
142 | |
143 scoped_refptr<base::SequencedTaskRunner> profile_io_task_runner_; | |
144 }; | 100 }; |
145 | 101 |
146 // Test OnProfileCreate is called with is_new_profile set to true when | 102 // Test OnProfileCreate is called with is_new_profile set to true when |
147 // creating a new profile synchronously. | 103 // creating a new profile synchronously. |
148 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, CreateNewProfileSynchronous) { | 104 // |
| 105 // Flaky (sometimes timeout): http://crbug.com/141141 |
| 106 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| 107 DISABLED_CreateNewProfileSynchronous) { |
149 base::ScopedTempDir temp_dir; | 108 base::ScopedTempDir temp_dir; |
150 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 109 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
151 | 110 |
152 MockProfileDelegate delegate; | 111 MockProfileDelegate delegate; |
153 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); | 112 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); |
154 | 113 |
155 { | 114 { |
156 scoped_ptr<Profile> profile(CreateProfile( | 115 scoped_ptr<Profile> profile(CreateProfile( |
157 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); | 116 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); |
158 CheckChromeVersion(profile.get(), true); | 117 CheckChromeVersion(profile.get(), true); |
159 } | 118 } |
160 | 119 |
161 FlushIoTaskRunnerAndSpinThreads(); | 120 SpinThreads(); |
162 } | 121 } |
163 | 122 |
164 // Test OnProfileCreate is called with is_new_profile set to false when | 123 // Test OnProfileCreate is called with is_new_profile set to false when |
165 // creating a profile synchronously with an existing prefs file. | 124 // creating a profile synchronously with an existing prefs file. |
166 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, CreateOldProfileSynchronous) { | 125 // Flaky: http://crbug.com/141517 |
| 126 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| 127 DISABLED_CreateOldProfileSynchronous) { |
167 base::ScopedTempDir temp_dir; | 128 base::ScopedTempDir temp_dir; |
168 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 129 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
169 CreatePrefsFileInDirectory(temp_dir.path()); | 130 CreatePrefsFileInDirectory(temp_dir.path()); |
170 | 131 |
171 MockProfileDelegate delegate; | 132 MockProfileDelegate delegate; |
172 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false)); | 133 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false)); |
173 | 134 |
174 { | 135 { |
175 scoped_ptr<Profile> profile(CreateProfile( | 136 scoped_ptr<Profile> profile(CreateProfile( |
176 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); | 137 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); |
177 CheckChromeVersion(profile.get(), false); | 138 CheckChromeVersion(profile.get(), false); |
178 } | 139 } |
179 | 140 |
180 FlushIoTaskRunnerAndSpinThreads(); | 141 SpinThreads(); |
181 } | 142 } |
182 | 143 |
183 // Test OnProfileCreate is called with is_new_profile set to true when | 144 // Test OnProfileCreate is called with is_new_profile set to true when |
184 // creating a new profile asynchronously. | 145 // creating a new profile asynchronously. |
185 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, CreateNewProfileAsynchronous) { | 146 // This test is flaky on Linux, Win and Mac. See crbug.com/142787 |
| 147 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| 148 DISABLED_CreateNewProfileAsynchronous) { |
186 base::ScopedTempDir temp_dir; | 149 base::ScopedTempDir temp_dir; |
187 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 150 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
188 | 151 |
189 MockProfileDelegate delegate; | 152 MockProfileDelegate delegate; |
190 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); | 153 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); |
191 | 154 |
192 { | 155 { |
193 content::WindowedNotificationObserver observer( | 156 content::WindowedNotificationObserver observer( |
194 chrome::NOTIFICATION_PROFILE_CREATED, | 157 chrome::NOTIFICATION_PROFILE_CREATED, |
195 content::NotificationService::AllSources()); | 158 content::NotificationService::AllSources()); |
196 | 159 |
197 scoped_ptr<Profile> profile(CreateProfile( | 160 scoped_ptr<Profile> profile(CreateProfile( |
198 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS)); | 161 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS)); |
199 | 162 |
200 // Wait for the profile to be created. | 163 // Wait for the profile to be created. |
201 observer.Wait(); | 164 observer.Wait(); |
202 CheckChromeVersion(profile.get(), true); | 165 CheckChromeVersion(profile.get(), true); |
203 } | 166 } |
204 | 167 |
205 FlushIoTaskRunnerAndSpinThreads(); | 168 SpinThreads(); |
206 } | 169 } |
207 | 170 |
208 // Test OnProfileCreate is called with is_new_profile set to false when | 171 // Test OnProfileCreate is called with is_new_profile set to false when |
209 // creating a profile asynchronously with an existing prefs file. | 172 // creating a profile asynchronously with an existing prefs file. |
210 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, CreateOldProfileAsynchronous) { | 173 // Flaky: http://crbug.com/141517 |
| 174 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| 175 DISABLED_CreateOldProfileAsynchronous) { |
211 base::ScopedTempDir temp_dir; | 176 base::ScopedTempDir temp_dir; |
212 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 177 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
213 CreatePrefsFileInDirectory(temp_dir.path()); | 178 CreatePrefsFileInDirectory(temp_dir.path()); |
214 | 179 |
215 MockProfileDelegate delegate; | 180 MockProfileDelegate delegate; |
216 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false)); | 181 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false)); |
217 | 182 |
218 { | 183 { |
219 content::WindowedNotificationObserver observer( | 184 content::WindowedNotificationObserver observer( |
220 chrome::NOTIFICATION_PROFILE_CREATED, | 185 chrome::NOTIFICATION_PROFILE_CREATED, |
221 content::NotificationService::AllSources()); | 186 content::NotificationService::AllSources()); |
222 | 187 |
223 scoped_ptr<Profile> profile(CreateProfile( | 188 scoped_ptr<Profile> profile(CreateProfile( |
224 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS)); | 189 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS)); |
225 | 190 |
226 // Wait for the profile to be created. | 191 // Wait for the profile to be created. |
227 observer.Wait(); | 192 observer.Wait(); |
228 CheckChromeVersion(profile.get(), false); | 193 CheckChromeVersion(profile.get(), false); |
229 } | 194 } |
230 | 195 |
231 FlushIoTaskRunnerAndSpinThreads(); | 196 SpinThreads(); |
232 } | 197 } |
233 | 198 |
234 // Test that a README file is created for profiles that didn't have it. | 199 // Test that a README file is created for profiles that didn't have it. |
235 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ProfileReadmeCreated) { | 200 // Flaky: http://crbug.com/140882 |
| 201 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, DISABLED_ProfileReadmeCreated) { |
236 base::ScopedTempDir temp_dir; | 202 base::ScopedTempDir temp_dir; |
237 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 203 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
238 | 204 |
239 MockProfileDelegate delegate; | 205 MockProfileDelegate delegate; |
240 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); | 206 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); |
241 | 207 |
242 // No delay before README creation. | 208 // No delay before README creation. |
243 ProfileImpl::create_readme_delay_ms = 0; | 209 ProfileImpl::create_readme_delay_ms = 0; |
244 | 210 |
245 { | 211 { |
246 content::WindowedNotificationObserver observer( | 212 content::WindowedNotificationObserver observer( |
247 chrome::NOTIFICATION_PROFILE_CREATED, | 213 chrome::NOTIFICATION_PROFILE_CREATED, |
248 content::NotificationService::AllSources()); | 214 content::NotificationService::AllSources()); |
249 | 215 |
250 scoped_ptr<Profile> profile(CreateProfile( | 216 scoped_ptr<Profile> profile(CreateProfile( |
251 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS)); | 217 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS)); |
252 | 218 |
253 // Wait for the profile to be created. | 219 // Wait for the profile to be created. |
254 observer.Wait(); | 220 observer.Wait(); |
255 | 221 |
256 // Wait for file thread to create the README. | 222 // Wait for file thread to create the README. |
257 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 223 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
258 | 224 |
259 // Verify that README exists. | 225 // Verify that README exists. |
260 EXPECT_TRUE(base::PathExists( | 226 EXPECT_TRUE(base::PathExists( |
261 temp_dir.path().Append(chrome::kReadmeFilename))); | 227 temp_dir.path().Append(chrome::kReadmeFilename))); |
262 } | 228 } |
263 | 229 |
264 FlushIoTaskRunnerAndSpinThreads(); | 230 SpinThreads(); |
265 } | 231 } |
266 | 232 |
267 // Test that Profile can be deleted before README file is created. | 233 // Test that Profile can be deleted before README file is created. |
268 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ProfileDeletedBeforeReadmeCreated) { | 234 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ProfileDeletedBeforeReadmeCreated) { |
269 base::ScopedTempDir temp_dir; | 235 base::ScopedTempDir temp_dir; |
270 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 236 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
271 | 237 |
272 MockProfileDelegate delegate; | 238 MockProfileDelegate delegate; |
273 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); | 239 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); |
274 | 240 |
(...skipping 14 matching lines...) Expand all Loading... |
289 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); | 255 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); |
290 | 256 |
291 // Delete the Profile instance before we give the file thread a chance to | 257 // Delete the Profile instance before we give the file thread a chance to |
292 // create the README. | 258 // create the README. |
293 profile.reset(); | 259 profile.reset(); |
294 | 260 |
295 // Now unblock the file thread again and run pending tasks (this includes the | 261 // Now unblock the file thread again and run pending tasks (this includes the |
296 // task for README creation). | 262 // task for README creation). |
297 unblock->Signal(); | 263 unblock->Signal(); |
298 | 264 |
299 FlushIoTaskRunnerAndSpinThreads(); | 265 SpinThreads(); |
300 } | 266 } |
301 | 267 |
302 // Test that repeated setting of exit type is handled correctly. | 268 // Test that repeated setting of exit type is handled correctly. |
303 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ExitType) { | 269 #if defined(OS_WIN) |
| 270 // Flaky on Windows: http://crbug.com/163713 |
| 271 #define MAYBE_ExitType DISABLED_ExitType |
| 272 #else |
| 273 #define MAYBE_ExitType ExitType |
| 274 #endif |
| 275 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, MAYBE_ExitType) { |
304 base::ScopedTempDir temp_dir; | 276 base::ScopedTempDir temp_dir; |
305 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 277 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
306 | 278 |
307 MockProfileDelegate delegate; | 279 MockProfileDelegate delegate; |
308 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); | 280 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); |
309 { | 281 { |
310 scoped_ptr<Profile> profile(CreateProfile( | 282 scoped_ptr<Profile> profile(CreateProfile( |
311 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); | 283 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); |
312 | 284 |
313 PrefService* prefs = profile->GetPrefs(); | 285 PrefService* prefs = profile->GetPrefs(); |
314 // The initial state is crashed; store for later reference. | 286 // The initial state is crashed; store for later reference. |
315 std::string crash_value(prefs->GetString(prefs::kSessionExitType)); | 287 std::string crash_value(prefs->GetString(prefs::kSessionExitType)); |
316 | 288 |
317 // The first call to a type other than crashed should change the value. | 289 // The first call to a type other than crashed should change the value. |
318 profile->SetExitType(Profile::EXIT_SESSION_ENDED); | 290 profile->SetExitType(Profile::EXIT_SESSION_ENDED); |
319 std::string first_call_value(prefs->GetString(prefs::kSessionExitType)); | 291 std::string first_call_value(prefs->GetString(prefs::kSessionExitType)); |
320 EXPECT_NE(crash_value, first_call_value); | 292 EXPECT_NE(crash_value, first_call_value); |
321 | 293 |
322 // Subsequent calls to a non-crash value should be ignored. | 294 // Subsequent calls to a non-crash value should be ignored. |
323 profile->SetExitType(Profile::EXIT_NORMAL); | 295 profile->SetExitType(Profile::EXIT_NORMAL); |
324 std::string second_call_value(prefs->GetString(prefs::kSessionExitType)); | 296 std::string second_call_value(prefs->GetString(prefs::kSessionExitType)); |
325 EXPECT_EQ(first_call_value, second_call_value); | 297 EXPECT_EQ(first_call_value, second_call_value); |
326 | 298 |
327 // Setting back to a crashed value should work. | 299 // Setting back to a crashed value should work. |
328 profile->SetExitType(Profile::EXIT_CRASHED); | 300 profile->SetExitType(Profile::EXIT_CRASHED); |
329 std::string final_value(prefs->GetString(prefs::kSessionExitType)); | 301 std::string final_value(prefs->GetString(prefs::kSessionExitType)); |
330 EXPECT_EQ(crash_value, final_value); | 302 EXPECT_EQ(crash_value, final_value); |
331 } | 303 } |
332 | 304 |
333 FlushIoTaskRunnerAndSpinThreads(); | 305 SpinThreads(); |
334 } | 306 } |
335 | |
336 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, | |
337 WritesProfilesSynchronouslyOnEndSession) { | |
338 base::ScopedTempDir temp_dir; | |
339 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
340 | |
341 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
342 ASSERT_TRUE(profile_manager); | |
343 std::vector<Profile*> loaded_profiles = profile_manager->GetLoadedProfiles(); | |
344 | |
345 ASSERT_NE(loaded_profiles.size(), 0UL); | |
346 for (size_t i = 0; i < loaded_profiles.size(); ++i) { | |
347 Profile* profile = loaded_profiles[i]; | |
348 | |
349 // Flush the profile data to disk for all loaded profiles. | |
350 profile->SetExitType(Profile::EXIT_CRASHED); | |
351 FlushTaskRunner(profile->GetIOTaskRunner()); | |
352 | |
353 // Make sure that the prefs file was written with the expected key/value. | |
354 ASSERT_EQ(GetExitTypePreferenceFromDisk(profile), "Crashed"); | |
355 } | |
356 | |
357 // This must not return until the profile data has been written to disk. | |
358 // If this test flakes, then logoff on Windows has broken again. | |
359 g_browser_process->EndSession(); | |
360 | |
361 // Verify that the setting was indeed written. | |
362 for (size_t i = 0; i < loaded_profiles.size(); ++i) { | |
363 Profile* profile = loaded_profiles[i]; | |
364 // Make sure that the prefs file was written with the expected key/value. | |
365 ASSERT_EQ(GetExitTypePreferenceFromDisk(profile), "SessionEnded"); | |
366 } | |
367 } | |
OLD | NEW |