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