Chromium Code Reviews| Index: chrome/browser/profiles/profile_browsertest.cc |
| diff --git a/chrome/browser/profiles/profile_browsertest.cc b/chrome/browser/profiles/profile_browsertest.cc |
| index 0cc502f4ea4b0aa0a8587afc44ff0970963d1ab5..4c266fea16cfc1af6a110510818ec63591427cbb 100644 |
| --- a/chrome/browser/profiles/profile_browsertest.cc |
| +++ b/chrome/browser/profiles/profile_browsertest.cc |
| @@ -7,12 +7,17 @@ |
| #include "base/command_line.h" |
| #include "base/file_util.h" |
| #include "base/files/scoped_temp_dir.h" |
| +#include "base/json/json_reader.h" |
| #include "base/prefs/pref_service.h" |
| +#include "base/sequenced_task_runner.h" |
| #include "base/synchronization/waitable_event.h" |
| +#include "base/values.h" |
| #include "base/version.h" |
| +#include "chrome/browser/browser_process.h" |
| #include "chrome/browser/chrome_notification_types.h" |
| #include "chrome/browser/profiles/chrome_version_service.h" |
| #include "chrome/browser/profiles/profile_impl.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| #include "chrome/browser/profiles/startup_task_runner_service.h" |
| #include "chrome/browser/profiles/startup_task_runner_service_factory.h" |
| #include "chrome/common/chrome_constants.h" |
| @@ -42,20 +47,6 @@ void CreatePrefsFileInDirectory(const base::FilePath& directory_path) { |
| ASSERT_TRUE(base::WriteFile(pref_path, data.c_str(), data.size())); |
| } |
| -scoped_ptr<Profile> CreateProfile( |
| - const base::FilePath& path, |
| - Profile::Delegate* delegate, |
| - Profile::CreateMode create_mode) { |
| - scoped_ptr<Profile> profile(Profile::CreateProfile( |
| - path, delegate, create_mode)); |
| - EXPECT_TRUE(profile.get()); |
| - // This is necessary to avoid a memleak from BookmarkModel::Load. |
| - // Unfortunately, this also results in warnings during debug runs. |
| - StartupTaskRunnerServiceFactory::GetForProfile(profile.get())-> |
| - StartDeferredTaskRunners(); |
| - return profile.Pass(); |
| -} |
| - |
| void CheckChromeVersion(Profile *profile, bool is_new) { |
| std::string created_by_version; |
| if (is_new) { |
| @@ -77,6 +68,38 @@ void BlockThread( |
| unblock->Wait(); |
| } |
| +void FlushTaskRunner(base::SequencedTaskRunner* runner) { |
| + ASSERT_TRUE(runner); |
| + base::WaitableEvent unblock(false, false); |
| + |
| + runner->PostTask(FROM_HERE, |
| + base::Bind(&base::WaitableEvent::Signal, base::Unretained(&unblock))); |
| + |
| + unblock.Wait(); |
| +} |
| + |
| +std::string GetExitTypePreferenceFromDisk(Profile* profile) { |
| + base::FilePath prefs_path = |
| + profile->GetPath().Append(chrome::kPreferencesFilename); |
| + std::string prefs; |
| + if (!base::ReadFileToString(prefs_path, &prefs)) |
| + return ""; |
|
gab
2014/07/04 15:22:18
s/""/std::string()/
I forget when I was told this
gab
2014/07/04 23:39:27
Ping
Sigurður Ásgeirsson
2014/07/05 20:03:01
Done.
|
| + |
| + scoped_ptr<base::Value> value(base::JSONReader::Read(prefs)); |
| + if (!value) |
| + return ""; |
| + |
| + base::DictionaryValue* dict; |
|
gab
2014/07/04 15:22:17
Initialize to NULL (otherwise |!dict| can be readi
gab
2014/07/04 23:39:27
Ping
Sigurður Ásgeirsson
2014/07/05 20:03:01
Done.
|
| + if (!value->GetAsDictionary(&dict) || !dict) |
| + return ""; |
| + |
| + std::string exit_type; |
| + if (!dict->GetString("profile.exit_type", &exit_type)) |
| + return ""; |
| + |
| + return exit_type; |
| +} |
| + |
| void SpinThreads() { |
| // Give threads a chance to do their stuff before shutting down (i.e. |
| // deleting scoped temp dir etc). |
| @@ -97,14 +120,32 @@ class ProfileBrowserTest : public InProcessBrowserTest { |
| chromeos::switches::kIgnoreUserProfileMappingForTests); |
| #endif |
| } |
| + |
| + scoped_ptr<Profile> CreateProfile( |
| + const base::FilePath& path, |
| + Profile::Delegate* delegate, |
| + Profile::CreateMode create_mode) { |
| + scoped_ptr<Profile> profile(Profile::CreateProfile( |
| + path, delegate, create_mode)); |
| + EXPECT_TRUE(profile.get()); |
| + |
| + // Store the Profile's IO task runner so we can wind it down. |
| + profile_io_task_runner_ = profile->GetIOTaskRunner(); |
| + |
| + return profile.Pass(); |
| + } |
| + |
| + void FlushIoTaskRunnerAndSpinThreads() { |
| + FlushTaskRunner(profile_io_task_runner_); |
| + SpinThreads(); |
| + } |
| + |
| + scoped_refptr<base::SequencedTaskRunner> profile_io_task_runner_; |
| }; |
| // Test OnProfileCreate is called with is_new_profile set to true when |
| // creating a new profile synchronously. |
| -// |
| -// Flaky (sometimes timeout): http://crbug.com/141141 |
| -IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| - DISABLED_CreateNewProfileSynchronous) { |
| +IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, CreateNewProfileSynchronous) { |
| base::ScopedTempDir temp_dir; |
| ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| @@ -117,14 +158,12 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| CheckChromeVersion(profile.get(), true); |
| } |
| - SpinThreads(); |
| + FlushIoTaskRunnerAndSpinThreads(); |
| } |
| // Test OnProfileCreate is called with is_new_profile set to false when |
| // creating a profile synchronously with an existing prefs file. |
| -// Flaky: http://crbug.com/141517 |
| -IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| - DISABLED_CreateOldProfileSynchronous) { |
| +IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, CreateOldProfileSynchronous) { |
| base::ScopedTempDir temp_dir; |
| ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| CreatePrefsFileInDirectory(temp_dir.path()); |
| @@ -138,14 +177,12 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| CheckChromeVersion(profile.get(), false); |
| } |
| - SpinThreads(); |
| + FlushIoTaskRunnerAndSpinThreads(); |
| } |
| // Test OnProfileCreate is called with is_new_profile set to true when |
| // creating a new profile asynchronously. |
| -// This test is flaky on Linux, Win and Mac. See crbug.com/142787 |
| -IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| - DISABLED_CreateNewProfileAsynchronous) { |
| +IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, CreateNewProfileAsynchronous) { |
| base::ScopedTempDir temp_dir; |
| ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| @@ -165,14 +202,12 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| CheckChromeVersion(profile.get(), true); |
| } |
| - SpinThreads(); |
| + FlushIoTaskRunnerAndSpinThreads(); |
| } |
| // Test OnProfileCreate is called with is_new_profile set to false when |
| // creating a profile asynchronously with an existing prefs file. |
| -// Flaky: http://crbug.com/141517 |
| -IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| - DISABLED_CreateOldProfileAsynchronous) { |
| +IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, CreateOldProfileAsynchronous) { |
| base::ScopedTempDir temp_dir; |
| ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| CreatePrefsFileInDirectory(temp_dir.path()); |
| @@ -193,12 +228,11 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| CheckChromeVersion(profile.get(), false); |
| } |
| - SpinThreads(); |
| + FlushIoTaskRunnerAndSpinThreads(); |
| } |
| // Test that a README file is created for profiles that didn't have it. |
| -// Flaky: http://crbug.com/140882 |
| -IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, DISABLED_ProfileReadmeCreated) { |
| +IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ProfileReadmeCreated) { |
| base::ScopedTempDir temp_dir; |
| ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| @@ -227,7 +261,7 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, DISABLED_ProfileReadmeCreated) { |
| temp_dir.path().Append(chrome::kReadmeFilename))); |
| } |
| - SpinThreads(); |
| + FlushIoTaskRunnerAndSpinThreads(); |
| } |
| // Test that Profile can be deleted before README file is created. |
| @@ -262,17 +296,11 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ProfileDeletedBeforeReadmeCreated) { |
| // task for README creation). |
| unblock->Signal(); |
| - SpinThreads(); |
| + FlushIoTaskRunnerAndSpinThreads(); |
| } |
| // Test that repeated setting of exit type is handled correctly. |
| -#if defined(OS_WIN) |
| -// Flaky on Windows: http://crbug.com/163713 |
| -#define MAYBE_ExitType DISABLED_ExitType |
| -#else |
| -#define MAYBE_ExitType ExitType |
| -#endif |
| -IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, MAYBE_ExitType) { |
| +IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ExitType) { |
| base::ScopedTempDir temp_dir; |
| ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| @@ -302,5 +330,37 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, MAYBE_ExitType) { |
| EXPECT_EQ(crash_value, final_value); |
| } |
| - SpinThreads(); |
| + FlushIoTaskRunnerAndSpinThreads(); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, |
| + WritesProfilesSynchronouslyOnEndSession) { |
| + base::ScopedTempDir temp_dir; |
| + ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| + |
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + ASSERT_TRUE(profile_manager); |
| + std::vector<Profile*> loaded_profiles = profile_manager->GetLoadedProfiles(); |
| + |
| + for (size_t i = 0; i < loaded_profiles.size(); ++i) { |
|
noms (inactive)
2014/07/04 15:41:18
Can you add an assert or check that there's at lea
Sigurður Ásgeirsson
2014/07/05 20:03:01
Done.
|
| + Profile* profile = loaded_profiles[i]; |
| + |
| + // Flush the profile data to disk for all loaded profiles. |
| + profile->SetExitType(Profile::EXIT_CRASHED); |
| + FlushTaskRunner(profile->GetIOTaskRunner()); |
| + |
| + // Make sure that the prefs file was written with the expected key/value. |
| + ASSERT_EQ(GetExitTypePreferenceFromDisk(profile), "Crashed"); |
| + } |
| + |
| + // This must not return until the profile data has been written to disk. |
| + // If this test flakes, then logoff on Windows has broken again. |
| + g_browser_process->EndSession(); |
| + |
| + // Verify that the setting was indeed written. |
| + for (size_t i = 0; i < loaded_profiles.size(); ++i) { |
| + Profile* profile = loaded_profiles[i]; |
| + // Make sure that the prefs file was written with the expected key/value. |
| + ASSERT_EQ(GetExitTypePreferenceFromDisk(profile), "SessionEnded"); |
| + } |
| } |