All Downloads are FREE. Search and download functionalities are using the official Maven repository.

goog.async.delay_test.js Maven / Gradle / Ivy

Go to download

The Google Closure Library is a collection of JavaScript code designed for use with the Google Closure JavaScript Compiler. This non-official distribution was prepared by the ClojureScript team at http://clojure.org/

There is a newer version: 0.0-20230227-c7c0a541
Show newest version
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.async.DelayTest');
goog.setTestOnly('goog.async.DelayTest');

goog.require('goog.async.Delay');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.jsunit');

var invoked = false;
var delay = null;
var clock = null;


function callback() {
  invoked = true;
}


function setUp() {
  clock = new goog.testing.MockClock(true);
  invoked = false;
  delay = new goog.async.Delay(callback, 200);
}

function tearDown() {
  clock.dispose();
  delay.dispose();
}


function testDelay() {
  delay.start();
  assertFalse(invoked);

  clock.tick(100);
  assertFalse(invoked);

  clock.tick(100);
  assertTrue(invoked);
}


function testStop() {
  delay.start();

  clock.tick(100);
  assertFalse(invoked);

  delay.stop();
  clock.tick(100);
  assertFalse(invoked);
}


function testIsActive() {
  assertFalse(delay.isActive());
  delay.start();
  assertTrue(delay.isActive());
  clock.tick(200);
  assertFalse(delay.isActive());
}


function testRestart() {
  delay.start();
  clock.tick(100);

  delay.stop();
  assertFalse(invoked);

  delay.start();
  clock.tick(199);
  assertFalse(invoked);

  clock.tick(1);
  assertTrue(invoked);

  invoked = false;
  delay.start();
  clock.tick(200);
  assertTrue(invoked);
}


function testStartIfNotActive() {
  delay.startIfNotActive();
  clock.tick(100);

  delay.stop();
  assertFalse(invoked);

  delay.startIfNotActive();
  clock.tick(199);
  assertFalse(invoked);

  clock.tick(1);
  assertTrue(invoked);

  invoked = false;
  delay.start();
  clock.tick(199);

  assertFalse(invoked);

  delay.startIfNotActive();
  clock.tick(1);

  assertTrue(invoked);
}


function testOverride() {
  delay.start(50);
  clock.tick(49);
  assertFalse(invoked);

  clock.tick(1);
  assertTrue(invoked);
}


function testDispose() {
  delay.start();
  delay.dispose();
  assertTrue(delay.isDisposed());

  clock.tick(500);
  assertFalse(invoked);
}


function testFire() {
  delay.start();

  clock.tick(50);
  delay.fire();
  assertTrue(invoked);
  assertFalse(delay.isActive());

  invoked = false;
  clock.tick(200);
  assertFalse(
      'Delay fired early with fire call, timeout should have been ' +
          'cleared',
      invoked);
}

function testFireIfActive() {
  delay.fireIfActive();
  assertFalse(invoked);

  delay.start();
  delay.fireIfActive();
  assertTrue(invoked);
  invoked = false;
  clock.tick(300);
  assertFalse(
      'Delay fired early with fireIfActive, timeout should have been ' +
          'cleared',
      invoked);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy