Browse Source

added serialization tests for all commands

Mario Schuettel 9 years ago
parent
commit
2409f3e2f8
1 changed files with 222 additions and 5 deletions
  1. 222 5
      test/unit/editor/TestSerialization.js

+ 222 - 5
test/unit/editor/TestSerialization.js

@@ -5,6 +5,8 @@ test( "Test Serialization (simple)", function() {
 	// setup
 	// setup
 	var editor = new Editor();
 	var editor = new Editor();
 
 
+	var green   = 12581843; // bffbd3
+
 	var addObject = function () {
 	var addObject = function () {
 
 
 		// setup
 		// setup
@@ -16,6 +18,7 @@ test( "Test Serialization (simple)", function() {
 
 
 		editor.execute( cmd );
 		editor.execute( cmd );
 
 
+		return "addObject";
 	};
 	};
 
 
 	var addScript = function () {
 	var addScript = function () {
@@ -33,6 +36,8 @@ test( "Test Serialization (simple)", function() {
 
 
 		editor.execute( cmd );
 		editor.execute( cmd );
 
 
+		return "addScript";
+
 	};
 	};
 
 
 	var moveObject = function () {
 	var moveObject = function () {
@@ -49,15 +54,227 @@ test( "Test Serialization (simple)", function() {
 		// Tell Luke, Anakin is his father
 		// Tell Luke, Anakin is his father
 		editor.execute( new CmdMoveObject( lukeSkywalker, anakinSkywalker ) );
 		editor.execute( new CmdMoveObject( lukeSkywalker, anakinSkywalker ) );
 
 
+		return "moveObject";
+	};
+
+	var removeScript = function () {
+
+		var box = aBox( 'Box with no script' );
+		editor.execute( new CmdAddObject( box ) );
+
+		var script = { "name":"test","source":"console.log(\"hello world\");" } ;
+		var cmd = new CmdAddScript( box, script );
+		cmd.updatable = false;
+
+		editor.execute( cmd );
+
+		cmd = new CmdRemoveScript( box, script );
+		editor.execute( cmd );
+
+		return "removeScript";
+	};
+
+	var setColor = function () {
+
+		var pointLight = aPointlight( "The light Light" );
+
+		editor.execute( new CmdAddObject( pointLight ) );
+		var cmd = new CmdSetColor( pointLight, 'color', green );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setColor";
+	};
+
+	var setGeometry = function () {
+
+		var box = aBox( 'Guinea Pig' ); // default ( 100, 100, 100, 1, 1, 1 )
+		var boxGeometry = { geometry: { parameters: { width: 200, height: 201, depth: 202, widthSegments: 2, heightSegments: 3, depthSegments: 4 } } };
+
+		editor.execute( new CmdAddObject( box ) );
+
+		var cmd = new CmdSetGeometry( box, getGeometry( "BoxGeometry", boxGeometry ) );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setGeometry";
+	};
+
+
+	var setGeometryValue = function() {
+
+		var box = aBox( 'Geometry Value Box' );
+		editor.execute( new CmdAddObject( box ) );
+
+		cmd = new CmdSetGeometryValue( box, 'uuid', THREE.Math.generateUUID() );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setGeometryValue";
+	};
+
+	var setMaterial = function () {
+
+		var sphere = aSphere( 'The Sun' );
+		editor.execute( new CmdAddObject( sphere ) );
+
+		var material = new THREE[ 'MeshPhongMaterial' ]();
+		var cmd = new CmdSetMaterial( sphere, material );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setMaterial";
+	};
+
+	var setMaterialColor = function () {
+
+		var box = aBox( 'Box with colored material' );
+		editor.execute( new CmdAddObject( box ) );
+
+		var cmd = new CmdSetMaterialColor( box, 'color', green );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setMaterialColor";
+	};
+
+	var setMaterialMap = function () {
+
+		var sphere = aSphere( 'Sphere with texture' );
+		editor.execute( new CmdAddObject( sphere ) );
+
+		// dirt.png
+		var data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2L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J0nSMazY1fxHyriXxV584tmEedcfGNrA/5cmK8AAwCT9ATehDDyzwAAAABJRU5ErkJggg==';
+		var img = new Image();
+		img.src = data;
+
+		var texture = new THREE.Texture( img, 'map' );
+		texture.sourceFile = 'dirt.png';
+
+		var cmd = new CmdSetMaterialMap( sphere, 'map', texture );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setMaterialMap";
+	};
+
+	var setMaterialValue = function () {
+
+		var box = aBox( 'Box with values' );
+		editor.execute( new CmdAddObject( box ) );
+
+		var cmd = new CmdSetMaterialValue( box, 'name', 'Bravo' );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setMaterialValue";
+	};
+
+	var setPosition = function () {
+
+		var sphere = aSphere( 'Sphere with position' );
+		editor.execute( new CmdAddObject( sphere ) );
+
+		var newPosition = new THREE.Vector3( 101, 202, 303 );
+		var cmd = new CmdSetPosition( sphere, newPosition );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setPosition";
+	};
+
+	var setRotation = function () {
+
+		var box = aBox( 'Box with rotation' );
+		editor.execute( new CmdAddObject( box ) );
+
+		var newRotation = new THREE.Euler( 0.3, -1.7, 2 );
+		var cmd = new CmdSetRotation( box, newRotation );
+		cmd.updatable = false;
+		editor.execute ( cmd );
+
+		return "setRotation";
+
+	};
+
+	var setScale = function () {
+
+		var sphere = aSphere( 'Sphere with scale' );
+		editor.execute( new CmdAddObject( sphere) );
+
+		var newScale = new THREE.Vector3( 1.2, 3.3, 4.6 );
+		var cmd = new CmdSetScale( sphere, newScale );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setScale";
+	};
+
+	var setScriptValue = function () {
+
+		var box = aBox( 'Box with script' );
+		editor.execute( new CmdAddObject( box ) );
+		var script = { name: "Alert", source: "alert( null );" };
+		editor.execute( new CmdAddScript( box, script ) );
+
+		var newScript = { name: "Console", source: "console.log( null );" };
+		var cmd = new CmdSetScriptValue( box, script, 'source', newScript.source , 0 );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setScriptValue";
+
+	};
+
+	var setUuid = function () {
+
+		var sphere = aSphere( 'Sphere with UUID' );
+		editor.execute( new CmdAddObject( sphere ) );
+
+		var cmd = new CmdSetUuid( sphere, THREE.Math.generateUUID() );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setUuid";
+	};
+
+	var setValue = function () {
+
+		var box = aBox( 'Box with value' );
+		editor.execute( new CmdAddObject( box ) );
+
+		var cmd = new CmdSetValue( box, 'intensity', 2.3 );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		return "setValue";
 	};
 	};
 
 
-	var setups = [ addObject, addScript, moveObject ];
+	var setups = [
+		addObject,
+		addScript,
+		moveObject,
+		removeScript,
+		setColor,
+		setGeometry,
+		setGeometryValue,
+		setMaterial,
+		setMaterialColor,
+		setMaterialMap,
+		setMaterialValue,
+		setPosition,
+		setRotation,
+		setScale,
+		setScriptValue,
+		setUuid,
+		setValue
+	];
 
 
 	// Forward tests
 	// Forward tests
 
 
 	for ( var i = 0; i < setups.length ; i++ ) {
 	for ( var i = 0; i < setups.length ; i++ ) {
 
 
-		setups[i]();
+		var name = setups[i]();
 
 
 		// Check for correct serialization
 		// Check for correct serialization
 
 
@@ -75,7 +292,7 @@ test( "Test Serialization (simple)", function() {
 
 
 		var history2 = JSON.stringify( editor.history.toJSON() );
 		var history2 = JSON.stringify( editor.history.toJSON() );
 
 
-		ok( history == history2 , "OK, forward serializing was successful " );
+		ok( history == history2 , "OK, forward serializing was successful for " + name );
 
 
 		editor.clear();
 		editor.clear();
 
 
@@ -85,7 +302,7 @@ test( "Test Serialization (simple)", function() {
 
 
 	for (var i = 0; i < setups.length ; i++ ) {
 	for (var i = 0; i < setups.length ; i++ ) {
 
 
-		setups[i]();
+		var name = setups[i]();
 
 
 		editor.history.goToState( 0 );
 		editor.history.goToState( 0 );
 
 
@@ -100,7 +317,7 @@ test( "Test Serialization (simple)", function() {
 
 
 		var history2 = JSON.stringify( editor.history.toJSON() );
 		var history2 = JSON.stringify( editor.history.toJSON() );
 
 
-		ok( history == history2 , "OK, backward serializing was successful " );
+		ok( history == history2 , "OK, backward serializing was successful for " + name );
 
 
 		editor.clear();
 		editor.clear();