summaryrefslogtreecommitdiffstats
path: root/embedding/test/test_bug293834.html
blob: 0747bf8b7436fdcc90829697ffed07d5762593b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=293834
-->
<head>
  <title>Test for Bug 293834</title>
  <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\s+state/m.test(textContent),
            "Modified textarea "+textareas[i]+" form state not preserved!");
    }
    for (var 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");
}

const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;

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) {
    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 = Cc["@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() {
    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>