Index: remoting/webapp/browser_test/timeout_waiter.js |
diff --git a/remoting/webapp/browser_test/timeout_waiter.js b/remoting/webapp/browser_test/timeout_waiter.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..82cfcdcbc15c4e06fe90e6c034d05942d3760f02 |
--- /dev/null |
+++ b/remoting/webapp/browser_test/timeout_waiter.js |
@@ -0,0 +1,90 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+/** |
+ * @fileoverview |
+ * Provide polling-based "wait for" functionality, and defines some useful |
+ * predicates. |
+ */ |
+ |
+'use strict'; |
+ |
+/** @suppress {duplicate} */ |
+var browserTest = browserTest || {}; |
+ |
+/** @enum {number} */ |
+browserTest.Timeout = { |
+ NONE: -1, |
+ DEFAULT: 5000 |
+}; |
+ |
+/** @interface */ |
+browserTest.Predicate = function() {}; |
+ |
+/** @return {boolean} */ |
+browserTest.Predicate.prototype.evaluate = function() {}; |
+ |
+/** @return {string} */ |
+browserTest.Predicate.prototype.description = function() {}; |
+ |
+/** |
+ * @param {browserTest.Predicate} predicate |
+ * @param {number=} opt_number |
kelvinp
2015/01/08 22:54:41
s/opt_timeout/opt_number should also probably cal
Jamie
2015/01/08 23:43:08
Done.
|
+ * @return {Promise} |
+ */ |
+browserTest.waitFor = function(predicate, opt_timeout) { |
+ return new Promise(function(fulfill, reject) { |
+ if (opt_timeout === undefined) { |
+ opt_timeout = browserTest.Timeout.DEFAULT; |
+ } |
+ var end = Number(new Date()) + opt_timeout; |
+ var testPredicate = function() { |
+ if (predicate.evaluate()) { |
+ console.log(predicate.description() + ' satisfied.'); |
+ fulfill(); |
+ } else if (Number(new Date()) >= end) { |
+ reject(new Error('Timed out (' + opt_timeout + 'ms) waiting for ' + |
+ predicate.description())); |
+ } else { |
+ console.log(predicate.description() + ' not yet satisfied.'); |
+ window.setTimeout(testPredicate, 500); |
+ } |
+ }; |
+ testPredicate(); |
+ }); |
+}; |
+ |
+/** |
+ * @param {string} id |
+ * @return {browserTest.Predicate} |
+ */ |
+browserTest.isVisible = function(id) { |
+ var element = document.getElementById(id); |
+ browserTest.expect(element, 'No such element: ' + id); |
+ return { |
+ evaluate: function() { |
+ return element.getBoundingClientRect().width != 0; |
kelvinp
2015/01/08 22:54:41
Just want to call out that this check will not wor
Jamie
2015/01/08 23:43:08
Ack.
|
+ }, |
+ description: function() { |
+ return 'isVisible(' + id + ')'; |
+ } |
+ }; |
+}; |
+ |
+/** |
+ * @param {string} id |
+ * @return {browserTest.Predicate} |
+ */ |
+browserTest.isEnabled = function(id) { |
+ var element = document.getElementById(id); |
+ browserTest.expect(element, 'No such element: ' + id); |
+ return { |
+ evaluate: function() { |
+ return !element.disabled; |
+ }, |
+ description: function() { |
+ return 'isEnabled(' + id + ')'; |
+ } |
+ }; |
+}; |