OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 cr.define('test_util', function() { | 5 cr.define('test_util', function() { |
6 /** | 6 /** |
7 * Observes an HTML attribute and fires a promise when it matches a given | 7 * Observes an HTML attribute and fires a promise when it matches a given |
8 * value. | 8 * value. |
9 * @param {!HTMLElement} target | 9 * @param {!HTMLElement} target |
10 * @param {string} attributeName | 10 * @param {string} attributeName |
(...skipping 23 matching lines...) Expand all Loading... |
34 * @param {string} eventType | 34 * @param {string} eventType |
35 * @param {!HTMLElement} target | 35 * @param {!HTMLElement} target |
36 * @return {!Promise} A promise firing once the event occurs. | 36 * @return {!Promise} A promise firing once the event occurs. |
37 */ | 37 */ |
38 function eventToPromise(eventType, target) { | 38 function eventToPromise(eventType, target) { |
39 return new Promise(function(resolve, reject) { | 39 return new Promise(function(resolve, reject) { |
40 target.addEventListener(eventType, resolve); | 40 target.addEventListener(eventType, resolve); |
41 }); | 41 }); |
42 } | 42 } |
43 | 43 |
| 44 /** |
| 45 * Data-binds two Polymer properties using the property-changed events and |
| 46 * set/notifyPath API. Useful for testing components which would normally be |
| 47 * used together. |
| 48 * @param {!HTMLElement} el1 |
| 49 * @param {!HTMLElement} el2 |
| 50 * @param {string} property |
| 51 */ |
| 52 function fakeDataBind(el1, el2, property) { |
| 53 var forwardChange = function(el, event) { |
| 54 if (event.detail.hasOwnProperty('path')) |
| 55 el.notifyPath(event.detail.path, event.detail.value); |
| 56 else |
| 57 el.set(property, event.detail.value); |
| 58 }; |
| 59 // Add the listeners symmetrically. Polymer will prevent recursion. |
| 60 el1.addEventListener(property + '-changed', forwardChange.bind(null, el2)); |
| 61 el2.addEventListener(property + '-changed', forwardChange.bind(null, el1)); |
| 62 } |
| 63 |
44 return { | 64 return { |
45 eventToPromise: eventToPromise, | 65 eventToPromise: eventToPromise, |
| 66 fakeDataBind: fakeDataBind, |
46 whenAttributeIs: whenAttributeIs, | 67 whenAttributeIs: whenAttributeIs, |
47 }; | 68 }; |
48 }); | 69 }); |
OLD | NEW |