Commit 67c285ed authored by Ben Karel's avatar Ben Karel
Browse files

Bug 293834 test. r=bzbarsky

parent 4c0efcaf
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -56,4 +56,8 @@ ifdef ENABLE_TESTS
XPCSHELL_TESTS = tests/unit
endif

ifdef MOZ_MOCHITEST
DIRS += test
endif

include $(topsrcdir)/config/rules.mk
+52 −0
Original line number Diff line number Diff line
#
# ***** 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)
+26 −0
Original line number Diff line number Diff line
<!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>
+142 −0
Original line number Diff line number Diff line
<!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>