embedding/test/test_bug293834.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/embedding/test/test_bug293834.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,137 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=293834
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 293834</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body>
    1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=293834">Mozilla Bug 293834</a>
    1.16 +<p id="display">
    1.17 +
    1.18 +</p>
    1.19 +<pre id="results"></pre>
    1.20 +<div id="content" style="display: none">
    1.21 +    <iframe src="bug293834_form.html" id="source"></iframe>
    1.22 +    <br>
    1.23 +    <iframe id="dest"></iframe>
    1.24 +</div>
    1.25 +<pre id="test">
    1.26 +<script class="testbody" type="text/javascript">
    1.27 +/** Test for Bug 293834 **/
    1.28 +
    1.29 +var textareas = ["a-textbox", "a-prefilled-textbox"];
    1.30 +var textboxes = ["a-text", "a-prefilled-text"];
    1.31 +
    1.32 +function fillform(doc) {
    1.33 +    for (var i in textareas) {
    1.34 +        doc.getElementById(textareas[i]).textContent += "form state";
    1.35 +    }
    1.36 +    for (var i in textboxes) {
    1.37 +        doc.getElementById(textboxes[i]).value += "form state";
    1.38 +    }
    1.39 +    doc.getElementById('a-checkbox').checked = true;
    1.40 +    doc.getElementById("radioa").checked = true;
    1.41 +    doc.getElementById("aselect").selectedIndex = 0;
    1.42 +}
    1.43 +
    1.44 +function checkform(doc) {
    1.45 +    for (var i in textareas) {
    1.46 +        var textContent = doc.getElementById(textareas[i]).textContent;
    1.47 +        ok(/form\s+state/m.test(textContent),
    1.48 +            "Modified textarea "+textareas[i]+" form state not preserved!");
    1.49 +    }
    1.50 +    for (var i in textboxes) {
    1.51 +        var value = doc.getElementById(textboxes[i]).value;
    1.52 +        ok(/form\s+state/m.test(value),
    1.53 +            "Modified textbox "+textboxes[i]+" form state not preserved!");
    1.54 +    }
    1.55 +    ok(doc.getElementById('a-checkbox').checked,
    1.56 +        "Modified checkbox checked state not preserved!");
    1.57 +    ok(doc.getElementById("radioa").checked,
    1.58 +        "Modified radio checked state not preserved!");
    1.59 +    ok(doc.getElementById("aselect").selectedIndex == 0,
    1.60 +        "Modified select selected index not preserved");
    1.61 +}
    1.62 +
    1.63 +const Cc = SpecialPowers.Cc;
    1.64 +const Ci = SpecialPowers.Ci;
    1.65 +
    1.66 +function getTempDir() {
    1.67 +    return Cc["@mozilla.org/file/directory_service;1"]
    1.68 +            .getService(Ci.nsIProperties)
    1.69 +            .get("TmpD", Ci.nsILocalFile);
    1.70 +}
    1.71 +
    1.72 +function getFileContents(aFile) {
    1.73 +    const PR_RDONLY = 0x01;
    1.74 +    var fileStream = Cc["@mozilla.org/network/file-input-stream;1"]
    1.75 +                        .createInstance(Ci.nsIFileInputStream);
    1.76 +    fileStream.init(aFile, PR_RDONLY, 0400,
    1.77 +                    Ci.nsIFileInputStream.DELETE_ON_CLOSE
    1.78 +                  | Ci.nsIFileInputStream.CLOSE_ON_EOF);
    1.79 +    var inputStream = Cc["@mozilla.org/scriptableinputstream;1"]
    1.80 +                        .createInstance(Ci.nsIScriptableInputStream);
    1.81 +    inputStream.init(fileStream);
    1.82 +    var data = "";
    1.83 +    do {
    1.84 +        var str = inputStream.read(inputStream.available());
    1.85 +        data += str;
    1.86 +    } while(str.length > 0);
    1.87 +
    1.88 +    return data;
    1.89 +}
    1.90 +
    1.91 +function persistDocument(aDoc) {
    1.92 +    const nsIWBP = Ci.nsIWebBrowserPersist;
    1.93 +    const persistFlags =
    1.94 +                  nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES
    1.95 +                | nsIWBP.PERSIST_FLAGS_FROM_CACHE
    1.96 +                | nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION
    1.97 +    const encodingFlags =
    1.98 +                  nsIWBP.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES;
    1.99 +
   1.100 +    var ioService = Cc["@mozilla.org/network/io-service;1"]
   1.101 +                    .getService(Ci.nsIIOService);
   1.102 +
   1.103 +
   1.104 +    var file = getTempDir();
   1.105 +    file.append("bug293834-serialized.html");
   1.106 +
   1.107 +    var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
   1.108 +                    .createInstance(Ci.nsIWebBrowserPersist);
   1.109 +    persist.progressListener = null;
   1.110 +    persist.persistFlags = persistFlags;
   1.111 +    const kWrapColumn = 80;
   1.112 +    var folder = getTempDir();
   1.113 +    folder.append("bug293834-serialized");
   1.114 +    persist.saveDocument(aDoc, ioService.newFileURI(file),
   1.115 +                         folder,
   1.116 +                         aDoc.contentType,
   1.117 +                         encodingFlags, kWrapColumn);
   1.118 +    return getFileContents(file);
   1.119 +}
   1.120 +
   1.121 +SimpleTest.waitForExplicitFinish();
   1.122 +
   1.123 +addLoadEvent(function() {
   1.124 +    var srcDoc = document.getElementById('source').contentDocument;
   1.125 +    fillform(srcDoc);
   1.126 +    checkform(srcDoc);
   1.127 +    var serializedString = persistDocument(srcDoc);
   1.128 +
   1.129 +    // We can't access file:/// URLs directly for security reasons,
   1.130 +    // so we have to parse the serialized content string indirectly
   1.131 +    var targetDoc = document.getElementById('dest').contentDocument;
   1.132 +    targetDoc.write(serializedString);
   1.133 +
   1.134 +    checkform(targetDoc);
   1.135 +    SimpleTest.finish();
   1.136 +});
   1.137 +</script>
   1.138 +</pre>
   1.139 +</body>
   1.140 +</html>

mercurial