forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSerialization.js
More file actions
executable file
·358 lines (225 loc) · 8.94 KB
/
TestSerialization.js
File metadata and controls
executable file
·358 lines (225 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* @author lxxxvi / https://github.com/lxxxvi
* Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
*/
module( "Serialization" );
test( "Test Serialization", function( assert ) {
// setup
var editor = new Editor();
var done = assert.async();
editor.storage.init( function () {
performTests();
done(); // continue running other tests
} );
var green = 12581843; // bffbd3
var addObject = function () {
// setup
var box = aBox( 'The Box' );
// Test Add
var cmd = new AddObjectCommand( box );
cmd.updatable = false;
editor.execute( cmd );
return "addObject";
};
var addScript = function () {
// setup
var box = aBox( 'The Box' );
// Test Add
var cmd = new AddObjectCommand( box );
editor.execute( cmd );
var cmd = new AddScriptCommand( box, { "name": "test", "source": "console.log(\"hello world\");" } );
cmd.updatable = false;
editor.execute( cmd );
return "addScript";
};
var moveObject = function () {
// create some objects
var anakinsName = 'Anakin Skywalker';
var lukesName = 'Luke Skywalker';
var anakinSkywalker = aSphere( anakinsName );
var lukeSkywalker = aBox( lukesName );
editor.execute( new AddObjectCommand( anakinSkywalker ) );
editor.execute( new AddObjectCommand( lukeSkywalker ) );
// Tell Luke, Anakin is his father
editor.execute( new MoveObjectCommand( lukeSkywalker, anakinSkywalker ) );
return "moveObject";
};
var removeScript = function () {
var box = aBox( 'Box with no script' );
editor.execute( new AddObjectCommand( box ) );
var script = { "name": "test", "source": "console.log(\"hello world\");" } ;
var cmd = new AddScriptCommand( box, script );
cmd.updatable = false;
editor.execute( cmd );
cmd = new RemoveScriptCommand( box, script );
editor.execute( cmd );
return "removeScript";
};
var setColor = function () {
var pointLight = aPointlight( "The light Light" );
editor.execute( new AddObjectCommand( pointLight ) );
var cmd = new SetColorCommand( 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 AddObjectCommand( box ) );
var cmd = new SetGeometryCommand( box, getGeometry( "BoxGeometry", boxGeometry ) );
cmd.updatable = false;
editor.execute( cmd );
return "setGeometry";
};
var setGeometryValue = function() {
var box = aBox( 'Geometry Value Box' );
editor.execute( new AddObjectCommand( box ) );
cmd = new SetGeometryValueCommand( box, 'uuid', THREE.Math.generateUUID() );
cmd.updatable = false;
editor.execute( cmd );
return "setGeometryValue";
};
var setMaterial = function () {
var sphere = aSphere( 'The Sun' );
editor.execute( new AddObjectCommand( sphere ) );
var material = new THREE[ 'MeshPhongMaterial' ]();
var cmd = new SetMaterialCommand( sphere, material );
cmd.updatable = false;
editor.execute( cmd );
return "setMaterial";
};
var setMaterialColor = function () {
var box = aBox( 'Box with colored material' );
editor.execute( new AddObjectCommand( box ) );
var cmd = new SetMaterialColorCommand( box, 'color', green );
cmd.updatable = false;
editor.execute( cmd );
return "setMaterialColor";
};
var setMaterialMap = function () {
var sphere = aSphere( 'Sphere with texture' );
editor.execute( new AddObjectCommand( sphere ) );
// dirt.png
var data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoTWFjaW50b3NoKSIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpDMjYxMEI4MzVENDMxMUU1OTdEQUY4QkNGNUVENjg4MyIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpDMjYxMEI4NDVENDMxMUU1OTdEQUY4QkNGNUVENjg4MyI+IDx4bXBNTTpEZXJpdmVkRnJvbSBzdFJlZjppbnN0YW5jZUlEPSJ4bXAuaWlkOkMyNjEwQjgxNUQ0MzExRTU5N0RBRjhCQ0Y1RUQ2ODgzIiBzdFJlZjpkb2N1bWVudElEPSJ4bXAuZGlkOkMyNjEwQjgyNUQ0MzExRTU5N0RBRjhCQ0Y1RUQ2ODgzIi8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+txizaQAAABVQTFRFh4eHbGxsdFhEWT0puYVclmxKeVU6ppwr+AAAAHtJREFUeNosjgEWBCEIQplFuP+RB5h9lZn2EZxkLzC3D1YSgSlmk7i0ctzDZNBz/VSoX1KwjlFI8WmA2R7JqUa0LJJcd1rLNWRRaMyi+3Y16qMKHhdE48XLsDyHKJ0nSMazY1fxHyriXxV584tmEedcfGNrA/5cmK8AAwCT9ATehDDyzwAAAABJRU5ErkJggg==';
var img = new Image();
img.src = data;
var texture = new THREE.Texture( img, 'map' );
texture.sourceFile = 'dirt.png';
var cmd = new SetMaterialMapCommand( sphere, 'map', texture );
cmd.updatable = false;
editor.execute( cmd );
return "setMaterialMap";
};
var setMaterialValue = function () {
var box = aBox( 'Box with values' );
editor.execute( new AddObjectCommand( box ) );
var cmd = new SetMaterialValueCommand( box, 'name', 'Bravo' );
cmd.updatable = false;
editor.execute( cmd );
return "setMaterialValue";
};
var setPosition = function () {
var sphere = aSphere( 'Sphere with position' );
editor.execute( new AddObjectCommand( sphere ) );
var newPosition = new THREE.Vector3( 101, 202, 303 );
var cmd = new SetPositionCommand( sphere, newPosition );
cmd.updatable = false;
editor.execute( cmd );
return "setPosition";
};
var setRotation = function () {
var box = aBox( 'Box with rotation' );
editor.execute( new AddObjectCommand( box ) );
var newRotation = new THREE.Euler( 0.3, - 1.7, 2 );
var cmd = new SetRotationCommand( box, newRotation );
cmd.updatable = false;
editor.execute ( cmd );
return "setRotation";
};
var setScale = function () {
var sphere = aSphere( 'Sphere with scale' );
editor.execute( new AddObjectCommand( sphere ) );
var newScale = new THREE.Vector3( 1.2, 3.3, 4.6 );
var cmd = new SetScaleCommand( sphere, newScale );
cmd.updatable = false;
editor.execute( cmd );
return "setScale";
};
var setScriptValue = function () {
var box = aBox( 'Box with script' );
editor.execute( new AddObjectCommand( box ) );
var script = { name: "Alert", source: "alert( null );" };
editor.execute( new AddScriptCommand( box, script ) );
var newScript = { name: "Console", source: "console.log( null );" };
var cmd = new SetScriptValueCommand( 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 AddObjectCommand( sphere ) );
var cmd = new SetUuidCommand( sphere, THREE.Math.generateUUID() );
cmd.updatable = false;
editor.execute( cmd );
return "setUuid";
};
var setValue = function () {
var box = aBox( 'Box with value' );
editor.execute( new AddObjectCommand( box ) );
var cmd = new SetValueCommand( box, 'intensity', 2.3 );
cmd.updatable = false;
editor.execute( cmd );
return "setValue";
};
var setups = [
addObject,
addScript,
moveObject,
removeScript,
setColor,
setGeometry,
setGeometryValue,
setMaterial,
setMaterialColor,
setMaterialMap,
setMaterialValue,
setPosition,
setRotation,
setScale,
setScriptValue,
setUuid,
setValue
];
function performTests() {
// Forward tests
for ( var i = 0; i < setups.length ; i ++ ) {
var name = setups[ i ]();
// Check for correct serialization
editor.history.goToState( 0 );
editor.history.goToState( 1000 );
var history = JSON.stringify( editor.history.toJSON() );
editor.history.clear();
editor.history.fromJSON( JSON.parse( history ) );
editor.history.goToState( 0 );
editor.history.goToState( 1000 );
var history2 = JSON.stringify( editor.history.toJSON() );
ok( history == history2, "OK, forward serializing was successful for " + name );
editor.clear();
}
// Backward tests
for ( var i = 0; i < setups.length ; i ++ ) {
var name = setups[ i ]();
editor.history.goToState( 0 );
var history = JSON.stringify( editor.history.toJSON() );
editor.history.clear();
editor.history.fromJSON( JSON.parse( history ) );
editor.history.goToState( 1000 );
editor.history.goToState( 0 );
var history2 = JSON.stringify( editor.history.toJSON() );
ok( history == history2, "OK, backward serializing was successful for " + name );
editor.clear();
}
}
} );