Merge pull request #2668 from BeksOmega/fixes/DeprecateSetText

Deprecated Field.setText
This commit is contained in:
Rachel Fenichel
2019-07-26 13:02:35 -07:00
committed by GitHub
6 changed files with 105 additions and 19 deletions

View File

@@ -1078,7 +1078,7 @@ Blockly.Block.prototype.updateVarName = function(variable) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field.referencesVariables() &&
variable.getId() == field.getValue()) {
field.setText(variable.name);
field.refreshVariableName();
}
}
}

View File

@@ -723,6 +723,7 @@ Blockly.Field.prototype.getText = function() {
/**
* Set the text in this field. Trigger a rerender of the source block.
* @param {*} newText New text.
* @deprecated 2019 setText should not be used directly. Use setValue instead.
*/
Blockly.Field.prototype.setText = function(newText) {
if (newText === null) {

View File

@@ -170,8 +170,19 @@ Blockly.FieldImage.prototype.getFlipRtl = function() {
* Set the alt text of this image.
* @param {?string} alt New alt text.
* @override
* @deprecated 2019 setText has been deprecated for all fields. Instead use
* setAlt to set the alt text of the field.
*/
Blockly.FieldImage.prototype.setText = function(alt) {
this.setAlt(alt);
};
/**
* Set the alt text of this image.
* @param {?string} alt New alt text.
* @public
*/
Blockly.FieldImage.prototype.setAlt = function(alt) {
if (alt === null) {
// No change if null.
return;

View File

@@ -253,7 +253,7 @@ Blockly.FieldVariable.prototype.doClassValidation_ = function(newId) {
Blockly.FieldVariable.prototype.doValueUpdate_ = function(newId) {
this.variable_ = Blockly.Variables.getVariable(this.workspace_, newId);
this.value_ = newId;
this.text_ = (this.variable_.name);
this.text_ = this.variable_.name;
this.isDirty_ = true;
};
@@ -341,6 +341,17 @@ Blockly.FieldVariable.prototype.setTypes_ = function(opt_variableTypes,
this.variableTypes = variableTypes;
};
/**
* Refreshes the name of the variable by grabbing the name of the model.
* Used when a variable gets renamed, but the ID stays the same. Should only
* be called by the block.
* @package
*/
Blockly.FieldVariable.prototype.refreshVariableName = function() {
this.text_ = this.variable_.name;
this.forceRerender();
};
/**
* Return a sorted list of variable names for variable dropdown menus.
* Include a special option at the end for creating a new variable name.

View File

@@ -154,17 +154,49 @@ suite ('Image Fields', function() {
this.imageField.setValue(undefined);
assertValue(this.imageField, 'src', 'alt');
});
test('New Src, New Alt', function() {
test('Good Src', function() {
this.imageField.setValue('newSrc');
assertValue(this.imageField, 'newSrc', 'alt');
this.imageField.setText('newAlt');
assertValue(this.imageField, 'newSrc', 'newAlt');
});
test('New Alt, New Src', function() {
this.imageField.setText('newAlt');
assertValue(this.imageField, 'src', 'newAlt');
this.imageField.setValue('newSrc');
assertValue(this.imageField, 'newSrc', 'newAlt');
});
suite('setAlt', function() {
suite('No Alt -> New Alt', function() {
setup(function() {
this.imageField = new Blockly.FieldImage('src', 1, 1);
});
test('Backwards Compat - setText', function() {
this.imageField.setText('newAlt');
assertValue(this.imageField, 'src', 'newAlt');
});
test('Null', function() {
this.imageField.setText(null);
assertValue(this.imageField, 'src', '');
});
test('Good Alt', function() {
this.imageField.setText('newAlt');
assertValue(this.imageField, 'src', 'newAlt');
});
});
suite('Alt -> New Alt', function() {
setup(function() {
this.imageField = new Blockly.FieldImage('src', 1, 1, 'alt');
});
test('Backwards Compat - setText', function() {
this.imageField.setText('newAlt');
assertValue(this.imageField, 'src', 'newAlt');
});
test('Null', function() {
this.imageField.setText(null);
assertValue(this.imageField, 'src', 'alt');
});
test('Empty String', function() {
this.imageField.setText('');
assertValue(this.imageField, 'src', '');
});
test('Good Alt', function() {
this.imageField.setText('newAlt');
assertValue(this.imageField, 'src', 'newAlt');
});
});
});
});

View File

@@ -27,7 +27,9 @@ suite('Variable Fields', function() {
'workspace': workspace,
'isShadow': function() {
return false;
}
},
'renameVarById': Blockly.Block.prototype.renameVarById,
'updateVarName': Blockly.Block.prototype.updateVarName,
};
}
function initField(fieldVariable, workspace) {
@@ -80,7 +82,7 @@ suite('Variable Fields', function() {
var result_options = Blockly.FieldVariable.dropdownCreate.call(
fieldVariable);
// Expect three variable options and a rename option.
// Expect three variable options, a rename option, and a delete option.
assertEquals(result_options.length, 5);
isEqualArrays(result_options[0], ['name1', 'id1']);
isEqualArrays(result_options[1], ['name2', 'id2']);
@@ -214,7 +216,6 @@ suite('Variable Fields', function() {
this.workspace.createVariable('name1', 'type1');
this.workspace.createVariable('name2', 'type2');
});
test('variableTypes is undefined', function() {
// Expect that since variableTypes is undefined, only type empty string
// will be returned (regardless of what types are available on the workspace).
@@ -222,7 +223,6 @@ suite('Variable Fields', function() {
var resultTypes = fieldVariable.getVariableTypes_();
isEqualArrays(resultTypes, ['']);
});
test('variableTypes is explicit', function() {
// Expect that since variableTypes is defined, it will be the return
// value, regardless of what types are available on the workspace.
@@ -233,7 +233,6 @@ suite('Variable Fields', function() {
assertEquals('Default type was wrong', 'type1',
fieldVariable.defaultType_);
});
test('variableTypes is null', function() {
// Expect all variable types to be returned.
// The field does not need to be initialized to do this--it just needs
@@ -247,7 +246,6 @@ suite('Variable Fields', function() {
// The empty string is always one of the options.
isEqualArrays(resultTypes, ['type1', 'type2', '']);
});
test('variableTypes is the empty list', function() {
var fieldVariable = new Blockly.FieldVariable('name1');
var mockBlock = getMockBlock(this.workspace);
@@ -265,7 +263,6 @@ suite('Variable Fields', function() {
assertEquals('The variable field\'s default type should be "b"',
'b', fieldVariable.defaultType_);
});
test('No default type', function() {
var fieldVariable = new Blockly.FieldVariable(null);
assertEquals('The variable field\'s default type should be the empty string',
@@ -273,14 +270,12 @@ suite('Variable Fields', function() {
assertNull('The variable field\'s allowed types should be null',
fieldVariable.variableTypes);
});
test('Default type mismatch', function() {
// Invalid default type when creating a variable field.
chai.assert.throws(function() {
var _fieldVariable = new Blockly.FieldVariable(null, null, ['a'], 'b');
});
});
test('Default type mismatch with empty array', function() {
// Invalid default type when creating a variable field.
chai.assert.throws(function() {
@@ -288,4 +283,40 @@ suite('Variable Fields', function() {
});
});
});
suite('Renaming Variables', function() {
setup(function() {
this.workspace.createVariable('name1', null, 'id1');
Blockly.defineBlocksWithJsonArray([{
"type": "field_variable_test_block",
"message0": "%1",
"args0": [
{
"type": "field_variable",
"name": "VAR",
"variable": "name1"
}
],
}]);
this.variableBlock = new Blockly.Block(this.workspace,
'field_variable_test_block');
this.variableField = this.variableBlock.getField('VAR');
});
teardown(function() {
this.variableBlock.dispose();
this.variableBlock = null;
this.variableField = null;
delete Blockly.Blocks['field_variable_test_block'];
});
test('Rename & Keep Old ID', function() {
this.workspace.renameVariableById('id1', 'name2');
chai.assert.equal(this.variableField.getText(), 'name2');
chai.assert.equal(this.variableField.getValue(), 'id1');
});
test('Rename & Get New ID', function() {
this.workspace.createVariable('name2', null, 'id2');
this.workspace.renameVariableById('id1', 'name2');
chai.assert.equal(this.variableField.getText(), 'name2');
chai.assert.equal(this.variableField.getValue(), 'id2');
});
});
});