Changes browser_tests to not run a nested message loop.

BUG=none
TEST=none

Review URL: http://codereview.chromium.org/1513014

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44004 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/test/in_process_browser_test.cc b/chrome/test/in_process_browser_test.cc
index 78ba891..d8f54d15 100644
--- a/chrome/test/in_process_browser_test.cc
+++ b/chrome/test/in_process_browser_test.cc
@@ -162,8 +162,14 @@
 
   SandboxInitWrapper sandbox_wrapper;
   MainFunctionParams params(*command_line, sandbox_wrapper, NULL);
+#if defined(OS_MACOSX)
+  params.ui_task =
+      NewRunnableMethod(this,
+      &InProcessBrowserTest::RunTestOnMainThreadLoopDeprecated);
+#else
   params.ui_task =
       NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop);
+#endif
 
   host_resolver_ = new net::RuleBasedHostResolverProc(
       new IntranetRedirectHostResolverProc(NULL));
@@ -247,7 +253,8 @@
   return browser;
 }
 
-void InProcessBrowserTest::RunTestOnMainThreadLoop() {
+#if defined(OS_MACOSX)
+void InProcessBrowserTest::RunTestOnMainThreadLoopDeprecated() {
   // In the long term it would be great if we could use a TestingProfile
   // here and only enable services you want tested, but that requires all
   // consumers of Profile to handle NULL services.
@@ -278,12 +285,11 @@
   // may need to wait for beforeunload and unload handlers to fire in a tab.
   // When all windows are closed, the last window will call Quit(). Call
   // Quit() explicitly if no windows are open.
-#if defined(OS_MACOSX)
+  //
   // When the browser window closes, Cocoa will generate an inner-loop that
   // processes the RenderProcessHost delete task, so allow task nesting.
   bool old_state = MessageLoopForUI::current()->NestableTasksAllowed();
   MessageLoopForUI::current()->SetNestableTasksAllowed(true);
-#endif
   BrowserList::const_iterator browser = BrowserList::begin();
   if (browser == BrowserList::end()) {
     MessageLoopForUI::current()->Quit();
@@ -291,28 +297,70 @@
     for (; browser != BrowserList::end(); ++browser)
       (*browser)->CloseWindow();
   }
-#if defined(OS_MACOSX)
   MessageLoopForUI::current()->SetNestableTasksAllowed(old_state);
-#endif
+
+  // Stop the HTTP server.
+  http_server_ = NULL;
+}
+#endif  // defined(OS_MACOSX)
+
+void InProcessBrowserTest::RunTestOnMainThreadLoop() {
+  // Pump startup related events.
+  MessageLoopForUI::current()->RunAllPending();
+
+  // In the long term it would be great if we could use a TestingProfile
+  // here and only enable services you want tested, but that requires all
+  // consumers of Profile to handle NULL services.
+  Profile* profile = ProfileManager::GetDefaultProfile();
+  if (!profile) {
+    // We should only be able to get here if the profile already exists and
+    // has been created.
+    NOTREACHED();
+    return;
+  }
+
+  ChromeThread::PostTask(
+      ChromeThread::IO, FROM_HERE,
+      NewRunnableFunction(chrome_browser_net::SetUrlRequestMocksEnabled, true));
+
+  browser_ = CreateBrowser(profile);
+
+  // Start the timeout timer to prevent hangs.
+  MessageLoopForUI::current()->PostDelayedTask(FROM_HERE,
+      NewRunnableMethod(this, &InProcessBrowserTest::TimedOut),
+      initial_timeout_);
+
+  // Pump any pending events that were created as a result of creating a
+  // browser.
+  MessageLoopForUI::current()->RunAllPending();
+
+  RunTestOnMainThread();
+
+  CleanUpOnMainThread();
+
+  QuitBrowsers();
 
   // Stop the HTTP server.
   http_server_ = NULL;
 }
 
-void InProcessBrowserTest::TimedOut() {
-  DCHECK(MessageLoopForUI::current()->IsNested());
+void InProcessBrowserTest::QuitBrowsers() {
+  // Invoke CloseAllBrowsersAndExit on a running message loop.
+  // CloseAllBrowsersAndExit exits the message loop after everything has been
+  // shut down properly.
+  MessageLoopForUI::current()->PostTask(
+      FROM_HERE,
+      NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit));
+  ui_test_utils::RunMessageLoop();
+}
 
+void InProcessBrowserTest::TimedOut() {
   std::string error_message = "Test timed out. Each test runs for a max of ";
   error_message += IntToString(kInitialTimeoutInMS);
   error_message += " ms (kInitialTimeoutInMS).";
 
   GTEST_NONFATAL_FAILURE_(error_message.c_str());
 
-  // Start the timeout timer to prevent hangs.
-  MessageLoopForUI::current()->PostDelayedTask(FROM_HERE,
-      NewRunnableMethod(this, &InProcessBrowserTest::TimedOut),
-      kSubsequentTimeoutInMS);
-
   MessageLoopForUI::current()->Quit();
 }