TestSerialization.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. module( "Serialization" );
  2. test( "Test Serialization", function() {
  3. // setup
  4. var editor = new Editor();
  5. var green = 12581843; // bffbd3
  6. var addObject = function () {
  7. // setup
  8. var box = aBox( 'The Box' );
  9. // Test Add
  10. var cmd = new CmdAddObject( box );
  11. cmd.updatable = false;
  12. editor.execute( cmd );
  13. return "addObject";
  14. };
  15. var addScript = function () {
  16. // setup
  17. var box = aBox( 'The Box' );
  18. // Test Add
  19. var cmd = new CmdAddObject( box );
  20. editor.execute( cmd );
  21. var cmd = new CmdAddScript( box, { "name":"test","source":"console.log(\"hello world\");" } );
  22. cmd.updatable = false;
  23. editor.execute( cmd );
  24. return "addScript";
  25. };
  26. var moveObject = function () {
  27. // create some objects
  28. var anakinsName = 'Anakin Skywalker';
  29. var lukesName = 'Luke Skywalker';
  30. var anakinSkywalker = aSphere( anakinsName );
  31. var lukeSkywalker = aBox( lukesName );
  32. editor.execute( new CmdAddObject( anakinSkywalker ) );
  33. editor.execute( new CmdAddObject( lukeSkywalker ) );
  34. // Tell Luke, Anakin is his father
  35. editor.execute( new CmdMoveObject( lukeSkywalker, anakinSkywalker ) );
  36. return "moveObject";
  37. };
  38. var removeScript = function () {
  39. var box = aBox( 'Box with no script' );
  40. editor.execute( new CmdAddObject( box ) );
  41. var script = { "name":"test","source":"console.log(\"hello world\");" } ;
  42. var cmd = new CmdAddScript( box, script );
  43. cmd.updatable = false;
  44. editor.execute( cmd );
  45. cmd = new CmdRemoveScript( box, script );
  46. editor.execute( cmd );
  47. return "removeScript";
  48. };
  49. var setColor = function () {
  50. var pointLight = aPointlight( "The light Light" );
  51. editor.execute( new CmdAddObject( pointLight ) );
  52. var cmd = new CmdSetColor( pointLight, 'color', green );
  53. cmd.updatable = false;
  54. editor.execute( cmd );
  55. return "setColor";
  56. };
  57. var setGeometry = function () {
  58. var box = aBox( 'Guinea Pig' ); // default ( 100, 100, 100, 1, 1, 1 )
  59. var boxGeometry = { geometry: { parameters: { width: 200, height: 201, depth: 202, widthSegments: 2, heightSegments: 3, depthSegments: 4 } } };
  60. editor.execute( new CmdAddObject( box ) );
  61. var cmd = new CmdSetGeometry( box, getGeometry( "BoxGeometry", boxGeometry ) );
  62. cmd.updatable = false;
  63. editor.execute( cmd );
  64. return "setGeometry";
  65. };
  66. var setGeometryValue = function() {
  67. var box = aBox( 'Geometry Value Box' );
  68. editor.execute( new CmdAddObject( box ) );
  69. cmd = new CmdSetGeometryValue( box, 'uuid', THREE.Math.generateUUID() );
  70. cmd.updatable = false;
  71. editor.execute( cmd );
  72. return "setGeometryValue";
  73. };
  74. var setMaterial = function () {
  75. var sphere = aSphere( 'The Sun' );
  76. editor.execute( new CmdAddObject( sphere ) );
  77. var material = new THREE[ 'MeshPhongMaterial' ]();
  78. var cmd = new CmdSetMaterial( sphere, material );
  79. cmd.updatable = false;
  80. editor.execute( cmd );
  81. return "setMaterial";
  82. };
  83. var setMaterialColor = function () {
  84. var box = aBox( 'Box with colored material' );
  85. editor.execute( new CmdAddObject( box ) );
  86. var cmd = new CmdSetMaterialColor( box, 'color', green );
  87. cmd.updatable = false;
  88. editor.execute( cmd );
  89. return "setMaterialColor";
  90. };
  91. var setMaterialMap = function () {
  92. var sphere = aSphere( 'Sphere with texture' );
  93. editor.execute( new CmdAddObject( sphere ) );
  94. // dirt.png
  95. var data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoL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Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+txizaQAAABVQTFRFh4eHbGxsdFhEWT0puYVclmxKeVU6ppwr+AAAAHtJREFUeNosjgEWBCEIQplFuP+RB5h9lZn2EZxkLzC3D1YSgSlmk7i0ctzDZNBz/VSoX1KwjlFI8WmA2R7JqUa0LJJcd1rLNWRRaMyi+3Y16qMKHhdE48XLsDyHKJ0nSMazY1fxHyriXxV584tmEedcfGNrA/5cmK8AAwCT9ATehDDyzwAAAABJRU5ErkJggg==';
  96. var img = new Image();
  97. img.src = data;
  98. var texture = new THREE.Texture( img, 'map' );
  99. texture.sourceFile = 'dirt.png';
  100. var cmd = new CmdSetMaterialMap( sphere, 'map', texture );
  101. cmd.updatable = false;
  102. editor.execute( cmd );
  103. return "setMaterialMap";
  104. };
  105. var setMaterialValue = function () {
  106. var box = aBox( 'Box with values' );
  107. editor.execute( new CmdAddObject( box ) );
  108. var cmd = new CmdSetMaterialValue( box, 'name', 'Bravo' );
  109. cmd.updatable = false;
  110. editor.execute( cmd );
  111. return "setMaterialValue";
  112. };
  113. var setPosition = function () {
  114. var sphere = aSphere( 'Sphere with position' );
  115. editor.execute( new CmdAddObject( sphere ) );
  116. var newPosition = new THREE.Vector3( 101, 202, 303 );
  117. var cmd = new CmdSetPosition( sphere, newPosition );
  118. cmd.updatable = false;
  119. editor.execute( cmd );
  120. return "setPosition";
  121. };
  122. var setRotation = function () {
  123. var box = aBox( 'Box with rotation' );
  124. editor.execute( new CmdAddObject( box ) );
  125. var newRotation = new THREE.Euler( 0.3, -1.7, 2 );
  126. var cmd = new CmdSetRotation( box, newRotation );
  127. cmd.updatable = false;
  128. editor.execute ( cmd );
  129. return "setRotation";
  130. };
  131. var setScale = function () {
  132. var sphere = aSphere( 'Sphere with scale' );
  133. editor.execute( new CmdAddObject( sphere) );
  134. var newScale = new THREE.Vector3( 1.2, 3.3, 4.6 );
  135. var cmd = new CmdSetScale( sphere, newScale );
  136. cmd.updatable = false;
  137. editor.execute( cmd );
  138. return "setScale";
  139. };
  140. var setScriptValue = function () {
  141. var box = aBox( 'Box with script' );
  142. editor.execute( new CmdAddObject( box ) );
  143. var script = { name: "Alert", source: "alert( null );" };
  144. editor.execute( new CmdAddScript( box, script ) );
  145. var newScript = { name: "Console", source: "console.log( null );" };
  146. var cmd = new CmdSetScriptValue( box, script, 'source', newScript.source , 0 );
  147. cmd.updatable = false;
  148. editor.execute( cmd );
  149. return "setScriptValue";
  150. };
  151. var setUuid = function () {
  152. var sphere = aSphere( 'Sphere with UUID' );
  153. editor.execute( new CmdAddObject( sphere ) );
  154. var cmd = new CmdSetUuid( sphere, THREE.Math.generateUUID() );
  155. cmd.updatable = false;
  156. editor.execute( cmd );
  157. return "setUuid";
  158. };
  159. var setValue = function () {
  160. var box = aBox( 'Box with value' );
  161. editor.execute( new CmdAddObject( box ) );
  162. var cmd = new CmdSetValue( box, 'intensity', 2.3 );
  163. cmd.updatable = false;
  164. editor.execute( cmd );
  165. return "setValue";
  166. };
  167. var setups = [
  168. addObject,
  169. addScript,
  170. moveObject,
  171. removeScript,
  172. setColor,
  173. setGeometry,
  174. setGeometryValue,
  175. setMaterial,
  176. setMaterialColor,
  177. setMaterialMap,
  178. setMaterialValue,
  179. setPosition,
  180. setRotation,
  181. setScale,
  182. setScriptValue,
  183. setUuid,
  184. setValue
  185. ];
  186. // Forward tests
  187. for ( var i = 0; i < setups.length ; i++ ) {
  188. var name = setups[i]();
  189. // Check for correct serialization
  190. editor.history.goToState( 0 );
  191. editor.history.goToState( 1000 );
  192. var history = JSON.stringify( editor.history.toJSON() );
  193. editor.history.clear();
  194. editor.history.fromJSON( JSON.parse( history ) );
  195. editor.history.goToState( 0 );
  196. editor.history.goToState( 1000 );
  197. var history2 = JSON.stringify( editor.history.toJSON() );
  198. ok( history == history2 , "OK, forward serializing was successful for " + name );
  199. editor.clear();
  200. }
  201. // Backward tests
  202. for (var i = 0; i < setups.length ; i++ ) {
  203. var name = setups[i]();
  204. editor.history.goToState( 0 );
  205. var history = JSON.stringify( editor.history.toJSON() );
  206. editor.history.clear();
  207. editor.history.fromJSON( JSON.parse( history ) );
  208. editor.history.goToState( 1000 );
  209. editor.history.goToState( 0 );
  210. var history2 = JSON.stringify( editor.history.toJSON() );
  211. ok( history == history2 , "OK, backward serializing was successful for " + name );
  212. editor.clear();
  213. }
  214. });