Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| /** | |
| * @license | |
| * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | |
| * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | |
| * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
| * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | |
| * Code distributed by Google as part of the polymer project is also | |
| * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | |
| */ | |
| // Make sure that we use native timers, in case they're being stubbed out. | |
| var setInterval = window.setInterval; // jshint ignore:line | |
| var setTimeout = window.setTimeout; // jshint ignore:line | |
| var requestAnimationFrame = window.requestAnimationFrame; // jshint ignore:line | |
| /** | |
| * Runs `stepFn`, catching any error and passing it to `callback` (Node-style). | |
| * Otherwise, calls `callback` with no arguments on success. | |
| * | |
| * @param {function()} callback | |
| * @param {function()} stepFn | |
| */ | |
| window.safeStep = function safeStep(callback, stepFn) { | |
| var err; | |
| try { | |
| stepFn(); | |
| } catch (error) { | |
| err = error; | |
| } | |
| callback(err); | |
| }; | |
| /** | |
| * Runs your test at declaration time (before Mocha has begun tests). Handy for | |
| * when you need to test document initialization. | |
| * | |
| * Be aware that any errors thrown asynchronously cannot be tied to your test. | |
| * You may want to catch them and pass them to the done event, instead. See | |
| * `safeStep`. | |
| * | |
| * @param {string} name The name of the test. | |
| * @param {function(?function())} testFn The test function. If an argument is | |
| * accepted, the test will be treated as async, just like Mocha tests. | |
| */ | |
| window.testImmediate = function testImmediate(name, testFn) { | |
| if (testFn.length > 0) { | |
| return testImmediateAsync(name, testFn); | |
| } | |
| var err; | |
| try { | |
| testFn(); | |
| } catch (error) { | |
| err = error; | |
| } | |
| test(name, function(done) { | |
| done(err); | |
| }); | |
| }; | |
| /** | |
| * An async-only variant of `testImmediate`. | |
| * | |
| * @param {string} name | |
| * @param {function(?function())} testFn | |
| */ | |
| window.testImmediateAsync = function testImmediateAsync(name, testFn) { | |
| var testComplete = false; | |
| var err; | |
| test(name, function(done) { | |
| var intervalId = setInterval(function() { | |
| if (!testComplete) return; | |
| clearInterval(intervalId); | |
| done(err); | |
| }, 10); | |
| }); | |
| try { | |
| testFn(function(error) { | |
| if (error) err = error; | |
| testComplete = true; | |
| }); | |
| } catch (error) { | |
| err = error; | |
| testComplete = true; | |
| } | |
| }; | |
| /** | |
| * Triggers a flush of any pending events, observations, etc and calls you back | |
| * after they have been processed. | |
| * | |
| * @param {function()} callback | |
| */ | |
| window.flush = function flush(callback) { | |
| // Ideally, this function would be a call to Polymer.dom.flush, but that doesn't | |
| // support a callback yet (https://github.com/Polymer/polymer-dev/issues/851), | |
| // ...and there's cross-browser flakiness to deal with. | |
| // Make sure that we're invoking the callback with no arguments so that the | |
| // caller can pass Mocha callbacks, etc. | |
| var done = function done() { callback(); }; | |
| // Because endOfMicrotask is flaky for IE, we perform microtask checkpoints | |
| // ourselves (https://github.com/Polymer/polymer-dev/issues/114): | |
| var isIE = navigator.appName == 'Microsoft Internet Explorer'; | |
| if (isIE && window.Platform && window.Platform.performMicrotaskCheckpoint) { | |
| var reallyDone = done; | |
| done = function doneIE() { | |
| Platform.performMicrotaskCheckpoint(); | |
| setTimeout(reallyDone, 0); | |
| }; | |
| } | |
| // Everyone else gets a regular flush. | |
| var scope; | |
| if (window.Polymer && window.Polymer.dom && window.Polymer.dom.flush) { | |
| scope = window.Polymer.dom; | |
| } else if (window.Polymer && window.Polymer.flush) { | |
| scope = window.Polymer; | |
| } else if (window.WebComponents && window.WebComponents.flush) { | |
| scope = window.WebComponents; | |
| } | |
| if (scope) { | |
| scope.flush(); | |
| } | |
| // Ensure that we are creating a new _task_ to allow all active microtasks to | |
| // finish (the code you're testing may be using endOfMicrotask, too). | |
| setTimeout(done, 0); | |
| }; | |
| /** | |
| * Advances a single animation frame. | |
| * | |
| * Calls `flush`, `requestAnimationFrame`, `flush`, and `callback` sequentially | |
| * @param {function()} callback | |
| */ | |
| window.animationFrameFlush = function animationFrameFlush(callback) { | |
| flush(function() { | |
| requestAnimationFrame(function() { | |
| flush(callback); | |
| }); | |
| }); | |
| }; | |
| /** | |
| * DEPRECATED: Use `flush`. | |
| * @param {function} callback | |
| */ | |
| window.asyncPlatformFlush = function asyncPlatformFlush(callback) { | |
| console.warn('asyncPlatformFlush is deprecated in favor of the more terse flush()'); | |
| return window.flush(callback); | |
| }; | |
| /** | |
| * | |
| */ | |
| window.waitFor = function waitFor(fn, next, intervalOrMutationEl, timeout, timeoutTime) { | |
| timeoutTime = timeoutTime || Date.now() + (timeout || 1000); | |
| intervalOrMutationEl = intervalOrMutationEl || 32; | |
| try { | |
| fn(); | |
| } catch (e) { | |
| if (Date.now() > timeoutTime) { | |
| throw e; | |
| } else { | |
| if (isNaN(intervalOrMutationEl)) { | |
| intervalOrMutationEl.onMutation(intervalOrMutationEl, function() { | |
| waitFor(fn, next, intervalOrMutationEl, timeout, timeoutTime); | |
| }); | |
| } else { | |
| setTimeout(function() { | |
| waitFor(fn, next, intervalOrMutationEl, timeout, timeoutTime); | |
| }, intervalOrMutationEl); | |
| } | |
| return; | |
| } | |
| } | |
| next(); | |
| }; |