2017-06-06 14:07:14 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Visual Blocks Editor
|
|
|
|
*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
* https://developers.google.com/blockly/
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-06-09 15:37:43 -07:00
|
|
|
* @fileoverview Tests for Blockly.FieldVariable
|
2017-06-06 14:07:14 -07:00
|
|
|
* @author marisaleung@google.com (Marisa Leung)
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
goog.require('goog.testing');
|
2017-06-26 16:50:42 -07:00
|
|
|
goog.require('goog.testing.MockControl');
|
2017-06-06 14:07:14 -07:00
|
|
|
|
2017-06-26 16:50:42 -07:00
|
|
|
var workspace;
|
|
|
|
var mockControl_;
|
|
|
|
|
|
|
|
function fieldVariableTestWithMocks_setUp() {
|
|
|
|
workspace = new Blockly.Workspace();
|
|
|
|
mockControl_ = new goog.testing.MockControl();
|
|
|
|
}
|
|
|
|
|
|
|
|
function fieldVariableTestWithMocks_tearDown() {
|
|
|
|
mockControl_.$tearDown();
|
|
|
|
workspace.dispose();
|
|
|
|
}
|
2017-06-06 14:07:14 -07:00
|
|
|
|
2017-06-09 15:37:43 -07:00
|
|
|
function fieldVariable_mockBlock(workspace) {
|
2017-06-06 14:07:14 -07:00
|
|
|
return {'workspace': workspace, 'isShadow': function(){return false;}};
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_fieldVariable_Constructor() {
|
2017-06-26 16:50:42 -07:00
|
|
|
workspace = new Blockly.Workspace();
|
2017-06-06 14:07:14 -07:00
|
|
|
var fieldVariable = new Blockly.FieldVariable('name1');
|
|
|
|
assertEquals('name1', fieldVariable.getText());
|
2017-06-09 15:37:43 -07:00
|
|
|
workspace.dispose();
|
2017-06-06 14:07:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_fieldVariable_setValueMatchId() {
|
|
|
|
// Expect the fieldVariable value to be set to variable name
|
2017-06-26 16:50:42 -07:00
|
|
|
workspace = new Blockly.Workspace();
|
2017-06-06 14:07:14 -07:00
|
|
|
workspace.createVariable('name2', null, 'id1');
|
|
|
|
var fieldVariable = new Blockly.FieldVariable('name1');
|
2017-06-09 15:37:43 -07:00
|
|
|
var mockBlock = fieldVariable_mockBlock(workspace);
|
2017-06-06 14:07:14 -07:00
|
|
|
fieldVariable.setSourceBlock(mockBlock);
|
|
|
|
fieldVariable.setValue('id1');
|
|
|
|
assertEquals('name2', fieldVariable.getText());
|
|
|
|
assertEquals('id1', fieldVariable.value_);
|
2017-06-09 15:37:43 -07:00
|
|
|
workspace.dispose();
|
2017-06-06 14:07:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_fieldVariable_setValueMatchName() {
|
|
|
|
// Expect the fieldVariable value to be set to variable name
|
2017-06-26 16:50:42 -07:00
|
|
|
workspace = new Blockly.Workspace();
|
2017-06-06 14:07:14 -07:00
|
|
|
workspace.createVariable('name2', null, 'id2');
|
|
|
|
var fieldVariable = new Blockly.FieldVariable('name1');
|
2017-06-09 15:37:43 -07:00
|
|
|
var mockBlock = fieldVariable_mockBlock(workspace);
|
2017-06-06 14:07:14 -07:00
|
|
|
fieldVariable.setSourceBlock(mockBlock);
|
|
|
|
fieldVariable.setValue('name2');
|
|
|
|
assertEquals('name2', fieldVariable.getText());
|
|
|
|
assertEquals('id2', fieldVariable.value_);
|
2017-06-09 15:37:43 -07:00
|
|
|
workspace.dispose();
|
2017-06-06 14:07:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_fieldVariable_setValueNoVariable() {
|
|
|
|
// Expect the fieldVariable value to be set to the passed in string. No error
|
|
|
|
// should be thrown.
|
2017-06-26 16:50:42 -07:00
|
|
|
workspace = new Blockly.Workspace();
|
2017-06-06 14:07:14 -07:00
|
|
|
var fieldVariable = new Blockly.FieldVariable('name1');
|
|
|
|
var mockBlock = {'workspace': workspace,
|
|
|
|
'isShadow': function(){return false;}};
|
|
|
|
fieldVariable.setSourceBlock(mockBlock);
|
|
|
|
fieldVariable.setValue('id1');
|
|
|
|
assertEquals('id1', fieldVariable.getText());
|
|
|
|
assertEquals('id1', fieldVariable.value_);
|
2017-06-09 15:37:43 -07:00
|
|
|
workspace.dispose();
|
2017-06-06 14:07:14 -07:00
|
|
|
}
|
2017-06-26 16:50:42 -07:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|