Bug 1446037 - Remove CPOWs from browser_storage_dynamic_windows.js and simplify the test r=jryans
authorMichael Ratcliffe <mratcliffe@mozilla.com>
Thu, 15 Mar 2018 17:17:53 +0000
changeset 408607 84badebad54b181df9667c15a07641dbf6780d11
parent 408606 415082a3b84d5820bb6b8eee993dabd024a560e9
child 408608 3e8de42ee46ffc9603c3d02884b913200a207338
push id100996
push userbtara@mozilla.com
push dateSat, 17 Mar 2018 10:37:43 +0000
treeherdermozilla-inbound@97160a734959 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjryans
bugs1446037
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 1446037 - Remove CPOWs from browser_storage_dynamic_windows.js and simplify the test r=jryans Try to comprehend exactly how the original test worked... I dare you! MozReview-Commit-ID: 5EYOfhw3hVw
devtools/server/tests/browser/browser.ini
devtools/server/tests/browser/browser_storage_dynamic_windows.js
--- a/devtools/server/tests/browser/browser.ini
+++ b/devtools/server/tests/browser/browser.ini
@@ -90,17 +90,16 @@ skip-if = true # Needs to be updated for
 [browser_perf-recording-actor-01.js]
 skip-if = e10s # Bug 1183605 - devtools/server/tests/browser/ tests are still disabled in E10S
 [browser_perf-recording-actor-02.js]
 skip-if = e10s # Bug 1183605 - devtools/server/tests/browser/ tests are still disabled in E10S
 [browser_perf-samples-01.js]
 [browser_perf-samples-02.js]
 [browser_storage_cookies-duplicate-names.js]
 [browser_storage_dynamic_windows.js]
-uses-unsafe-cpows = true
 [browser_storage_listings.js]
 [browser_storage_updates.js]
 uses-unsafe-cpows = true
 [browser_stylesheets_getTextEmpty.js]
 [browser_stylesheets_nested-iframes.js]
 [browser_timeline.js]
 [browser_timeline_actors.js]
 [browser_timeline_iframes.js]
--- a/devtools/server/tests/browser/browser_storage_dynamic_windows.js
+++ b/devtools/server/tests/browser/browser_storage_dynamic_windows.js
@@ -1,263 +1,190 @@
 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-const {StorageFront} = require("devtools/shared/fronts/storage");
-Services.scriptloader.loadSubScript("chrome://mochitests/content/browser/devtools/server/tests/browser/storage-helpers.js", this);
+const { StorageFront } = require("devtools/shared/fronts/storage");
+Services.scriptloader.loadSubScript(
+  "chrome://mochitests/content/browser/devtools/server/tests/browser/storage-helpers.js",
+  this);
 
+// beforeReload references an object representing the initialized state of the
+// storage actor.
 const beforeReload = {
   cookies: {
     "http://test1.example.org": ["c1", "cs2", "c3", "uc1"],
     "http://sectest1.example.org": ["uc1", "cs2"]
   },
-  localStorage: {
-    "http://test1.example.org": ["ls1", "ls2"],
-    "http://sectest1.example.org": ["iframe-u-ls1"]
-  },
-  sessionStorage: {
-    "http://test1.example.org": ["ss1"],
-    "http://sectest1.example.org": ["iframe-u-ss1", "iframe-u-ss2"]
-  },
   indexedDB: {
     "http://test1.example.org": [
       JSON.stringify(["idb1", "obj1"]),
       JSON.stringify(["idb1", "obj2"]),
       JSON.stringify(["idb2", "obj3"]),
     ],
     "http://sectest1.example.org": []
+  },
+  localStorage: {
+    "http://test1.example.org": ["ls1", "ls2"],
+    "http://sectest1.example.org": ["iframe-u-ls1"]
+  },
+  sessionStorage: {
+    "http://test1.example.org": ["ss1"],
+    "http://sectest1.example.org": ["iframe-u-ss1", "iframe-u-ss2"]
   }
 };
 
-async function testStores(data, front) {
-  testWindowsBeforeReload(data);
-
-  // FIXME: Bug 1183581 - browser_storage_dynamic_windows.js IsSafeToRunScript
-  //                      errors when testing reload in E10S mode
-  // yield testReload(front);
-  await testAddIframe(front);
-  await testRemoveIframe(front);
-}
-
-function testWindowsBeforeReload(data) {
-  for (let storageType in beforeReload) {
-    ok(data[storageType], storageType + " storage actor is present");
-    is(Object.keys(data[storageType].hosts).length,
-       Object.keys(beforeReload[storageType]).length,
-       "Number of hosts for " + storageType + "match");
-    for (let host in beforeReload[storageType]) {
-      ok(data[storageType].hosts[host], "Host " + host + " is present");
-    }
-  }
-}
-
-function markOutMatched(toBeEmptied, data, deleted) {
-  if (!Object.keys(toBeEmptied).length) {
-    info("Object empty");
-    return;
+// afterIframeAdded references the items added when an iframe containing storage
+// items is added to the page.
+const afterIframeAdded = {
+  cookies: {
+    "https://sectest1.example.org": [
+      getCookieId("cs2", ".example.org", "/"),
+      getCookieId("sc1", "sectest1.example.org",
+                  "/browser/devtools/server/tests/browser/")
+    ],
+    "http://sectest1.example.org": [
+      getCookieId("sc1", "sectest1.example.org",
+                  "/browser/devtools/server/tests/browser/")
+    ]
+  },
+  indexedDB: {
+    // empty because indexed db creation happens after the page load, so at
+    // the time of window-ready, there was no indexed db present.
+    "https://sectest1.example.org": []
+  },
+  localStorage: {
+    "https://sectest1.example.org": ["iframe-s-ls1"]
+  },
+  sessionStorage: {
+    "https://sectest1.example.org": ["iframe-s-ss1"]
   }
-  ok(Object.keys(data).length,
-     "At least one storage type should be present");
-  for (let storageType in toBeEmptied) {
-    if (!data[storageType]) {
-      continue;
-    }
-    info("Testing for " + storageType);
-    for (let host in data[storageType]) {
-      ok(toBeEmptied[storageType][host], "Host " + host + " found");
-
-      if (!deleted) {
-        for (let item of data[storageType][host]) {
-          let index = toBeEmptied[storageType][host].indexOf(item);
-          ok(index > -1, "Item found - " + item);
-          if (index > -1) {
-            toBeEmptied[storageType][host].splice(index, 1);
-          }
-        }
-        if (!toBeEmptied[storageType][host].length) {
-          delete toBeEmptied[storageType][host];
-        }
-      } else {
-        delete toBeEmptied[storageType][host];
-      }
-    }
-    if (!Object.keys(toBeEmptied[storageType]).length) {
-      delete toBeEmptied[storageType];
-    }
-  }
-}
-
-function testAddIframe(front) {
-  info("Testing if new iframe addition works properly");
-  return new Promise(resolve => {
-    let shouldBeEmpty = {
-      localStorage: {
-        "https://sectest1.example.org": ["iframe-s-ls1"]
-      },
-      sessionStorage: {
-        "https://sectest1.example.org": ["iframe-s-ss1"]
-      },
-      cookies: {
-        "https://sectest1.example.org": [
-          getCookieId("cs2", ".example.org", "/"),
-          getCookieId("sc1", "sectest1.example.org",
-                      "/browser/devtools/server/tests/browser/")
-        ],
-        "http://sectest1.example.org": [
-          getCookieId("sc1", "sectest1.example.org",
-                      "/browser/devtools/server/tests/browser/")
-        ]
-      },
-      indexedDB: {
-        // empty because indexed db creation happens after the page load, so at
-        // the time of window-ready, there was no indexed db present.
-        "https://sectest1.example.org": []
-      },
-      Cache: {
-        "https://sectest1.example.org": []
-      }
-    };
-
-    let onStoresUpdate = data => {
-      info("checking if the hosts list is correct for this iframe addition");
-
-      markOutMatched(shouldBeEmpty, data.added);
+};
 
-      ok(!data.changed || !data.changed.cookies ||
-         !data.changed.cookies["https://sectest1.example.org"],
-         "Nothing got changed for cookies");
-      ok(!data.changed || !data.changed.localStorage ||
-         !data.changed.localStorage["https://sectest1.example.org"],
-         "Nothing got changed for local storage");
-      ok(!data.changed || !data.changed.sessionStorage ||
-         !data.changed.sessionStorage["https://sectest1.example.org"],
-         "Nothing got changed for session storage");
-      ok(!data.changed || !data.changed.indexedDB ||
-         !data.changed.indexedDB["https://sectest1.example.org"],
-         "Nothing got changed for indexed db");
-
-      ok(!data.deleted || !data.deleted.cookies ||
-         !data.deleted.cookies["https://sectest1.example.org"],
-         "Nothing got deleted for cookies");
-      ok(!data.deleted || !data.deleted.localStorage ||
-         !data.deleted.localStorage["https://sectest1.example.org"],
-         "Nothing got deleted for local storage");
-      ok(!data.deleted || !data.deleted.sessionStorage ||
-         !data.deleted.sessionStorage["https://sectest1.example.org"],
-         "Nothing got deleted for session storage");
-      ok(!data.deleted || !data.deleted.indexedDB ||
-         !data.deleted.indexedDB["https://sectest1.example.org"],
-         "Nothing got deleted for indexed db");
-
-      if (!Object.keys(shouldBeEmpty).length) {
-        info("Everything to be received is received.");
-        endTestReloaded();
-      }
-    };
-
-    let endTestReloaded = () => {
-      front.off("stores-update", onStoresUpdate);
-      resolve();
-    };
-
-    front.on("stores-update", onStoresUpdate);
-
-    // eslint-disable-next-line mozilla/no-cpows-in-tests
-    let iframe = gBrowser.contentDocumentAsCPOW.createElement("iframe");
-    iframe.src = ALT_DOMAIN_SECURED + "storage-secured-iframe.html";
-    // eslint-disable-next-line mozilla/no-cpows-in-tests
-    gBrowser.contentDocumentAsCPOW.querySelector("body").appendChild(iframe);
-  });
-}
-
-function testRemoveIframe(front) {
-  info("Testing if iframe removal works properly");
-  return new Promise(resolve => {
-    let shouldBeEmpty = {
-      localStorage: {
-        "http://sectest1.example.org": []
-      },
-      sessionStorage: {
-        "http://sectest1.example.org": []
-      },
-      Cache: {
-        "http://sectest1.example.org": []
-      },
-      indexedDB: {
-        "http://sectest1.example.org": []
-      }
-    };
-
-    let onStoresUpdate = data => {
-      info("checking if the hosts list is correct for this iframe deletion");
-
-      markOutMatched(shouldBeEmpty, data.deleted, true);
-
-      ok(!data.deleted.cookies || !data.deleted.cookies["sectest1.example.org"],
-        "Nothing got deleted for Cookies as " +
-        "the same hostname is still present");
-
-      ok(!data.changed || !data.changed.cookies ||
-         !data.changed.cookies["http://sectest1.example.org"],
-         "Nothing got changed for cookies");
-      ok(!data.changed || !data.changed.localStorage ||
-         !data.changed.localStorage["http://sectest1.example.org"],
-         "Nothing got changed for local storage");
-      ok(!data.changed || !data.changed.sessionStorage ||
-         !data.changed.sessionStorage["http://sectest1.example.org"],
-         "Nothing got changed for session storage");
-
-      ok(!data.added || !data.added.cookies ||
-         !data.added.cookies["http://sectest1.example.org"],
-         "Nothing got added for cookies");
-      ok(!data.added || !data.added.localStorage ||
-         !data.added.localStorage["http://sectest1.example.org"],
-         "Nothing got added for local storage");
-      ok(!data.added || !data.added.sessionStorage ||
-         !data.added.sessionStorage["http://sectest1.example.org"],
-         "Nothing got added for session storage");
-
-      if (!Object.keys(shouldBeEmpty).length) {
-        info("Everything to be received is received.");
-        endTestReloaded();
-      }
-    };
-
-    let endTestReloaded = () => {
-      front.off("stores-update", onStoresUpdate);
-      resolve();
-    };
-
-    front.on("stores-update", onStoresUpdate);
-
-    ContentTask.spawn(gBrowser.selectedBrowser, {}, () => {
-      for (let iframe of content.document.querySelectorAll("iframe")) {
-        if (iframe.src.startsWith("http:")) {
-          iframe.remove();
-          break;
-        }
-      }
-    });
-  });
-}
+// afterIframeRemoved references the items deleted when an iframe containing
+// storage items is removed from the page.
+const afterIframeRemoved = {
+  cookies: {
+    "http://sectest1.example.org": []
+  },
+  indexedDB: {
+    "http://sectest1.example.org": []
+  },
+  localStorage: {
+    "http://sectest1.example.org": []
+  },
+  sessionStorage: {
+    "http://sectest1.example.org": []
+  },
+};
 
 add_task(async function() {
   await openTabAndSetupStorage(MAIN_DOMAIN + "storage-dynamic-windows.html");
 
   initDebuggerServer();
   let client = new DebuggerClient(DebuggerServer.connectPipe());
   let form = await connectDebuggerClient(client);
   let front = StorageFront(client, form);
   let data = await front.listStores();
+
   await testStores(data, front);
 
   await clearStorage();
 
   // Forcing GC/CC to get rid of docshells and windows created by this test.
   forceCollections();
   await client.close();
   forceCollections();
   DebuggerServer.destroy();
   forceCollections();
 });
+
+async function testStores(data, front) {
+  testWindowsBeforeReload(data);
+
+  await testAddIframe(front);
+  await testRemoveIframe(front);
+}
+
+function testWindowsBeforeReload(data) {
+  for (let storageType in beforeReload) {
+    ok(data[storageType], `${storageType} storage actor is present`);
+    is(Object.keys(data[storageType].hosts).length,
+       Object.keys(beforeReload[storageType]).length,
+        `Number of hosts for ${storageType} match`);
+    for (let host in beforeReload[storageType]) {
+      ok(data[storageType].hosts[host], `Host ${host} is present`);
+    }
+  }
+}
+
+async function testAddIframe(front) {
+  info("Testing if new iframe addition works properly");
+
+  let update = front.once("stores-update");
+
+  await ContentTask.spawn(gBrowser.selectedBrowser, ALT_DOMAIN_SECURED,
+    secured => {
+      let doc = content.document;
+
+      let iframe = doc.createElement("iframe");
+      iframe.src = secured + "storage-secured-iframe.html";
+
+      doc.querySelector("body").appendChild(iframe);
+    }
+  );
+
+  let data = await update;
+
+  validateStorage(data, afterIframeAdded, "added");
+}
+
+async function testRemoveIframe(front) {
+  info("Testing if iframe removal works properly");
+
+  let update = front.once("stores-update");
+
+  await ContentTask.spawn(gBrowser.selectedBrowser, {}, () => {
+    for (let iframe of content.document.querySelectorAll("iframe")) {
+      if (iframe.src.startsWith("http:")) {
+        iframe.remove();
+        break;
+      }
+    }
+  });
+
+  let data = await update;
+
+  validateStorage(data, afterIframeRemoved, "deleted");
+}
+
+function validateStorage(actual, expected, category = "") {
+  if (category) {
+    for (let cat of ["added", "changed", "deleted"]) {
+      if (cat === category) {
+        ok(actual[cat], `Data from the iframe has been ${cat}.`);
+      } else {
+        ok(!actual[cat], `No data was ${cat}.`);
+      }
+    }
+  }
+
+  for (let [type, expectedData] of Object.entries(expected)) {
+    let actualData = category ? actual[category][type] : actual[type];
+
+    ok(actualData, `${type} contains data.`);
+
+    let actualKeys = Object.keys(actualData);
+    let expectedKeys = Object.keys(expectedData);
+    is(actualKeys.length, expectedKeys.length, `${type} data is the correct length.`);
+
+    for (let [key, dataValues] of Object.entries(expectedData)) {
+      ok(actualData[key], `${type} data contains the key ${key}.`);
+
+      for (let dataValue of dataValues) {
+        ok(actualData[key].includes(dataValue),
+          `${type}[${key}] contains "${dataValue}".`);
+      }
+    }
+  }
+}