Files
blockly/tests/mocha/field_checkbox_test.js
Sam El-Husseini 29dc7bd7f7 Adjust marker and cursor colours from the theme (#3735)
* Support adjusting marker and cursor colours from the theme
2020-03-11 11:23:29 -07:00

229 lines
7.8 KiB
JavaScript

/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
suite('Checkbox Fields', function() {
function assertValue(checkboxField, expectedValue, expectedText) {
var actualValue = checkboxField.getValue();
var actualText = checkboxField.getText();
assertEquals(actualValue, expectedValue);
assertEquals(actualText, expectedText);
}
function assertValueDefault(checkboxField) {
assertValue(checkboxField, 'FALSE', 'false');
}
suite('Constructor', function() {
test('Empty', function() {
var checkboxField = new Blockly.FieldCheckbox();
assertValueDefault(checkboxField);
});
test('Undefined', function() {
var checkboxField = new Blockly.FieldCheckbox(undefined);
assertValueDefault(checkboxField);
});
test('True', function() {
var checkboxField = new Blockly.FieldCheckbox(true);
assertValue(checkboxField, 'TRUE', 'true');
});
test('False', function() {
var checkboxField = new Blockly.FieldCheckbox(false);
assertValue(checkboxField, 'FALSE', 'false');
});
test('String TRUE', function() {
var checkboxField = new Blockly.FieldCheckbox('TRUE');
assertValue(checkboxField, 'TRUE', 'true');
});
test('String FALSE', function() {
var checkboxField = new Blockly.FieldCheckbox('FALSE');
assertValue(checkboxField, 'FALSE', 'false');
});
});
suite('fromJson', function() {
test('Empty', function() {
var checkboxField = Blockly.FieldCheckbox.fromJson({});
assertValueDefault(checkboxField);
});
test('Undefined', function() {
var checkboxField = Blockly.FieldCheckbox.fromJson({ checked: undefined});
assertValueDefault(checkboxField);
});
test('True', function() {
var checkboxField = Blockly.FieldCheckbox.fromJson({ checked: true});
assertValue(checkboxField, 'TRUE', 'true');
});
test('False', function() {
var checkboxField = Blockly.FieldCheckbox.fromJson({ checked: false});
assertValue(checkboxField, 'FALSE', 'false');
});
test('String TRUE', function() {
var checkboxField = Blockly.FieldCheckbox.fromJson({ checked: 'TRUE'});
assertValue(checkboxField, 'TRUE', 'true');
});
test('String FALSE', function() {
var checkboxField = Blockly.FieldCheckbox.fromJson({ checked: 'FALSE'});
assertValue(checkboxField, 'FALSE', 'false');
});
});
suite('setValue', function() {
suite('True -> New Value', function() {
setup(function() {
this.checkboxField = new Blockly.FieldCheckbox('TRUE');
});
test('Null', function() {
this.checkboxField.setValue(null);
assertValue(this.checkboxField, 'TRUE', 'true');
});
test('Undefined', function() {
this.checkboxField.setValue(undefined);
assertValue(this.checkboxField, 'TRUE', 'true');
});
test('Non-Parsable String', function() {
this.checkboxField.setValue('bad');
assertValue(this.checkboxField, 'TRUE', 'true');
});
test('False', function() {
this.checkboxField.setValue('FALSE');
assertValue(this.checkboxField, 'FALSE', 'false');
});
});
suite('False -> New Value', function() {
setup(function() {
this.checkboxField = new Blockly.FieldCheckbox('FALSE');
});
test('Null', function() {
this.checkboxField.setValue(null);
assertValue(this.checkboxField, 'FALSE', 'false');
});
test('Undefined', function() {
this.checkboxField.setValue(undefined);
assertValue(this.checkboxField, 'FALSE', 'false');
});
test('Non-Parsable String', function() {
this.checkboxField.setValue('bad');
assertValue(this.checkboxField, 'FALSE', 'false');
});
test('True', function() {
this.checkboxField.setValue('TRUE');
assertValue(this.checkboxField, 'TRUE', 'true');
});
});
});
suite('Validators', function() {
setup(function() {
this.checkboxField = new Blockly.FieldCheckbox(true);
});
teardown(function() {
this.checkboxField.setValidator(null);
});
suite('Null Validator', function() {
setup(function() {
this.checkboxField.setValidator(function() {
return null;
});
});
test('New Value', function() {
this.checkboxField.setValue('FALSE');
assertValue(this.checkboxField, 'TRUE', 'true');
});
});
suite('Always True Validator', function() {
setup(function() {
this.checkboxField.setValidator(function() {
return 'TRUE';
});
});
test('New Value', function() {
this.checkboxField.setValue('FALSE');
assertValue(this.checkboxField, 'TRUE', 'true');
});
});
suite('Always False Validator', function() {
setup(function() {
this.checkboxField.setValidator(function() {
return 'FALSE';
});
});
test('New Value', function() {
this.checkboxField.setValue('TRUE');
assertValue(this.checkboxField, 'FALSE', 'false');
});
});
suite('Returns Undefined Validator', function() {
setup(function() {
this.checkboxField.setValidator(function() {});
});
test('New Value', function() {
this.checkboxField.setValue('FALSE');
assertValue(this.checkboxField, 'FALSE', 'false');
});
});
});
suite('Customizations', function() {
suite('Check Character', function() {
function assertCharacter(field, char) {
field.fieldGroup_ = Blockly.utils.dom.createSvgElement('g', {}, null);
field.sourceBlock_ = {
RTL: false,
rendered: true,
workspace: {
keyboardAccessibilityMode: false
},
render: function() { field.render_(); },
bumpNeighbours: function() {}
};
field.constants_ = {
FIELD_CHECKBOX_X_OFFSET: 2,
FIELD_CHECKBOX_Y_OFFSET: 2
};
field.initView();
field.render_();
chai.assert(field.textContent_.nodeValue, char);
}
test('Constant', function() {
var checkChar = Blockly.FieldCheckbox.CHECK_CHAR;
// Note: Developers shouldn't actually do this. IMO they should change
// the file and then recompile. But this is fine for testing.
Blockly.FieldCheckbox.CHECK_CHAR = '\u2661';
var field = new Blockly.FieldCheckbox(true);
assertCharacter(field, '\u2661');
Blockly.FieldCheckbox.CHECK_CHAR = checkChar;
});
test('JS Constructor', function() {
var field = new Blockly.FieldCheckbox(true, null, {
checkCharacter: '\u2661'
});
assertCharacter(field, '\u2661');
});
test('JSON Definition', function() {
var field = Blockly.FieldCheckbox.fromJson({
checkCharacter: '\u2661'
});
assertCharacter(field, '\u2661');
});
test('setCheckCharacter', function() {
var field = new Blockly.FieldCheckbox();
assertCharacter(field, Blockly.FieldCheckbox.CHECK_CHAR);
field.setCheckCharacter('\u2661');
// Don't call assertCharacter b/c we don't want to re-initialize.
chai.assert.equal(field.textContent_.nodeValue, '\u2661');
});
test('setCheckCharacter Before Init', function() {
var field = new Blockly.FieldCheckbox();
field.setCheckCharacter('\u2661');
assertCharacter(field, '\u2661');
});
test('Remove Custom Character', function() {
var field = new Blockly.FieldCheckbox(true, null, {
'checkCharacter': '\u2661'
});
assertCharacter(field, '\u2661');
field.setCheckCharacter(null);
chai.assert(field.textContent_.nodeValue,
Blockly.FieldCheckbox.CHECK_CHAR);
});
});
});
});