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 |
(...skipping 16 matching lines...) Expand all Loading... |
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 return new Promise(function(fulfill, reject) { | 37 /** |
| 38 * @param {function():void} fulfill |
| 39 * @param {function(Error):void} reject |
| 40 */ |
| 41 return new Promise(function (fulfill, reject) { |
38 if (opt_timeout === undefined) { | 42 if (opt_timeout === undefined) { |
39 opt_timeout = browserTest.Timeout.DEFAULT; | 43 opt_timeout = browserTest.Timeout.DEFAULT; |
40 } | 44 } |
41 var end = Number(new Date()) + opt_timeout; | 45 |
| 46 var timeout = /** @type {number} */ opt_timeout; |
| 47 var end = Number(Date.now()) + timeout; |
42 var testPredicate = function() { | 48 var testPredicate = function() { |
43 if (predicate.evaluate()) { | 49 if (predicate.evaluate()) { |
44 console.log(predicate.description() + ' satisfied.'); | 50 console.log(predicate.description() + ' satisfied.'); |
45 fulfill(); | 51 fulfill(); |
46 } else if (Number(new Date()) >= end) { | 52 } else if (Date.now() >= end) { |
47 reject(new Error('Timed out (' + opt_timeout + 'ms) waiting for ' + | 53 reject(new Error('Timed out (' + opt_timeout + 'ms) waiting for ' + |
48 predicate.description())); | 54 predicate.description())); |
49 } else { | 55 } else { |
50 console.log(predicate.description() + ' not yet satisfied.'); | 56 console.log(predicate.description() + ' not yet satisfied.'); |
51 window.setTimeout(testPredicate, 500); | 57 window.setTimeout(testPredicate, 500); |
52 } | 58 } |
53 }; | 59 }; |
54 testPredicate(); | 60 testPredicate(); |
55 }); | 61 }); |
56 }; | 62 }; |
57 | 63 |
58 /** | 64 /** |
| 65 * @suppress {checkTypes} |
59 * @param {string} id | 66 * @param {string} id |
60 * @return {browserTest.Predicate} | 67 * @return {browserTest.Predicate} |
61 */ | 68 */ |
62 browserTest.isVisible = function(id) { | 69 browserTest.isVisible = function(id) { |
63 var element = document.getElementById(id); | 70 /** @type {browserTest.Predicate} */ |
64 browserTest.expect(element, 'No such element: ' + id); | |
65 return { | 71 return { |
66 evaluate: function() { | 72 evaluate: function() { |
67 return element.getBoundingClientRect().width != 0; | 73 /** @type {HTMLElement} */ |
| 74 var element = document.getElementById(id); |
| 75 browserTest.expect(Boolean(element), 'No such element: ' + id); |
| 76 return element.getBoundingClientRect().width !== 0; |
68 }, | 77 }, |
69 description: function() { | 78 description: function() { |
70 return 'isVisible(' + id + ')'; | 79 return 'isVisible(' + id + ')'; |
71 } | 80 } |
72 }; | 81 }; |
73 }; | 82 }; |
74 | 83 |
75 /** | 84 /** |
| 85 * @suppress {checkTypes} |
76 * @param {string} id | 86 * @param {string} id |
77 * @return {browserTest.Predicate} | 87 * @return {browserTest.Predicate} |
78 */ | 88 */ |
79 browserTest.isEnabled = function(id) { | 89 browserTest.isEnabled = function(id) { |
80 var element = document.getElementById(id); | 90 |
81 browserTest.expect(element, 'No such element: ' + id); | 91 /** @type {browserTest.Predicate} */ |
82 return { | 92 return { |
83 evaluate: function() { | 93 evaluate: function() { |
| 94 /** @type {HTMLElement} */ |
| 95 var element = document.getElementById(id); |
| 96 browserTest.expect(Boolean(element), 'No such element: ' + id); |
84 return !element.disabled; | 97 return !element.disabled; |
85 }, | 98 }, |
86 description: function() { | 99 description: function() { |
87 return 'isEnabled(' + id + ')'; | 100 return 'isEnabled(' + id + ')'; |
88 } | 101 } |
89 }; | 102 }; |
90 }; | 103 }; |
| 104 |
OLD | NEW |