TestCmdSerialization.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. module( "Serialization" );
  2. test( "Test Serialization (simple)", function() {
  3. // setup
  4. var editor = new Editor();
  5. var box = aBox( 'The Box' );
  6. var light = aPointlight( 'The PointLight' );
  7. var camera = aPerspectiveCamera( 'The Camera' );
  8. var addObject = function () {
  9. // Test Add
  10. var cmd = new CmdAddObject( box );
  11. cmd.updatable = false;
  12. editor.execute( cmd );
  13. };
  14. var addScript = function () {
  15. // Test Add
  16. var cmd = new CmdAddObject( box );
  17. editor.execute( cmd );
  18. var cmd = new CmdAddScript( box, { "name":"test","source":"console.log(\"hello world\");" } );
  19. cmd.updatable = false;
  20. editor.execute( cmd );
  21. };
  22. var moveObject = function () {
  23. // create some objects
  24. var anakinsName = 'Anakin Skywalker';
  25. var lukesName = 'Luke Skywalker';
  26. var anakinSkywalker = aSphere( anakinsName );
  27. var lukeSkywalker = aBox( lukesName );
  28. editor.execute( new CmdAddObject( anakinSkywalker ) );
  29. editor.execute( new CmdAddObject( lukeSkywalker ) );
  30. // Tell Luke, Anakin is his father
  31. editor.execute( new CmdMoveObject( lukeSkywalker, anakinSkywalker ) );
  32. };
  33. var functions = [ addObject, addScript, moveObject ];
  34. // Forward tests
  35. for (var i = 0; i < functions.length ; i++ ) {
  36. functions[i]();
  37. // Check for correct serialization
  38. var history = JSON.stringify( editor.history.toJSON() );
  39. editor.history.clear();
  40. editor.history.fromJSON( JSON.parse( history ) );
  41. editor.history.goToState( 0 );
  42. editor.history.goToState( 1000 );
  43. var history2 = JSON.stringify( editor.history.toJSON() );
  44. ok( history == history2 , "OK, forward serializing was successful " );
  45. editor.clear();
  46. }
  47. // Backward tests
  48. for (var i = 0; i < functions.length ; i++ ) {
  49. functions[i]();
  50. editor.history.goToState( 0 );
  51. var history = JSON.stringify( editor.history.toJSON() );
  52. editor.history.clear();
  53. editor.history.fromJSON( JSON.parse( history ) );
  54. editor.history.goToState( 1000 );
  55. editor.history.goToState( 0 );
  56. var history2 = JSON.stringify( editor.history.toJSON() );
  57. ok( history == history2 , "OK, backward serializing was successful " );
  58. editor.clear();
  59. }
  60. });