diff --git a/core/field_variable.js b/core/field_variable.js index 99f061dfa..2e55faafa 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -148,7 +148,7 @@ Blockly.FieldVariable.dropdownCreate = function() { if (workspace) { // Get a copy of the list, so that adding rename and new variable options // doesn't modify the workspace's list. - var variableModelList = workspace.getVariablesOfType(''); + variableModelList = workspace.getVariablesOfType(''); for (var i = 0; i < variableModelList.length; i++){ if (createSelectedVariable && goog.string.caseInsensitiveEquals(variableModelList[i].name, name)) { diff --git a/core/variable_map.js b/core/variable_map.js index 7157467ba..0eb2a9154 100644 --- a/core/variable_map.js +++ b/core/variable_map.js @@ -211,7 +211,7 @@ Blockly.VariableMap.prototype.getVariablesOfType = function(type) { type = type || ''; var variable_list = this.variableMap_[type]; if (variable_list) { - return variable_list; + return variable_list.slice(); } return []; }; diff --git a/tests/jsunit/field_variable_test.js b/tests/jsunit/field_variable_test.js index 59c3d2675..76873e622 100644 --- a/tests/jsunit/field_variable_test.js +++ b/tests/jsunit/field_variable_test.js @@ -25,14 +25,27 @@ 'use strict'; goog.require('goog.testing'); +goog.require('goog.testing.MockControl'); +var workspace; +var mockControl_; + +function fieldVariableTestWithMocks_setUp() { + workspace = new Blockly.Workspace(); + mockControl_ = new goog.testing.MockControl(); +} + +function fieldVariableTestWithMocks_tearDown() { + mockControl_.$tearDown(); + workspace.dispose(); +} function fieldVariable_mockBlock(workspace) { return {'workspace': workspace, 'isShadow': function(){return false;}}; } function test_fieldVariable_Constructor() { - var workspace = new Blockly.Workspace(); + workspace = new Blockly.Workspace(); var fieldVariable = new Blockly.FieldVariable('name1'); assertEquals('name1', fieldVariable.getText()); workspace.dispose(); @@ -40,7 +53,7 @@ function test_fieldVariable_Constructor() { function test_fieldVariable_setValueMatchId() { // Expect the fieldVariable value to be set to variable name - var workspace = new Blockly.Workspace(); + workspace = new Blockly.Workspace(); workspace.createVariable('name2', null, 'id1'); var fieldVariable = new Blockly.FieldVariable('name1'); var mockBlock = fieldVariable_mockBlock(workspace); @@ -53,7 +66,7 @@ function test_fieldVariable_setValueMatchId() { function test_fieldVariable_setValueMatchName() { // Expect the fieldVariable value to be set to variable name - var workspace = new Blockly.Workspace(); + workspace = new Blockly.Workspace(); workspace.createVariable('name2', null, 'id2'); var fieldVariable = new Blockly.FieldVariable('name1'); var mockBlock = fieldVariable_mockBlock(workspace); @@ -67,7 +80,7 @@ function test_fieldVariable_setValueMatchName() { function test_fieldVariable_setValueNoVariable() { // Expect the fieldVariable value to be set to the passed in string. No error // should be thrown. - var workspace = new Blockly.Workspace(); + workspace = new Blockly.Workspace(); var fieldVariable = new Blockly.FieldVariable('name1'); var mockBlock = {'workspace': workspace, 'isShadow': function(){return false;}}; @@ -77,3 +90,65 @@ function test_fieldVariable_setValueNoVariable() { assertEquals('id1', fieldVariable.value_); workspace.dispose(); } + +function test_fieldVariable_dropdownCreateVariablesExist() { + // Expect that the dropdown options will contain the variables that exist. + workspace = new Blockly.Workspace(); + workspace.createVariable('name1', '', 'id1'); + workspace.createVariable('name2', '', 'id2'); + var result_options = Blockly.FieldVariable.dropdownCreate.call( + { + 'sourceBlock_': {'workspace': workspace}, + 'getText': function(){return 'name1';} + }); + assertEquals(result_options.length, 3); + isEqualArrays(result_options[0], ['name1', 'id1']); + isEqualArrays(result_options[1], ['name2', 'id2']); + + workspace.dispose(); +} + +function test_fieldVariable_dropdownVariableAndTypeDoesNotExist() { + // Expect a variable will be created for the selected option. Expect the + // workspace variable map to contain the new variable once. + fieldVariableTestWithMocks_setUp(); + setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['id1', null]); + + var result_options = Blockly.FieldVariable.dropdownCreate.call( + { + 'sourceBlock_': {'workspace': workspace}, + 'getText': function(){return 'name1';} + }); + + // Check the options. + assertEquals(2, result_options.length); + isEqualArrays(result_options[0], ['name1', 'id1']); + // Check the variable map. + assertEquals(1, workspace.getAllVariables().length); + checkVariableValues(workspace, 'name1', '', 'id1'); + + fieldVariableTestWithMocks_tearDown(); +} + +function test_fieldVariable_dropdownVariableDoesNotExistTypeDoes() { + // Expect a variable will be created for the selected option. Expect the + // workspace variable map to contain the new variable once. + fieldVariableTestWithMocks_setUp(); + workspace.createVariable('name1', '', 'id1'); + setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['id2', null]); + + var result_options = Blockly.FieldVariable.dropdownCreate.call( + { + 'sourceBlock_': {'workspace': workspace}, + 'getText': function(){return 'name2';} + }); + + assertEquals(3, result_options.length); + isEqualArrays(result_options[0], ['name1', 'id1']); + isEqualArrays(result_options[1], ['name2', 'id2']); + assertEquals(2, workspace.variableMap_.getAllVariables().length); + checkVariableValues(workspace, 'name1', '', 'id1'); + checkVariableValues(workspace, 'name2', '', 'id2'); + + fieldVariableTestWithMocks_tearDown(); +}