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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/*
* These are routines used by our testing code.
*
* jQuery is not necessarily present. Don't rely on it
* for routine operations.
*/
function ph_select(sel) {
if (!window.Sizzle)
throw "Sizzle was not properly loaded"
return window.Sizzle(sel);
}
function ph_only(els, sel)
{
if (els.length === 0)
throw sel + " not found";
if (els.length > 1)
throw sel + " is ambiguous";
return els[0];
}
function ph_find (sel)
{
var els = ph_select(sel);
return ph_only(els, sel);
}
function ph_count(sel) {
var els = ph_select(sel);
return els.length;
}
function ph_count_check(sel, expected_num) {
return (ph_count(sel) == expected_num);
}
function ph_val (sel)
{
var el = ph_find(sel);
if (el.value === undefined)
throw sel + " does not have a value";
return el.value;
}
function ph_set_val (sel, val)
{
var el = ph_find(sel);
if (el.value === undefined)
throw sel + " does not have a value";
el.value = val;
var ev = new Event("change", { bubbles: true, cancelable: false });
el.dispatchEvent(ev);
}
function ph_has_val (sel, val)
{
return ph_val(sel) == val;
}
function ph_collected_text_is (sel, val)
{
var els = ph_select(sel);
var rest = els.map(el => {
if (el.textContent === undefined)
throw sel + " can not have text";
return el.textContent.replace(/\xa0/g, " ")
}).join("");
return rest === val;
}
function ph_text (sel)
{
var el = ph_find(sel);
if (el.textContent === undefined)
throw sel + " can not have text";
// 0xa0 is a non-breakable space, which is a rendering detail of Chromium
// and awkward to handle in tests; turn it into normal spaces
return el.textContent.replace(/\xa0/g, " ")
}
function ph_attr (sel, attr)
{
return ph_find(sel).getAttribute(attr);
}
function ph_set_attr (sel, attr, val)
{
var el = ph_find(sel);
if (val === null || val === undefined)
el.removeAttribute(attr);
else
el.setAttribute(attr, val);
var ev = new Event("change", { bubbles: true, cancelable: false });
el.dispatchEvent(ev);
}
function ph_has_attr (sel, attr, val)
{
return ph_attr(sel, attr) == val;
}
function ph_attr_contains (sel, attr, val)
{
var a = ph_attr(sel, attr);
return a && a.indexOf(val) > -1;
}
function ph_mouse(sel, type, x, y, btn, ctrlKey, shiftKey, altKey, metaKey) {
let el = ph_find(sel);
/* The element has to be visible, and not collapsed */
if (el.offsetWidth <= 0 && el.offsetHeight <= 0)
throw sel + " is not visible";
/* The event has to actually work */
var processed = false;
function handler() {
processed = true;
}
el.addEventListener(type, handler, true);
let elp = el;
let left = elp.offsetLeft;
let top = elp.offsetTop;
while (elp.offsetParent) {
elp = elp.offsetParent;
left += elp.offsetLeft;
top += elp.offsetTop;
}
var detail = 0;
if (["click", "mousedown", "mouseup"].indexOf(type) > -1)
detail = 1;
else if (type === "dblclick")
detail = 2;
var ev = new MouseEvent(type, {
bubbles: true,
cancelable: true,
view: window,
detail: detail,
screenX: left + x,
screenY: top + y,
clientX: left + x,
clientY: top + y,
button: btn,
ctrlKey: ctrlKey || false,
shiftKey: shiftKey || false,
altKey: altKey || false,
metaKey: metaKey || false
});
el.dispatchEvent(ev);
el.removeEventListener(type, handler, true);
/* It really had to work */
if (!processed)
throw sel + " is disabled or somehow doesn't process events";
}
function ph_get_checked (sel)
{
var el = ph_find(sel);
if (el.checked === undefined)
throw sel + " is not checkable";
return el.checked;
}
function ph_set_checked (sel, val)
{
var el = ph_find(sel);
if (el.checked === undefined)
throw sel + " is not checkable";
if (el.checked != val)
ph_mouse(sel, "click", 0, 0, 0);
}
function ph_is_visible (sel)
{
var el = ph_find(sel);
return (el.offsetWidth > 0 || el.offsetHeight > 0) && !(el.style.visibility == "hidden" || el.style.display == "none");
}
function ph_is_present(sel)
{
var els = ph_select(sel);
return els.length > 0;
}
function ph_in_text (sel, text)
{
return ph_text(sel).indexOf(text) != -1;
}
function ph_text_is (sel, text)
{
return ph_text(sel) == text;
}
function ph_go(href) {
if (href.indexOf("#") === 0) {
window.location.hash = href;
} else {
if (window.name.indexOf("cockpit1") !== 0)
throw "ph_go() called in non cockpit window";
var control = {
command: "jump",
location: href
};
window.parent.postMessage("\n" + JSON.stringify(control), "*");
}
}
function ph_focus(sel)
{
ph_find(sel).focus();
}
function ph_blur(sel)
{
ph_find(sel).blur();
}
class PhWaitCondTimeout extends Error {
constructor() {
super("condition did not become true");
}
}
function ph_wait_cond(cond, timeout) {
return new Promise((resolve, reject) => {
// poll every 100 ms for now; FIXME: poll less often and re-check on mutations using
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
let stepTimer = null;
let tm = window.setTimeout( () => {
if (stepTimer)
window.clearTimeout(stepTimer);
reject(new PhWaitCondTimeout());
}, timeout);
function step() {
try {
if (cond()) {
window.clearTimeout(tm);
resolve();
return;
}
} catch (err) {
reject(err);
}
stepTimer = window.setTimeout(step, 100);
}
step();
});
}
|