瀏覽代碼

Merge pull request #7202 from simonThiele/unittests

Clock additions and Unit-Tests for Clock
Mr.doob 10 年之前
父節點
當前提交
52b8338348
共有 4 個文件被更改,包括 152 次插入6 次删除
  1. 10 6
      src/core/Clock.js
  2. 52 0
      test/unit/core/Clock.js
  3. 88 0
      test/unit/core/EventDispatcher.js
  4. 2 0
      test/unit/unittests_three.html

+ 10 - 6
src/core/Clock.js

@@ -20,9 +20,7 @@ THREE.Clock.prototype = {
 
 	start: function () {
 
-		this.startTime = self.performance !== undefined && self.performance.now !== undefined
-					 ? self.performance.now()
-					 : Date.now();
+		this.startTime = this._getNow();
 
 		this.oldTime = this.startTime;
 		this.running = true;
@@ -55,9 +53,7 @@ THREE.Clock.prototype = {
 
 		if ( this.running ) {
 
-			var newTime = self.performance !== undefined && self.performance.now !== undefined
-					 ? self.performance.now()
-					 : Date.now();
+			var newTime = this._getNow();
 
 			diff = 0.001 * ( newTime - this.oldTime );
 			this.oldTime = newTime;
@@ -68,6 +64,14 @@ THREE.Clock.prototype = {
 
 		return diff;
 
+	},
+
+	_getNow: function () {
+		var now = self.performance !== undefined && self.performance.now !== undefined
+			? self.performance.now()
+			: Date.now();
+
+		return now;
 	}
 
 };

+ 52 - 0
test/unit/core/Clock.js

@@ -0,0 +1,52 @@
+/**
+ * @author simonThiele / https://github.com/simonThiele
+ */
+
+module( "Clock" );
+
+function mockPerformance() {
+	self.performance = {
+		deltaTime: 0,
+
+		next: function( delta ) {
+			this.deltaTime += delta;
+		},
+
+		now: function() {
+			return this.deltaTime;
+		}
+	};
+}
+
+test( "clock with performance", function() {
+	mockPerformance();
+
+	var clock = new THREE.Clock();
+
+	clock.start();
+
+	self.performance.next(123);
+	ok( clock.getElapsedTime() === 0.123 , "okay");
+
+	self.performance.next(100);
+	ok( clock.getElapsedTime() === 0.223 , "okay");
+
+	clock.stop();
+
+	self.performance.next(1000);
+	ok( clock.getElapsedTime() === 0.223 , "don't update time if the clock was stopped");
+});
+
+test( "clock with date", function() {
+	// remove the performance object so that clock takes Date()
+	self.performance = undefined;
+
+	var clock = new THREE.Clock();
+
+	clock.start();
+
+	// if a value was calculated, the clock took the alternative Date() object
+	ok( clock.getElapsedTime() >= 0 , "okay");
+
+	clock.stop();
+});

+ 88 - 0
test/unit/core/EventDispatcher.js

@@ -0,0 +1,88 @@
+/**
+ * @author simonThiele / https://github.com/simonThiele
+ */
+
+module( "EventDispatcher" );
+
+test( "apply", function() {
+	var innocentObject = {};
+	var eventDispatcher = new THREE.EventDispatcher();
+
+	eventDispatcher.apply( innocentObject );
+
+	ok( innocentObject.addEventListener !== undefined &&
+			innocentObject.hasEventListener !== undefined &&
+		innocentObject.removeEventListener !== undefined &&
+		innocentObject.dispatchEvent !== undefined, "events where added to object" );
+});
+
+test( "addEventListener", function() {
+	var eventDispatcher = new THREE.EventDispatcher();
+
+	var listener = {};
+	eventDispatcher.addEventListener( 'anyType', listener );
+
+	ok( eventDispatcher._listeners.anyType.length === 1, "listener with unknown type was added" );
+	ok( eventDispatcher._listeners.anyType[0] === listener, "listener with unknown type was added" );
+
+	eventDispatcher.addEventListener( 'anyType', listener );
+
+	ok( eventDispatcher._listeners.anyType.length === 1, "can't add one listener twice to same type" );
+	ok( eventDispatcher._listeners.anyType[0] === listener, "listener is still there" );
+});
+
+test( "hasEventListener", function() {
+	var eventDispatcher = new THREE.EventDispatcher();
+
+	var listener = {};
+	eventDispatcher.addEventListener( 'anyType', listener );
+
+	ok( eventDispatcher.hasEventListener( 'anyType', listener ), "listener was found" );
+	ok( !eventDispatcher.hasEventListener( 'anotherType', listener ), "listener was not found which is good" );
+});
+
+test( "removeEventListener", function() {
+	var eventDispatcher = new THREE.EventDispatcher();
+
+	var listener = {};
+
+	ok ( eventDispatcher._listeners === undefined, "there are no listeners by default" );
+
+	eventDispatcher.addEventListener( 'anyType', listener );
+	ok ( Object.keys( eventDispatcher._listeners ).length === 1 &&
+		eventDispatcher._listeners.anyType.length === 1, "if a listener was added, there is a new key" );
+
+	eventDispatcher.removeEventListener( 'anyType', listener );
+	ok ( eventDispatcher._listeners.anyType.length === 0, "listener was deleted" );
+
+	eventDispatcher.removeEventListener( 'unknownType', listener );
+	ok ( eventDispatcher._listeners.unknownType === undefined, "unknown types will be ignored" );
+
+	eventDispatcher.removeEventListener( 'anyType', undefined );
+	ok ( eventDispatcher._listeners.anyType.length === 0, "undefined listeners are ignored" );
+});
+
+test( "dispatchEvent", function() {
+	var eventDispatcher = new THREE.EventDispatcher();
+
+	var callCount = 0;
+	var listener = function() { callCount++; };
+
+	eventDispatcher.addEventListener( 'anyType', listener );
+	ok( callCount === 0, "no event, no call" );
+
+	eventDispatcher.dispatchEvent( { type: 'anyType' } );
+	ok( callCount === 1, "one event, one call" );
+
+	eventDispatcher.dispatchEvent( { type: 'anyType' } );
+	ok( callCount === 2, "two events, two calls" );
+});
+
+
+
+
+
+
+
+
+//

+ 2 - 0
test/unit/unittests_three.html

@@ -24,6 +24,8 @@
 
   <script src="core/BufferAttribute.js"></script>
   <script src="core/BufferGeometry.js"></script>
+  <script src="core/Clock.js"></script>
+  <script src="core/EventDispatcher.js"></script>
   <script src="core/Object3D.js"></script>
 
   <script src="math/Constants.js"></script>