diff --git a/tests/jsunit/block_test.js b/tests/jsunit/block_test.js
index d7efed435..b80b3d61d 100644
--- a/tests/jsunit/block_test.js
+++ b/tests/jsunit/block_test.js
@@ -22,9 +22,14 @@
* @fileoverview Tests for Blockly.Block
* @author fenichel@google.com (Rachel Fenichel)
*/
+
+goog.require('goog.testing');
+goog.require('goog.testing.MockControl');
+
'use strict';
var workspace;
+var mockControl_;
function defineTestBlocks() {
Blockly.defineBlocksWithJsonArray([{
@@ -53,6 +58,7 @@ function undefineTestBlocks() {
function blockTest_setUp() {
defineTestBlocks();
+ mockControl_ = new goog.testing.MockControl();
workspace = new Blockly.Workspace();
}
@@ -252,3 +258,43 @@ function test_block_row_unplug_multi_inputs_child() {
blockTest_tearDown();
}
}
+
+function test_set_style() {
+ blockTest_setUp();
+ var styleStub = {
+ getBlockStyle: function() {
+ return{
+ "primaryColour": "colour1",
+ "secondaryColour":"colour2",
+ "tertiaryColour":"colour3"
+ }
+ }
+ };
+ setUpMockMethod(mockControl_, Blockly, 'getStyle', null, [styleStub]);
+ var blockA = workspace.newBlock('row_block');
+ blockA.setStyle('styleOne');
+
+ assertEquals(blockA.colour_, 'colour1');
+ assertEquals(blockA.secondaryColour_, 'colour2');
+ assertEquals(blockA.tertiaryColour_, 'colour3');
+
+ blockTest_tearDown();
+}
+
+function test_set_style_throw_exception() {
+ blockTest_setUp();
+ var styleStub = {
+ getBlockStyle: function() {
+ return null;
+ }
+ };
+ setUpMockMethod(mockControl_, Blockly, 'getStyle', null, [styleStub]);
+ var blockA = workspace.newBlock('row_block');
+ try {
+ blockA.setStyle('styleOne');
+ }catch(error){
+ assertEquals(error, "Invalid style name: styleOne");
+ }finally {
+ blockTest_tearDown();
+ }
+}
diff --git a/tests/jsunit/index.html b/tests/jsunit/index.html
index c84375f88..a54339b26 100644
--- a/tests/jsunit/index.html
+++ b/tests/jsunit/index.html
@@ -29,6 +29,7 @@
+
diff --git a/tests/jsunit/style_test.js b/tests/jsunit/style_test.js
new file mode 100644
index 000000000..4b7be88b1
--- /dev/null
+++ b/tests/jsunit/style_test.js
@@ -0,0 +1,156 @@
+/**
+ * @license
+ * Visual Blocks Editor
+ *
+ * Copyright 2018 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.
+ */
+
+ /**
+ * @fileoverview Tests for Blockly.Style
+ * @author aschmiedt@google.com (Abby Schmiedt)
+ */
+'use strict';
+
+function defineStyleTestBlocks() {
+ Blockly.defineBlocksWithJsonArray([{
+ "type": "stack_block",
+ "message0": "",
+ "previousStatement": null,
+ "nextStatement": null
+ },
+ {
+ "type": "row_block",
+ "message0": "%1",
+ "args0": [
+ {
+ "type": "input_value",
+ "name": "INPUT"
+ }
+ ],
+ "output": null
+ }]);
+};
+
+function undefineStyleTestBlocks() {
+ delete Blockly.Blocks['stack_block'];
+ delete Blockly.Blocks['row_block'];
+}
+
+
+function createBlockStyles() {
+ return {
+ "styleOne": {
+ "primaryColour": "colour1",
+ "secondaryColour":"colour2",
+ "tertiaryColour":"colour3"
+ }
+ };
+}
+
+function createMultipleBlockStyles() {
+ return {
+ "styleOne": {
+ "primaryColour": "colour1",
+ "secondaryColour":"colour2",
+ "tertiaryColour":"colour3"
+ },
+ "styleTwo": {
+ "primaryColour": "colour1",
+ "secondaryColour":"colour2",
+ "tertiaryColour":"colour3"
+ }
+ };
+}
+
+function test_setAllBlockStyles() {
+ var style = new Blockly.Style(createBlockStyles());
+ stringifyAndCompare(createBlockStyles(), style.blockStyles_);
+ style.setAllBlockStyles(createMultipleBlockStyles());
+ stringifyAndCompare(createMultipleBlockStyles(), style.blockStyles_);
+}
+
+function test_getAllBlockStyles() {
+ var style = new Blockly.Style(createMultipleBlockStyles());
+ var allBlocks = style.getAllBlockStyles();
+ stringifyAndCompare(createMultipleBlockStyles(), allBlocks);
+
+}
+
+function test_getBlockStyles() {
+ var style = new Blockly.Style(createBlockStyles());
+ var blockStyle = style.getBlockStyle("styleOne");
+
+ stringifyAndCompare(blockStyle, createBlockStyles().styleOne);
+}
+
+function test_setBlockStyleUpdate() {
+ var style = new Blockly.Style(createBlockStyles());
+ var blockStyle = createBlockStyles();
+ blockStyle.styleOne.primaryColour = "somethingElse";
+
+ style.setBlockStyle('styleOne', blockStyle.styleOne);
+
+ stringifyAndCompare(style.blockStyles_, blockStyle);
+}
+
+function test_setBlockStyleAdd() {
+ var style = new Blockly.Style(createBlockStyles());
+ var blockStyle = createMultipleBlockStyles();
+
+ style.setBlockStyle('styleTwo', blockStyle.styleTwo);
+
+ stringifyAndCompare(style.blockStyles_, blockStyle);
+}
+
+function test_setStyleForBlockly() {
+ defineStyleTestBlocks();
+ var blockStyles = createBlockStyles();
+ var workspace = new Blockly.WorkspaceSvg({});
+ var blockA = workspace.newBlock('stack_block');
+ var blocks;
+
+ blockA.setStyle = function(){this.styleName_ = 'styleTwo'};
+ var something = 1;
+ workspace.refreshToolboxSelection = function(){
+ return ++something;
+ };
+ blockA.styleName_ = 'styleOne';
+
+ blocks = [blockA];
+
+ setUpMockMethod(mockControl_, Blockly, 'getMainWorkspace', null, [workspace]);
+
+ Blockly.setStyle(blockStyles);
+
+ //Checks that the style set correctly on Blockly namespace
+ stringifyAndCompare(Blockly.getStyle(), blockStyles);
+
+ //Checks that the setStyle function was called on the block
+ assertEquals(blockA.getStyleName(), 'styleTwo');
+
+ //check that the toolbox refreshed method was called
+ assertEquals(workspace.refreshToolboxSelection(), 3);
+
+ assertEquals(Blockly.Events.FIRE_QUEUE_.pop().element, "styleChanged");
+
+ undefineStyleTestBlocks();
+}
+
+function stringifyAndCompare(val1, val2) {
+ var stringVal1 = JSON.stringify(val1);
+ var stringVal2 = JSON.stringify(val2);
+ assertEquals(stringVal1, stringVal2);
+}