TestSerialization.js 8.6 KB

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