TestSerialization.js 2.1 KB

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