Bug 1253777 P2 Test passing a file-backed blob to FetchEvent.respondWith(). r=jdm a=ritu
authorBen Kelly <ben@wanderview.com>
Wed, 09 Mar 2016 13:41:29 -0800
changeset 323400 4ce28157620f5ce2cbd32638d7e634780c8b10aa
parent 323399 668216085fce11fa3a8eea1af17e6da1827652a1
child 323401 edcc2ad52ece9427517f1bc7d1d4e9497ed479f6
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm, ritu
bugs1253777
milestone47.0a2
Bug 1253777 P2 Test passing a file-backed blob to FetchEvent.respondWith(). r=jdm a=ritu MozReview-Commit-ID: 3ktncVSAQ4y
dom/workers/test/serviceworkers/file_blob_response_worker.js
dom/workers/test/serviceworkers/mochitest.ini
dom/workers/test/serviceworkers/test_file_blob_response.html
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/file_blob_response_worker.js
@@ -0,0 +1,38 @@
+function makeFileBlob(obj) {
+  return new Promise(function(resolve, reject) {
+    var request = indexedDB.open('file_blob_response_worker', 1);
+    request.onerror = reject;
+    request.onupgradeneeded = function(evt) {
+      var db = evt.target.result;
+      db.onerror = reject;
+
+      var objectStore = db.createObjectStore('test', { autoIncrement: true });
+      var index = objectStore.createIndex('test', 'index');
+    };
+
+    request.onsuccess = function(evt) {
+      var db = evt.target.result;
+      db.onerror = reject;
+
+      var blob = new Blob([JSON.stringify(obj)],
+                          { type: 'application/json' });
+      var data = { blob: blob, index: 5 };
+
+      objectStore = db.transaction('test', 'readwrite').objectStore('test');
+      objectStore.add(data).onsuccess = function(evt) {
+        var key = evt.target.result;
+        objectStore = db.transaction('test').objectStore('test');
+        objectStore.get(key).onsuccess = function(evt) {
+          resolve(evt.target.result.blob);
+        };
+      };
+    };
+  });
+}
+
+self.addEventListener('fetch', function(evt) {
+  var result = { value: 'success' };
+  evt.respondWith(makeFileBlob(result).then(function(blob) {
+    return new Response(blob)
+  }));
+});
--- a/dom/workers/test/serviceworkers/mochitest.ini
+++ b/dom/workers/test/serviceworkers/mochitest.ini
@@ -192,16 +192,17 @@ support-files =
   xslt_worker.js
   xslt/*
   unresolved_fetch_worker.js
   header_checker.sjs
   openWindow_worker.js
   redirect.sjs
   open_window/client.html
   lorem_script.js
+  file_blob_response_worker.js
 
 [test_bug1151916.html]
 [test_claim.html]
 [test_claim_fetch.html]
 [test_claim_oninstall.html]
 [test_close.html]
 [test_controller.html]
 [test_cross_origin_url_after_redirect.html]
@@ -293,8 +294,9 @@ skip-if = e10s && debug && os == 'win'
 [test_csp_upgrade-insecure_intercept.html]
 [test_serviceworker_header.html]
 [test_openWindow.html]
 skip-if = toolkit == "android" || toolkit == "gonk" || (e10s && debug && os == 'win')
 [test_imagecache.html]
 [test_imagecache_max_age.html]
 [test_importscript_mixedcontent.html]
 tags = mcb
+[test_file_blob_response.html]
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/test_file_blob_response.html
@@ -0,0 +1,86 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Bug 1253777 - Test interception using file blob response body</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none"></div>
+<pre id="test"></pre>
+<script class="testbody" type="text/javascript">
+  var registration;
+  var scope = './file_blob_response/';
+  function start() {
+    return navigator.serviceWorker.register("file_blob_response_worker.js",
+                                            { scope: scope })
+      .then(function(swr) {
+        registration = swr;
+        return new Promise(function(resolve) {
+          registration.installing.onstatechange = function(evt) {
+            if (evt.target.state === 'activated') {
+              evt.target.onstate = null;
+              resolve();
+            }
+          }
+        });
+      });
+  }
+
+  function unregister() {
+    return registration.unregister().then(function(result) {
+      ok(result, "Unregister should return true.");
+    }, function(e) {
+      ok(false, "Unregistering the SW failed with " + e + "\n");
+    });
+  }
+
+  function withFrame(url) {
+    return new Promise(function(resolve, reject) {
+      var content = document.getElementById("content");
+      ok(content, "Parent exists.");
+
+      var frame = document.createElement("iframe");
+      frame.setAttribute('src', url);
+      content.appendChild(frame);
+
+      frame.addEventListener('load', function loadCallback(evt) {
+        frame.removeEventListener('load', loadCallback);
+        resolve(frame);
+      });
+    });
+  }
+
+  function runTest() {
+    start()
+      .then(function() {
+        return withFrame(scope + 'dummy.txt');
+      })
+      .then(function(frame) {
+        var result = JSON.parse(frame.contentWindow.document.body.textContent);
+        frame.remove();
+        is(result.value, 'success');
+      })
+      .catch(function(e) {
+        ok(false, "Some test failed with error " + e);
+      })
+      .then(unregister)
+      .then(SimpleTest.finish);
+  }
+
+  SimpleTest.waitForExplicitFinish();
+  SpecialPowers.pushPrefEnv({"set": [
+    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
+    ["dom.serviceWorkers.enabled", true],
+    ["dom.serviceWorkers.testing.enabled", true]
+  ]}, runTest);
+</script>
+</pre>
+</body>
+</html>
+