TestCmdAddScript.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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( "CmdAddScript" );
  6. test( "Test CmdAddScript (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. var scriptsKeys = Object.keys( editor.scripts );
  27. ok( getScriptCount( editor ) == scripts.length, "OK, correct number of scripts have been added" );
  28. for ( var i = 0; i < objects.length; i ++ ) {
  29. ok( objects[ i ].uuid == scriptsKeys[ i ], "OK, script key #" + i + " matches the object's UUID" );
  30. }
  31. editor.undo();
  32. ok( getScriptCount( editor ) == scripts.length - 1, "OK, one script has been removed by undo" );
  33. editor.redo();
  34. ok( getScriptCount( editor ) == scripts.length, "OK, one script has been added again by redo" );
  35. for ( var i = 0; i < scriptsKeys.length; i ++ ) {
  36. ok( editor.scripts[ scriptsKeys[ i ] ][ 0 ] == scripts[ i ], "OK, script #" + i + " is still assigned correctly" );
  37. }
  38. } );