Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8636)

Unified Diff: chrome/browser/profiles/profile_browsertest.cc

Issue 225693005: Try to fix ProfileBrowserTest. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup: Common helper function to spin threads. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/profiles/profile_browsertest.cc
diff --git a/chrome/browser/profiles/profile_browsertest.cc b/chrome/browser/profiles/profile_browsertest.cc
index aa5039d3251af16e20a8941785952a134161626b..2dc31a583b2f8e3d68e716cb46d961dc805eff47 100644
--- a/chrome/browser/profiles/profile_browsertest.cc
+++ b/chrome/browser/profiles/profile_browsertest.cc
@@ -7,12 +7,11 @@
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/prefs/pref_service.h"
+#include "base/synchronization/waitable_event.h"
#include "base/version.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/startup_task_runner_service.h"
-#include "chrome/browser/profiles/startup_task_runner_service_factory.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
@@ -50,6 +49,23 @@ void CheckChromeVersion(Profile *profile, bool is_new) {
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
typedef InProcessBrowserTest ProfileBrowserTest;
@@ -66,12 +82,14 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
MockProfileDelegate delegate;
EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
- scoped_ptr<Profile> profile(Profile::CreateProfile(
- temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
- ASSERT_TRUE(profile.get());
- CheckChromeVersion(profile.get(), true);
- StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
- StartDeferredTaskRunners();
+ {
+ scoped_ptr<Profile> profile(Profile::CreateProfile(
+ temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
+ ASSERT_TRUE(profile.get());
+ CheckChromeVersion(profile.get(), true);
+ }
+
+ SpinThreads();
}
// Test OnProfileCreate is called with is_new_profile set to false when
@@ -86,12 +104,14 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
MockProfileDelegate delegate;
EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false));
- scoped_ptr<Profile> profile(Profile::CreateProfile(
- temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
- ASSERT_TRUE(profile.get());
- CheckChromeVersion(profile.get(), false);
- StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
- StartDeferredTaskRunners();
+ {
+ scoped_ptr<Profile> profile(Profile::CreateProfile(
+ temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
+ ASSERT_TRUE(profile.get());
+ CheckChromeVersion(profile.get(), false);
+ }
+
+ SpinThreads();
}
// Test OnProfileCreate is called with is_new_profile set to true when
@@ -105,18 +125,21 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
MockProfileDelegate delegate;
EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
- 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(
- chrome::NOTIFICATION_PROFILE_CREATED,
- content::Source<Profile>(profile.get()));
- observer.Wait();
- CheckChromeVersion(profile.get(), true);
+ {
+ content::WindowedNotificationObserver observer(
+ chrome::NOTIFICATION_PROFILE_CREATED,
+ content::NotificationService::AllSources());
+
+ scoped_ptr<Profile> profile(Profile::CreateProfile(
+ temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
+ ASSERT_TRUE(profile.get());
+
+ // Wait for the profile to be created.
+ observer.Wait();
+ CheckChromeVersion(profile.get(), true);
+ }
+
+ SpinThreads();
}
// Test OnProfileCreate is called with is_new_profile set to false when
@@ -130,18 +153,22 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
MockProfileDelegate delegate;
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(
- chrome::NOTIFICATION_PROFILE_CREATED,
- content::Source<Profile>(profile.get()));
- observer.Wait();
- CheckChromeVersion(profile.get(), false);
+
+ {
+ content::WindowedNotificationObserver observer(
+ chrome::NOTIFICATION_PROFILE_CREATED,
+ content::NotificationService::AllSources());
+
+ scoped_ptr<Profile> profile(Profile::CreateProfile(
+ temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
+ ASSERT_TRUE(profile.get());
+
+ // Wait for the profile to be created.
+ observer.Wait();
+ CheckChromeVersion(profile.get(), false);
+ }
+
+ SpinThreads();
}
// Test that a README file is created for profiles that didn't have it.
@@ -156,23 +183,27 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, DISABLED_ProfileReadmeCreated) {
// No delay before README creation.
ProfileImpl::create_readme_delay_ms = 0;
- scoped_ptr<Profile> profile(Profile::CreateProfile(
- temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
- ASSERT_TRUE(profile.get());
- StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
- StartDeferredTaskRunners();
+ {
+ content::WindowedNotificationObserver observer(
+ chrome::NOTIFICATION_PROFILE_CREATED,
+ content::NotificationService::AllSources());
- // Wait for the profile to be created.
- content::WindowedNotificationObserver observer(
- chrome::NOTIFICATION_PROFILE_CREATED,
- content::Source<Profile>(profile.get()));
- observer.Wait();
+ scoped_ptr<Profile> profile(Profile::CreateProfile(
+ temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
+ ASSERT_TRUE(profile.get());
- content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
+ // Wait for the profile to be created.
+ observer.Wait();
+
+ // Wait for file thread to create the README.
+ content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
+
+ // Verify that README exists.
+ EXPECT_TRUE(base::PathExists(
+ temp_dir.path().Append(chrome::kReadmeFilename)));
+ }
- // Verify that README exists.
- EXPECT_TRUE(base::PathExists(
- temp_dir.path().Append(chrome::kReadmeFilename)));
+ SpinThreads();
}
// Test that Profile can be deleted before README file is created.
@@ -186,18 +217,29 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ProfileDeletedBeforeReadmeCreated) {
// No delay before README creation.
ProfileImpl::create_readme_delay_ms = 0;
+ 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(Profile::CreateProfile(
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
- // for README creation).
+ // Delete the Profile instance before we give the file thread a chance to
+ // create the README.
profile.reset();
- content::RunAllPendingInMessageLoop();
- content::RunAllPendingInMessageLoop(content::BrowserThread::DB);
- content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
+
+ // Now unblock the file thread again and run pending tasks (this includes the
+ // task for README creation).
+ unblock->Signal();
+
+ SpinThreads();
}
// Test that repeated setting of exit type is handled correctly.
@@ -213,36 +255,30 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, MAYBE_ExitType) {
MockProfileDelegate delegate;
EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
+ {
+ scoped_ptr<Profile> profile(Profile::CreateProfile(
+ temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
+ ASSERT_TRUE(profile.get());
+
+ PrefService* prefs = profile->GetPrefs();
+ // The initial state is crashed; store for later reference.
+ std::string crash_value(prefs->GetString(prefs::kSessionExitType));
+
+ // The first call to a type other than crashed should change the value.
+ profile->SetExitType(Profile::EXIT_SESSION_ENDED);
+ std::string first_call_value(prefs->GetString(prefs::kSessionExitType));
+ EXPECT_NE(crash_value, first_call_value);
+
+ // Subsequent calls to a non-crash value should be ignored.
+ profile->SetExitType(Profile::EXIT_NORMAL);
+ std::string second_call_value(prefs->GetString(prefs::kSessionExitType));
+ EXPECT_EQ(first_call_value, second_call_value);
+
+ // Setting back to a crashed value should work.
+ profile->SetExitType(Profile::EXIT_CRASHED);
+ std::string final_value(prefs->GetString(prefs::kSessionExitType));
+ EXPECT_EQ(crash_value, final_value);
+ }
- scoped_ptr<Profile> profile(Profile::CreateProfile(
- temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
- ASSERT_TRUE(profile.get());
- StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
- StartDeferredTaskRunners();
-
- PrefService* prefs = profile->GetPrefs();
- // The initial state is crashed; store for later reference.
- std::string crash_value(prefs->GetString(prefs::kSessionExitType));
-
- // The first call to a type other than crashed should change the value.
- profile->SetExitType(Profile::EXIT_SESSION_ENDED);
- std::string first_call_value(prefs->GetString(prefs::kSessionExitType));
- EXPECT_NE(crash_value, first_call_value);
-
- // Subsequent calls to a non-crash value should be ignored.
- profile->SetExitType(Profile::EXIT_NORMAL);
- std::string second_call_value(prefs->GetString(prefs::kSessionExitType));
- EXPECT_EQ(first_call_value, second_call_value);
-
- // Setting back to a crashed value should work.
- profile->SetExitType(Profile::EXIT_CRASHED);
- std::string final_value(prefs->GetString(prefs::kSessionExitType));
- EXPECT_EQ(crash_value, final_value);
-
- // This test runs fast enough that the WebDataService may still be
- // 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);
+ SpinThreads();
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698