Browse Source

added test for CmdSetGeometry
generalized CommonUtils.js to get Geometries

Mario Schuettel 10 years ago
parent
commit
e58ff75e6e

+ 90 - 22
test/unit/editor/CommonUtilities.js

@@ -1,38 +1,106 @@
 // module( "CommonUtilities" );
 // module( "CommonUtilities" );
 
 
-function aBox( name ) {
+function mergeParams( defaults, customParams ) {
 
 
-	var width = 100;
-	var height = 100;
-	var depth = 100;
+	if ( typeof customParams == "undefined" ) return defaults;
 
 
-	var widthSegments = 1;
-	var heightSegments = 1;
-	var depthSegments = 1;
+	var defaultKeys = Object.keys( defaults );
+	var params = {};
 
 
-	var geometry = new THREE.BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments );
-	var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial() );
-	mesh.name = name || "Box 1";
+	defaultKeys.map( function( key ) {
+		params[ key ] = customParams[ key ] || defaultKeys[ key ];
+	});
 
 
-	return mesh;
+	return params;
 
 
 }
 }
 
 
-function aSphere( name ) {
 
 
-	var width = 100;
-	var height = 100;
-	var depth = 100;
+function getParams( type, customParams ) {
 
 
-	var widthSegments = 1;
-	var heightSegments = 1;
-	var depthSegments = 1;
+	var defaults = {};
 
 
-	var geometry = new THREE.SphereGeometry( width, height, depth, widthSegments, heightSegments, depthSegments );
-	var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial() );
-	mesh.name = name || "Sphere 1";
+	switch ( type ) {
 
 
-	return mesh;
+		case "Box":
+
+			defaults = { width: 100, height: 100, depth: 100, widthSegments: 1, heightSegments: 1, depthSegments: 1 };
+			break;
+
+		case "Sphere":
+
+			defaults = { radius: 75, widthSegments: 32, heightSegments: 16, phiStart: 0, phiLength: 6.28, thetaStart: 0.00, thetaLength: 3.14 };
+			break;
+
+		default:
+
+			console.error( "Type '" + type + "' is not known while creating params" );
+			return false;
+
+	}
+
+	return mergeParams( defaults, customParams );
+
+}
+
+function getGeometry( type, customParams ) {
+
+	var params = getParams( type, customParams );
+
+	switch ( type ) {
+
+		case "Box":
+
+			return new THREE.BoxGeometry(
+				params['width'],
+				params['height'],
+				params['depth'],
+				params['widthSegments'],
+				params['heightSegments'],
+				params['depthSegments']
+			);
+
+		case "Sphere":
+
+			return new THREE.SphereGeometry(
+				params['radius'],
+				params['widthSegments'],
+				params['heightSegments'],
+				params['phiStart'],
+				params['phiLength'],
+				params['thetaStart'],
+				params['thetaLength']
+			);
+
+		default:
+
+			console.error( "Type '" + type + "' is not known while creating geometry " );
+			return false;
+
+	}
+
+}
+
+function getObject( name, type, customParams ) {
+
+	var geometry = getGeometry( type, customParams );
+
+	var object = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial() );
+	object.name = name || type + " 1";
+
+	return object;
+
+}
+
+
+function aBox( name, customParams ) {
+
+	return getObject( name, "Box", customParams );
+}
+
+function aSphere( name, customParams ) {
+
+	return getObject( name, "Sphere", customParams );
 
 
 }
 }
 
 

+ 61 - 0
test/unit/editor/TestCmdSetGeometry.js

