mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
Add tests for field serializability.
This commit is contained in:
parent
0ea49318d3
commit
3d44202834
5 changed files with 63 additions and 7 deletions
|
@ -1336,7 +1336,7 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
|
|||
case 'field_label':
|
||||
field = Blockly.Block.newFieldLabelFromJson_(element);
|
||||
break;
|
||||
case 'field_label_editable':
|
||||
case 'field_label_serializable':
|
||||
field = Blockly.Block.newFieldLabelSerializableFromJson_(element);
|
||||
break;
|
||||
case 'field_input':
|
||||
|
|
|
@ -144,7 +144,7 @@ Blockly.Field.prototype.EDITABLE = true;
|
|||
* @type {boolean}
|
||||
* @public
|
||||
*/
|
||||
Blockly.Field.prototype.SERIALIZABLE = Blockly.Field.prototype.EDITABLE;
|
||||
Blockly.Field.prototype.SERIALIZABLE = true;
|
||||
|
||||
/**
|
||||
* Attach this field to a block.
|
||||
|
|
|
@ -47,10 +47,20 @@ Blockly.FieldLabel = function(text, opt_class) {
|
|||
goog.inherits(Blockly.FieldLabel, Blockly.Field);
|
||||
|
||||
/**
|
||||
* Editable fields are saved by the XML renderer, non-editable fields are not.
|
||||
* Editable fields usually show some sort of UI for the user to change them.
|
||||
* @type {boolean}
|
||||
* @public
|
||||
*/
|
||||
Blockly.FieldLabel.prototype.EDITABLE = false;
|
||||
|
||||
/**
|
||||
* Serializable fields are saved by the XML renderer, non-serializable fields
|
||||
* are not. Editable fields should be serialized.
|
||||
* @type {boolean}
|
||||
* @public
|
||||
*/
|
||||
Blockly.FieldLabel.prototype.SERIALIZABLE = false;
|
||||
|
||||
/**
|
||||
* Install this text on a block.
|
||||
*/
|
||||
|
|
|
@ -48,8 +48,14 @@ function test_fieldvariablegetter_isEditableBlock() {
|
|||
|
||||
field.sourceBlock_ = editableBlock;
|
||||
|
||||
// EDITABLE is true by default, but without a source block a field can't be
|
||||
// edited.
|
||||
assertTrue('Variable getter field on a block is editable',
|
||||
// Variable getter fields aren't user editable.
|
||||
assertFalse('Variable getter field should not be editable',
|
||||
field.isCurrentlyEditable());
|
||||
}
|
||||
|
||||
function test_fieldvariablegetter_isSerializable() {
|
||||
var field = new Blockly.FieldVariableGetter('text', 'name');
|
||||
// Variable getter fields are serializable by default.
|
||||
assertTrue('Variable getter field should be serializable',
|
||||
field.SERIALIZABLE);
|
||||
}
|
||||
|
|
|
@ -67,7 +67,22 @@ function xmlTest_setUpWithMockBlocks() {
|
|||
'name': 'VAR',
|
||||
'variable': 'item'
|
||||
}
|
||||
],
|
||||
]
|
||||
},
|
||||
{
|
||||
'type': 'field_serializable_test_block',
|
||||
'message0': '%1 %2',
|
||||
'args0': [
|
||||
{
|
||||
'type': 'field_label_serializable',
|
||||
'name': 'FIELD'
|
||||
},
|
||||
{
|
||||
"type": "field_input",
|
||||
"name": "TEXTINPUT",
|
||||
"text": "default"
|
||||
}
|
||||
]
|
||||
}]);
|
||||
}
|
||||
|
||||
|
@ -368,3 +383,28 @@ function test_variablesToDom_noVariables() {
|
|||
assertEquals(1, resultDom.children.length);
|
||||
xmlTest_tearDown();
|
||||
}
|
||||
|
||||
function test_fieldIsSerialized() {
|
||||
xmlTest_setUpWithMockBlocks();
|
||||
var block = new Blockly.Block(workspace, 'field_serializable_test_block');
|
||||
block.getField('FIELD').setValue('serialized');
|
||||
|
||||
var resultDom = Blockly.Xml.blockToDom(block).childNodes[0];
|
||||
assertEquals('serialized', resultDom.textContent);
|
||||
assertEquals('FIELD', resultDom.getAttribute('name'));
|
||||
|
||||
xmlTest_tearDownWithMockBlocks();
|
||||
}
|
||||
|
||||
function test_fieldIsNotSerialized() {
|
||||
xmlTest_setUpWithMockBlocks();
|
||||
var block = new Blockly.Block(workspace, 'field_serializable_test_block');
|
||||
block.getField('FIELD').SERIALIZABLE = false;
|
||||
block.getField('FIELD').setValue('serialized');
|
||||
|
||||
var resultDom = Blockly.Xml.blockToDom(block).childNodes[0];
|
||||
assertEquals('default', resultDom.textContent);
|
||||
assertEquals('TEXTINPUT', resultDom.getAttribute('name'));
|
||||
|
||||
xmlTest_tearDownWithMockBlocks();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue