Bug 1455985 - IDB Private Browsing Tests. r=janv
authorAndrew Sutherland <asutherland@asutherland.org>
Mon, 06 Nov 2017 18:16:40 -0800
changeset 468651 9e2f70627e79a22bcacd41d97f671bbeb9bd98ea
parent 468650 cb7f6ce7dff5ecf1a5ae4186a86736a4afaf7b0d
child 468652 7793bcae69f6a0faea864b26a3de9b657b99bdcc
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjanv
bugs1455985
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1455985 - IDB Private Browsing Tests. r=janv
dom/indexedDB/test/browser.ini
dom/indexedDB/test/browser_private_idb.js
dom/indexedDB/test/page_private_idb.html
--- a/dom/indexedDB/test/browser.ini
+++ b/dom/indexedDB/test/browser.ini
@@ -9,16 +9,18 @@ support-files =
   browserHelpers.js
   browser_permissionsPrompt.html
   browser_permissionsSharedWorker.html
   browser_permissionsSharedWorker.js
   browser_permissionsWorker.html
   browser_permissionsWorker.js
   bug839193.js
   bug839193.xul
+  page_private_idb.html
 
 [browser_forgetThisSite.js]
 [browser_permissionsPromptAllow.js]
 [browser_permissionsPromptDeny.js]
 [browser_permissionsPromptWorker.js]
 [browser_perwindow_privateBrowsing.js]
 skip-if = os == 'linux' && debug # bug 1394671
+[browser_private_idb.js]
 [browser_bug839193.js]
new file mode 100644
--- /dev/null
+++ b/dom/indexedDB/test/browser_private_idb.js
@@ -0,0 +1,157 @@
+async function idbCheckFunc() {
+  let factory;
+  try {
+    // in a worker, this resolves directly.
+    factory = indexedDB;
+  } catch (ex) {
+    // in a frame-script, we need to pierce "content"
+    // eslint-disable-next-line mozilla/no-cpows-in-tests
+    factory = content.indexedDB;
+  }
+  try {
+    console.log("opening db");
+    const req = factory.open("db", 1);
+    const result = await new Promise((resolve, reject) => {
+      req.onerror = () => { resolve("error"); };
+      // we expect the db to not exist and for created to resolve first
+      req.onupgradeneeded = () => { resolve("created"); };
+      // ...so this will lose the race
+      req.onsuccess = (event) => {
+        resolve("already-exists");
+      };
+    });
+    const db = req.result;
+    console.log("db req completed:", result);
+    if (result !== "error") {
+      db.close();
+      console.log("deleting database");
+      await new Promise((resolve, reject) => {
+        const delReq = factory.deleteDatabase("db");
+        delReq.onerror = reject;
+        delReq.onsuccess = resolve;
+      });
+      console.log("deleted database");
+    }
+    return result;
+  } catch (ex) {
+    console.error("received error:", ex);
+    return "exception";
+  }
+}
+
+async function workerDriverFunc() {
+  const resultPromise = idbCheckFunc();
+  /* eslint-env worker */
+  // (SharedWorker)
+  if (!("postMessage" in self)) {
+    addEventListener("connect", function(evt) {
+      const port = evt.ports[0];
+      resultPromise.then((result) => {
+        console.log("worker test completed, postMessage-ing result:", result);
+        port.postMessage({ idbResult: result });
+      });
+    });
+  }
+  const result = await resultPromise;
+  // (DedicatedWorker)
+  if ("postMessage" in self) {
+    console.log("worker test completed, postMessage-ing result:", result);
+    postMessage({ idbResult: result });
+  }
+}
+
+const workerScript = `
+${idbCheckFunc.toSource()}
+(${workerDriverFunc.toSource()})();
+`;
+const workerScriptBlob = new Blob([workerScript]);
+
+/**
+ * This function is deployed via ContextTask.spawn and operates in a tab
+ * frame script context.  Its job is to create the worker that will run the
+ * idbCheckFunc and return the result to us.
+ */
+async function workerCheckDeployer({ srcBlob, workerType }) {
+  let worker, port;
+  // eslint-disable-next-line mozilla/no-cpows-in-tests
+  const url = content.URL.createObjectURL(srcBlob);
+  if (workerType === "dedicated") {
+    worker = new content.Worker(url);
+    port = worker;
+  } else if (workerType === "shared") {
+    worker = new content.SharedWorker(url);
+    port = worker.port;
+    port.start();
+  } else {
+    throw new Error("bad worker type!");
+  }
+
+  const result = await new Promise((resolve, reject) => {
+    port.addEventListener("message", function(evt) {
+      resolve(evt.data.idbResult);
+    }, { once: true });
+    worker.addEventListener("error", function(evt) {
+      console.error("worker problem:", evt);
+      reject(evt);
+    });
+  })
+  console.log("worker completed test with result:", result);
+
+  return result;
+}
+
+function checkTabWindowIDB(tab) {
+  return ContentTask.spawn(tab.linkedBrowser, null, idbCheckFunc);
+}
+
+async function checkTabDedicatedWorkerIDB(tab) {
+  return ContentTask.spawn(
+    tab.linkedBrowser,
+    {
+      srcBlob: workerScriptBlob,
+      workerType: "dedicated"
+    },
+    workerCheckDeployer);
+}
+
+async function checkTabSharedWorkerIDB(tab) {
+  return ContentTask.spawn(
+    tab.linkedBrowser,
+    {
+      srcBlob: workerScriptBlob,
+      workerType: "shared"
+    },
+    workerCheckDeployer);
+}
+
+add_task(async function() {
+  const pageUrl =
+    "http://example.com/browser/dom/indexedDB/test/page_private_idb.html";
+
+  let normalWin = await BrowserTestUtils.openNewBrowserWindow();
+  let privateWin =
+    await BrowserTestUtils.openNewBrowserWindow({private: true});
+
+  let normalTab =
+    await BrowserTestUtils.openNewForegroundTab(normalWin.gBrowser, pageUrl);
+  let privateTab =
+    await BrowserTestUtils.openNewForegroundTab(privateWin.gBrowser, pageUrl);
+
+  is(await checkTabWindowIDB(normalTab), "created",
+     "IndexedDB works in a non-private-browsing page.");
+  is(await checkTabWindowIDB(privateTab), "error",
+     "IndexedDB does not work in a private-browsing page.");
+
+  is(await checkTabDedicatedWorkerIDB(normalTab), "created",
+     "IndexedDB works in a non-private-browsing Worker.");
+  is(await checkTabDedicatedWorkerIDB(privateTab), "error",
+     "IndexedDB does not work in a private-browsing Worker.");
+
+  is(await checkTabSharedWorkerIDB(normalTab), "created",
+     "IndexedDB works in a non-private-browsing SharedWorker.");
+  is(await checkTabSharedWorkerIDB(privateTab), "error",
+     "IndexedDB does not work in a private-browsing SharedWorker.");
+
+  await BrowserTestUtils.closeWindow(normalWin);
+  await BrowserTestUtils.closeWindow(privateWin);
+});
new file mode 100644
--- /dev/null
+++ b/dom/indexedDB/test/page_private_idb.html
@@ -0,0 +1,9 @@
+<!doctype html>
+<html>
+<head>
+  <meta charset="utf-8">
+</head>
+<body>
+  All the interesting stuff happens in ContentTask.spawn() calls.
+</body>
+</html>