Bug 293834 test. r=bzbarsky
authorBen Karel <eschew@gmail.com>
Mon, 25 Aug 2008 13:21:28 -0400
changeset 18376 574dff8d8e89101cecd0f827cf5b99145b6fe1dd
parent 18375 9dd58a43924c6b978c8c5777898a019bd2775990
child 18377 86061324c4d3c6b26b1d30075540481e09ad2cb7
push idunknown
push userunknown
push dateunknown
reviewersbzbarsky
bugs293834
milestone1.9.1a2pre
Bug 293834 test. r=bzbarsky
embedding/Makefile.in
embedding/test/Makefile.in
embedding/test/bug293834_form.html
embedding/test/test_bug293834.html
--- a/embedding/Makefile.in
+++ b/embedding/Makefile.in
@@ -51,9 +51,13 @@ DIRS = base components browser
 ifndef MOZ_XUL_APP
 DIRS += lite
 endif
 
 ifdef ENABLE_TESTS
 XPCSHELL_TESTS = tests/unit
 endif
 
+ifdef MOZ_MOCHITEST
+DIRS += test
+endif
+
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/embedding/test/Makefile.in
@@ -0,0 +1,52 @@
+#
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is mozilla.org code.
+#
+# The Initial Developer of the Original Code is Mozilla Foundation.
+# Portions created by the Initial Developer are Copyright (C) 2008
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either of the GNU General Public License Version 2 or later (the "GPL"),
+# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+DEPTH		= ../..
+topsrcdir	= @top_srcdir@
+srcdir		= @srcdir@
+VPATH		= @srcdir@
+relativesrcdir  = embedding/test
+
+include $(DEPTH)/config/autoconf.mk
+include $(topsrcdir)/config/rules.mk
+
+_TEST_FILES =	test_bug293834.html \
+		bug293834_form.html \
+		$(NULL)
+
+libs:: $(_TEST_FILES)
+	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
+
new file mode 100644
--- /dev/null
+++ b/embedding/test/bug293834_form.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+
+<html>
+    <head>
+        <title>Nested iframe for bug 293834</title>
+        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    </head>
+    <body>
+    <form>
+        not prefilled: <input id="a-text"  type="text"></input><br>
+        prefilled: <input id="a-prefilled-text"  type="text" value="prefill "></input><br>
+
+        <input name="a-radio" id="radioa" value="radio-a" type="radio">Should be saved checked</input><br>
+        <input name="a-radio" value="radio-c" type="radio" checked="true">Initially checked</input><br>
+        <select id="aselect">
+            <option value="target">Should be saved selected</option>
+            <option value="default" selected="selected">Default Selected</option>
+        </select><br>
+        not prefilled: <textarea id="a-textbox"></textarea><br>
+        prefilled: <textarea id="a-prefilled-textbox">prefill </textarea><br>
+        <input id="a-checkbox" type="checkbox">Should be saved checked</input><br>
+        <input id="a-prefilled-checkbox" type="checkbox" checked="true">Initiallly checked</input><br>
+    </form>
+    </body>
+</html>
+
new file mode 100644
--- /dev/null
+++ b/embedding/test/test_bug293834.html
@@ -0,0 +1,142 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=293834
+-->
+<head>
+  <title>Test for Bug 293834</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" 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">
+
+</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 **/
+
+var textareas = ["a-textbox", "a-prefilled-textbox"];
+var textboxes = ["a-text", "a-prefilled-text"];
+
+function fillform(doc) {
+    for (var i in textareas) {
+        doc.getElementById(textareas[i]).textContent += "form state";
+    }
+    for (var 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 (var i in textareas) {
+        var textContent = doc.getElementById(textareas[i]).textContent;
+        ok(/form state/.test(textContent),
+            "Modified textarea "+textareas[i]+" form state not preserved!");
+    }
+    for (var i in textboxes) {
+        var value = doc.getElementById(textboxes[i]).value;
+        ok(/form state/.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");
+}
+
+netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+
+function getTempDir() {
+    return Cc["@mozilla.org/file/directory_service;1"]
+            .getService(Ci.nsIProperties)
+            .get("TmpD", Ci.nsILocalFile);
+}
+
+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, 0400,
+                    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;
+}
+
+function persistDocument(aDoc) {
+    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
+
+    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 ioService = Components.classes["@mozilla.org/network/io-service;1"]
+                    .getService(Ci.nsIIOService);
+
+
+    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, ioService.newFileURI(file),
+                         folder,
+                         aDoc.contentType,
+                         encodingFlags, kWrapColumn);
+    return getFileContents(file);
+}
+
+SimpleTest.waitForExplicitFinish();
+
+addLoadEvent(function() {
+    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
+    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>