@@ -0,0 +1,61 @@
+module( "CmdSetGeometry" );
+
+test( "Test CmdSetGeometry", function() {
+
+	var editor = new Editor();
+
+	// initialize objects and geometries
+	var box = aBox( 'Guinea Pig' ); // default ( 100, 100, 100, 1, 1, 1 )
+	var boxGeometry1 = { width: 200, height: 201, depth: 202, widthSegments: 2, heightSegments: 3, depthSegments: 4 };
+	var boxGeometry2 = { width:  50, height:  51, depth:  52, widthSegments: 7, heightSegments: 8, depthSegments: 9 };
+	var geometryParams = [ boxGeometry1, boxGeometry2 ];
+
+	// add the object
+	var cmd = new CmdAddObject( box );
+	cmd.updatable = false;
+	editor.execute( cmd );
+
+	for ( var i = 0; i < geometryParams.length; i++ ) {
+
+		var cmd = new CmdSetGeometry( box, new THREE.BoxGeometry(
+			geometryParams[i]['width'],
+			geometryParams[i]['height'],
+			geometryParams[i]['depth'],
+			geometryParams[i]['widthSegments'],
+			geometryParams[i]['heightSegments'],
+			geometryParams[i]['depthSegments']
+		) );
+		cmd.updatable = false;
+		editor.execute( cmd );
+
+		var params = box.geometry.parameters;
+
+		ok( params.width == geometryParams[i]['width'], "OK, box width matches the corresponding value from boxGeometry"  + ( i + 1 ) );
+		ok( params.height == geometryParams[i]['height'], "OK, box height matches the corresponding value from boxGeometry" + ( i + 1 ) );
+		ok( params.depth == geometryParams[i]['depth'], "OK, box depth matches the corresponding value from boxGeometry"  + ( i + 1 ) );
+		ok( params.widthSegments == geometryParams[i]['widthSegments'], "OK, box widthSegments matches the corresponding value from boxGeometry"  + ( i + 1 ) );
+		ok( params.heightSegments == geometryParams[i]['heightSegments'], "OK, box heightSegments matches the corresponding value from boxGeometry"  + ( i + 1 ) );
+		ok( params.depthSegments == geometryParams[i]['depthSegments'], "OK, box depthSegments matches the corresponding value from boxGeometry"  + ( i + 1 ) );
+
+	}
+
+	editor.undo();
+	var params = box.geometry.parameters;
+	ok( params.width == geometryParams[0]['width'], "OK, box width matches the corresponding value from boxGeometry1 (after undo)");
+	ok( params.height == geometryParams[0]['height'], "OK, box height matches the corresponding value from boxGeometry1 (after undo)");
+	ok( params.depth == geometryParams[0]['depth'], "OK, box depth matches the corresponding value from boxGeometry1 (after undo)");
+	ok( params.widthSegments == geometryParams[0]['widthSegments'], "OK, box widthSegments matches the corresponding value from boxGeometry1 (after undo)");
+	ok( params.heightSegments == geometryParams[0]['heightSegments'], "OK, box heightSegments matches the corresponding value from boxGeometry1 (after undo)");
+	ok( params.depthSegments == geometryParams[0]['depthSegments'], "OK, box depthSegments matches the corresponding value from boxGeometry1 (after undo)");
+
+	editor.redo();
+	var params = box.geometry.parameters;
+	ok( params.width == geometryParams[1]['width'], "OK, box width matches the corresponding value from boxGeometry2 (after redo)");
+	ok( params.height == geometryParams[1]['height'], "OK, box height matches the corresponding value from boxGeometry2 (after redo)");
+	ok( params.depth == geometryParams[1]['depth'], "OK, box depth matches the corresponding value from boxGeometry2 (after redo)");
+	ok( params.widthSegments == geometryParams[1]['widthSegments'], "OK, box widthSegments matches the corresponding value from boxGeometry2 (after redo)");
+	ok( params.heightSegments == geometryParams[1]['heightSegments'], "OK, box heightSegments matches the corresponding value from boxGeometry2 (after redo)");
+	ok( params.depthSegments == geometryParams[1]['depthSegments'], "OK, box depthSegments matches the corresponding value from boxGeometry2 (after redo)");
+
+
+});

+ 2 - 0
test/unit/unittests_editor.html

@@ -77,6 +77,7 @@
 <script src="../../editor/js/CmdRemoveObject.js"></script>
 <script src="../../editor/js/CmdRemoveObject.js"></script>
 <script src="../../editor/js/CmdRemoveScript.js"></script>
 <script src="../../editor/js/CmdRemoveScript.js"></script>
 <script src="../../editor/js/CmdSetColor.js"></script>
 <script src="../../editor/js/CmdSetColor.js"></script>
+<script src="../../editor/js/CmdSetGeometry.js"></script>
 <script src="../../editor/js/CmdSetPosition.js"></script>
 <script src="../../editor/js/CmdSetPosition.js"></script>
 <script src="../../editor/js/CmdSetRotation.js"></script>
 <script src="../../editor/js/CmdSetRotation.js"></script>
 <script src="../../editor/js/CmdSetScale.js"></script>
 <script src="../../editor/js/CmdSetScale.js"></script>
@@ -97,6 +98,7 @@
 <script src="editor/TestCmdRemoveObject.js"></script>
 <script src="editor/TestCmdRemoveObject.js"></script>
 <script src="editor/TestCmdRemoveScript.js"></script>
 <script src="editor/TestCmdRemoveScript.js"></script>
 <script src="editor/TestCmdSetColor.js"></script>
 <script src="editor/TestCmdSetColor.js"></script>
+<script src="editor/TestCmdSetGeometry.js"></script>
 <script src="editor/TestCmdSetPosition.js"></script>
 <script src="editor/TestCmdSetPosition.js"></script>
 <script src="editor/TestCmdSetRotation.js"></script>
 <script src="editor/TestCmdSetRotation.js"></script>
 <script src="editor/TestCmdSetScale.js"></script>
 <script src="editor/TestCmdSetScale.js"></script>