browser/components/sessionstore/test/content-forms.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 /**
michael@0 8 * This frame script is only loaded for sessionstore mochitests. It contains
michael@0 9 * a bunch of utility functions used to test form data collection and
michael@0 10 * restoration in remote browsers.
michael@0 11 */
michael@0 12
michael@0 13 function queryElement(data) {
michael@0 14 let frame = content;
michael@0 15 if (data.hasOwnProperty("frame")) {
michael@0 16 frame = content.frames[data.frame];
michael@0 17 }
michael@0 18
michael@0 19 let doc = frame.document;
michael@0 20
michael@0 21 if (data.hasOwnProperty("id")) {
michael@0 22 return doc.getElementById(data.id);
michael@0 23 }
michael@0 24
michael@0 25 if (data.hasOwnProperty("selector")) {
michael@0 26 return doc.querySelector(data.selector);
michael@0 27 }
michael@0 28
michael@0 29 if (data.hasOwnProperty("xpath")) {
michael@0 30 let xptype = Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE;
michael@0 31 return doc.evaluate(data.xpath, doc, null, xptype, null).singleNodeValue;
michael@0 32 }
michael@0 33
michael@0 34 throw new Error("couldn't query element");
michael@0 35 }
michael@0 36
michael@0 37 function dispatchUIEvent(input, type) {
michael@0 38 let event = input.ownerDocument.createEvent("UIEvents");
michael@0 39 event.initUIEvent(type, true, true, input.ownerDocument.defaultView, 0);
michael@0 40 input.dispatchEvent(event);
michael@0 41 }
michael@0 42
michael@0 43 function defineListener(type, cb) {
michael@0 44 addMessageListener("ss-test:" + type, function ({data}) {
michael@0 45 sendAsyncMessage("ss-test:" + type, cb(data));
michael@0 46 });
michael@0 47 }
michael@0 48
michael@0 49 defineListener("sendKeyEvent", function (data) {
michael@0 50 let frame = content;
michael@0 51 if (data.hasOwnProperty("frame")) {
michael@0 52 frame = content.frames[data.frame];
michael@0 53 }
michael@0 54
michael@0 55 let ifreq = frame.QueryInterface(Ci.nsIInterfaceRequestor);
michael@0 56 let utils = ifreq.getInterface(Ci.nsIDOMWindowUtils);
michael@0 57
michael@0 58 let keyCode = data.key.charCodeAt(0);
michael@0 59 let charCode = Ci.nsIDOMKeyEvent.DOM_VK_A + keyCode - "a".charCodeAt(0);
michael@0 60
michael@0 61 utils.sendKeyEvent("keydown", keyCode, charCode, null);
michael@0 62 utils.sendKeyEvent("keypress", keyCode, charCode, null);
michael@0 63 utils.sendKeyEvent("keyup", keyCode, charCode, null);
michael@0 64 });
michael@0 65
michael@0 66 defineListener("getInnerHTML", function (data) {
michael@0 67 return queryElement(data).innerHTML;
michael@0 68 });
michael@0 69
michael@0 70 defineListener("getTextContent", function (data) {
michael@0 71 return queryElement(data).textContent;
michael@0 72 });
michael@0 73
michael@0 74 defineListener("getInputValue", function (data) {
michael@0 75 return queryElement(data).value;
michael@0 76 });
michael@0 77
michael@0 78 defineListener("setInputValue", function (data) {
michael@0 79 let input = queryElement(data);
michael@0 80 input.value = data.value;
michael@0 81 dispatchUIEvent(input, "input");
michael@0 82 });
michael@0 83
michael@0 84 defineListener("getInputChecked", function (data) {
michael@0 85 return queryElement(data).checked;
michael@0 86 });
michael@0 87
michael@0 88 defineListener("setInputChecked", function (data) {
michael@0 89 let input = queryElement(data);
michael@0 90 input.checked = data.checked;
michael@0 91 dispatchUIEvent(input, "change");
michael@0 92 });
michael@0 93
michael@0 94 defineListener("getSelectedIndex", function (data) {
michael@0 95 return queryElement(data).selectedIndex;
michael@0 96 });
michael@0 97
michael@0 98 defineListener("setSelectedIndex", function (data) {
michael@0 99 let input = queryElement(data);
michael@0 100 input.selectedIndex = data.index;
michael@0 101 dispatchUIEvent(input, "change");
michael@0 102 });
michael@0 103
michael@0 104 defineListener("getMultipleSelected", function (data) {
michael@0 105 let input = queryElement(data);
michael@0 106 return Array.map(input.options, (opt, idx) => idx)
michael@0 107 .filter(idx => input.options[idx].selected);
michael@0 108 });
michael@0 109
michael@0 110 defineListener("setMultipleSelected", function (data) {
michael@0 111 let input = queryElement(data);
michael@0 112 Array.forEach(input.options, (opt, idx) => opt.selected = data.indices.indexOf(idx) > -1);
michael@0 113 dispatchUIEvent(input, "change");
michael@0 114 });
michael@0 115
michael@0 116 defineListener("getFileNameArray", function (data) {
michael@0 117 return queryElement(data).mozGetFileNameArray();
michael@0 118 });
michael@0 119
michael@0 120 defineListener("setFileNameArray", function (data) {
michael@0 121 let input = queryElement(data);
michael@0 122 input.mozSetFileNameArray(data.names, data.names.length);
michael@0 123 dispatchUIEvent(input, "input");
michael@0 124 });
michael@0 125
michael@0 126 defineListener("setFormElementValues", function (data) {
michael@0 127 for (let elem of content.document.forms[0].elements) {
michael@0 128 elem.value = data.value;
michael@0 129 dispatchUIEvent(elem, "input");
michael@0 130 }
michael@0 131 });

mercurial