Skip to content

Commit e58ff75

Browse files
committed
added test for CmdSetGeometry
generalized CommonUtils.js to get Geometries
1 parent 472ed08 commit e58ff75

3 files changed

Lines changed: 153 additions & 22 deletions

File tree

test/unit/editor/CommonUtilities.js

Lines changed: 90 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,106 @@
11
// module( "CommonUtilities" );
22

3-
function aBox( name ) {
3+
function mergeParams( defaults, customParams ) {
44

5-
var width = 100;
6-
var height = 100;
7-
var depth = 100;
5+
if ( typeof customParams == "undefined" ) return defaults;
86

9-
var widthSegments = 1;
10-
var heightSegments = 1;
11-
var depthSegments = 1;
7+
var defaultKeys = Object.keys( defaults );
8+
var params = {};
129

13-
var geometry = new THREE.BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments );
14-
var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial() );
15-
mesh.name = name || "Box 1";
10+
defaultKeys.map( function( key ) {
11+
params[ key ] = customParams[ key ] || defaultKeys[ key ];
12+
});
1613

17-
return mesh;
14+
return params;
1815

1916
}
2017

21-
function aSphere( name ) {
2218

23-
var width = 100;
24-
var height = 100;
25-
var depth = 100;
19+
function getParams( type, customParams ) {
2620

27-
var widthSegments = 1;
28-
var heightSegments = 1;
29-
var depthSegments = 1;
21+
var defaults = {};
3022

31-
var geometry = new THREE.SphereGeometry( width, height, depth, widthSegments, heightSegments, depthSegments );
32-
var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial() );
33-
mesh.name = name || "Sphere 1";
23+
switch ( type ) {
3424

35-
return mesh;
25+
case "Box":
26+
27+
defaults = { width: 100, height: 100, depth: 100, widthSegments: 1, heightSegments: 1, depthSegments: 1 };
28+
break;
29+
30+
case "Sphere":
31+
32+
defaults = { radius: 75, widthSegments: 32, heightSegments: 16, phiStart: 0, phiLength: 6.28, thetaStart: 0.00, thetaLength: 3.14 };
33+
break;
34+
35+
default:
36+
37+
console.error( "Type '" + type + "' is not known while creating params" );
38+
return false;
39+
40+
}
41+
42+
return mergeParams( defaults, customParams );
43+
44+
}
45+
46+
function getGeometry( type, customParams ) {
47+
48+
var params = getParams( type, customParams );
49+
50+
switch ( type ) {
51+
52+
case "Box":
53+
54+
return new THREE.BoxGeometry(
55+
params['width'],
56+
params['height'],
57+
params['depth'],
58+
params['widthSegments'],
59+
params['heightSegments'],
60+
params['depthSegments']
61+
);
62+
63+
case "Sphere":
64+
65+
return new THREE.SphereGeometry(
66+
params['radius'],
67+
params['widthSegments'],
68+
params['heightSegments'],
69+
params['phiStart'],
70+
params['phiLength'],
71+
params['thetaStart'],
72+
params['thetaLength']
73+
);
74+
75+
default:
76+
77+
console.error( "Type '" + type + "' is not known while creating geometry " );
78+
return false;
79+
80+
}
81+
82+
}
83+
84+
function getObject( name, type, customParams ) {
85+
86+
var geometry = getGeometry( type, customParams );
87+
88+
var object = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial() );
89+
object.name = name || type + " 1";
90+
91+
return object;
92+
93+
}
94+
95+
96+
function aBox( name, customParams ) {
97+
98+
return getObject( name, "Box", customParams );
99+
}
100+
101+
function aSphere( name, customParams ) {
102+
103+
return getObject( name, "Sphere", customParams );
36104

37105
}
38106

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module( "CmdSetGeometry" );
2+
3+
test( "Test CmdSetGeometry", function() {
4+
5+
var editor = new Editor();
6+
7+
// initialize objects and geometries
8+
var box = aBox( 'Guinea Pig' ); // default ( 100, 100, 100, 1, 1, 1 )
9+
var boxGeometry1 = { width: 200, height: 201, depth: 202, widthSegments: 2, heightSegments: 3, depthSegments: 4 };
10+
var boxGeometry2 = { width: 50, height: 51, depth: 52, widthSegments: 7, heightSegments: 8, depthSegments: 9 };
11+
var geometryParams = [ boxGeometry1, boxGeometry2 ];
12+
13+
// add the object
14+
var cmd = new CmdAddObject( box );
15+
cmd.updatable = false;
16+
editor.execute( cmd );
17+
18+
for ( var i = 0; i < geometryParams.length; i++ ) {
19+
20+
var cmd = new CmdSetGeometry( box, new THREE.BoxGeometry(
21+
geometryParams[i]['width'],
22+
geometryParams[i]['height'],
23+
geometryParams[i]['depth'],
24+
geometryParams[i]['widthSegments'],
25+
geometryParams[i]['heightSegments'],
26+
geometryParams[i]['depthSegments']
27+
) );
28+
cmd.updatable = false;
29+
editor.execute( cmd );
30+
31+
var params = box.geometry.parameters;
32+
33+
ok( params.width == geometryParams[i]['width'], "OK, box width matches the corresponding value from boxGeometry" + ( i + 1 ) );
34+
ok( params.height == geometryParams[i]['height'], "OK, box height matches the corresponding value from boxGeometry" + ( i + 1 ) );
35+
ok( params.depth == geometryParams[i]['depth'], "OK, box depth matches the corresponding value from boxGeometry" + ( i + 1 ) );
36+
ok( params.widthSegments == geometryParams[i]['widthSegments'], "OK, box widthSegments matches the corresponding value from boxGeometry" + ( i + 1 ) );
37+
ok( params.heightSegments == geometryParams[i]['heightSegments'], "OK, box heightSegments matches the corresponding value from boxGeometry" + ( i + 1 ) );
38+
ok( params.depthSegments == geometryParams[i]['depthSegments'], "OK, box depthSegments matches the corresponding value from boxGeometry" + ( i + 1 ) );
39+
40+
}
41+
42+
editor.undo();
43+
var params = box.geometry.parameters;
44+
ok( params.width == geometryParams[0]['width'], "OK, box width matches the corresponding value from boxGeometry1 (after undo)");
45+
ok( params.height == geometryParams[0]['height'], "OK, box height matches the corresponding value from boxGeometry1 (after undo)");
46+
ok( params.depth == geometryParams[0]['depth'], "OK, box depth matches the corresponding value from boxGeometry1 (after undo)");
47+
ok( params.widthSegments == geometryParams[0]['widthSegments'], "OK, box widthSegments matches the corresponding value from boxGeometry1 (after undo)");
48+
ok( params.heightSegments == geometryParams[0]['heightSegments'], "OK, box heightSegments matches the corresponding value from boxGeometry1 (after undo)");
49+
ok( params.depthSegments == geometryParams[0]['depthSegments'], "OK, box depthSegments matches the corresponding value from boxGeometry1 (after undo)");
50+
51+
editor.redo();
52+
var params = box.geometry.parameters;
53+
ok( params.width == geometryParams[1]['width'], "OK, box width matches the corresponding value from boxGeometry2 (after redo)");
54+
ok( params.height == geometryParams[1]['height'], "OK, box height matches the corresponding value from boxGeometry2 (after redo)");
55+
ok( params.depth == geometryParams[1]['depth'], "OK, box depth matches the corresponding value from boxGeometry2 (after redo)");
56+
ok( params.widthSegments == geometryParams[1]['widthSegments'], "OK, box widthSegments matches the corresponding value from boxGeometry2 (after redo)");
57+
ok( params.heightSegments == geometryParams[1]['heightSegments'], "OK, box heightSegments matches the corresponding value from boxGeometry2 (after redo)");
58+
ok( params.depthSegments == geometryParams[1]['depthSegments'], "OK, box depthSegments matches the corresponding value from boxGeometry2 (after redo)");
59+
60+
61+
});

test/unit/unittests_editor.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<script src="../../editor/js/CmdRemoveObject.js"></script>
7878
<script src="../../editor/js/CmdRemoveScript.js"></script>
7979
<script src="../../editor/js/CmdSetColor.js"></script>
80+
<script src="../../editor/js/CmdSetGeometry.js"></script>
8081
<script src="../../editor/js/CmdSetPosition.js"></script>
8182
<script src="../../editor/js/CmdSetRotation.js"></script>
8283
<script src="../../editor/js/CmdSetScale.js"></script>
@@ -97,6 +98,7 @@
9798
<script src="editor/TestCmdRemoveObject.js"></script>
9899
<script src="editor/TestCmdRemoveScript.js"></script>
99100
<script src="editor/TestCmdSetColor.js"></script>
101+
<script src="editor/TestCmdSetGeometry.js"></script>
100102
<script src="editor/TestCmdSetPosition.js"></script>
101103
<script src="editor/TestCmdSetRotation.js"></script>
102104
<script src="editor/TestCmdSetScale.js"></script>

0 commit comments

Comments
 (0)