TestSerialization.js 8.8 KB

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