| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 * @fileoverview | 6 * @fileoverview |
| 7 * Provide polling-based "wait for" functionality, and defines some useful | 7 * Provide polling-based "wait for" functionality, and defines some useful |
| 8 * predicates. | 8 * predicates. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 'use strict'; | 11 'use strict'; |
| 12 | 12 |
| 13 /** @suppress {duplicate} */ | 13 /** @suppress {duplicate} */ |
| 14 var browserTest = browserTest || {}; | 14 var browserTest = browserTest || {}; |
| 15 | 15 |
| 16 /** @enum {number} */ | 16 /** @enum {number} */ |
| 17 browserTest.Timeout = { | 17 browserTest.Timeout = { |
| 18 NONE: -1, | 18 NONE: -1, |
| 19 DEFAULT: 5000 | 19 DEFAULT: 5000 |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 /** @interface */ | 22 /** @constructor */ |
| 23 browserTest.Predicate = function() {}; | 23 browserTest.Predicate = function() {}; |
| 24 | 24 |
| 25 /** @return {boolean} */ | 25 /** @return {boolean} */ |
| 26 browserTest.Predicate.prototype.evaluate = function() {}; | 26 browserTest.Predicate.prototype.evaluate = function() {}; |
| 27 | 27 |
| 28 /** @return {string} */ | 28 /** @return {string} */ |
| 29 browserTest.Predicate.prototype.description = function() {}; | 29 browserTest.Predicate.prototype.description = function() {}; |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * @param {browserTest.Predicate} predicate | 32 * @param {browserTest.Predicate} predicate |
| 33 * @param {number=} opt_timeout Timeout in ms. | 33 * @param {number=} opt_timeout Timeout in ms. |
| 34 * @return {Promise} | 34 * @return {Promise} |
| 35 */ | 35 */ |
| 36 browserTest.waitFor = function(predicate, opt_timeout) { | 36 browserTest.waitFor = function(predicate, opt_timeout) { |
| 37 /** | 37 /** |
| 38 * @param {function():void} fulfill | 38 * @param {function():void} fulfill |
| 39 * @param {function(Error):void} reject | 39 * @param {function(Error):void} reject |
| 40 */ | 40 */ |
| 41 return new Promise(function (fulfill, reject) { | 41 return new Promise(function (fulfill, reject) { |
| 42 if (opt_timeout === undefined) { | 42 if (opt_timeout === undefined) { |
| 43 opt_timeout = browserTest.Timeout.DEFAULT; | 43 opt_timeout = browserTest.Timeout.DEFAULT; |
| 44 } | 44 } |
| 45 | 45 |
| 46 var timeout = /** @type {number} */ opt_timeout; | 46 var timeout = /** @type {number} */ (opt_timeout); |
| 47 var end = Number(Date.now()) + timeout; | 47 var end = Number(Date.now()) + timeout; |
| 48 var testPredicate = function() { | 48 var testPredicate = function() { |
| 49 if (predicate.evaluate()) { | 49 if (predicate.evaluate()) { |
| 50 console.log(predicate.description() + ' satisfied.'); | 50 console.log(predicate.description() + ' satisfied.'); |
| 51 fulfill(); | 51 fulfill(true); |
| 52 } else if (Date.now() >= end) { | 52 } else if (Date.now() >= end) { |
| 53 reject(new Error('Timed out (' + opt_timeout + 'ms) waiting for ' + | 53 reject(new Error('Timed out (' + opt_timeout + 'ms) waiting for ' + |
| 54 predicate.description())); | 54 predicate.description())); |
| 55 } else { | 55 } else { |
| 56 console.log(predicate.description() + ' not yet satisfied.'); | 56 console.log(predicate.description() + ' not yet satisfied.'); |
| 57 window.setTimeout(testPredicate, 500); | 57 window.setTimeout(testPredicate, 500); |
| 58 } | 58 } |
| 59 }; | 59 }; |
| 60 testPredicate(); | 60 testPredicate(); |
| 61 }); | 61 }); |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * @suppress {checkTypes} | |
| 66 * @param {string} id | 65 * @param {string} id |
| 67 * @return {browserTest.Predicate} | 66 * @return {browserTest.Predicate} |
| 68 */ | 67 */ |
| 69 browserTest.isVisible = function(id) { | 68 browserTest.isVisible = function(id) { |
| 70 /** @type {browserTest.Predicate} */ | 69 var pred = new browserTest.Predicate(); |
| 71 return { | 70 pred.evaluate = function() { |
| 72 evaluate: function() { | 71 /** @type {HTMLElement} */ |
| 73 /** @type {HTMLElement} */ | 72 var element = document.getElementById(id); |
| 74 var element = document.getElementById(id); | 73 browserTest.expect(Boolean(element), 'No such element: ' + id); |
| 75 browserTest.expect(Boolean(element), 'No such element: ' + id); | 74 return element.getBoundingClientRect().width !== 0; |
| 76 return element.getBoundingClientRect().width !== 0; | |
| 77 }, | |
| 78 description: function() { | |
| 79 return 'isVisible(' + id + ')'; | |
| 80 } | |
| 81 }; | 75 }; |
| 76 pred.description = function() { |
| 77 return 'isVisible(' + id + ')'; |
| 78 }; |
| 79 return pred; |
| 82 }; | 80 }; |
| 83 | 81 |
| 84 /** | 82 /** |
| 85 * @suppress {checkTypes} | |
| 86 * @param {string} id | 83 * @param {string} id |
| 87 * @return {browserTest.Predicate} | 84 * @return {browserTest.Predicate} |
| 88 */ | 85 */ |
| 89 browserTest.isEnabled = function(id) { | 86 browserTest.isEnabled = function(id) { |
| 90 | 87 var pred = new browserTest.Predicate(); |
| 91 /** @type {browserTest.Predicate} */ | 88 pred.evaluate = function() { |
| 92 return { | 89 /** @type {Element} */ |
| 93 evaluate: function() { | 90 var element = document.getElementById(id); |
| 94 /** @type {HTMLElement} */ | 91 browserTest.expect(Boolean(element), 'No such element: ' + id); |
| 95 var element = document.getElementById(id); | 92 return !element.disabled; |
| 96 browserTest.expect(Boolean(element), 'No such element: ' + id); | |
| 97 return !element.disabled; | |
| 98 }, | |
| 99 description: function() { | |
| 100 return 'isEnabled(' + id + ')'; | |
| 101 } | |
| 102 }; | 93 }; |
| 94 pred.description = function() { |
| 95 return 'isEnabled(' + id + ')'; |
| 96 }; |
| 97 return pred; |
| 103 }; | 98 }; |
| 104 | |
| OLD | NEW |