|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'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 fieldVariable_createAndInitField(workspace) {
|
|
|
var fieldVariable = new Blockly.FieldVariable('name1');
|
|
|
var mockBlock = fieldVariable_mockBlock(workspace);
|
|
|
fieldVariable.setSourceBlock(mockBlock);
|
|
|
|
|
|
fieldVariable.initModel();
|
|
|
return fieldVariable;
|
|
|
}
|
|
|
|
|
|
function test_fieldVariable_Constructor() {
|
|
|
workspace = new Blockly.Workspace();
|
|
|
var fieldVariable = new Blockly.FieldVariable('name1');
|
|
|
|
|
|
assertEquals('', fieldVariable.getText());
|
|
|
workspace.dispose();
|
|
|
}
|
|
|
|
|
|
function test_fieldVariable_setValueMatchId() {
|
|
|
|
|
|
fieldVariableTestWithMocks_setUp();
|
|
|
workspace.createVariable('name2', null, 'id2');
|
|
|
|
|
|
var fieldVariable = fieldVariable_createAndInitField(workspace);
|
|
|
|
|
|
var oldId = fieldVariable.getValue();
|
|
|
var event = new Blockly.Events.BlockChange(
|
|
|
fieldVariable.sourceBlock_, 'field', undefined, oldId, 'id2');
|
|
|
setUpMockMethod(mockControl_, Blockly.Events, 'fire', [event], null);
|
|
|
|
|
|
fieldVariable.setValue('id2');
|
|
|
assertEquals('name2', fieldVariable.getText());
|
|
|
assertEquals('id2', fieldVariable.getValue());
|
|
|
fieldVariableTestWithMocks_tearDown();
|
|
|
}
|
|
|
|
|
|
function test_fieldVariable_setValueNoVariable() {
|
|
|
fieldVariableTestWithMocks_setUp();
|
|
|
|
|
|
var fieldVariable = fieldVariable_createAndInitField(workspace);
|
|
|
var mockBlock = fieldVariable.sourceBlock_;
|
|
|
mockBlock.isShadow = function() {
|
|
|
return false;
|
|
|
};
|
|
|
|
|
|
try {
|
|
|
fieldVariable.setValue('id1');
|
|
|
|
|
|
fail();
|
|
|
} catch (e) {
|
|
|
|
|
|
} finally {
|
|
|
fieldVariableTestWithMocks_tearDown();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
function test_fieldVariable_dropdownCreateVariablesExist() {
|
|
|
|
|
|
workspace = new Blockly.Workspace();
|
|
|
workspace.createVariable('name1', '', 'id1');
|
|
|
workspace.createVariable('name2', '', 'id2');
|
|
|
|
|
|
var fieldVariable = fieldVariable_createAndInitField(workspace);
|
|
|
|
|
|
var result_options = Blockly.FieldVariable.dropdownCreate.call(
|
|
|
fieldVariable);
|
|
|
|
|
|
assertEquals(result_options.length, 3);
|
|
|
isEqualArrays(result_options[0], ['name1', 'id1']);
|
|
|
isEqualArrays(result_options[1], ['name2', 'id2']);
|
|
|
|
|
|
workspace.dispose();
|
|
|
}
|
|
|
|
|
|
function test_fieldVariable_setValueNull() {
|
|
|
|
|
|
fieldVariableTestWithMocks_setUp();
|
|
|
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['id1', null]);
|
|
|
|
|
|
var fieldVariable = fieldVariable_createAndInitField(workspace);
|
|
|
try {
|
|
|
fieldVariable.setValue(null);
|
|
|
fail();
|
|
|
} catch (e) {
|
|
|
|
|
|
} finally {
|
|
|
fieldVariableTestWithMocks_tearDown();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
function test_fieldVariable_getVariableTypes_undefinedVariableTypes() {
|
|
|
|
|
|
|
|
|
workspace = new Blockly.Workspace();
|
|
|
workspace.createVariable('name1', 'type1');
|
|
|
workspace.createVariable('name2', 'type2');
|
|
|
|
|
|
var fieldVariable = new Blockly.FieldVariable('name1');
|
|
|
var resultTypes = fieldVariable.getVariableTypes_();
|
|
|
isEqualArrays(resultTypes, ['']);
|
|
|
workspace.dispose();
|
|
|
}
|
|
|
|
|
|
function test_fieldVariable_getVariableTypes_givenVariableTypes() {
|
|
|
|
|
|
|
|
|
workspace = new Blockly.Workspace();
|
|
|
workspace.createVariable('name1', 'type1');
|
|
|
workspace.createVariable('name2', 'type2');
|
|
|
|
|
|
var fieldVariable = new Blockly.FieldVariable(
|
|
|
'name1', null, ['type1', 'type2']);
|
|
|
var resultTypes = fieldVariable.getVariableTypes_();
|
|
|
isEqualArrays(resultTypes, ['type1', 'type2']);
|
|
|
workspace.dispose();
|
|
|
}
|
|
|
|
|
|
function test_fieldVariable_getVariableTypes_nullVariableTypes() {
|
|
|
|
|
|
|
|
|
|
|
|
workspace = new Blockly.Workspace();
|
|
|
workspace.createVariable('name1', 'type1');
|
|
|
workspace.createVariable('name2', 'type2');
|
|
|
|
|
|
var fieldVariable = new Blockly.FieldVariable('name1');
|
|
|
var mockBlock = fieldVariable_mockBlock(workspace);
|
|
|
fieldVariable.setSourceBlock(mockBlock);
|
|
|
fieldVariable.variableTypes = null;
|
|
|
|
|
|
var resultTypes = fieldVariable.getVariableTypes_();
|
|
|
|
|
|
isEqualArrays(resultTypes, ['type1', 'type2', '']);
|
|
|
workspace.dispose();
|
|
|
}
|
|
|
|
|
|
function test_fieldVariable_getVariableTypes_emptyListVariableTypes() {
|
|
|
|
|
|
workspace = new Blockly.Workspace();
|
|
|
workspace.createVariable('name1', 'type1');
|
|
|
workspace.createVariable('name2', 'type2');
|
|
|
|
|
|
var fieldVariable = new Blockly.FieldVariable('name1');
|
|
|
var mockBlock = fieldVariable_mockBlock(workspace);
|
|
|
fieldVariable.setSourceBlock(mockBlock);
|
|
|
fieldVariable.variableTypes = [];
|
|
|
|
|
|
try {
|
|
|
fieldVariable.getVariableTypes_();
|
|
|
fail();
|
|
|
} catch (e) {
|
|
|
|
|
|
} finally {
|
|
|
workspace.dispose();
|
|
|
}
|
|
|
}
|
|
|
|