Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Namespace for test related things. | 6 * Namespace for test related things. |
| 7 */ | 7 */ |
| 8 var test = test || {}; | 8 var test = test || {}; |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * Extract the information of the given element. | 11 * Extract the information of the given element. |
| 12 * @param {Element} element Element to be extracted. | 12 * @param {Element} element Element to be extracted. |
| 13 * @param {Window} contentWindow Window to be tested. | 13 * @param {Window} contentWindow Window to be tested. |
| 14 * @param {Array<string>=} opt_styleNames List of CSS property name to be | 14 * @param {Array<string>=} opt_styleNames List of CSS property name to be |
| 15 * obtained. | 15 * obtained. |
| 16 * @return {{attributes:Object<string>, text:string, | 16 * @return {{attributes:Object<string>, text:string, |
| 17 * styles:Object<string>, hidden:boolean}} Element | 17 * styles:Object<string>, hidden:boolean}} Element |
| 18 * information that contains contentText, attribute names and | 18 * information that contains contentText, attribute names and |
| 19 * values, hidden attribute, and style names and values. | 19 * values, hidden attribute, and style names and values. |
| 20 */ | 20 */ |
| 21 function extractElementInfo(element, contentWindow, opt_styleNames) { | 21 function extractElementInfo(element, contentWindow, opt_styleNames) { |
| 22 var attributes = {}; | 22 var attributes = {}; |
| 23 for (var i = 0; i < element.attributes.length; i++) { | 23 for (var i = 0; i < element.attributes.length; i++) { |
| 24 attributes[element.attributes[i].nodeName] = | 24 attributes[element.attributes[i].nodeName] = |
| 25 element.attributes[i].nodeValue; | 25 element.attributes[i].nodeValue; |
| 26 } | 26 } |
| 27 var styles = {}; | 27 var styles = {}; |
| 28 var styleNames = opt_styleNames || []; | 28 var styleNames = opt_styleNames || []; |
| 29 assert(Array.isArray(styleNames)); | |
| 29 var computedStyles = contentWindow.getComputedStyle(element); | 30 var computedStyles = contentWindow.getComputedStyle(element); |
| 30 for (var i = 0; i < styleNames.length; i++) { | 31 for (var i = 0; i < styleNames.length; i++) { |
| 31 styles[styleNames[i]] = computedStyles[styleNames[i]]; | 32 styles[styleNames[i]] = computedStyles[styleNames[i]]; |
| 32 } | 33 } |
| 33 var text = element.textContent; | 34 var text = element.textContent; |
| 34 var size = element.getBoundingClientRect(); | 35 var size = element.getBoundingClientRect(); |
| 35 return { | 36 return { |
| 36 attributes: attributes, | 37 attributes: attributes, |
| 37 text: text, | 38 text: text, |
| 38 value: element.value, | 39 value: element.value, |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 * @param {Window} contentWindow Window to be tested. | 217 * @param {Window} contentWindow Window to be tested. |
| 217 * @return {boolean} True if the window is maximized now. | 218 * @return {boolean} True if the window is maximized now. |
| 218 */ | 219 */ |
| 219 test.util.sync.isWindowMaximized = function(contentWindow) { | 220 test.util.sync.isWindowMaximized = function(contentWindow) { |
| 220 return window.background.appWindows[contentWindow.appID].isMaximized(); | 221 return window.background.appWindows[contentWindow.appID].isMaximized(); |
| 221 }; | 222 }; |
| 222 | 223 |
| 223 /** | 224 /** |
| 224 * Queries all elements. | 225 * Queries all elements. |
| 225 * | 226 * |
| 226 * @param {Window} contentWindow Window to be tested. | 227 * @param {!Window} contentWindow Window to be tested. |
| 227 * @param {string} targetQuery Query to specify the element. | 228 * @param {string} targetQuery Query to specify the element. |
| 228 * @param {?string} iframeQuery Iframe selector or null if no iframe. | 229 * @param {?string} iframeQuery Iframe selector or null if no iframe. |
| 229 * @param {Array<string>=} opt_styleNames List of CSS property name to be | 230 * @param {Array<string>=} opt_styleNames List of CSS property name to be |
| 230 * obtained. | 231 * obtained. |
| 231 * @return {Array<{attributes:Object<string>, text:string, | 232 * @return {!Array<{attributes:Object<string>, text:string, |
| 232 * styles:Object<string>, hidden:boolean}>} Element | 233 * styles:Object<string>, hidden:boolean}>} Element |
| 233 * information that contains contentText, attribute names and | 234 * information that contains contentText, attribute names and |
| 234 * values, hidden attribute, and style names and values. | 235 * values, hidden attribute, and style names and values. |
| 235 */ | 236 */ |
| 236 test.util.sync.queryAllElements = function( | 237 test.util.sync.queryAllElements = function( |
| 237 contentWindow, targetQuery, iframeQuery, opt_styleNames) { | 238 contentWindow, targetQuery, iframeQuery, opt_styleNames) { |
| 239 return test.util.sync.deepQueryAllElements( | |
| 240 contentWindow, [targetQuery], iframeQuery, opt_styleNames); | |
| 241 }; | |
| 242 | |
| 243 /** | |
| 244 * Queries elements inside shadow DOM. | |
| 245 * | |
| 246 * @param {!Window} contentWindow Window to be tested. | |
| 247 * @param {!Array<string>} targetQuery Query to specify the element. | |
| 248 * |targetQuery[0]| specifies the first element(s). |targetQuery[1]| specifies | |
| 249 * elements inside the shadow DOM of the first element, and so on. | |
| 250 * @param {?string} iframeQuery Iframe selector or null if no iframe. | |
| 251 * @param {Array<string>=} opt_styleNames List of CSS property name to be | |
| 252 * obtained. | |
| 253 * @return {!Array<{attributes:Object<string>, text:string, | |
| 254 * styles:Object<string>, hidden:boolean}>} Element | |
| 255 * information that contains contentText, attribute names and | |
| 256 * values, hidden attribute, and style names and values. | |
| 257 */ | |
| 258 test.util.sync.deepQueryAllElements = function( | |
| 259 contentWindow, targetQuery, iframeQuery, opt_styleNames) { | |
| 238 var doc = test.util.sync.getDocument_( | 260 var doc = test.util.sync.getDocument_( |
| 239 contentWindow, iframeQuery || undefined); | 261 contentWindow, iframeQuery || undefined); |
| 240 if (!doc) | 262 if (!doc) |
| 241 return []; | 263 return []; |
| 242 // The return value of querySelectorAll is not an array. | 264 var elems = test.util.sync.deepQuerySelectorAll_(doc, targetQuery); |
| 243 return Array.prototype.map.call( | 265 |
| 244 doc.querySelectorAll(targetQuery), | 266 return elems.map(function(element) { |
| 245 function(element) { | 267 return extractElementInfo(element, contentWindow, opt_styleNames); |
| 246 return extractElementInfo(element, contentWindow, opt_styleNames); | |
| 247 }); | 268 }); |
|
fukino
2016/08/15 02:02:08
nit: correct the indent level.
oka
2016/08/15 06:03:41
Done.
| |
| 248 }; | 269 }; |
| 249 | 270 |
| 250 /** | 271 /** |
| 272 * @param {(!HTMLElement|!Document)} root Element to search from. | |
|
fukino
2016/08/15 02:02:08
nit: Add a one-line description for this method.
oka
2016/08/15 06:03:41
Done.
| |
| 273 * @param {!Array<string>} targetQuery Query to specify the element. | |
| 274 * |targetQuery[0]| specifies the first element(s). |targetQuery[1]| specifies | |
| 275 * elements inside the shadow DOM of the first element, and so on. | |
| 276 * @return {!Array<!HTMLElement>} Matched elements. | |
| 277 * | |
| 278 * @private | |
| 279 */ | |
| 280 test.util.sync.deepQuerySelectorAll_ = function(root, targetQuery) { | |
| 281 var elems = Array.prototype.slice.call(root.querySelectorAll(targetQuery[0])); | |
| 282 var remaining = targetQuery.slice(1); | |
| 283 if (remaining.length === 0) | |
| 284 return elems; | |
| 285 | |
| 286 var res = []; | |
| 287 for (var i = 0; i < elems.length; i++) { | |
| 288 if (elems[i].shadowRoot) { | |
| 289 res = res.concat( | |
| 290 test.util.sync.deepQuerySelectorAll_(elems[i].shadowRoot, remaining)); | |
| 291 } | |
| 292 } | |
| 293 return res; | |
| 294 }; | |
| 295 | |
| 296 /** | |
| 251 * Get the information of the active element. | 297 * Get the information of the active element. |
| 252 * | 298 * |
| 253 * @param {Window} contentWindow Window to be tested. | 299 * @param {Window} contentWindow Window to be tested. |
| 254 * @param {string} targetQuery Query to specify the element. | 300 * @param {string} targetQuery Query to specify the element. |
| 255 * @param {?string} iframeQuery Iframe selector or null if no iframe. | 301 * @param {?string} iframeQuery Iframe selector or null if no iframe. |
| 256 * @param {Array<string>=} opt_styleNames List of CSS property name to be | 302 * @param {Array<string>=} opt_styleNames List of CSS property name to be |
| 257 * obtained. | 303 * obtained. |
| 258 * @return {?{attributes:Object<string>, text:string, | 304 * @return {?{attributes:Object<string>, text:string, |
| 259 * styles:Object<string>, hidden:boolean}} Element | 305 * styles:Object<string>, hidden:boolean}} Element |
| 260 * information that contains contentText, attribute names and | 306 * information that contains contentText, attribute names and |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 583 return true; | 629 return true; |
| 584 } else if (test.util.sync[request.func]) { | 630 } else if (test.util.sync[request.func]) { |
| 585 sendResponse(test.util.sync[request.func].apply(null, args)); | 631 sendResponse(test.util.sync[request.func].apply(null, args)); |
| 586 return false; | 632 return false; |
| 587 } else { | 633 } else { |
| 588 console.error('Invalid function name.'); | 634 console.error('Invalid function name.'); |
| 589 return false; | 635 return false; |
| 590 } | 636 } |
| 591 }); | 637 }); |
| 592 }; | 638 }; |
| OLD | NEW |