[Extension SW] Fix flakiness of tests calling StartTestFromBackgroundPage

StartTestFromBackgroundPage opens an extension background page
and then calls ExectuteScript*. It is possible that the function
we call through ExectuteScript isn't defined yet. This can cause
tests to time out.

Let the background page send a "ready" message so that browser_tests
can wait for it before proceeding to call ExectuteScript*.

Bug: 877218
Change-Id: I637a2b69229c78dd645b7b078197b15015bf1b60
Reviewed-on: https://chromium-review.googlesource.com/1192352
Commit-Queue: Istiaque Ahmed <[email protected]>
Reviewed-by: Karan Bhatia <[email protected]>
Cr-Commit-Position: refs/heads/master@{#586577}
diff --git a/chrome/browser/extensions/service_worker_apitest.cc b/chrome/browser/extensions/service_worker_apitest.cc
index 46ac979..9228e92 100644
--- a/chrome/browser/extensions/service_worker_apitest.cc
+++ b/chrome/browser/extensions/service_worker_apitest.cc
@@ -156,12 +156,16 @@
   // may be empty) is written to it.
   const Extension* StartTestFromBackgroundPage(const char* script_name,
                                                std::string* error_or_null) {
+    ExtensionTestMessageListener ready_listener("ready", false);
     const Extension* extension =
         LoadExtension(test_data_dir_.AppendASCII("service_worker/background"));
     CHECK(extension);
+    CHECK(ready_listener.WaitUntilSatisfied());
+
     ExtensionHost* background_host =
         process_manager()->GetBackgroundHostForExtension(extension->id());
     CHECK(background_host);
+
     std::string error;
     CHECK(content::ExecuteScriptAndExtractString(
         background_host->host_contents(),