Commit ffdb1d5f authored by treib@chromium.org's avatar treib@chromium.org

Try to fix ProfileBrowserTest, again.

Previous CL (which caused memleaks):
https://codereview.chromium.org/225693005

BUG=165760, 140882, 141141, 141517, 142787

Review URL: https://codereview.chromium.org/246923004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@266850 0039d316-1c4b-4281-b951-d872f2087c98
parent cbeca15c
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/file_util.h" #include "base/file_util.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
#include "base/synchronization/waitable_event.h"
#include "base/version.h" #include "base/version.h"
#include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/chrome_version_service.h" #include "chrome/browser/profiles/chrome_version_service.h"
...@@ -36,6 +37,20 @@ void CreatePrefsFileInDirectory(const base::FilePath& directory_path) { ...@@ -36,6 +37,20 @@ void CreatePrefsFileInDirectory(const base::FilePath& directory_path) {
ASSERT_TRUE(base::WriteFile(pref_path, data.c_str(), data.size())); 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) { void CheckChromeVersion(Profile *profile, bool is_new) {
std::string created_by_version; std::string created_by_version;
if (is_new) { if (is_new) {
...@@ -50,6 +65,23 @@ void CheckChromeVersion(Profile *profile, bool is_new) { ...@@ -50,6 +65,23 @@ void CheckChromeVersion(Profile *profile, bool is_new) {
EXPECT_EQ(created_by_version, pref_version); EXPECT_EQ(created_by_version, pref_version);
} }
void BlockThread(
base::WaitableEvent* is_blocked,
base::WaitableEvent* unblock) {
is_blocked->Signal();
unblock->Wait();
}
void SpinThreads() {
// Give threads a chance to do their stuff before shutting down (i.e.
// deleting scoped temp dir etc).
// Should not be necessary anymore once Profile deletion is fixed
// (see crbug.com/88586).
content::RunAllPendingInMessageLoop();
content::RunAllPendingInMessageLoop(content::BrowserThread::DB);
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
}
} // namespace } // namespace
typedef InProcessBrowserTest ProfileBrowserTest; typedef InProcessBrowserTest ProfileBrowserTest;
...@@ -66,12 +98,13 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ...@@ -66,12 +98,13 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
MockProfileDelegate delegate; MockProfileDelegate delegate;
EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
scoped_ptr<Profile> profile(Profile::CreateProfile( {
scoped_ptr<Profile> profile(CreateProfile(
temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
ASSERT_TRUE(profile.get());
CheckChromeVersion(profile.get(), true); CheckChromeVersion(profile.get(), true);
StartupTaskRunnerServiceFactory::GetForProfile(profile.get())-> }
StartDeferredTaskRunners();
SpinThreads();
} }
// Test OnProfileCreate is called with is_new_profile set to false when // Test OnProfileCreate is called with is_new_profile set to false when
...@@ -86,12 +119,13 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ...@@ -86,12 +119,13 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
MockProfileDelegate delegate; MockProfileDelegate delegate;
EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false)); EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false));
scoped_ptr<Profile> profile(Profile::CreateProfile( {
scoped_ptr<Profile> profile(CreateProfile(
temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
ASSERT_TRUE(profile.get());
CheckChromeVersion(profile.get(), false); CheckChromeVersion(profile.get(), false);
StartupTaskRunnerServiceFactory::GetForProfile(profile.get())-> }
StartDeferredTaskRunners();
SpinThreads();
} }
// Test OnProfileCreate is called with is_new_profile set to true when // Test OnProfileCreate is called with is_new_profile set to true when
...@@ -105,18 +139,20 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ...@@ -105,18 +139,20 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
MockProfileDelegate delegate; MockProfileDelegate delegate;
EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
scoped_ptr<Profile> profile(Profile::CreateProfile( {
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_PROFILE_CREATED,
content::NotificationService::AllSources());
scoped_ptr<Profile> profile(CreateProfile(
temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS)); temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
ASSERT_TRUE(profile.get());
StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
StartDeferredTaskRunners();
// Wait for the profile to be created. // Wait for the profile to be created.
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_PROFILE_CREATED,
content::Source<Profile>(profile.get()));
observer.Wait(); observer.Wait();
CheckChromeVersion(profile.get(), true); CheckChromeVersion(profile.get(), true);
}
SpinThreads();
} }
// Test OnProfileCreate is called with is_new_profile set to false when // Test OnProfileCreate is called with is_new_profile set to false when
...@@ -130,18 +166,21 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ...@@ -130,18 +166,21 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
MockProfileDelegate delegate; MockProfileDelegate delegate;
EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false)); EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false));
scoped_ptr<Profile> profile(Profile::CreateProfile(
temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
ASSERT_TRUE(profile.get());
StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
StartDeferredTaskRunners();
// Wait for the profile to be created. {
content::WindowedNotificationObserver observer( content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_PROFILE_CREATED, chrome::NOTIFICATION_PROFILE_CREATED,
content::Source<Profile>(profile.get())); content::NotificationService::AllSources());
scoped_ptr<Profile> profile(CreateProfile(
temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
// Wait for the profile to be created.
observer.Wait(); observer.Wait();
CheckChromeVersion(profile.get(), false); CheckChromeVersion(profile.get(), false);
}
SpinThreads();
} }
// Test that a README file is created for profiles that didn't have it. // Test that a README file is created for profiles that didn't have it.
...@@ -156,23 +195,26 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, DISABLED_ProfileReadmeCreated) { ...@@ -156,23 +195,26 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, DISABLED_ProfileReadmeCreated) {
// No delay before README creation. // No delay before README creation.
ProfileImpl::create_readme_delay_ms = 0; ProfileImpl::create_readme_delay_ms = 0;
scoped_ptr<Profile> profile(Profile::CreateProfile( {
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_PROFILE_CREATED,
content::NotificationService::AllSources());
scoped_ptr<Profile> profile(CreateProfile(
temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS)); temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
ASSERT_TRUE(profile.get());
StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
StartDeferredTaskRunners();
// Wait for the profile to be created. // Wait for the profile to be created.
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_PROFILE_CREATED,
content::Source<Profile>(profile.get()));
observer.Wait(); observer.Wait();
// Wait for file thread to create the README.
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
// Verify that README exists. // Verify that README exists.
EXPECT_TRUE(base::PathExists( EXPECT_TRUE(base::PathExists(
temp_dir.path().Append(chrome::kReadmeFilename))); temp_dir.path().Append(chrome::kReadmeFilename)));
}
SpinThreads();
} }
// Test that Profile can be deleted before README file is created. // Test that Profile can be deleted before README file is created.
...@@ -186,18 +228,28 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ProfileDeletedBeforeReadmeCreated) { ...@@ -186,18 +228,28 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ProfileDeletedBeforeReadmeCreated) {
// No delay before README creation. // No delay before README creation.
ProfileImpl::create_readme_delay_ms = 0; ProfileImpl::create_readme_delay_ms = 0;
scoped_ptr<Profile> profile(Profile::CreateProfile( base::WaitableEvent is_blocked(false, false);
base::WaitableEvent* unblock = new base::WaitableEvent(false, false);
// Block file thread.
content::BrowserThread::PostTask(
content::BrowserThread::FILE, FROM_HERE,
base::Bind(&BlockThread, &is_blocked, base::Owned(unblock)));
// Wait for file thread to actually be blocked.
is_blocked.Wait();
scoped_ptr<Profile> profile(CreateProfile(
temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
ASSERT_TRUE(profile.get());
StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
StartDeferredTaskRunners();
// Delete the Profile instance and run pending tasks (this includes the task // Delete the Profile instance before we give the file thread a chance to
// for README creation). // create the README.
profile.reset(); profile.reset();
content::RunAllPendingInMessageLoop();
content::RunAllPendingInMessageLoop(content::BrowserThread::DB); // Now unblock the file thread again and run pending tasks (this includes the
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); // task for README creation).
unblock->Signal();
SpinThreads();
} }
// Test that repeated setting of exit type is handled correctly. // Test that repeated setting of exit type is handled correctly.
...@@ -213,12 +265,9 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, MAYBE_ExitType) { ...@@ -213,12 +265,9 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, MAYBE_ExitType) {
MockProfileDelegate delegate; MockProfileDelegate delegate;
EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
{
scoped_ptr<Profile> profile(Profile::CreateProfile( scoped_ptr<Profile> profile(CreateProfile(
temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
ASSERT_TRUE(profile.get());
StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
StartDeferredTaskRunners();
PrefService* prefs = profile->GetPrefs(); PrefService* prefs = profile->GetPrefs();
// The initial state is crashed; store for later reference. // The initial state is crashed; store for later reference.
...@@ -238,11 +287,7 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, MAYBE_ExitType) { ...@@ -238,11 +287,7 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, MAYBE_ExitType) {
profile->SetExitType(Profile::EXIT_CRASHED); profile->SetExitType(Profile::EXIT_CRASHED);
std::string final_value(prefs->GetString(prefs::kSessionExitType)); std::string final_value(prefs->GetString(prefs::kSessionExitType));
EXPECT_EQ(crash_value, final_value); EXPECT_EQ(crash_value, final_value);
}
// This test runs fast enough that the WebDataService may still be SpinThreads();
// initializing (which uses the temp directory) when the test
// ends. Give it a chance to complete.
profile.reset();
content::RunAllPendingInMessageLoop();
content::RunAllPendingInMessageLoop(content::BrowserThread::DB);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment