Bug 1252223 - convert nsIWebBrowserPersist test to a browser mochitest so it works in e10s, r=mak
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Fri, 07 Jun 2019 23:16:04 +0000
changeset 477926 bdd7b4bfa07a3faa58706b788c7eea301f8fcda2
parent 477925 fd3d6eb262be9f5dd13746eb0dd8e2991e1d59e1
child 477927 84c57f9dda5ba7eb5715b7213ac92f29ed42457f
push id36127
push usernbeleuzu@mozilla.com
push dateSat, 08 Jun 2019 09:39:25 +0000
treeherdermozilla-central@b26753fb35e7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1252223
milestone69.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 1252223 - convert nsIWebBrowserPersist test to a browser mochitest so it works in e10s, r=mak Differential Revision: https://phabricator.services.mozilla.com/D33341
toolkit/components/windowcreator/test/browser.ini
toolkit/components/windowcreator/test/browser_save_form_input_state.js
toolkit/components/windowcreator/test/bug293834_form.html
toolkit/components/windowcreator/test/file_form_state.html
toolkit/components/windowcreator/test/mochitest.ini
toolkit/components/windowcreator/test/test_bug293834.html
--- a/toolkit/components/windowcreator/test/browser.ini
+++ b/toolkit/components/windowcreator/test/browser.ini
@@ -1,6 +1,7 @@
-[DEFAULT]
+[browser_bug1204626.js]
 support-files =
   bug1204626_doc0.html
   bug1204626_doc1.html
-
-[browser_bug1204626.js]
+[browser_save_form_input_state.js]
+support-files =
+  file_form_state.html
rename from toolkit/components/windowcreator/test/test_bug293834.html
rename to toolkit/components/windowcreator/test/browser_save_form_input_state.js
--- a/toolkit/components/windowcreator/test/test_bug293834.html
+++ b/toolkit/components/windowcreator/test/browser_save_form_input_state.js
@@ -1,131 +1,99 @@
-<!DOCTYPE HTML>
-<html>
-<!--
-https://bugzilla.mozilla.org/show_bug.cgi?id=293834
--->
-<head>
-  <title>Test for Bug 293834</title>
-  <script src="/tests/SimpleTest/SimpleTest.js"></script>
-  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
-</head>
-<body>
-<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=293834">Mozilla Bug 293834</a>
-<p id="display">
+/* Any copyright is dedicated to the Public Domain.
+ * https://creativecommons.org/publicdomain/zero/1.0/ */
 
-</p>
-<pre id="results"></pre>
-<div id="content" style="display: none">
-    <iframe src="bug293834_form.html" id="source"></iframe>
-    <br>
-    <iframe id="dest"></iframe>
-</div>
-<pre id="test">
-<script class="testbody" type="text/javascript">
-/** Test for Bug 293834 **/
+"use strict";
+
+const TEST_PATH = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "http://example.com");
 
 var textareas = ["a-textbox", "a-prefilled-textbox"];
 var textboxes = ["a-text", "a-prefilled-text"];
 
-function fillform(doc) {
-    for (let i in textareas) {
-        doc.getElementById(textareas[i]).textContent += "form state";
-    }
-    for (let i in textboxes) {
-        doc.getElementById(textboxes[i]).value += "form state";
-    }
-    doc.getElementById("a-checkbox").checked = true;
-    doc.getElementById("radioa").checked = true;
-    doc.getElementById("aselect").selectedIndex = 0;
+/**
+ * Ported from mochitest, this test verifies that form state is saved by our
+ * webbrowserpersist code. See bug 293834 for context.
+ */
+add_task(async function checkFormStateSaved() {
+  await BrowserTestUtils.withNewTab(TEST_PATH + "file_form_state.html", async (browser) => {
+    await ContentTask.spawn(browser, {textareas, textboxes}, fillform);
+    let fileURISpec = await new Promise((resolve, reject) => {
+      let stack = Components.stack.caller;
+      browser.frameLoader.startPersistence(0, {
+        onDocumentReady(document) {
+          // Note that 'document' here is going to be an nsIWebBrowserPersistDocument,
+          // not a regular DOM document.
+          resolve(persistDocument(document));
+        },
+        onError(status) {
+          reject(Components.Exception("saveBrowser failed asynchronously in startPersistence",
+                                      status, stack));
+        },
+      });
+    });
+    await BrowserTestUtils.withNewTab(fileURISpec, async (otherBrowser) => {
+      await ContentTask.spawn(otherBrowser, {textareas, textboxes}, checkform);
+    });
+  });
+});
+
+// eslint-disable-next-line no-shadow
+function fillform({textareas, textboxes}) {
+  let doc = content.document;
+  for (let i in textareas) {
+    doc.getElementById(textareas[i]).textContent += "form state";
+  }
+  for (let i in textboxes) {
+    doc.getElementById(textboxes[i]).value += "form state";
+  }
+  doc.getElementById("a-checkbox").checked = true;
+  doc.getElementById("radioa").checked = true;
+  doc.getElementById("aselect").selectedIndex = 0;
 }
 
-function checkform(doc) {
-    for (let i in textareas) {
-        var textContent = doc.getElementById(textareas[i]).textContent;
-        ok(/form\s+state/m.test(textContent),
-            "Modified textarea " + textareas[i] + " form state not preserved!");
-    }
-    for (let i in textboxes) {
-        var value = doc.getElementById(textboxes[i]).value;
-        ok(/form\s+state/m.test(value),
-            "Modified textbox " + textboxes[i] + " form state not preserved!");
-    }
-    ok(doc.getElementById("a-checkbox").checked,
-        "Modified checkbox checked state not preserved!");
-    ok(doc.getElementById("radioa").checked,
-        "Modified radio checked state not preserved!");
-    ok(doc.getElementById("aselect").selectedIndex == 0,
-        "Modified select selected index not preserved");
+// eslint-disable-next-line no-shadow
+function checkform({textareas, textboxes}) {
+  let doc = content.document;
+  for (let i in textareas) {
+    var textContent = doc.getElementById(textareas[i]).textContent;
+    Assert.ok(/form\s+state/m.test(textContent),
+              "Modified textarea " + textareas[i] + " form state should be preserved!");
+  }
+  for (let i in textboxes) {
+    var value = doc.getElementById(textboxes[i]).value;
+    Assert.ok(/form\s+state/m.test(value),
+              "Modified textbox " + textboxes[i] + " form state should be preserved!");
+  }
+  Assert.ok(doc.getElementById("a-checkbox").checked,
+            "Modified checkbox checked state should be preserved!");
+  Assert.ok(doc.getElementById("radioa").checked,
+            "Modified radio checked state should be preserved!");
+  Assert.equal(doc.getElementById("aselect").selectedIndex, 0,
+               "Modified select selected index should be preserved");
 }
 
-const Cc = SpecialPowers.Cc;
-const Ci = SpecialPowers.Ci;
-
 function getTempDir() {
-    return SpecialPowers.Services.dirsvc.get("TmpD", Ci.nsIFile);
-}
-
-function getFileContents(aFile) {
-    const PR_RDONLY = 0x01;
-    var fileStream = Cc["@mozilla.org/network/file-input-stream;1"]
-                        .createInstance(Ci.nsIFileInputStream);
-    fileStream.init(aFile, PR_RDONLY, 0o400,
-                    Ci.nsIFileInputStream.DELETE_ON_CLOSE
-                  | Ci.nsIFileInputStream.CLOSE_ON_EOF);
-    var inputStream = Cc["@mozilla.org/scriptableinputstream;1"]
-                        .createInstance(Ci.nsIScriptableInputStream);
-    inputStream.init(fileStream);
-    var data = "";
-    do {
-        var str = inputStream.read(inputStream.available());
-        data += str;
-    } while (str.length > 0);
-
-    return data;
+  return Services.dirsvc.get("TmpD", Ci.nsIFile);
 }
 
 function persistDocument(aDoc) {
-    const nsIWBP = Ci.nsIWebBrowserPersist;
-    const persistFlags =
-                  nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES
-                | nsIWBP.PERSIST_FLAGS_FROM_CACHE
-                | nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
-    const encodingFlags =
-                  nsIWBP.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES;
+  const nsIWBP = Ci.nsIWebBrowserPersist;
+  const persistFlags =
+    nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES
+    | nsIWBP.PERSIST_FLAGS_FROM_CACHE
+    | nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
+  const encodingFlags =
+    nsIWBP.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES;
 
-    var file = getTempDir();
-    file.append("bug293834-serialized.html");
+  var file = getTempDir();
+  file.append("bug293834-serialized.html");
 
-    var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
-                    .createInstance(Ci.nsIWebBrowserPersist);
-    persist.progressListener = null;
-    persist.persistFlags = persistFlags;
-    const kWrapColumn = 80;
-    var folder = getTempDir();
-    folder.append("bug293834-serialized");
-    persist.saveDocument(aDoc, SpecialPowers.Services.io.newFileURI(file),
-                         folder,
-                         aDoc.contentType,
-                         encodingFlags, kWrapColumn);
-    return getFileContents(file);
+  var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
+    .createInstance(Ci.nsIWebBrowserPersist);
+  persist.progressListener = null;
+  persist.persistFlags = persistFlags;
+  const kWrapColumn = 80;
+  var folder = getTempDir();
+  folder.append("bug293834-serialized");
+  persist.saveDocument(aDoc, file, folder, "text/html",
+                       encodingFlags, kWrapColumn);
+  return Services.io.newFileURI(file).spec;
 }
-
-SimpleTest.waitForExplicitFinish();
-
-addLoadEvent(function() {
-    var srcDoc = document.getElementById("source").contentDocument;
-    fillform(srcDoc);
-    checkform(srcDoc);
-    var serializedString = persistDocument(srcDoc);
-
-    // We can't access file:/// URLs directly for security reasons,
-    // so we have to parse the serialized content string indirectly
-    var targetDoc = document.getElementById("dest").contentDocument;
-    targetDoc.write(serializedString);
-
-    checkform(targetDoc);
-    SimpleTest.finish();
-});
-</script>
-</pre>
-</body>
-</html>
rename from toolkit/components/windowcreator/test/bug293834_form.html
rename to toolkit/components/windowcreator/test/file_form_state.html
--- a/toolkit/components/windowcreator/test/mochitest.ini
+++ b/toolkit/components/windowcreator/test/mochitest.ini
@@ -1,13 +1,7 @@
-[DEFAULT]
-support-files =
-  bug293834_form.html
-
-[test_bug293834.html]
-skip-if = (toolkit == "cocoa" && e10s) # bug 1252223
 [test_bug499115.html]
 [test_nsFind.html]
 [test_private_window_from_content.html]
 [test_window_open_position_constraint.html]
 skip-if = toolkit == 'android'
 [test_window_open_units.html]
 skip-if = toolkit == 'android' || (devedition && os == 'win' && bits == 32) # Windows: bug 1540566