| 1 |
/* eslint no-unused-vars: 0 */ |
| 2 |
|
| 3 |
/* |
| 4 |
* These are routines used by our testing code. |
| 5 |
*/ |
| 6 |
|
| 7 |
/* Detect if we have any shadow DOM */ |
| 8 |
window.__haveShadowDom = function() { |
| 9 |
if (window.__haveShadowDomResult === undefined) |
| 10 |
window.__haveShadowDomResult = !!Array.from(document.querySelectorAll('*')).find(el => el.shadowRoot); |
| 11 |
|
| 12 |
return window.__haveShadowDomResult; |
| 13 |
}; |
| 14 |
|
| 15 |
// Like querySelectorAll(), but traverses shadow DOM |
| 16 |
window.querySelectorAllDeep = function(query, element) { |
| 17 |
const result = Array.from( |
| 18 |
element.shadowRoot |
| 19 |
? element.shadowRoot.childNodes |
| 20 |
: element.nodeName === 'SLOT' ? element.assignedElements() : element.childNodes, |
| 21 |
) |
| 22 |
.filter(element => element instanceof Element) |
| 23 |
.map(element => window.querySelectorAllDeep(query, element)) |
| 24 |
.flat(); |
| 25 |
|
| 26 |
if (element.matches?.(query)) |
| 27 |
result.push(element); |
| 28 |
return result; |
| 29 |
}; |
| 30 |
|
| 31 |
window.getQuerySelectorAll = function() { |
| 32 |
if (window.__querySelectorAllFunction === undefined) { |
| 33 |
if (window.__haveShadowDom()) { |
| 34 |
window.__querySelectorAllFunction = function (sel) { return window.querySelectorAllDeep(sel, document) }; |
| 35 |
} else { |
| 36 |
window.__querySelectorAllFunction = function (sel) { return Array.from(document.querySelectorAll(sel)) }; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return window.__querySelectorAllFunction; |
| 41 |
}; |
| 42 |
|
| 43 |
function elem_contains_text(elems, text) { |
| 44 |
return elems.filter(elem => elem && elem.innerText.includes(text)); |
| 45 |
} |
| 46 |
|
| 47 |
window.ph_select = function(sel) { |
| 48 |
const querySelectorAll = window.getQuerySelectorAll(); |
| 49 |
|
| 50 |
if (sel.includes(":contains(")) { |
| 51 |
if (!window.Sizzle) { |
| 52 |
// Best effort support `:contains()` |
| 53 |
// Cockpit supports multiple forms: |
| 54 |
// - :contains(foo) |
| 55 |
// - :contains('foo') |
| 56 |
// - :contains("foo") |
| 57 |
const re = /:contains\(([\sa-zA-Z0-9]+)\)|:contains\('([^']+)'\)|:contains\("([^"]+)"\)/g; |
| 58 |
const matches = re.exec(sel); |
| 59 |
if (matches === null) |
| 60 |
throw new Error("Unsupported ':contains' when window.Sizzle is not available."); |
| 61 |
|
| 62 |
if (matches.length !== 4) |
| 63 |
throw new Error(`Match not found for builtin :contains ${sel}`); |
| 64 |
|
| 65 |
if (re.exec(sel) !== null) |
| 66 |
throw new Error(`Unsupported multiple ':contains' when window.Sizzle is not available`); |
| 67 |
|
| 68 |
const searchText = matches[1] || matches[2] || matches[3]; |
| 69 |
const base = sel.replace(re, ''); |
| 70 |
|
| 71 |
const elems = querySelectorAll(base); |
| 72 |
return elem_contains_text(elems, searchText); |
| 73 |
} else { |
| 74 |
return window.Sizzle(sel); |
| 75 |
} |
| 76 |
} else { |
| 77 |
return querySelectorAll(sel); |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
window.ph_only = function(els, sel) { |
| 82 |
if (els.length === 0) |
| 83 |
throw new Error(sel + " not found"); |
| 84 |
if (els.length > 1) |
| 85 |
throw new Error(sel + " is ambiguous"); |
| 86 |
return els[0]; |
| 87 |
}; |
| 88 |
|
| 89 |
window.ph_find = function(sel) { |
| 90 |
const els = window.ph_select(sel); |
| 91 |
return window.ph_only(els, sel); |
| 92 |
}; |
| 93 |
|
| 94 |
window.ph_find_scroll_into_view = function(sel) { |
| 95 |
const el = window.ph_find(sel); |
| 96 |
/* we cannot make this conditional, as there is no way to find out whether |
| 97 |
an element is currently visible -- the usual trick to compare getBoundingClientRect() against |
| 98 |
window.innerHeight does not work if the element is in e.g. a scrollable dialog area */ |
| 99 |
return new Promise(resolve => { |
| 100 |
el.scrollIntoView({ behavior: 'instant', block: 'center', inline: 'center' }); |
| 101 |
// scrolling needs a little bit of time to stabilize, and it's not predictable |
| 102 |
// in particular, 'scrollend' is not reliably emitted |
| 103 |
window.setTimeout(() => resolve(el), 200); |
| 104 |
}); |
| 105 |
}; |
| 106 |
|
| 107 |
window.ph_count = function(sel) { |
| 108 |
const els = window.ph_select(sel); |
| 109 |
return els.length; |
| 110 |
}; |
| 111 |
|
| 112 |
window.ph_count_check = function(sel, expected_num) { |
| 113 |
return (window.ph_count(sel) == expected_num); |
| 114 |
}; |
| 115 |
|
| 116 |
window.ph_val = function(sel) { |
| 117 |
const el = window.ph_find(sel); |
| 118 |
if (el.value === undefined) |
| 119 |
throw new Error(sel + " does not have a value"); |
| 120 |
return el.value; |
| 121 |
}; |
| 122 |
|
| 123 |
window.ph_set_val = function(sel, val) { |
| 124 |
const el = window.ph_find(sel); |
| 125 |
if (el.value === undefined) |
| 126 |
throw new Error(sel + " does not have a value"); |
| 127 |
el.value = val; |
| 128 |
const ev = new Event("change", { bubbles: true, cancelable: false }); |
| 129 |
el.dispatchEvent(ev); |
| 130 |
}; |
| 131 |
|
| 132 |
window.ph_has_val = function(sel, val) { |
| 133 |
return window.ph_val(sel) == val; |
| 134 |
}; |
| 135 |
|
| 136 |
window.ph_collected_text_is = function(sel, val) { |
| 137 |
const els = window.ph_select(sel); |
| 138 |
const rest = els.map(el => { |
| 139 |
if (el.textContent === undefined) |
| 140 |
throw new Error(sel + " can not have text"); |
| 141 |
return el.textContent.replaceAll("\xa0", " "); |
| 142 |
}).join(""); |
| 143 |
return rest === val; |
| 144 |
}; |
| 145 |
|
| 146 |
window.ph_text = function(sel) { |
| 147 |
const el = window.ph_find(sel); |
| 148 |
if (el.textContent === undefined) |
| 149 |
throw new Error(sel + " can not have text"); |
| 150 |
|
| 151 |
// HACK: https://github.com/patternfly/patternfly-react/issues/11678 |
| 152 |
const el_copy = el.cloneNode(true); |
| 153 |
// find and clear .pf-v6-screen-reader subelements for HelperText |
| 154 |
el_copy.querySelectorAll('.pf-v6-c-helper-text__item-text > .pf-v6-screen-reader').forEach(el => { el.textContent = '' }); |
| 155 |
|
| 156 |
// 0xa0 is a non-breakable space, which is a rendering detail of Chromium |
| 157 |
// and awkward to handle in tests; turn it into normal spaces |
| 158 |
return el_copy.textContent.replaceAll("\xa0", " "); |
| 159 |
}; |
| 160 |
|
| 161 |
window.ph_attr = function(sel, attr) { |
| 162 |
return window.ph_find(sel).getAttribute(attr); |
| 163 |
}; |
| 164 |
|
| 165 |
window.ph_set_attr = function(sel, attr, val) { |
| 166 |
const el = window.ph_find(sel); |
| 167 |
if (val === null || val === undefined) |
| 168 |
el.removeAttribute(attr); |
| 169 |
else |
| 170 |
el.setAttribute(attr, val); |
| 171 |
|
| 172 |
const ev = new Event("change", { bubbles: true, cancelable: false }); |
| 173 |
el.dispatchEvent(ev); |
| 174 |
}; |
| 175 |
|
| 176 |
window.ph_has_attr = function(sel, attr, val) { |
| 177 |
return window.ph_attr(sel, attr) == val; |
| 178 |
}; |
| 179 |
|
| 180 |
window.ph_attr_contains = function(sel, attr, val) { |
| 181 |
const a = window.ph_attr(sel, attr); |
| 182 |
return a && a.indexOf(val) > -1; |
| 183 |
}; |
| 184 |
|
| 185 |
window.ph_mouse = function(sel, type, x, y, btn, ctrlKey, shiftKey, altKey, metaKey) { |
| 186 |
const el = window.ph_find(sel); |
| 187 |
|
| 188 |
/* The element has to be visible, and not collapsed */ |
| 189 |
if (el.offsetWidth <= 0 && el.offsetHeight <= 0 && el.tagName != 'svg') |
| 190 |
throw new Error(sel + " is not visible"); |
| 191 |
|
| 192 |
/* The event has to actually work */ |
| 193 |
let processed = false; |
| 194 |
function handler() { |
| 195 |
processed = true; |
| 196 |
} |
| 197 |
|
| 198 |
el.addEventListener(type, handler, true); |
| 199 |
|
| 200 |
let elp = el; |
| 201 |
let left = elp.offsetLeft || 0; |
| 202 |
let top = elp.offsetTop || 0; |
| 203 |
while (elp.offsetParent) { |
| 204 |
elp = elp.offsetParent; |
| 205 |
left += elp.offsetLeft; |
| 206 |
top += elp.offsetTop; |
| 207 |
} |
| 208 |
|
| 209 |
let detail = 0; |
| 210 |
if (["click", "mousedown", "mouseup"].indexOf(type) > -1) |
| 211 |
detail = 1; |
| 212 |
else if (type === "dblclick") |
| 213 |
detail = 2; |
| 214 |
|
| 215 |
const ev = new MouseEvent(type, { |
| 216 |
bubbles: true, |
| 217 |
cancelable: true, |
| 218 |
view: window, |
| 219 |
detail, |
| 220 |
screenX: left + x, |
| 221 |
screenY: top + y, |
| 222 |
clientX: left + x, |
| 223 |
clientY: top + y, |
| 224 |
button: btn, |
| 225 |
ctrlKey: ctrlKey || false, |
| 226 |
shiftKey: shiftKey || false, |
| 227 |
altKey: altKey || false, |
| 228 |
metaKey: metaKey || false |
| 229 |
}); |
| 230 |
|
| 231 |
el.dispatchEvent(ev); |
| 232 |
|
| 233 |
el.removeEventListener(type, handler, true); |
| 234 |
|
| 235 |
/* It really had to work */ |
| 236 |
if (!processed) |
| 237 |
throw new Error(sel + " is disabled or somehow doesn't process events"); |
| 238 |
}; |
| 239 |
|
| 240 |
window.ph_get_checked = function(sel) { |
| 241 |
const el = window.ph_find(sel); |
| 242 |
if (el.checked === undefined) |
| 243 |
throw new Error(sel + " is not checkable"); |
| 244 |
|
| 245 |
return el.checked; |
| 246 |
}; |
| 247 |
|
| 248 |
window.ph_set_checked = function(sel, val) { |
| 249 |
const el = window.ph_find(sel); |
| 250 |
if (el.checked === undefined) |
| 251 |
throw new Error(sel + " is not checkable"); |
| 252 |
|
| 253 |
if (el.checked != val) |
| 254 |
window.ph_mouse(sel, "click", 0, 0, 0); |
| 255 |
}; |
| 256 |
|
| 257 |
window.ph_is_visible = function(sel) { |
| 258 |
const el = window.ph_find(sel); |
| 259 |
return el.tagName == "svg" || ((el.offsetWidth > 0 || el.offsetHeight > 0) && !(getComputedStyle(el).visibility == "hidden" || getComputedStyle(el).display == "none")); |
| 260 |
}; |
| 261 |
|
| 262 |
window.ph_is_present = function(sel) { |
| 263 |
const els = window.ph_select(sel); |
| 264 |
return els.length > 0; |
| 265 |
}; |
| 266 |
|
| 267 |
window.ph_in_text = function(sel, text) { |
| 268 |
return window.ph_text(sel).indexOf(text) != -1; |
| 269 |
}; |
| 270 |
|
| 271 |
window.ph_text_is = function(sel, text) { |
| 272 |
return window.ph_text(sel) == text; |
| 273 |
}; |
| 274 |
|
| 275 |
window.ph_text_matches = function(sel, pattern) { |
| 276 |
return window.ph_text(sel).match(pattern); |
| 277 |
}; |
| 278 |
|
| 279 |
window.ph_jump = function(path) { |
| 280 |
if (path[0] !== '/') |
| 281 |
throw new Error("ph_jump() needs to be called with a path"); |
| 282 |
|
| 283 |
if (window.name.indexOf("cockpit1") !== 0) |
| 284 |
throw new Error("ph_jump() called in non cockpit window"); |
| 285 |
const control = { |
| 286 |
command: "jump", |
| 287 |
location: path |
| 288 |
}; |
| 289 |
window.parent.postMessage("\n" + JSON.stringify(control), "*"); |
| 290 |
}; |
| 291 |
|
| 292 |
window.ph_focus = function(sel) { |
| 293 |
window.ph_find(sel).focus(); |
| 294 |
}; |
| 295 |
|
| 296 |
window.ph_scrollIntoViewIfNeeded = function(sel) { |
| 297 |
window.ph_find(sel).scrollIntoViewIfNeeded(); |
| 298 |
}; |
| 299 |
|
| 300 |
window.ph_blur = function(sel) { |
| 301 |
window.ph_find(sel).blur(); |
| 302 |
}; |
| 303 |
|
| 304 |
window.ph_blur_active = function() { |
| 305 |
const elt = window.document.activeElement; |
| 306 |
if (elt) |
| 307 |
elt.blur(); |
| 308 |
}; |
| 309 |
|
| 310 |
class PhWaitCondTimeout extends Error { |
| 311 |
constructor(description) { |
| 312 |
if (description && description.apply) |
| 313 |
description = description.apply(); |
| 314 |
if (description) |
| 315 |
super(description); |
| 316 |
else |
| 317 |
super("condition did not become true"); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
window.ph_wait_cond = function(cond, timeout, error_description) { |
| 322 |
return new Promise((resolve, reject) => { |
| 323 |
// poll every 100 ms for now; FIXME: poll less often and re-check on mutations using |
| 324 |
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver |
| 325 |
let stepTimer = null; |
| 326 |
let last_err = null; |
| 327 |
const tm = window.setTimeout(() => { |
| 328 |
if (stepTimer) |
| 329 |
window.clearTimeout(stepTimer); |
| 330 |
reject(last_err || new PhWaitCondTimeout(error_description)); |
| 331 |
}, timeout); |
| 332 |
function step() { |
| 333 |
try { |
| 334 |
if (cond()) { |
| 335 |
window.clearTimeout(tm); |
| 336 |
resolve(); |
| 337 |
return; |
| 338 |
} |
| 339 |
} catch (err) { |
| 340 |
last_err = err; |
| 341 |
} |
| 342 |
stepTimer = window.setTimeout(step, 100); |
| 343 |
} |
| 344 |
step(); |
| 345 |
}); |
| 346 |
}; |
| 347 |
|
| 348 |
function currentFrameAbsolutePosition() { |
| 349 |
let currentWindow = window; |
| 350 |
let currentParentWindow; |
| 351 |
const positions = []; |
| 352 |
let rect; |
| 353 |
|
| 354 |
while (currentWindow !== window.top) { |
| 355 |
currentParentWindow = currentWindow.parent; |
| 356 |
for (let idx = 0; idx < currentParentWindow.frames.length; idx++) |
| 357 |
if (currentParentWindow.frames[idx] === currentWindow) { |
| 358 |
for (const frameElement of currentParentWindow.document.getElementsByTagName('iframe')) { |
| 359 |
if (frameElement.contentWindow === currentWindow) { |
| 360 |
rect = frameElement.getBoundingClientRect(); |
| 361 |
positions.push({ x: rect.x, y: rect.y }); |
| 362 |
} |
| 363 |
} |
| 364 |
currentWindow = currentParentWindow; |
| 365 |
break; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
return positions.reduce((accumulator, currentValue) => { |
| 370 |
return { |
| 371 |
x: accumulator.x + currentValue.x, |
| 372 |
y: accumulator.y + currentValue.y |
| 373 |
}; |
| 374 |
}, { x: 0, y: 0 }); |
| 375 |
} |
| 376 |
|
| 377 |
window.ph_selector_clips = function(sels) { |
| 378 |
const f = currentFrameAbsolutePosition(); |
| 379 |
const elts = sels.map(window.ph_select).flat(); |
| 380 |
return elts.map(e => { |
| 381 |
const r = e.getBoundingClientRect(); |
| 382 |
return { x: r.x + f.x, y: r.y + f.y, width: r.width, height: r.height, scale: 1 }; |
| 383 |
}); |
| 384 |
}; |
| 385 |
|
| 386 |
window.ph_element_clip = function(sel) { |
| 387 |
window.ph_find(sel); // just to make sure it is not ambiguous |
| 388 |
return window.ph_selector_clips([sel])[0]; |
| 389 |
}; |
| 390 |
|
| 391 |
window.ph_count_animations = function(sel) { |
| 392 |
let animations = window.ph_find(sel).getAnimations({ subtree: true }); |
| 393 |
// ignore animations that have already finished running |
| 394 |
animations = animations.filter(a => a.playState !== "finished"); |
| 395 |
|
| 396 |
return animations.length; |
| 397 |
}; |
| 398 |
|
| 399 |
window.ph_set_texts = function(new_texts) { |
| 400 |
for (const sel in new_texts) { |
| 401 |
const elts = window.ph_select(sel); |
| 402 |
if (elts.length == 0) |
| 403 |
throw new Error(sel + " not found"); |
| 404 |
for (let elt of elts) { |
| 405 |
// We have to be careful to not replace any actual nodes |
| 406 |
// in the DOM since that would cause React to fail later |
| 407 |
// when it tries to remove some of its nodes that are no |
| 408 |
// longer in the DOM. This means that setting the |
| 409 |
// "textContent" property is out, for example. |
| 410 |
// |
| 411 |
// Instead, we insist on finding an actual "Text" node |
| 412 |
// that we then modify. If the given selector results in |
| 413 |
// elements that have other elements in them, we refuse to |
| 414 |
// mock them. |
| 415 |
// |
| 416 |
// However, for convenience, this function digs into |
| 417 |
// elements that have exactly one other child element. |
| 418 |
while (elt.children.length == 1) |
| 419 |
elt = elt.children[0]; |
| 420 |
if (elt.children.length != 0) |
| 421 |
throw new Error(sel + " can not be mocked since it contains more than text"); |
| 422 |
let subst = new_texts[sel]; |
| 423 |
for (const n of elt.childNodes) { |
| 424 |
if (n.nodeType == 3) { // 3 == TEXT |
| 425 |
n.data = subst; |
| 426 |
subst = ""; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
}; |