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 262 263 264 265 266
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* A class that provides test support for C++ tests of HTML pages.
* The C++ end is implemented in ash::AutomationTestUtils.
*/
class AutomationTestSupport {
constructor() {
this.desktop_ = null;
this.init_();
}
/** @private */
async init_() {
this.desktop_ =
await new Promise(resolve => chrome.automation.getDesktop(resolve));
this.notifyCcTests_('ready');
}
/**
* Waits for the page with the given `url` to finish loading.
* @param {string} url
*/
async waitForPageLoad(url) {
const findParams = {
role: 'rootWebArea',
attributes: {url, 'docLoaded': true},
};
await this.findNode_(findParams);
this.notifyCcTests_('ready');
}
/**
* Waits for the page with the given `url` to exist, then
* gets its bounds.
* @param {string} url
*/
async getBoundsForRootWebArea(url) {
const findParams = {
role: 'rootWebArea',
attributes: {url},
};
await this.getBoundsForNodeWithParams_(findParams);
}
/**
* Gets the bounds for the automation node with the given `name` and
* `role`. Waits for the node to exist if it does not yet.
* @param {string} name
* @param {string} role
*/
async getBoundsForNode(name, role) {
const findParams = {role, attributes: {name}};
await this.getBoundsForNodeWithParams_(findParams);
}
/**
* Gets the bounds for the automation node with the given `className`.
* Waits for the node to exist if it does not yet.
* @param {string} className
*/
async getBoundsForNodeByClassName(className) {
const findParams = {attributes: {className}};
await this.getBoundsForNodeWithParams_(findParams);
}
/**
* Sets focus on the automation node with the given `name` and
* `role`. Waits for the node to exist if it does not yet.
* @param {string} name
* @param {string} role
*/
async setFocusOnNode(name, role) {
const findParams = {role, attributes: {name}};
const node = await this.findNode_(findParams);
if (!node) {
// Failed, node never existed.
console.error('Failed to find node', name, 'with role', role);
return;
}
node.focus();
this.notifyCcTests_('ready');
}
/**
* Checks if an automation node with the given `name` and
* `role` exists in the desktop tree, without waiting.
* @param {string} name
* @param {string} role
*/
nodeExistsNoWait(name, role) {
const findParams = {role, attributes: {name}};
const node = this.desktop_.find(findParams);
if (node) {
this.notifyCcTests_('true');
} else {
this.notifyCcTests_('false');
}
}
/**
* Does the default action on the node with the given `name` and `role` after
* waiting for that node to exist.
* @param {string} name
* @param {string} role
*/
doDefault(name, role) {
const findParams = {role, attributes: {name}};
const node = this.desktop_.find(findParams);
if (node) {
node.doDefault();
this.notifyCcTests_('ready');
}
}
/**
* @param {chrome.automation.FindParams} findParams
* @private
*/
async getBoundsForNodeWithParams_(findParams) {
const node = await this.findNode_(findParams);
if (!node || !node.location) {
// Failed.
return;
}
this.notifyCcTests_(`${node.location.left},${node.location.top},${
node.location.width},${node.location.height}`);
}
/**
* Finds the automation node with the given `findParams`. Waits
* for the node to exist if it does not yet.
* @param {chrome.automation.FindParams} findParams
* @return {chrome.automation.AutomationNode}
* @private
*/
async findNode_(findParams) {
const nodes = await this.findNumNodes_(findParams, 1);
return nodes[0];
}
/**
* Finds at least `minToFind` of the automation nodes matching the given
* `findParams`.
* @param {chrome.automation.FindParams} findParams
* @param {Number} minToFind
* @return {!Array<!chrome.automation.AutomationNode}
* @private
*/
async findNumNodes_(findParams, minToFind) {
const nodes = this.desktop_.findAll(findParams);
if (nodes && nodes.length >= minToFind) {
return nodes;
}
// If there weren't enough found yet, wait for them to show up.
return await new Promise(resolve => {
const listener = event => {
const nodes = this.desktop_.findAll(findParams);
if (nodes && nodes.length >= minToFind) {
this.desktop_.removeEventListener(
chrome.automation.EventType.LOAD_COMPLETE, listener, true);
this.desktop_.removeEventListener(
chrome.automation.EventType.CHILDREN_CHANGED, listener, true);
resolve(nodes);
}
};
this.desktop_.addEventListener(
chrome.automation.EventType.LOAD_COMPLETE, listener, true);
this.desktop_.addEventListener(
chrome.automation.EventType.CHILDREN_CHANGED, listener, true);
});
}
/**
* Waits for a chrome.automation.EventType.TEXT_SELECTION_CHANGED event to be
* fired on the desktop node.
*/
async waitForTextSelectionChangedEvent() {
await this.waitForEventHelper_(
chrome.automation.EventType.TEXT_SELECTION_CHANGED);
}
/**
* Waits for a chrome.automation.EventType.VALUE_CHANGED event to be fired on
* the desktop node.
*/
async waitForValueChangedEvent() {
await this.waitForEventHelper_(chrome.automation.EventType.VALUE_CHANGED);
}
/**
* Waits for a chrome.automation.EventType.CHILDREN_CHANGED event to be fired
* on the desktop node.
*/
async waitForChildrenChangedEvent() {
await this.waitForEventHelper_(
chrome.automation.EventType.CHILDREN_CHANGED);
}
/**
* Waits for the browser to have `num` tabs with name `name`.
*/
async waitForNumTabsWithName(num, name) {
const findParams = {
role: 'tab',
attributes: {name, className: 'Tab'},
};
// This will not return until it finds at least num.
const nodes = await this.findNumNodes_(findParams, num);
if (nodes.length > num) {
// Fail if we've found too many.
console.error(
'Error: found ' + nodes.length + ' tabs with name ' + name +
', expected only ' + num);
} else {
this.notifyCcTests_('ready');
}
}
/** @param {string} className */
async getValueForNodeWithClassName(className) {
const findParams = {attributes: {className}};
const node = await this.findNode_(findParams);
if (!node || !node.value) {
return;
}
this.notifyCcTests_(node.value);
}
/**
* @param {string} className
* @param {string} value
*/
async waitForNodeWithClassNameAndValue(className, value) {
const findParams = {attributes: {className, value}};
const node = await this.findNode_(findParams);
this.notifyCcTests_('ready');
}
/**
* @param {chrome.automation.EventType} event
* @private
*/
async waitForEventHelper_(event) {
const desktop = await new Promise(resolve => {
chrome.automation.getDesktop(d => resolve(d));
});
await new Promise(resolve => {
desktop.addEventListener(event, resolve);
});
this.notifyCcTests_('ready');
}
/**
* @param {string} The result to send to the C++ tests.
* @private
*/
notifyCcTests_(params) {
chrome.test.sendScriptResult(params);
}
}
globalThis.automationTestSupport = new AutomationTestSupport();
|