OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 cr.define('test_util', function() { |
| 6 /** |
| 7 * Observes an HTML attribute and fires a promise when it matches a given |
| 8 * value. |
| 9 * @param {!HTMLElement} target |
| 10 * @param {string} attributeName |
| 11 * @param {*} attributeValue |
| 12 * @return {!Promise} |
| 13 */ |
| 14 function whenAttributeIs(target, attributeName, attributeValue) { |
| 15 function isDone() { return target[attributeName] === attributeValue; } |
| 16 |
| 17 return isDone() ? Promise.resolve() : new Promise(function(resolve) { |
| 18 new MutationObserver(function(mutations, observer) { |
| 19 for (var mutation of mutations) { |
| 20 assertEquals('attributes', mutation.type); |
| 21 if (mutation.attributeName == attributeName && isDone()) { |
| 22 observer.disconnect(); |
| 23 resolve(); |
| 24 return; |
| 25 } |
| 26 } |
| 27 }).observe( |
| 28 target, {attributes: true, childList: false, characterData: false}); |
| 29 }); |
| 30 } |
| 31 |
| 32 /** |
| 33 * Converts an event occurrence to a promise. |
| 34 * @param {string} eventType |
| 35 * @param {!HTMLElement} target |
| 36 * @return {!Promise} A promise firing once the event occurs. |
| 37 */ |
| 38 function eventToPromise(eventType, target) { |
| 39 return new Promise(function(resolve, reject) { |
| 40 target.addEventListener(eventType, resolve); |
| 41 }); |
| 42 } |
| 43 |
| 44 return { |
| 45 eventToPromise: eventToPromise, |
| 46 whenAttributeIs: whenAttributeIs, |
| 47 }; |
| 48 }); |
OLD | NEW |