SetScriptValueCommand.tests.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. QUnit.module( "SetScriptValueCommand" );
  2. QUnit.test( "Test SetScriptValueCommand for source (Undo and Redo)", function( assert ) {
  3. // setup
  4. var editor = new Editor();
  5. var box = aBox( "The scripted box" );
  6. var cmd = new AddObjectCommand( box );
  7. cmd.updatable = false;
  8. editor.execute( cmd );
  9. var translateScript = { name: "Translate", source: "function( update ) {}" };
  10. cmd = new AddScriptCommand( box, translateScript );
  11. cmd.updatable = false;
  12. editor.execute( cmd );
  13. var testSourceData = [
  14. { name: "Translate", source: "function update( event ) { this.position.x = this.position.x + 1; }" },
  15. { name: "Translate", source: "function update( event ) { this.position.y = this.position.y + 1; }" },
  16. { name: "Translate", source: "function update( event ) { this.position.z = this.position.z + 1; }" }
  17. ];
  18. // test source
  19. testSourceData.map( function( script ) {
  20. var cmd = new SetScriptValueCommand( box, translateScript, 'source', script.source, 0 );
  21. cmd.updatable = false;
  22. editor.execute( cmd );
  23. } );
  24. var length = testSourceData.length;
  25. assert.ok( editor.scripts[ box.uuid ][ 0 ][ 'source' ] == testSourceData[ length - 1 ].source,
  26. "OK, 'source' was set correctly to the last value (expected: '" + testSourceData[ length - 1 ].source + "', actual: '" + editor.scripts[ box.uuid ][ 0 ][ 'source' ] + "')" );
  27. editor.undo();
  28. assert.ok( editor.scripts[ box.uuid ][ 0 ][ 'source' ] == testSourceData[ length - 2 ].source,
  29. "OK, 'source' was set correctly to the second to the last value after undo (expected: '" + testSourceData[ length - 2 ].source + "', actual: '" + editor.scripts[ box.uuid ][ 0 ][ 'source' ] + "')" );
  30. editor.redo();
  31. assert.ok( editor.scripts[ box.uuid ][ 0 ][ 'source' ] == testSourceData[ length - 1 ].source,
  32. "OK, 'source' was set correctly to the last value again after redo (expected: '" + testSourceData[ length - 1 ].source + "', actual: '" + editor.scripts[ box.uuid ][ 0 ][ 'source' ] + "')" );
  33. var names = [ "X Script", "Y Script", "Z Script" ];
  34. names.map( function( name ) {
  35. cmd = new SetScriptValueCommand( box, translateScript, 'name', name );
  36. cmd.updatable = false;
  37. editor.execute( cmd );
  38. } );
  39. var scriptName = editor.scripts[ box.uuid ][ 0 ][ "name" ];
  40. assert.ok( scriptName == names[ names.length - 1 ], "OK, the script name corresponds to the last script name that was assigned" );
  41. editor.undo();
  42. scriptName = editor.scripts[ box.uuid ][ 0 ][ "name" ];
  43. assert.ok( scriptName == names[ names.length - 2 ], "OK, the script name corresponds to the second last script name that was assigned" );
  44. editor.redo();
  45. scriptName = editor.scripts[ box.uuid ][ 0 ][ "name" ];
  46. assert.ok( scriptName == names[ names.length - 1 ], "OK, the script name corresponds to the last script name that was assigned, again" );
  47. } );