Extension SW - add test for ServiceWorkerRegistration.sync.

The test puts the sync manager to offline and mode and then requests
  a sync through ServiceWorker. The ServiceWorker's onsync method
  fires when we put the sync manager back to online mode.

BUG=545535

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

Cr-Commit-Position: refs/heads/master@{#360417}
diff --git a/chrome/browser/extensions/service_worker_apitest.cc b/chrome/browser/extensions/service_worker_apitest.cc
index b1018d8c..a4786927 100644
--- a/chrome/browser/extensions/service_worker_apitest.cc
+++ b/chrome/browser/extensions/service_worker_apitest.cc
@@ -12,7 +12,9 @@
 #include "content/public/browser/navigation_controller.h"
 #include "content/public/browser/navigation_entry.h"
 #include "content/public/browser/web_contents.h"
+#include "content/public/common/content_switches.h"
 #include "content/public/common/page_type.h"
+#include "content/public/test/background_sync_test_util.h"
 #include "content/public/test/browser_test_utils.h"
 #include "extensions/browser/extension_host.h"
 #include "extensions/browser/process_manager.h"
@@ -113,6 +115,27 @@
   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest);
 };
 
+class ServiceWorkerBackgroundSyncTest : public ServiceWorkerTest {
+ public:
+  ServiceWorkerBackgroundSyncTest() {}
+  ~ServiceWorkerBackgroundSyncTest() override {}
+
+  void SetUpCommandLine(base::CommandLine* command_line) override {
+    // ServiceWorkerRegistration.sync requires experimental flag.
+    command_line->AppendSwitch(
+        switches::kEnableExperimentalWebPlatformFeatures);
+    ServiceWorkerTest::SetUpCommandLine(command_line);
+  }
+
+  void SetUp() override {
+    content::background_sync_test_util::SetIgnoreNetworkChangeNotifier(true);
+    ServiceWorkerTest::SetUp();
+  }
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerBackgroundSyncTest);
+};
+
 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceedsOnTrunk) {
   StartTestFromBackgroundPage("register.js", kExpectSuccess);
 }
@@ -342,4 +365,30 @@
                                   "page.html"));
 }
 
+IN_PROC_BROWSER_TEST_F(ServiceWorkerBackgroundSyncTest, Sync) {
+  const Extension* extension = LoadExtensionWithFlags(
+      test_data_dir_.AppendASCII("service_worker/sync"), kFlagNone);
+  ASSERT_TRUE(extension);
+  ui_test_utils::NavigateToURL(browser(),
+                               extension->GetResourceURL("page.html"));
+  content::WebContents* web_contents =
+      browser()->tab_strip_model()->GetActiveWebContents();
+
+  // Prevent firing by going offline.
+  content::background_sync_test_util::SetOnline(web_contents, false);
+
+  ExtensionTestMessageListener sync_listener("SYNC: send-chats", false);
+  sync_listener.set_failure_message("FAIL");
+
+  std::string result;
+  ASSERT_TRUE(content::ExecuteScriptAndExtractString(
+      web_contents, "window.runServiceWorker()", &result));
+  ASSERT_EQ("SERVICE_WORKER_READY", result);
+
+  EXPECT_FALSE(sync_listener.was_satisfied());
+  // Resume firing by going online.
+  content::background_sync_test_util::SetOnline(web_contents, true);
+  EXPECT_TRUE(sync_listener.WaitUntilSatisfied());
+}
+
 }  // namespace extensions