TestSerialization.js 8.6 KB

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