TestCmdRemoveScript.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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( "CmdRemoveScript" );
  6. test( "Test CmdRemoveScript (Undo and Redo)", function() {
  7. var editor = new Editor();
  8. // prepare
  9. var box = aBox( "The scripted box" );
  10. var sphere = aSphere( "The scripted sphere" );
  11. var objects = [ box, sphere ];
  12. var xMove = { name: "", source: "function update( event ) { this.position.x = this.position.x + 1; }" };
  13. var yMove = { name: "", source: "function update( event ) { this.position.y = this.position.y + 1; }" };
  14. var scripts = [ xMove, yMove ];
  15. // add objects to editor
  16. objects.map( function( item ) {
  17. editor.execute( new CmdAddObject( item ) );
  18. } );
  19. ok( editor.scene.children.length == 2, "OK, the box and the sphere have been added" );
  20. // add scripts to the objects
  21. for ( var i = 0; i < scripts.length; i ++ ) {
  22. var cmd = new CmdAddScript( objects[ i ], scripts[ i ] );
  23. cmd.updatable = false;
  24. editor.execute( cmd );
  25. }
  26. for ( var i = 0; i < scripts.length; i ++ ) {
  27. var cmd = new CmdRemoveScript( objects[ i ], scripts[ i ] );
  28. cmd.updatable = false;
  29. editor.execute( cmd );
  30. }
  31. ok( getScriptCount( editor ) == 0, "OK, all scripts have been removed" );
  32. scripts.map( function() {
  33. editor.undo();
  34. } );
  35. ok( getScriptCount( editor ) == scripts.length, "OK, all scripts have been added again by undo(s)" );
  36. var scriptsKeys = Object.keys( editor.scripts );
  37. for ( var i = 0; i < scriptsKeys.length; i ++ ) {
  38. ok( editor.scripts[ scriptsKeys[ i ] ][ 0 ] == scripts[ i ], "OK, script #" + i + " is still assigned correctly" );
  39. }
  40. editor.redo();
  41. ok( getScriptCount( editor ) == scripts.length - 1, "OK, one script has been removed again by redo" );
  42. } );