Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / browser_test / timeout_waiter.js
blobfd75ada23172bf6723614541d32e1650467cb350
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
3 // found in the LICENSE file.
5 /**
6 * @fileoverview
7 * Provide polling-based "wait for" functionality, and defines some useful
8 * predicates.
9 */
11 'use strict';
13 /** @suppress {duplicate} */
14 var browserTest = browserTest || {};
16 /** @enum {number} */
17 browserTest.Timeout = {
18 NONE: -1,
19 DEFAULT: 5000
22 /** @constructor */
23 browserTest.Predicate = function() {};
25 /** @return {boolean} */
26 browserTest.Predicate.prototype.evaluate = function() {};
28 /** @return {string} */
29 browserTest.Predicate.prototype.description = function() {};
31 /**
32 * @param {browserTest.Predicate} predicate
33 * @param {number=} opt_timeout Timeout in ms.
34 * @return {Promise}
36 browserTest.waitFor = function(predicate, opt_timeout) {
37 /**
38 * @param {function():void} fulfill
39 * @param {function(Error):void} reject
41 return new Promise(function (fulfill, reject) {
42 if (opt_timeout === undefined) {
43 opt_timeout = browserTest.Timeout.DEFAULT;
46 var timeout = /** @type {number} */ (opt_timeout);
47 var end = Number(Date.now()) + timeout;
48 var testPredicate = function() {
49 if (predicate.evaluate()) {
50 console.log(predicate.description() + ' satisfied.');
51 fulfill(true);
52 } else if (Date.now() >= end) {
53 reject(new Error('Timed out (' + opt_timeout + 'ms) waiting for ' +
54 predicate.description()));
55 } else {
56 console.log(predicate.description() + ' not yet satisfied.');
57 window.setTimeout(testPredicate, 500);
60 testPredicate();
61 });
64 /**
65 * @param {string} id
66 * @return {browserTest.Predicate}
68 browserTest.isVisible = function(id) {
69 var pred = new browserTest.Predicate();
70 pred.evaluate = function() {
71 /** @type {HTMLElement} */
72 var element = document.getElementById(id);
73 browserTest.expect(Boolean(element), 'No such element: ' + id);
74 return element.getBoundingClientRect().width !== 0;
76 pred.description = function() {
77 return 'isVisible(' + id + ')';
79 return pred;
82 /**
83 * @param {string} id
84 * @return {browserTest.Predicate}
86 browserTest.isEnabled = function(id) {
87 var pred = new browserTest.Predicate();
88 pred.evaluate = function() {
89 /** @type {Element} */
90 var element = document.getElementById(id);
91 browserTest.expect(Boolean(element), 'No such element: ' + id);
92 return !element.disabled;
94 pred.description = function() {
95 return 'isEnabled(' + id + ')';
97 return pred;