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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
/**
* This frame script is only loaded for sessionstore mochitests. It contains
* a bunch of utility functions used to test form data collection and
* restoration in remote browsers.
*/
function queryElement(data) {
let frame = content;
if (data.hasOwnProperty("frame")) {
frame = content.frames[data.frame];
}
let doc = frame.document;
if (data.hasOwnProperty("id")) {
return doc.getElementById(data.id);
}
if (data.hasOwnProperty("selector")) {
return doc.querySelector(data.selector);
}
if (data.hasOwnProperty("xpath")) {
let xptype = Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE;
return doc.evaluate(data.xpath, doc, null, xptype, null).singleNodeValue;
}
throw new Error("couldn't query element");
}
function dispatchUIEvent(input, type) {
let event = input.ownerDocument.createEvent("UIEvents");
event.initUIEvent(type, true, true, input.ownerGlobal, 0);
input.dispatchEvent(event);
}
function defineListener(type, cb) {
addMessageListener("ss-test:" + type, function ({data}) {
sendAsyncMessage("ss-test:" + type, cb(data));
});
}
defineListener("sendKeyEvent", function (data) {
let frame = content;
if (data.hasOwnProperty("frame")) {
frame = content.frames[data.frame];
}
let ifreq = frame.QueryInterface(Ci.nsIInterfaceRequestor);
let utils = ifreq.getInterface(Ci.nsIDOMWindowUtils);
let keyCode = data.key.charCodeAt(0);
let charCode = Ci.nsIDOMKeyEvent.DOM_VK_A + keyCode - "a".charCodeAt(0);
utils.sendKeyEvent("keydown", keyCode, charCode, null);
utils.sendKeyEvent("keypress", keyCode, charCode, null);
utils.sendKeyEvent("keyup", keyCode, charCode, null);
});
defineListener("getInnerHTML", function (data) {
return queryElement(data).innerHTML;
});
defineListener("getTextContent", function (data) {
return queryElement(data).textContent;
});
defineListener("getInputValue", function (data) {
return queryElement(data).value;
});
defineListener("setInputValue", function (data) {
let input = queryElement(data);
input.value = data.value;
dispatchUIEvent(input, "input");
});
defineListener("getInputChecked", function (data) {
return queryElement(data).checked;
});
defineListener("setInputChecked", function (data) {
let input = queryElement(data);
input.checked = data.checked;
dispatchUIEvent(input, "change");
});
defineListener("getSelectedIndex", function (data) {
return queryElement(data).selectedIndex;
});
defineListener("setSelectedIndex", function (data) {
let input = queryElement(data);
input.selectedIndex = data.index;
dispatchUIEvent(input, "change");
});
defineListener("getMultipleSelected", function (data) {
let input = queryElement(data);
return Array.map(input.options, (opt, idx) => idx)
.filter(idx => input.options[idx].selected);
});
defineListener("setMultipleSelected", function (data) {
let input = queryElement(data);
Array.forEach(input.options, (opt, idx) => opt.selected = data.indices.indexOf(idx) > -1);
dispatchUIEvent(input, "change");
});
defineListener("getFileNameArray", function (data) {
return queryElement(data).mozGetFileNameArray();
});
defineListener("setFileNameArray", function (data) {
let input = queryElement(data);
input.mozSetFileNameArray(data.names, data.names.length);
dispatchUIEvent(input, "input");
});
defineListener("setFormElementValues", function (data) {
for (let elem of content.document.forms[0].elements) {
elem.value = data.value;
dispatchUIEvent(elem, "input");
}
});
|