Initial commit

This commit is contained in:
pasketti
2026-04-05 16:14:49 -04:00
commit ebee3a5534
14059 changed files with 2588797 additions and 0 deletions

View File

@@ -0,0 +1,307 @@
/**
* @license
* Blockly Tests
*
* Copyright 2015 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.
*/
'use strict';
function verify_DB_(msg, expected, db) {
var equal = (expected.length == db.length);
if (equal) {
for (var i = 0; i < expected.length; i++) {
if (expected[i] != db[i]) {
equal = false;
break;
}
}
}
if (equal) {
assertTrue(msg, true);
} else {
assertEquals(msg, expected, db);
}
}
function test_DB_addConnection() {
var db = new Blockly.ConnectionDB();
var o2 = {y_: 2, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o2);
verify_DB_('Adding connection #2', [o2], db);
var o4 = {y_: 4, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o4);
verify_DB_('Adding connection #4', [o2, o4], db);
var o1 = {y_: 1, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o1);
verify_DB_('Adding connection #1', [o1, o2, o4], db);
var o3a = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o3a);
verify_DB_('Adding connection #3a', [o1, o2, o3a, o4], db);
var o3b = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o3b);
verify_DB_('Adding connection #3b', [o1, o2, o3b, o3a, o4], db);
}
function test_DB_removeConnection() {
var db = new Blockly.ConnectionDB();
var o1 = {y_: 1, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o2 = {y_: 2, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o3a = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o3b = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o3c = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o4 = {y_: 4, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o1);
db.addConnection(o2);
db.addConnection(o3c);
db.addConnection(o3b);
db.addConnection(o3a);
db.addConnection(o4);
verify_DB_('Adding connections 1-4', [o1, o2, o3a, o3b, o3c, o4], db);
db.removeConnection_(o2);
verify_DB_('Removing connection #2', [o1, o3a, o3b, o3c, o4], db);
db.removeConnection_(o4);
verify_DB_('Removing connection #4', [o1, o3a, o3b, o3c], db);
db.removeConnection_(o1);
verify_DB_('Removing connection #1', [o3a, o3b, o3c], db);
db.removeConnection_(o3a);
verify_DB_('Removing connection #3a', [o3b, o3c], db);
db.removeConnection_(o3c);
verify_DB_('Removing connection #3c', [o3b], db);
db.removeConnection_(o3b);
verify_DB_('Removing connection #3b', [], db);
}
function test_DB_getNeighbours() {
var db = new Blockly.ConnectionDB();
// Search an empty list.
assertEquals(helper_getNeighbours(db,
10 /* x */, 10 /* y */, 100 /* radius */).length, 0);
// Set up some connections.
for (var i = 0; i < 10; i++) {
db.addConnection(helper_createConnection(0, i,
Blockly.PREVIOUS_STATEMENT, null, true));
}
// Test block belongs at beginning.
var result = helper_getNeighbours(db, 0, 0, 4);
assertEquals(5, result.length);
for (i = 0; i < result.length; i++) {
assertNotEquals(result.indexOf(db[i]), -1); // contains
}
// Test block belongs at middle.
result = helper_getNeighbours(db, 0, 4, 2);
assertEquals(5, result.length);
for (i = 0; i < result.length; i++) {
assertNotEquals(result.indexOf(db[i + 2]), -1); // contains
}
// Test block belongs at end.
result = helper_getNeighbours(db, 0, 9, 4);
assertEquals(5, result.length);
for (i = 0; i < result.length; i++) {
assertNotEquals(result.indexOf(db[i + 5]), -1); // contains
}
// Test block has no neighbours due to being out of range in the x direction.
result = helper_getNeighbours(db, 10, 9, 4);
assertEquals(result.length, 0);
// Test block has no neighbours due to being out of range in the y direction.
result = helper_getNeighbours(db, 0, 19, 4);
assertEquals(result.length, 0);
// Test block has no neighbours due to being out of range diagonally.
result = helper_getNeighbours(db, -2, -2, 2);
assertEquals(result.length, 0);
}
function test_DB_findPositionForConnection() {
var db = new Blockly.ConnectionDB();
db.addConnection(helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT,
null, true));
db.addConnection(helper_createConnection(0, 1, Blockly.PREVIOUS_STATEMENT,
null, true));
db.addConnection(helper_createConnection(0, 2, Blockly.PREVIOUS_STATEMENT,
null, true));
db.addConnection(helper_createConnection(0, 4, Blockly.PREVIOUS_STATEMENT,
null, true));
db.addConnection(helper_createConnection(0, 5, Blockly.PREVIOUS_STATEMENT,
null, true));
assertEquals(5, db.length);
var conn = helper_createConnection(0, 3, Blockly.PREVIOUS_STATEMENT, null,
true);
assertEquals(3, db.findPositionForConnection_(conn));
}
function test_DB_findConnection() {
var db = new Blockly.ConnectionDB();
for (var i = 0; i < 10; i++) {
db.addConnection(helper_createConnection(i, 0,
Blockly.PREVIOUS_STATEMENT, null, true));
db.addConnection(helper_createConnection(0, i,
Blockly.PREVIOUS_STATEMENT, null, true));
}
var conn = helper_createConnection(3, 3, Blockly.PREVIOUS_STATEMENT, null,
true);
db.addConnection(conn);
assertEquals(conn, db[db.findConnection(conn)]);
conn = helper_createConnection(3, 3, Blockly.PREVIOUS_STATEMENT, null, true);
assertEquals(-1, db.findConnection(conn));
}
function test_DB_ordering() {
var db = new Blockly.ConnectionDB();
for (var i = 0; i < 10; i++) {
db.addConnection(helper_createConnection(0, 9 - i,
Blockly.PREVIOUS_STATEMENT), null, true);
}
for (i = 0; i < 10; i++) {
assertEquals(i, db[i].y_);
}
// quasi-random
var xCoords = [-29, -47, -77, 2, 43, 34, -59, -52, -90, -36, -91, 38, 87, -20,
60, 4, -57, 65, -37, -81, 57, 58, -96, 1, 67, -79, 34, 93, -90, -99, -62,
4, 11, -36, -51, -72, 3, -50, -24, -45, -92, -38, 37, 24, -47, -73, 79,
-20, 99, 43, -10, -87, 19, 35, -62, -36, 49, 86, -24, -47, -89, 33, -44,
25, -73, -91, 85, 6, 0, 89, -94, 36, -35, 84, -9, 96, -21, 52, 10, -95, 7,
-67, -70, 62, 9, -40, -95, -9, -94, 55, 57, -96, 55, 8, -48, -57, -87, 81,
23, 65];
var yCoords = [-81, 82, 5, 47, 30, 57, -12, 28, 38, 92, -25, -20, 23, -51, 73,
-90, 8, 28, -51, -15, 81, -60, -6, -16, 77, -62, -42, -24, 35, 95, -46,
-7, 61, -16, 14, 91, 57, -38, 27, -39, 92, 47, -98, 11, -33, -72, 64, 38,
-64, -88, -35, -59, -76, -94, 45, -25, -100, -95, 63, -97, 45, 98, 99, 34,
27, 52, -18, -45, 66, -32, -38, 70, -73, -23, 5, -2, -13, -9, 48, 74, -97,
-11, 35, -79, -16, -77, 83, -57, -53, 35, -44, 100, -27, -15, 5, 39, 33,
-19, -20, -95];
for (i = 0; i < xCoords.length; i++) {
db.addConnection(helper_createConnection(xCoords[i], yCoords[i],
Blockly.PREVIOUS_STATEMENT), null, true);
}
for (i = 1; i < xCoords.length; i++) {
assertTrue(db[i].y_ >= db[i - 1].y_);
}
}
function test_SearchForClosest() {
var db = new Blockly.ConnectionDB();
var sharedWorkspace = {id: "Shared workspace"};
// Search an empty list.
assertEquals(null, helper_searchDB(db, 10 /* x */, 10 /* y */,
100 /* radius */));
db.addConnection(helper_createConnection(100, 0, Blockly.PREVIOUS_STATEMENT,
sharedWorkspace, true));
assertEquals(null, helper_searchDB(db, 0, 0, 5, sharedWorkspace));
db = new Blockly.ConnectionDB();
for (var i = 0; i < 10; i++) {
var tempConn = helper_createConnection(0, i, Blockly.PREVIOUS_STATEMENT,
sharedWorkspace, true);
tempConn.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
db.addConnection(tempConn);
}
// Should be at 0, 9.
var last = db[db.length - 1];
// Correct connection is last in db; many connections in radius.
assertEquals(last, helper_searchDB(db, 0, 10, 15, sharedWorkspace));
// Nothing nearby.
assertEquals(null, helper_searchDB(db, 100, 100, 3, sharedWorkspace));
// First in db, exact match.
assertEquals(db[0], helper_searchDB(db, 0, 0, 0, sharedWorkspace));
tempConn = helper_createConnection(6, 6, Blockly.PREVIOUS_STATEMENT,
sharedWorkspace, true);
tempConn.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
db.addConnection(tempConn);
tempConn = helper_createConnection(5, 5, Blockly.PREVIOUS_STATEMENT,
sharedWorkspace, true);
tempConn.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
db.addConnection(tempConn);
var result = helper_searchDB(db, 4, 6, 3, sharedWorkspace);
assertEquals(5, result.x_);
assertEquals(5, result.y_);
}
function helper_getNeighbours(db, x, y, radius) {
return db.getNeighbours(helper_createConnection(x, y, Blockly.NEXT_STATEMENT,
null, true),
radius);
}
function helper_searchDB(db, x, y, radius, shared_workspace) {
var tempConn = helper_createConnection(x, y,
Blockly.NEXT_STATEMENT, shared_workspace, true);
tempConn.sourceBlock_ = helper_makeSourceBlock(shared_workspace);
var closest = db.searchForClosest(tempConn, radius, {x: 0, y: 0});
return closest.connection;
}
function helper_makeSourceBlock(sharedWorkspace) {
return {workspace: sharedWorkspace,
parentBlock_: null,
getParent: function() { return null; },
movable_: true,
isMovable: function() { return true; },
isShadow: function() { return false; }
};
}
function helper_createConnection(x, y, type, opt_shared_workspace,
opt_rendered) {
var workspace = opt_shared_workspace ? opt_shared_workspace : {};
if (opt_rendered) {
var conn = new Blockly.RenderedConnection({workspace: workspace}, type);
} else {
var conn = new Blockly.Connection({workspace: workspace}, type);
}
conn.x_ = x;
conn.y_ = y;
return conn;
}

View File

@@ -0,0 +1,324 @@
/**
* @license
* Blockly Tests
*
* Copyright 2016 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 connection logic.
* @author fenichel@google.com (Rachel Fenichel)
*/
'use strict';
var input;
var output;
var previous;
var next;
var dummyWorkspace;
function connectionTest_setUp() {
dummyWorkspace = {};
function createDummyBlock() {
return {
workspace: dummyWorkspace,
isShadow: function() {return false;}
};
}
input = new Blockly.Connection(createDummyBlock(),
Blockly.INPUT_VALUE);
output = new Blockly.Connection(createDummyBlock(),
Blockly.OUTPUT_VALUE);
previous = new Blockly.Connection(createDummyBlock(),
Blockly.PREVIOUS_STATEMENT);
next = new Blockly.Connection(createDummyBlock(),
Blockly.NEXT_STATEMENT);
}
function connectionTest_tearDown() {
input = null;
output = null;
previous = null;
next = null;
dummyWorkspace = null;
}
var isMovableFn = function() { return true; };
/**
* These tests check that the reasons for failures to connect are consistent
* (internal view of error states).
*/
function testCanConnectWithReason_TargetNull() {
connectionTest_setUp();
assertEquals(Blockly.Connection.REASON_TARGET_NULL,
input.canConnectWithReason_(null));
connectionTest_tearDown();
}
function testCanConnectWithReason_Disconnect() {
connectionTest_setUp();
var tempConnection = new Blockly.Connection({workspace: dummyWorkspace, isMovable: isMovableFn},
Blockly.OUTPUT_VALUE);
Blockly.Connection.connectReciprocally_(input, tempConnection);
assertEquals(Blockly.Connection.CAN_CONNECT,
input.canConnectWithReason_(output));
connectionTest_tearDown();
}
function testCanConnectWithReason_DifferentWorkspaces() {
connectionTest_setUp();
input = new Blockly.Connection({workspace: {}}, Blockly.INPUT_VALUE);
output = new Blockly.Connection({workspace: dummyWorkspace},
Blockly.OUTPUT_VALUE);
assertEquals(Blockly.Connection.REASON_DIFFERENT_WORKSPACES,
input.canConnectWithReason_(output));
connectionTest_tearDown();
}
function testCanConnectWithReason_Self() {
connectionTest_setUp();
var block = {type_: "test block"};
input.sourceBlock_ = block;
assertEquals(Blockly.Connection.REASON_SELF_CONNECTION,
input.canConnectWithReason_(input));
connectionTest_tearDown();
}
function testCanConnectWithReason_Type() {
connectionTest_setUp();
assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
input.canConnectWithReason_(previous));
assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
input.canConnectWithReason_(next));
assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
output.canConnectWithReason_(previous));
assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
output.canConnectWithReason_(next));
assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
previous.canConnectWithReason_(input));
assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
previous.canConnectWithReason_(output));
assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
next.canConnectWithReason_(input));
assertEquals(Blockly.Connection.REASON_WRONG_TYPE,
next.canConnectWithReason_(output));
connectionTest_tearDown();
}
function testCanConnectWithReason_CanConnect() {
connectionTest_setUp();
assertEquals(Blockly.Connection.CAN_CONNECT,
previous.canConnectWithReason_(next));
assertEquals(Blockly.Connection.CAN_CONNECT,
next.canConnectWithReason_(previous));
assertEquals(Blockly.Connection.CAN_CONNECT,
input.canConnectWithReason_(output));
assertEquals(Blockly.Connection.CAN_CONNECT,
output.canConnectWithReason_(input));
connectionTest_tearDown();
}
/**
* The next set of tests checks that exceptions are being thrown at the correct
* times (external view of errors).
*/
function testCheckConnection_Self() {
connectionTest_setUp();
var block = {type_: "test block"};
input.sourceBlock_ = block;
try {
input.checkConnection_(input);
fail();
} catch (e) {
// expected
}
connectionTest_tearDown();
}
function testCheckConnection_TypeInputPrev() {
connectionTest_setUp();
try {
input.checkConnection_(previous);
fail();
} catch (e) {
// expected
}
connectionTest_tearDown();
}
function testCheckConnection_TypeInputNext() {
connectionTest_setUp();
try {
input.checkConnection_(next);
fail();
} catch (e) {
// expected
}
connectionTest_tearDown();
}
function testCheckConnection_TypeOutputPrev() {
connectionTest_setUp();
try {
output.checkConnection_(previous);
fail();
} catch (e) {
// expected
}
connectionTest_tearDown();
}
function testCheckConnection_TypePrevInput() {
connectionTest_setUp();
try {
previous.checkConnection_(input);
fail();
} catch (e) {
// expected
}
connectionTest_tearDown();
}
function testCheckConnection_TypePrevOutput() {
connectionTest_setUp();
try {
previous.checkConnection_(output);
fail();
} catch (e) {
// expected
}
connectionTest_tearDown();
}
function testCheckConnection_TypeNextInput() {
connectionTest_setUp();
try {
next.checkConnection_(input);
fail();
} catch (e) {
// expected
}
connectionTest_tearDown();
}
function testCheckConnection_TypeNextOutput() {
connectionTest_setUp();
try {
next.checkConnection_(output);
fail();
} catch (e) {
// expected
}
connectionTest_tearDown();
}
function test_isConnectionAllowed_Distance() {
var sharedWorkspace = {};
// Two connections of opposite types near each other.
var one = helper_createConnection(5 /* x */, 10 /* y */,
Blockly.INPUT_VALUE, null, true);
one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
var two = helper_createConnection(10 /* x */, 15 /* y */,
Blockly.OUTPUT_VALUE, null, true);
two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
assertTrue(one.isConnectionAllowed(two, 20.0));
// Move connections farther apart.
two.x_ = 100;
two.y_ = 100;
assertFalse(one.isConnectionAllowed(two, 20.0));
}
function test_isConnectionAllowed_Unrendered() {
var sharedWorkspace = {};
var one = helper_createConnection(5 /* x */, 10 /* y */,
Blockly.INPUT_VALUE);
one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
// Don't offer to connect an already connected left (male) value plug to
// an available right (female) value plug.
var two = helper_createConnection(0, 0, Blockly.OUTPUT_VALUE);
two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
assertTrue(one.isConnectionAllowed(two));
var three = helper_createConnection(0, 0, Blockly.INPUT_VALUE);
three.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
Blockly.Connection.connectReciprocally_(two, three);
assertFalse(one.isConnectionAllowed(two));
// Don't connect two connections on the same block.
two.sourceBlock_ = one.sourceBlock_;
assertFalse(one.isConnectionAllowed(two));
}
function test_isConnectionAllowed_NoNext() {
var sharedWorkspace = {};
var one = helper_createConnection(0, 0, Blockly.NEXT_STATEMENT);
one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
one.sourceBlock_.nextConnection = one;
var two = helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT);
two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
assertTrue(two.isConnectionAllowed(one));
var three = helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT);
three.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
three.sourceBlock_.previousConnection = three;
Blockly.Connection.connectReciprocally_(one, three);
// A terminal block is allowed to replace another terminal block.
assertTrue(two.isConnectionAllowed(one));
}
function testCheckConnection_Okay() {
connectionTest_setUp();
previous.checkConnection_(next);
next.checkConnection_(previous);
input.checkConnection_(output);
output.checkConnection_(input);
connectionTest_tearDown();
}

View File

@@ -0,0 +1,746 @@
/**
* @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.
*/
/**
* @fileoverview Tests for Blockly.Events
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
goog.require('goog.testing');
goog.require('goog.testing.MockControl');
var mockControl_;
var workspace;
var savedFireFunc = Blockly.Events.fire;
function temporary_fireEvent(event) {
if (!Blockly.Events.isEnabled()) {
return;
}
Blockly.Events.FIRE_QUEUE_.push(event);
Blockly.Events.fireNow_();
}
function eventTest_setUp() {
workspace = new Blockly.Workspace();
mockControl_ = new goog.testing.MockControl();
}
function eventTest_setUpWithMockBlocks() {
eventTest_setUp();
// TODO: Replace with defineGetVarBlock();
Blockly.defineBlocksWithJsonArray([{
'type': 'field_variable_test_block',
'message0': '%1',
'args0': [
{
'type': 'field_variable',
'name': 'VAR',
'variable': 'item'
}
],
},
{
'type': 'simple_test_block',
'message0': 'simple test block'
}]);
}
function eventTest_tearDown() {
delete Blockly.Blocks['field_variable_test_block'];
delete Blockly.Blocks['simple_test_block'];
mockControl_.$tearDown();
workspace.dispose();
}
function eventTest_tearDownWithMockBlocks() {
eventTest_tearDown();
delete Blockly.Blocks.field_variable_test_block;
}
function test_block_base_constructor() {
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, '1');
try {
var block = createSimpleTestBlock(workspace);
// Here's the event we care about.
var event = new Blockly.Events.BlockBase(block);
assertUndefined(event.varId);
checkExactEventValues(event, {'blockId': '1', 'workspaceId': workspace.id,
'group': '', 'recordUndo': true});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_var_base_constructor() {
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, '1');
try {
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarBase(variable);
assertUndefined(event.blockId);
checkExactEventValues(event, {'varId': 'id1',
'workspaceId': workspace.id, 'group': '', 'recordUndo': true});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_abstract_constructor() {
eventTest_setUpWithMockBlocks();
try {
var event = new Blockly.Events.Abstract();
assertUndefined(event.blockId);
assertUndefined(event.workspaceId);
assertUndefined(event.varId);
checkExactEventValues(event, {'group': '', 'recordUndo': true});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
// Test util
function checkCreateEventValues(event, block, ids, type) {
var expected_xml = Blockly.Xml.domToText(Blockly.Xml.blockToDom(block));
var result_xml = Blockly.Xml.domToText(event.xml);
assertEquals(expected_xml, result_xml);
isEqualArrays(ids, event.ids);
assertEquals(type, event.type);
}
// Test util
function checkDeleteEventValues(event, block, ids, type) {
var expected_xml = Blockly.Xml.domToText(Blockly.Xml.blockToDom(block));
var result_xml = Blockly.Xml.domToText(event.oldXml);
assertEquals(expected_xml, result_xml);
isEqualArrays(ids, event.ids);
assertEquals(type, event.type);
}
// Test util
function checkExactEventValues(event, values) {
var keys = Object.keys(values);
for (var i = 0, field; field = keys[i]; i++) {
assertEquals(values[field], event[field]);
}
}
// Test util
function createSimpleTestBlock(workspace) {
// Disable events while constructing the block: this is a test of the
// Blockly.Event constructors, not the block constructor.
Blockly.Events.disable();
var block = new Blockly.Block(workspace, 'simple_test_block');
Blockly.Events.enable();
return block;
}
function test_create_constructor() {
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
var block = createSimpleTestBlock(workspace);
var event = new Blockly.Events.Create(block);
checkCreateEventValues(event, block, ['1'], 'create');
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_blockCreate_constructor() {
// expect that blockCreate behaves the same as create.
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
var block = createSimpleTestBlock(workspace);
var event = new Blockly.Events.BlockCreate(block);
checkCreateEventValues(event, block, ['1'], 'create');
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_delete_constructor() {
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
var block = createSimpleTestBlock(workspace);
var event = new Blockly.Events.Delete(block);
checkDeleteEventValues(event, block, ['1'], 'delete');
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_blockDelete_constructor() {
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
var block = createSimpleTestBlock(workspace);
var event = new Blockly.Events.BlockDelete(block);
checkDeleteEventValues(event, block, ['1'], 'delete');
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_change_constructor() {
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
Blockly.Events.disable();
var block = new Blockly.Block(workspace, 'field_variable_test_block');
Blockly.Events.enable();
var event = new Blockly.Events.Change(block, 'field', 'VAR', 'id1', 'id2');
checkExactEventValues(event, {'element': 'field', 'name': 'VAR',
'oldValue': 'id1', 'newValue': 'id2', 'type': 'change'});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_blockChange_constructor() {
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
Blockly.Events.disable();
var block = new Blockly.Block(workspace, 'field_variable_test_block');
Blockly.Events.enable();
var event = new Blockly.Events.BlockChange(block, 'field', 'VAR', 'id1',
'id2');
checkExactEventValues(event, {'element': 'field', 'name': 'VAR',
'oldValue': 'id1', 'newValue': 'id2', 'type': 'change'});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_move_constructorCoordinate() {
// Expect the oldCoordinate to be set.
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '2']);
try {
var block1 = createSimpleTestBlock(workspace);
var coordinate = new goog.math.Coordinate(3,4);
block1.xy_ = coordinate;
var event = new Blockly.Events.Move(block1);
checkExactEventValues(event, {'oldCoordinate': coordinate,
'type': 'move'});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_move_constructoroldParentId() {
// Expect the oldParentId to be set but not the oldCoordinate to be set.
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '2']);
try {
var block1 = createSimpleTestBlock(workspace);
var block2 = createSimpleTestBlock(workspace);
block1.parentBlock_ = block2;
block1.xy_ = new goog.math.Coordinate(3,4);
var event = new Blockly.Events.Move(block1);
checkExactEventValues(event, {'oldCoordinate': undefined,
'oldParentId': '2', 'type': 'move'});
block1.parentBlock_ = null;
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_blockMove_constructorCoordinate() {
// Expect the oldCoordinate to be set.
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '2']);
try {
var block1 = createSimpleTestBlock(workspace);
var coordinate = new goog.math.Coordinate(3,4);
block1.xy_ = coordinate;
var event = new Blockly.Events.BlockMove(block1);
checkExactEventValues(event, {'oldCoordinate': coordinate,
'type': 'move'});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_blockMove_constructoroldParentId() {
// Expect the oldParentId to be set but not the oldCoordinate to be set.
eventTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '2']);
try {
var block1 = createSimpleTestBlock(workspace);
var block2 = createSimpleTestBlock(workspace);
block1.parentBlock_ = block2;
block1.xy_ = new goog.math.Coordinate(3,4);
var event = new Blockly.Events.BlockMove(block1);
checkExactEventValues(event, {'oldCoordinate': undefined,
'oldParentId': '2', 'type': 'move'});
block1.parentBlock_ = null;
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_varCreate_constructor() {
eventTest_setUp();
try {
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarCreate(variable);
checkExactEventValues(event, {'varName': 'name1', 'varType': 'type1',
'type': 'var_create'});
} finally {
eventTest_tearDown();
}
}
function test_varCreate_toJson() {
eventTest_setUp();
try {
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarCreate(variable);
var json = event.toJson();
var expectedJson = ({type: "var_create", varId: "id1", varType: "type1",
varName: "name1"});
assertEquals(JSON.stringify(expectedJson), JSON.stringify(json));
} finally {
eventTest_tearDown();
}
}
function test_varCreate_fromJson() {
eventTest_setUp();
try {
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarCreate(variable);
var event2 = new Blockly.Events.VarCreate(null);
var json = event.toJson();
event2.fromJson(json);
assertEquals(JSON.stringify(json), JSON.stringify(event2.toJson()));
} finally {
eventTest_tearDown();
}
}
function test_varCreate_runForward() {
eventTest_setUp();
var json = {type: "var_create", varId: "id1", varType: "type1",
varName: "name1"};
var event = Blockly.Events.fromJson(json, workspace);
assertNull(workspace.getVariableById('id1'));
event.run(true);
checkVariableValues(workspace, 'name1', 'type1', 'id1');
eventTest_tearDown();
}
function test_varCreate_runBackwards() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarCreate(variable);
assertNotNull(workspace.getVariableById('id1'));
event.run(false);
assertNull(workspace.getVariableById('id1'));
eventTest_tearDown();
}
function test_varDelete_constructor() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarDelete(variable);
checkExactEventValues(event, {'varName': 'name1', 'varType': 'type1',
'varId':'id1', 'type': 'var_delete'});
eventTest_tearDown();
}
function test_varDelete_toJson() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarDelete(variable);
var json = event.toJson();
var expectedJson = ({type: "var_delete", varId: "id1", varType: "type1",
varName: "name1"});
assertEquals(JSON.stringify(expectedJson), JSON.stringify(json));
eventTest_tearDown();
}
function test_varDelete_fromJson() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarDelete(variable);
var event2 = new Blockly.Events.VarDelete(null);
var json = event.toJson();
event2.fromJson(json);
assertEquals(JSON.stringify(json), JSON.stringify(event2.toJson()));
eventTest_tearDown();
}
function test_varDelete_runForwards() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarDelete(variable);
assertNotNull(workspace.getVariableById('id1'));
event.run(true);
assertNull(workspace.getVariableById('id1'));
eventTest_tearDown();
}
function test_varDelete_runBackwards() {
eventTest_setUp();
var json = {type: "var_delete", varId: "id1", varType: "type1",
varName: "name1"};
var event = Blockly.Events.fromJson(json, workspace);
assertNull(workspace.getVariableById('id1'));
event.run(false);
checkVariableValues(workspace, 'name1', 'type1', 'id1');
eventTest_tearDown();
}
function test_varRename_constructor() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarRename(variable, 'name2');
checkExactEventValues(event, {'varId': 'id1', 'oldName': 'name1',
'newName': 'name2', 'type': 'var_rename'});
eventTest_tearDown();
}
function test_varRename_toJson() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarRename(variable, 'name2');
var json = event.toJson();
var expectedJson = ({type: "var_rename", varId: "id1", oldName: "name1",
newName: "name2"});
assertEquals(JSON.stringify(expectedJson), JSON.stringify(json));
eventTest_tearDown();
}
function test_varRename_fromJson() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarRename(variable, '');
var event2 = new Blockly.Events.VarRename(null);
var json = event.toJson();
event2.fromJson(json);
assertEquals(JSON.stringify(json), JSON.stringify(event2.toJson()));
eventTest_tearDown();
}
function test_varRename_runForward() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarRename(variable, 'name2');
event.run(true);
assertNull(workspace.getVariable('name1'));
checkVariableValues(workspace, 'name2', 'type1', 'id1');
eventTest_tearDown();
}
function test_varBackard_runForward() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarRename(variable, 'name2');
event.run(false);
assertNull(workspace.getVariable('name2'));
checkVariableValues(workspace, 'name1', 'type1', 'id1');
eventTest_tearDown();
}
function test_events_filter() {
eventTest_setUpWithMockBlocks();
try {
var block1 = workspace.newBlock('field_variable_test_block', '1');
var events = [
new Blockly.Events.BlockCreate(block1),
new Blockly.Events.BlockMove(block1),
new Blockly.Events.BlockChange(block1, 'field', 'VAR', 'id1', 'id2'),
new Blockly.Events.Ui(block1, 'click')
];
var filteredEvents = Blockly.Events.filter(events, true);
assertEquals(4, filteredEvents.length); // no event should have been removed.
// test that the order hasn't changed
assertTrue(filteredEvents[0] instanceof Blockly.Events.BlockCreate);
assertTrue(filteredEvents[1] instanceof Blockly.Events.BlockMove);
assertTrue(filteredEvents[2] instanceof Blockly.Events.BlockChange);
assertTrue(filteredEvents[3] instanceof Blockly.Events.Ui);
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_events_filterForward() {
eventTest_setUpWithMockBlocks();
try {
var block1 = workspace.newBlock('field_variable_test_block', '1');
var events = [
new Blockly.Events.BlockCreate(block1),
];
helper_addMoveEvent(events, block1, 1, 1);
helper_addMoveEvent(events, block1, 2, 2);
helper_addMoveEvent(events, block1, 3, 3);
var filteredEvents = Blockly.Events.filter(events, true);
assertEquals(2, filteredEvents.length); // duplicate moves should have been removed.
// test that the order hasn't changed
assertTrue(filteredEvents[0] instanceof Blockly.Events.BlockCreate);
assertTrue(filteredEvents[1] instanceof Blockly.Events.BlockMove);
assertEquals(3, filteredEvents[1].newCoordinate.x);
assertEquals(3, filteredEvents[1].newCoordinate.y);
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_events_filterBackward() {
eventTest_setUpWithMockBlocks();
try {
var block1 = workspace.newBlock('field_variable_test_block', '1');
var events = [
new Blockly.Events.BlockCreate(block1),
];
helper_addMoveEvent(events, block1, 1, 1);
helper_addMoveEvent(events, block1, 2, 2);
helper_addMoveEvent(events, block1, 3, 3);
var filteredEvents = Blockly.Events.filter(events, false);
assertEquals(2, filteredEvents.length); // duplicate event should have been removed.
// test that the order hasn't changed
assertTrue(filteredEvents[0] instanceof Blockly.Events.BlockCreate);
assertTrue(filteredEvents[1] instanceof Blockly.Events.BlockMove);
assertEquals(1, filteredEvents[1].newCoordinate.x);
assertEquals(1, filteredEvents[1].newCoordinate.y);
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_events_filterDifferentBlocks() {
eventTest_setUpWithMockBlocks();
var block1 = workspace.newBlock('field_variable_test_block', '1');
var block2 = workspace.newBlock('field_variable_test_block', '2');
var events = [
new Blockly.Events.BlockCreate(block1),
new Blockly.Events.BlockMove(block1),
new Blockly.Events.BlockCreate(block2),
new Blockly.Events.BlockMove(block2)
];
var filteredEvents = Blockly.Events.filter(events, true);
assertEquals(4, filteredEvents.length); // no event should have been removed.
eventTest_tearDownWithMockBlocks();
}
function test_events_mergeMove() {
eventTest_setUpWithMockBlocks();
var block1 = workspace.newBlock('field_variable_test_block', '1');
var events = [];
helper_addMoveEvent(events, block1, 0, 0);
helper_addMoveEvent(events, block1, 1, 1);
var filteredEvents = Blockly.Events.filter(events, true);
assertEquals(1, filteredEvents.length); // second move event merged into first
assertEquals(1, filteredEvents[0].newCoordinate.x);
assertEquals(1, filteredEvents[0].newCoordinate.y);
eventTest_tearDownWithMockBlocks();
}
function test_events_mergeChange() {
eventTest_setUpWithMockBlocks();
var block1 = workspace.newBlock('field_variable_test_block', '1');
var events = [
new Blockly.Events.Change(block1, 'field', 'VAR', 'item', 'item1'),
new Blockly.Events.Change(block1, 'field', 'VAR', 'item1', 'item2')
];
var filteredEvents = Blockly.Events.filter(events, true);
assertEquals(1, filteredEvents.length); // second change event merged into first
assertEquals('item', filteredEvents[0].oldValue);
assertEquals('item2', filteredEvents[0].newValue);
eventTest_tearDownWithMockBlocks();
}
function test_events_mergeUi() {
eventTest_setUpWithMockBlocks();
var block1 = workspace.newBlock('field_variable_test_block', '1');
var block2 = workspace.newBlock('field_variable_test_block', '2');
var block3 = workspace.newBlock('field_variable_test_block', '3');
var events = [
new Blockly.Events.Ui(block1, 'commentOpen', 'false', 'true'),
new Blockly.Events.Ui(block1, 'click', 'false', 'true'),
new Blockly.Events.Ui(block2, 'mutatorOpen', 'false', 'true'),
new Blockly.Events.Ui(block2, 'click', 'false', 'true'),
new Blockly.Events.Ui(block3, 'warningOpen', 'false', 'true'),
new Blockly.Events.Ui(block3, 'click', 'false', 'true')
];
var filteredEvents = Blockly.Events.filter(events, true);
assertEquals(3, filteredEvents.length); // click event merged into corresponding *Open event
assertEquals('commentOpen', filteredEvents[0].element);
assertEquals('mutatorOpen', filteredEvents[1].element);
assertEquals('warningOpen', filteredEvents[2].element);
eventTest_tearDownWithMockBlocks();
}
/**
* Tests that events that collide on a (event, block, workspace) tuple
* but cannot be merged do not get dropped during filtering.
*/
function test_events_stackclick() {
eventTest_setUpWithMockBlocks();
var block = workspace.newBlock('field_variable_test_block', '1');
var events = [
new Blockly.Events.Ui(block, 'click', undefined, undefined),
new Blockly.Events.Ui(block, 'stackclick', undefined, undefined)
];
var filteredEvents = Blockly.Events.filter(events, true);
// click and stackclick should both exist
assertEquals(2, filteredEvents.length);
assertEquals('click', filteredEvents[0].element);
assertEquals('stackclick', filteredEvents[1].element);
eventTest_tearDownWithMockBlocks();
}
/**
* Mutator composition could result in move events for blocks
* connected to the mutated block that were null operations. This
* leads to events in the undo/redo queue that do nothing, requiring
* an extra undo/redo to proceed to the next event. This test ensures
* that two move events that do get merged (disconnecting and
* reconnecting a block in response to a mutator change) are filtered
* from the queue.
*/
function test_events_filteraftermerge() {
eventTest_setUpWithMockBlocks();
var block = workspace.newBlock('field_variable_test_block', '1');
block.setParent(null);
var events = [];
helper_addMoveEventParent(events, block, null);
helper_addMoveEventParent(events, block, null);
var filteredEvents = Blockly.Events.filter(events, true);
// The two events should be merged, but because nothing has changed
// they will be filtered out.
assertEquals(0, filteredEvents.length);
eventTest_tearDownWithMockBlocks();
}
/**
* Helper function to simulate block move events.
*
* @param {!Array.<Blockly.Events.Abstract>} events a queue of events.
* @param {!Blockly.Block} block the block to be moved
* @param {number} newX new X coordinate of the block
* @param {number} newY new Y coordinate of the block
*/
function helper_addMoveEvent(events, block, newX, newY) {
events.push(new Blockly.Events.BlockMove(block));
block.xy_ = new goog.math.Coordinate(newX, newY);
events[events.length-1].recordNew();
}
function helper_addMoveEventParent(events, block, parent) {
events.push(new Blockly.Events.BlockMove(block));
block.setParent(parent);
events[events.length-1].recordNew();
}
function test_events_newblock_newvar() {
eventTest_setUpWithMockBlocks();
Blockly.Events.fire = temporary_fireEvent;
temporary_fireEvent.firedEvents_ = [];
// Expect three calls to genUid: one to set the block's ID, one for the event
// group's id, and one for the variable's ID.
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '2', '3']);
try {
var block = workspace.newBlock('field_variable_test_block');
var firedEvents = workspace.undoStack_;
// Expect two events: varCreate and block create.
assertEquals(2, firedEvents.length);
var event0 = firedEvents[0];
var event1 = firedEvents[1];
assertEquals('var_create', event0.type);
assertEquals('create', event1.type);
// Expect the events to have the same group ID.
assertEquals(event0.group, event1.group);
// Expect the group ID to be the result of the second call to genUid.
assertEquals('2', event0.group);
// Expect the workspace to have a variable with ID '3'.
assertNotNull(workspace.getVariableById('3'));
assertEquals('3', event0.varId);
} finally {
eventTest_tearDownWithMockBlocks();
Blockly.Events.fire = savedFireFunc;
}
}
// The sequence of events should be the same whether the block was created from
// XML or directly.
function test_events_newblock_newvar_xml() {
eventTest_setUpWithMockBlocks();
Blockly.Events.fire = temporary_fireEvent;
temporary_fireEvent.firedEvents_ = [];
try {
var dom = Blockly.Xml.textToDom(
'<xml>' +
' <block type="field_variable_test_block" id="block1">' +
' <field name="VAR" id="id1" variabletype="">name1</field>' +
' </block>' +
'</xml>');
Blockly.Xml.domToWorkspace(dom, workspace);
var firedEvents = workspace.undoStack_;
// Expect two events: varCreate and block create.
assertEquals(2, firedEvents.length);
var event0 = firedEvents[0];
var event1 = firedEvents[1];
assertEquals('var_create', event0.type);
assertEquals('create', event1.type);
// Expect the events to have the same group ID.
assertEquals(event0.group, event1.group);
// Expect the workspace to have a variable with ID 'id1'.
assertNotNull(workspace.getVariableById('id1'));
assertEquals('id1', event0.varId);
} finally {
eventTest_tearDownWithMockBlocks();
Blockly.Events.fire = savedFireFunc;
}
}

View File

@@ -0,0 +1,657 @@
/**
* @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.
*/
/**
* @fileoverview Tests for Blockly.Extensions
* @author Anm@anm.me (Andrew n marshall)
*/
'use strict';
function test_extension() {
var workspace = new Blockly.Workspace();
var block;
try {
assertUndefined(Blockly.Extensions.ALL_['extensions_test']);
var numCallsToBefore = 0;
var numCallsToAfter = 0;
// Extension defined before the block type is defined.
Blockly.Extensions.register('extensions_test_before', function () {
numCallsToBefore++;
this.extendedWithBefore = true;
});
Blockly.defineBlocksWithJsonArray([{
"type": "extension_test_block",
"message0": "extension_test_block",
"extensions": ["extensions_test_before", "extensions_test_after"]
}]);
// Extension defined after the block type (but before instantiation).
Blockly.Extensions.register('extensions_test_after', function () {
numCallsToAfter++;
this.extendedWithAfter = true;
});
assert(goog.isFunction(Blockly.Extensions.ALL_['extensions_test_before']));
assert(goog.isFunction(Blockly.Extensions.ALL_['extensions_test_after']));
assertEquals(0, numCallsToBefore);
assertEquals(0, numCallsToAfter);
block = new Blockly.Block(workspace, 'extension_test_block');
assertEquals(1, numCallsToBefore);
assertEquals(1, numCallsToAfter);
assert(block.extendedWithBefore);
assert(block.extendedWithAfter);
} finally {
block && block.dispose();
workspace.dispose();
delete Blockly.Extensions.ALL_['extensions_test_before'];
delete Blockly.Extensions.ALL_['extensions_test_after'];
delete Blockly.Blocks['extension_test_block'];
}
}
function test_extension_missing() {
var workspace = new Blockly.Workspace();
var block;
var exceptionWasThrown = false;
try {
assertUndefined(Blockly.Extensions.ALL_['missing_extension']);
Blockly.defineBlocksWithJsonArray([{
"type": "missing_extension_block",
"message0": "missing_extension_block",
"extensions": ["missing_extension"]
}]);
block = new Blockly.Block(workspace, 'missing_extension_block');
} catch (e) {
// Expected.
exceptionWasThrown = true;
} finally {
block && block.dispose();
workspace.dispose();
delete Blockly.Blocks['missing_extension_block'];
}
assert(exceptionWasThrown);
}
function test_extension_not_a_function() {
var exceptionWasThrown = false;
try {
assertUndefined(Blockly.Extensions.ALL_['extension_just_a_string']);
Blockly.Extensions.register('extension_just_a_string', 'extension_just_a_string');
} catch (e) {
// Expected.
exceptionWasThrown = true;
} finally {
delete Blockly.Extensions.ALL_['extension_just_a_string'];
}
assert(exceptionWasThrown);
var exceptionWasThrown = false;
try {
assertUndefined(Blockly.Extensions.ALL_['extension_is_null']);
Blockly.Extensions.register('extension_is_null', null);
} catch (e) {
// Expected.
exceptionWasThrown = true;
} finally {
delete Blockly.Extensions.ALL_['extension_is_null'];
}
assert(exceptionWasThrown);
var exceptionWasThrown = false;
try {
assertUndefined(Blockly.Extensions.ALL_['extension_is_undefined']);
Blockly.Extensions.register('extension_is_undefined');
} catch (e) {
// Expected.
exceptionWasThrown = true;
} finally {
delete Blockly.Extensions.ALL_['extension_is_undefined'];
}
assert(exceptionWasThrown);
}
function test_parent_tooltip_when_inline() {
var defaultTooltip = "defaultTooltip";
var parentTooltip = "parentTooltip";
var workspace = new Blockly.Workspace();
var block;
try {
Blockly.defineBlocksWithJsonArray([
{
"type": "test_parent_tooltip_when_inline",
"message0": "test_parent_tooltip_when_inline",
"output": true,
"tooltip": defaultTooltip,
"extensions": ["parent_tooltip_when_inline"]
},
{
"type": "test_parent",
"message0": "%1",
"args0": [
{
"type": "input_value",
"name": "INPUT"
}
],
"tooltip": parentTooltip
}
]);
block = new Blockly.Block(workspace, 'test_parent_tooltip_when_inline');
// Tooltip is dynamic after extension initialization.
assert(goog.isFunction(block.tooltip));
assertEquals(block.tooltip(), defaultTooltip);
// Tooltip is normal before connected to parent.
var parent = new Blockly.Block(workspace, 'test_parent');
assertEquals(parent.tooltip, parentTooltip);
assertFalse(!!parent.inputsInline);
// Tooltip is normal when parent is not inline.
parent.getInput('INPUT').connection.connect(block.outputConnection);
assertEquals(block.getParent(), parent);
assertEquals(block.tooltip(), defaultTooltip);
// Tooltip is parent's when parent is inline.
parent.setInputsInline(true);
assertEquals(block.tooltip(), parentTooltip);
// Tooltip revert when disconnected.
parent.getInput('INPUT').connection.disconnect();
assert(!block.getParent());
assertEquals(block.tooltip(), defaultTooltip);
} finally {
block && block.dispose();
workspace.dispose();
delete Blockly.Blocks['test_parent_tooltip_when_inline'];
delete Blockly.Blocks['test_parent'];
}
}
function test_mixin_extension() {
var TEST_MIXIN = {
field: 'FIELD',
method: function() {
console.log('TEXT_MIXIN method()');
}
};
var workspace = new Blockly.Workspace();
var block;
try {
assertUndefined(Blockly.Extensions.ALL_['mixin_test']);
// Extension defined before the block type is defined.
Blockly.Extensions.registerMixin('mixin_test', TEST_MIXIN);
assert(goog.isFunction(Blockly.Extensions.ALL_['mixin_test']));
Blockly.defineBlocksWithJsonArray([{
"type": "test_block_mixin",
"message0": "test_block_mixin",
"extensions": ["mixin_test"]
}]);
block = new Blockly.Block(workspace, 'test_block_mixin');
assertEquals(TEST_MIXIN.field, block.field);
assertEquals(TEST_MIXIN.method, block.method);
} finally {
block && block.dispose();
workspace.dispose();
delete Blockly.Extensions.ALL_['mixin_test'];
delete Blockly.Blocks['test_block_mixin'];
}
}
function test_bad_mixin_overwrites_local_value() {
var TEST_MIXIN_BAD_INPUTLIST = {
inputList: 'bad inputList' // Defined in constructor
};
var workspace = new Blockly.Workspace();
var block;
try {
assertUndefined(Blockly.Extensions.ALL_['mixin_bad_inputList']);
// Extension defined before the block type is defined.
Blockly.Extensions.registerMixin('mixin_bad_inputList', TEST_MIXIN_BAD_INPUTLIST);
assert(goog.isFunction(Blockly.Extensions.ALL_['mixin_bad_inputList']));
Blockly.defineBlocksWithJsonArray([{
"type": "test_block_bad_inputList",
"message0": "test_block_bad_inputList",
"extensions": ["mixin_bad_inputList"]
}]);
try {
block = new Blockly.Block(workspace, 'test_block_bad_inputList');
} catch (e) {
// Expected Error
assert(e.message.indexOf('inputList') >= 0); // Reference the conflict
return;
}
fail('Expected error when constructing block');
} finally {
block && block.dispose();
workspace.dispose();
delete Blockly.Extensions.ALL_['mixin_bad_inputList'];
delete Blockly.Blocks['test_block_bad_inputList'];
}
}
function test_bad_mixin_overwrites_prototype() {
var TEST_MIXIN_BAD_COLOUR = {
colour_: 'bad colour_' // Defined on prototype
};
var workspace = new Blockly.Workspace();
var block;
try {
assertUndefined(Blockly.Extensions.ALL_['mixin_bad_colour_']);
// Extension defined before the block type is defined.
Blockly.Extensions.registerMixin('mixin_bad_colour_', TEST_MIXIN_BAD_COLOUR);
assert(goog.isFunction(Blockly.Extensions.ALL_['mixin_bad_colour_']));
Blockly.defineBlocksWithJsonArray([{
"type": "test_block_bad_colour",
"message0": "test_block_bad_colour",
"extensions": ["mixin_bad_colour_"]
}]);
try {
block = new Blockly.Block(workspace, 'test_block_bad_colour');
} catch (e) {
// Expected Error
assert(e.message.indexOf('colour_') >= 0); // Reference the conflict
return;
}
fail('Expected error when constructing block');
} finally {
block && block.dispose();
workspace.dispose();
delete Blockly.Extensions.ALL_['mixin_bad_colour_'];
delete Blockly.Blocks['test_block_bad_colour'];
}
}
function test_mutator_mixin() {
var workspace = new Blockly.Workspace();
var block;
try {
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
"message0": "mutator_test_block",
"mutator": "mutator_test"
}]);
// Events code calls mutationToDom and expects it to give back a meaningful
// value.
Blockly.Events.disable();
Blockly.Extensions.registerMutator('mutator_test',
{
domToMutation: function() {
return 'domToMutationFn';
},
mutationToDom: function() {
return 'mutationToDomFn';
},
compose: function() {
return 'composeFn';
},
decompose: function() {
return 'decomposeFn';
}
});
block = new Blockly.Block(workspace, 'mutator_test_block');
// Make sure all of the functions were installed correctly.
assertEquals(block.domToMutation(), 'domToMutationFn');
assertEquals(block.mutationToDom(), 'mutationToDomFn');
assertEquals(block.compose(), 'composeFn');
assertEquals(block.decompose(), 'decomposeFn');
} finally {
if (block) {
block.dispose();
}
workspace.dispose();
Blockly.Events.enable();
delete Blockly.Extensions.ALL_['mutator_test'];
}
}
function test_mutator_mixin_no_dialog() {
var workspace = new Blockly.Workspace();
var block;
try {
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
"message0": "mutator_test_block",
"mutator": "mutator_test"
}]);
// Events code calls mutationToDom and expects it to give back a meaningful
// value.
Blockly.Events.disable();
assertUndefined(Blockly.Extensions.ALL_['mutator_test']);
Blockly.Extensions.registerMutator('mutator_test',
{
domToMutation: function() {
return 'domToMutationFn';
},
mutationToDom: function() {
return 'mutationToDomFn';
}
});
block = new Blockly.Block(workspace, 'mutator_test_block');
// Make sure all of the functions were installed correctly.
assertEquals(block.domToMutation(), 'domToMutationFn');
assertEquals(block.mutationToDom(), 'mutationToDomFn');
assertFalse(block.hasOwnProperty('compose'));
assertFalse(block.hasOwnProperty('decompose'));
} finally {
if (block) {
block.dispose();
}
workspace.dispose();
Blockly.Events.enable();
delete Blockly.Extensions.ALL_['mutator_test'];
}
}
// Explicitly check all four things that could be missing.
function test_mutator_mixin_no_decompose_fails() {
var exceptionWasThrown = false;
try {
Blockly.Extensions.registerMutator('mutator_test',
{
domToMutation: function() {
return 'domToMutationFn';
},
mutationToDom: function() {
return 'mutationToDomFn';
},
compose: function() {
return 'composeFn';
}
});
} catch (e) {
// Expected.
exceptionWasThrown = true;
} finally {
delete Blockly.Extensions.ALL_['mutator_test'];
}
assertTrue(exceptionWasThrown);
}
function test_mutator_mixin_no_compose_fails() {
var exceptionWasThrown = false;
try {
Blockly.Extensions.registerMutator('mutator_test',
{
domToMutation: function() {
return 'domToMutationFn';
},
mutationToDom: function() {
return 'mutationToDomFn';
},
decompose: function() {
return 'decomposeFn';
}
});
} catch (e) {
// Expected.
exceptionWasThrown = true;
} finally {
delete Blockly.Extensions.ALL_['mutator_test'];
}
assertTrue(exceptionWasThrown);
}
function test_mutator_mixin_no_domToMutation_fails() {
var exceptionWasThrown = false;
try {
Blockly.Extensions.registerMutator('mutator_test',
{
mutationToDom: function() {
return 'mutationToDomFn';
},
compose: function() {
return 'composeFn';
},
decompose: function() {
return 'decomposeFn';
}
});
} catch (e) {
// Expected.
exceptionWasThrown = true;
} finally {
delete Blockly.Extensions.ALL_['mutator_test'];
}
assertTrue(exceptionWasThrown);
}
function test_mutator_mixin_no_mutationToDom_fails() {
var exceptionWasThrown = false;
try {
Blockly.Extensions.registerMutator('mutator_test',
{
domToMutation: function() {
return 'domToMutationFn';
},
compose: function() {
return 'composeFn';
},
decompose: function() {
return 'decomposeFn';
}
});
} catch (e) {
// Expected.
exceptionWasThrown = true;
} finally {
delete Blockly.Extensions.ALL_['mutator_test'];
}
assertTrue(exceptionWasThrown);
}
function test_use_mutator_as_extension_fails() {
var workspace = new Blockly.Workspace();
var block;
var exceptionWasThrown = false;
try {
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
"message0": "mutator_test_block",
"extensions": ["mutator_test"]
}]);
Blockly.Events.disable();
assertUndefined(Blockly.Extensions.ALL_['mutator_test']);
Blockly.Extensions.registerMutator('mutator_test',
{
domToMutation: function() {
return 'domToMutationFn';
},
mutationToDom: function() {
return 'mutationToDomFn';
}
});
// Events code calls mutationToDom and expects it to give back a meaningful
// value.
block = new Blockly.Block(workspace, 'mutator_test_block');
} catch (e) {
// Expected
exceptionWasThrown = true;
// Should have failed on apply, not on register.
assertNotNull(Blockly.Extensions.ALL_['mutator_test']);
} finally {
if (block) {
block.dispose();
}
workspace.dispose();
Blockly.Events.enable();
delete Blockly.Extensions.ALL_['mutator_test'];
}
assertTrue(exceptionWasThrown);
}
function test_use_mutator_mixin_as_extension_fails() {
var workspace = new Blockly.Workspace();
var block;
var exceptionWasThrown = false;
try {
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
"message0": "mutator_test_block",
"extensions": ["mutator_test"]
}]);
// Events code calls mutationToDom and expects it to give back a meaningful
// value.
Blockly.Events.disable();
assertUndefined(Blockly.Extensions.ALL_['mutator_test']);
Blockly.Extensions.registerMixin('mutator_test',
{
domToMutation: function() {
return 'domToMutationFn';
},
mutationToDom: function() {
return 'mutationToDomFn';
}
});
block = new Blockly.Block(workspace, 'mutator_test_block');
} catch (e) {
// Expected
exceptionWasThrown = true;
// Should have failed on apply, not on register.
assertNotNull(Blockly.Extensions.ALL_['mutator_test']);
} finally {
if (block) {
block.dispose();
}
workspace.dispose();
Blockly.Events.enable();
delete Blockly.Extensions.ALL_['mutator_test'];
}
assertTrue(exceptionWasThrown);
}
function test_use_extension_as_mutator_fails() {
var workspace = new Blockly.Workspace();
var block;
var exceptionWasThrown = false;
try {
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
"message0": "mutator_test_block",
"mutator": ["extensions_test"]
}]);
// Events code calls mutationToDom and expects it to give back a meaningful
// value.
Blockly.Events.disable();
assertUndefined(Blockly.Extensions.ALL_['extensions_test']);
Blockly.Extensions.register('extensions_test', function() {
return 'extensions_test_fn';
});
block = new Blockly.Block(workspace, 'mutator_test_block');
} catch (e) {
// Expected
exceptionWasThrown = true;
// Should have failed on apply, not on register.
assertNotNull(Blockly.Extensions.ALL_['extensions_test']);
} finally {
if (block) {
block.dispose();
}
workspace.dispose();
Blockly.Events.enable();
delete Blockly.Extensions.ALL_['extensions_test'];
}
assertTrue(exceptionWasThrown);
}
function test_mutator_mixin_plus_function() {
var workspace = new Blockly.Workspace();
var block;
var fnWasCalled = false;
try {
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
"message0": "mutator_test_block",
"mutator": ["extensions_test"]
}]);
Blockly.Events.disable();
assertUndefined(Blockly.Extensions.ALL_['extensions_test']);
Blockly.Extensions.registerMutator('extensions_test',
{
domToMutation: function() {
return 'domToMutationFn';
},
mutationToDom: function() {
return 'mutationToDomFn';
}
},
function() {
fnWasCalled = true;
}
);
// Events code calls mutationToDom and expects it to give back a meaningful
// value.
block = new Blockly.Block(workspace, 'mutator_test_block');
} finally {
if (block) {
block.dispose();
}
workspace.dispose();
Blockly.Events.enable();
delete Blockly.Extensions.ALL_['extensions_test'];
}
assertTrue(fnWasCalled);
}

View File

@@ -0,0 +1,44 @@
/**
* @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.
*/
/**
* @fileoverview Tests for Blockly.FieldAngle
* @author Anm@anm.me (Andrew n marshall)
*/
'use strict';
function test_fieldangle_constructor() {
assertEquals(new Blockly.FieldAngle().getValue(), '0');
assertEquals(new Blockly.FieldAngle(null).getValue(), '0');
assertEquals(new Blockly.FieldAngle(undefined).getValue(), '0');
assertEquals(new Blockly.FieldAngle(1).getValue(), '1');
assertEquals(new Blockly.FieldAngle(1.5).getValue(), '1.5');
assertEquals(new Blockly.FieldAngle('2').getValue(), '2');
assertEquals(new Blockly.FieldAngle('2.5').getValue(), '2.5');
// Bad values
assertEquals(new Blockly.FieldAngle('bad').getValue(), '0');
assertEquals(new Blockly.FieldAngle(NaN).getValue(), '0');
}
function test_fieldangle_fromJson() {
assertEquals(Blockly.FieldAngle.fromJson({}).getValue(), '0');
assertEquals(Blockly.FieldAngle.fromJson({ angle: 1 }).getValue(), '1');
}

View File

@@ -0,0 +1,80 @@
/**
* @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.
*/
/**
* @fileoverview Tests for Blockly.FieldNumber
* @author Anm@anm.me (Andrew n marshall)
*/
'use strict';
function test_fieldnumber_constructor() {
// No arguments
var field = new Blockly.FieldNumber();
assertEquals(field.getValue(), '0');
assertEquals(field.min_, -Infinity);
assertEquals(field.max_, Infinity);
assertEquals(field.precision_, 0);
// Numeric values
field = new Blockly.FieldNumber(1);
assertEquals(field.getValue(), '1');
field = new Blockly.FieldNumber(1.5);
assertEquals(field.getValue(), '1.5');
// String value
field = new Blockly.FieldNumber('2');
assertEquals(field.getValue(), '2');
field = new Blockly.FieldNumber('2.5');
assertEquals(field.getValue(), '2.5');
// All values
field = new Blockly.FieldNumber(
/* value */ 0,
/* min */ -128,
/* max */ 127,
/* precision */ 1);
assertEquals(field.getValue(), '0');
assertEquals(field.min_, -128);
assertEquals(field.max_, 127);
assertEquals(field.precision_, 1);
// Bad value defaults to '0'
field = new Blockly.FieldNumber('bad');
assertEquals(field.getValue(), '0');
field = new Blockly.FieldNumber(NaN);
assertEquals(field.getValue(), '0');
}
function test_fieldnumber_fromJson() {
assertEquals(Blockly.FieldNumber.fromJson({}).getValue(), '0');
assertEquals(Blockly.FieldNumber.fromJson({ value: 1 }).getValue(), '1');
// All options
var field = Blockly.FieldNumber.fromJson({
value: 0,
min: -128,
max: 127,
precision: 1
});
assertEquals(field.getValue(), '0');
assertEquals(field.min_, -128);
assertEquals(field.max_, 127);
assertEquals(field.precision_, 1);
}

View File

@@ -0,0 +1,126 @@
/**
* @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.
*/
/**
* @fileoverview Tests for Blockly.Field
* @author fenichel@google.com (Rachel Fenichel)
*/
'use strict';
function test_field_isEditable_simple() {
var field = new Blockly.Field("Dummy text");
// EDITABLE is true by default, but without a source block a field can't be
// edited.
assertFalse('Field without a block is not editable',
field.isCurrentlyEditable());
}
function test_field_isEditable_false() {
// Setting EDITABLE to false doesn't matter.
var field = new Blockly.Field("Dummy text");
field.EDITABLE = false;
assertFalse('Field without a block is not editable',
field.isCurrentlyEditable());
}
function test_field_isEditable_editableBlock() {
var editableBlock = {
isEditable: function() {
return true;
}
};
var field = new Blockly.Field("Dummy text");
field.sourceBlock_ = editableBlock;
assertTrue('Editable field with editable block is editable',
field.isCurrentlyEditable());
}
function test_field_isEditable_editableBlock_false() {
var editableBlock = {
isEditable: function() {
return true;
}
};
var field = new Blockly.Field("Dummy text");
field.sourceBlock_ = editableBlock;
field.EDITABLE = false;
assertFalse('Non-editable field with editable block is not editable',
field.isCurrentlyEditable());
}
function test_field_isEditable_nonEditableBlock() {
var nonEditableBlock = {
isEditable: function() {
return false;
}
};
var field = new Blockly.Field("Dummy text");
field.sourceBlock_ = nonEditableBlock;
assertFalse('Editable field with non-editable block is not editable',
field.isCurrentlyEditable());
}
function test_field_isEditable_nonEditableBlock_false() {
var nonEditableBlock = {
isEditable: function() {
return false;
}
};
var field = new Blockly.Field("Dummy text");
field.sourceBlock_ = nonEditableBlock;
field.EDITABLE = false;
assertFalse('Non-editable field with non-editable block is not editable',
field.isCurrentlyEditable());
}
function test_field_register_with_custom_field() {
var CustomFieldType = function(value) {
CustomFieldType.superClass_.constructor.call(this, value);
};
goog.inherits(CustomFieldType, Blockly.Field);
CustomFieldType.fromJson = function(options) {
return new CustomFieldType(options['value']);
};
var json = {
type: 'field_custom_test',
value: 'ok'
};
// before registering
var field = Blockly.Field.fromJson(json);
assertNull(field);
Blockly.Field.register('field_custom_test', CustomFieldType);
// after registering
field = Blockly.Field.fromJson(json);
assertNotNull(field);
assertEquals(field.getValue(), 'ok');
}

View File

@@ -0,0 +1,237 @@
/**
* @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.
*/
/**
* @fileoverview Tests for Blockly.FieldVariable
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
goog.require('goog.testing');
goog.require('goog.testing.MockControl');
var workspace;
var mockControl_;
function fieldVariableTestWithMocks_setUp() {
workspace = new Blockly.Workspace();
mockControl_ = new goog.testing.MockControl();
}
function fieldVariableTestWithMocks_tearDown() {
mockControl_.$tearDown();
workspace.dispose();
}
function fieldVariable_mockBlock(workspace) {
return {'workspace': workspace, 'isShadow': function(){return false;}};
}
function fieldVariable_createAndInitField(workspace) {
var fieldVariable = new Blockly.FieldVariable('name1');
var mockBlock = fieldVariable_mockBlock(workspace);
fieldVariable.setSourceBlock(mockBlock);
// No view to initialize, but the model still needs work.
fieldVariable.initModel();
return fieldVariable;
}
function test_fieldVariable_Constructor() {
workspace = new Blockly.Workspace();
var fieldVariable = new Blockly.FieldVariable('name1');
// The field does not have a variable until after init() is called.
assertEquals('', fieldVariable.getText());
workspace.dispose();
}
function test_fieldVariable_setValueMatchId() {
// Expect the fieldVariable value to be set to variable name
fieldVariableTestWithMocks_setUp();
workspace.createVariable('name2', null, 'id2');
var fieldVariable = fieldVariable_createAndInitField(workspace);
var oldId = fieldVariable.getValue();
var event = new Blockly.Events.BlockChange(
fieldVariable.sourceBlock_, 'field', undefined, oldId, 'id2');
setUpMockMethod(mockControl_, Blockly.Events, 'fire', [event], null);
fieldVariable.setValue('id2');
assertEquals('name2', fieldVariable.getText());
assertEquals('id2', fieldVariable.getValue());
fieldVariableTestWithMocks_tearDown();
}
function test_fieldVariable_setValueNoVariable() {
fieldVariableTestWithMocks_setUp();
var fieldVariable = fieldVariable_createAndInitField(workspace);
var mockBlock = fieldVariable.sourceBlock_;
mockBlock.isShadow = function() {
return false;
};
try {
fieldVariable.setValue('id1');
// Calling setValue with a variable that doesn't exist throws an error.
fail();
} catch (e) {
// expected
} finally {
fieldVariableTestWithMocks_tearDown();
}
}
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 fieldVariable = fieldVariable_createAndInitField(workspace);
var result_options = Blockly.FieldVariable.dropdownCreate.call(
fieldVariable);
assertEquals(result_options.length, 3);
isEqualArrays(result_options[0], ['name1', 'id1']);
isEqualArrays(result_options[1], ['name2', 'id2']);
workspace.dispose();
}
function test_fieldVariable_setValueNull() {
// This should no longer create a variable for the selected option.
fieldVariableTestWithMocks_setUp();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['id1', null]);
var fieldVariable = fieldVariable_createAndInitField(workspace);
try {
fieldVariable.setValue(null);
fail();
} catch (e) {
// expected
} finally {
fieldVariableTestWithMocks_tearDown();
}
}
function test_fieldVariable_getVariableTypes_undefinedVariableTypes() {
// Expect that since variableTypes is undefined, only type empty string
// will be returned (regardless of what types are available on the workspace).
workspace = new Blockly.Workspace();
workspace.createVariable('name1', 'type1');
workspace.createVariable('name2', 'type2');
var fieldVariable = new Blockly.FieldVariable('name1');
var resultTypes = fieldVariable.getVariableTypes_();
isEqualArrays(resultTypes, ['']);
workspace.dispose();
}
function test_fieldVariable_getVariableTypes_givenVariableTypes() {
// Expect that since variableTypes is defined, it will be the return value,
// regardless of what types are available on the workspace.
workspace = new Blockly.Workspace();
workspace.createVariable('name1', 'type1');
workspace.createVariable('name2', 'type2');
var fieldVariable = new Blockly.FieldVariable(
'name1', null, ['type1', 'type2'], 'type1');
var resultTypes = fieldVariable.getVariableTypes_();
isEqualArrays(resultTypes, ['type1', 'type2']);
assertEquals('Default type was wrong', 'type1', fieldVariable.defaultType_);
workspace.dispose();
}
function test_fieldVariable_getVariableTypes_nullVariableTypes() {
// Expect all variable types to be returned.
// The variable does not need to be initialized to do this--it just needs a
// pointer to the workspace.
workspace = new Blockly.Workspace();
workspace.createVariable('name1', 'type1');
workspace.createVariable('name2', 'type2');
var fieldVariable = new Blockly.FieldVariable('name1');
var mockBlock = fieldVariable_mockBlock(workspace);
fieldVariable.setSourceBlock(mockBlock);
fieldVariable.variableTypes = null;
var resultTypes = fieldVariable.getVariableTypes_();
// The empty string is always one of the options.
isEqualArrays(resultTypes, ['type1', 'type2', '']);
workspace.dispose();
}
function test_fieldVariable_getVariableTypes_emptyListVariableTypes() {
// Expect an error to be thrown.
workspace = new Blockly.Workspace();
workspace.createVariable('name1', 'type1');
workspace.createVariable('name2', 'type2');
var fieldVariable = new Blockly.FieldVariable('name1');
var mockBlock = fieldVariable_mockBlock(workspace);
fieldVariable.setSourceBlock(mockBlock);
fieldVariable.variableTypes = [];
try {
fieldVariable.getVariableTypes_();
fail();
} catch (e) {
//expected
} finally {
workspace.dispose();
}
}
function test_fieldVariable_defaultType_exists() {
var fieldVariable = new Blockly.FieldVariable(null, null, ['b'], 'b');
assertEquals('The variable field\'s default type should be "b"',
'b', fieldVariable.defaultType_);
}
function test_fieldVariable_noDefaultType() {
var fieldVariable = new Blockly.FieldVariable(null);
assertEquals('The variable field\'s default type should be the empty string',
'', fieldVariable.defaultType_);
assertNull('The variable field\'s allowed types should be null',
fieldVariable.variableTypes);
}
function test_fieldVariable_defaultTypeMismatch() {
try {
var fieldVariable = new Blockly.FieldVariable(null, null, ['a'], 'b');
fail('Variable field creation should have failed due to an invalid ' +
'default type');
} catch (e) {
// expected
}
}
function test_fieldVariable_defaultTypeMismatch_empty() {
try {
var fieldVariable = new Blockly.FieldVariable(null, null, ['a']);
fail('Variable field creation should have failed due to an invalid ' +
'default type');
} catch (e) {
// expected
}
}

View File

@@ -0,0 +1,28 @@
/**
* @license
* Blockly Tests
*
* Copyright 2012 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.
*/
'use strict';
function test_prefix() {
var generator = new Blockly.Generator('INTERCAL');
assertEquals('Prefix nothing.', '', generator.prefixLines('', ''));
assertEquals('Prefix a word.', '@Hello', generator.prefixLines('Hello', '@'));
assertEquals('Prefix one line.', '12Hello\n', generator.prefixLines('Hello\n', '12'));
assertEquals('Prefix two lines.', '***Hello\n***World\n', generator.prefixLines('Hello\nWorld\n', '***'));
}

View File

@@ -0,0 +1,90 @@
/**
* @license
* Blockly Tests
*
* 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.
*/
/**
* @fileoverview Tests for gesture.
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
var e;
var workspace;
function gestureTest_setUp() {
workspace = new Blockly.Workspace();
e = {};
}
function gestureTest_tearDown() {
e = null;
workspace.dispose();
}
function test_gestureConstructor() {
var gesture = new Blockly.Gesture(e, workspace);
assertEquals(gesture.mostRecentEvent_, e);
assertEquals(gesture.creatorWorkspace_, workspace);
}
function test_gestureIsField_ClickInWorkspace() {
gestureTest_setUp();
var block = new Blockly.Block(workspace);
var field = new Blockly.Field();
field.setSourceBlock(block);
var gesture = new Blockly.Gesture(e, workspace);
gesture.setStartField(field);
var isFieldClick = gesture.isFieldClick_();
assertEquals(isFieldClick, true);
gestureTest_tearDown();
}
function gestureIsFieldClick_InFlyoutHelper(flyout, expectedResult){
// Assign workspace flyout
workspace.flyout_ = flyout;
// Create a Field inside of a Block
var block = new Blockly.Block(workspace);
var field = new Blockly.Field();
field.setSourceBlock(block);
// Create gesture from the flyout
var gesture = new Blockly.Gesture(e, workspace.flyout_);
// Populate gesture with click start information
gesture.setStartField(field);
gesture.setStartFlyout_(workspace.flyout_);
var isFieldClick = gesture.isFieldClick_();
assertEquals(isFieldClick, expectedResult);
}
function test_gestureIsFieldClick_AutoCloseFlyout() {
gestureTest_setUp();
var flyout = new Blockly.VerticalFlyout({});
gestureIsFieldClick_InFlyoutHelper(flyout, false);
gestureTest_tearDown();
}
function test_gestureIsFieldClick_AlwaysOpenFlyout() {
gestureTest_setUp();
var flyout = new Blockly.VerticalFlyout({});
flyout.autoClose = false;
gestureIsFieldClick_InFlyoutHelper(flyout, true);
gestureTest_tearDown();
}

View File

@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Unit Tests for Blockly</title>
<script src="../../blockly_uncompressed.js"></script>
<script>goog.require('goog.testing.jsunit');</script>
</head>
<body>
<script src="test_utilities.js"></script>
<script src="connection_db_test.js"></script>
<script src="connection_test.js"></script>
<script src="event_test.js"></script>
<script src="extensions_test.js"></script>
<script src="field_angle_test.js"></script>
<script src="field_number_test.js"></script>
<script src="field_test.js"></script>
<script src="field_variable_test.js"></script>
<script src="generator_test.js"></script>
<script src="gesture_test.js"></script>
<script src="input_test.js"></script>
<script src="json_test.js"></script>
<script src="metrics_test.js"></script>
<script src="names_test.js"></script>
<script src="procedures_test.js"></script>
<script src="utils_test.js"></script>
<script src="variables_test.js"></script>
<script src="variable_map_test.js"></script>
<script src="variable_model_test.js"></script>
<script src="widget_div_test.js"></script>
<script src="workspace_test.js"></script>
<script src="workspace_undo_redo_test.js"></script>
<script src="xml_test.js"></script>
</body>
</html>

View File

@@ -0,0 +1,202 @@
/**
* @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.
*/
/**
* @fileoverview Tests for Blockly.Input
*/
'use strict';
function test_appendField_simple() {
var ws = new Blockly.Workspace();
var block = new Blockly.Block(ws);
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
var field1 = new Blockly.FieldLabel('#1');
var field2 = new Blockly.FieldLabel('#2');
// Preconditions
assertEquals(0, input.fieldRow.length);
// Actual Tests
input.appendField(field1, 'first');
assertEquals(1, input.fieldRow.length);
assertEquals(field1, input.fieldRow[0]);
assertEquals('first', input.fieldRow[0].name);
assertEquals(block, field1.sourceBlock_);
input.appendField(field2, 'second');
assertEquals(2, input.fieldRow.length);
assertEquals(field2, input.fieldRow[1]);
assertEquals('second', input.fieldRow[1].name);
assertEquals(block, field2.sourceBlock_);
}
function test_appendField_string() {
var ws = new Blockly.Workspace();
var block = new Blockly.Block(ws);
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
var labelText = 'label';
// Preconditions
assertEquals(0, input.fieldRow.length);
// Actual Tests
input.appendField(labelText, 'name');
assertEquals(1, input.fieldRow.length);
assertEquals(Blockly.FieldLabel, input.fieldRow[0].constructor);
assertEquals(labelText, input.fieldRow[0].getValue());
assertEquals('name', input.fieldRow[0].name);
}
function test_appendField_prefix() {
var ws = new Blockly.Workspace();
var block = new Blockly.Block(ws);
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
var prefix = new Blockly.FieldLabel('prefix');
var field = new Blockly.FieldLabel('field');
field.prefixField = prefix;
// Preconditions
assertEquals(0, input.fieldRow.length);
// Actual Tests
input.appendField(field);
assertEquals(2, input.fieldRow.length);
assertEquals(prefix, input.fieldRow[0]);
assertEquals(field, input.fieldRow[1]);
}
function test_appendField_suffix() {
var ws = new Blockly.Workspace();
var block = new Blockly.Block(ws);
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
var suffix = new Blockly.FieldLabel('suffix');
var field = new Blockly.FieldLabel('field');
field.suffixField = suffix;
// Preconditions
assertEquals(0, input.fieldRow.length);
// Actual Tests
input.appendField(field);
assertEquals(2, input.fieldRow.length);
assertEquals(field, input.fieldRow[0]);
assertEquals(suffix, input.fieldRow[1]);
}
function test_insertFieldAt_simple() {
var ws = new Blockly.Workspace();
var block = new Blockly.Block(ws);
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
var before = new Blockly.FieldLabel('before');
var after = new Blockly.FieldLabel('after');
var between = new Blockly.FieldLabel('between');
input.appendField(before);
input.appendField(after);
// Preconditions
assertEquals(2, input.fieldRow.length);
assertEquals(before, input.fieldRow[0]);
assertEquals(after, input.fieldRow[1]);
// Actual Tests
input.insertFieldAt(1, between, 'name');
assertEquals(3, input.fieldRow.length);
assertEquals(before, input.fieldRow[0]);
assertEquals(between, input.fieldRow[1]);
assertEquals('name', input.fieldRow[1].name);
assertEquals(after, input.fieldRow[2]);
}
function test_insertFieldAt_string() {
var ws = new Blockly.Workspace();
var block = new Blockly.Block(ws);
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
var before = new Blockly.FieldLabel('before');
var after = new Blockly.FieldLabel('after');
var labelText = 'label';
input.appendField(before);
input.appendField(after);
// Preconditions
assertEquals(2, input.fieldRow.length);
assertEquals(before, input.fieldRow[0]);
assertEquals(after, input.fieldRow[1]);
// Actual Tests
input.insertFieldAt(1, labelText, 'name');
assertEquals(3, input.fieldRow.length);
assertEquals(before, input.fieldRow[0]);
assertEquals(Blockly.FieldLabel, input.fieldRow[1].constructor);
assertEquals(labelText, input.fieldRow[1].getValue());
assertEquals('name', input.fieldRow[1].name);
assertEquals(after, input.fieldRow[2]);
}
function test_insertFieldAt_prefix() {
var ws = new Blockly.Workspace();
var block = new Blockly.Block(ws);
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
var before = new Blockly.FieldLabel('before');
var after = new Blockly.FieldLabel('after');
var prefix = new Blockly.FieldLabel('prefix');
var between = new Blockly.FieldLabel('between');
between.prefixField = prefix;
input.appendField(before);
input.appendField(after);
// Preconditions
assertEquals(2, input.fieldRow.length);
assertEquals(before, input.fieldRow[0]);
assertEquals(after, input.fieldRow[1]);
// Actual Tests
input.insertFieldAt(1, between);
assertEquals(4, input.fieldRow.length);
assertEquals(before, input.fieldRow[0]);
assertEquals(prefix, input.fieldRow[1]);
assertEquals(between, input.fieldRow[2]);
assertEquals(after, input.fieldRow[3]);
}
function test_insertFieldAt_suffix() {
var ws = new Blockly.Workspace();
var block = new Blockly.Block(ws);
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
var before = new Blockly.FieldLabel('before');
var after = new Blockly.FieldLabel('after');
var suffix = new Blockly.FieldLabel('suffix');
var between = new Blockly.FieldLabel('between');
between.suffixField = suffix;
input.appendField(before);
input.appendField(after);
// Preconditions
assertEquals(2, input.fieldRow.length);
assertEquals(before, input.fieldRow[0]);
assertEquals(after, input.fieldRow[1]);
// Actual Tests
input.insertFieldAt(1, between);
assertEquals(4, input.fieldRow.length);
assertEquals(before, input.fieldRow[0]);
assertEquals(between, input.fieldRow[1]);
assertEquals(suffix, input.fieldRow[2]);
assertEquals(after, input.fieldRow[3]);
}

View File

@@ -0,0 +1,370 @@
/**
* @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.
*/
'use strict';
/** Ensure a block can be instantiated from a JSON definition. */
function test_json_minimal() {
var BLOCK_TYPE = 'test_json_minimal';
var workspace = new Blockly.Workspace();
var block;
try {
var warnings = captureWarnings(function() {
Blockly.defineBlocksWithJsonArray([{
"type": BLOCK_TYPE
}]);
block = new Blockly.Block(workspace, BLOCK_TYPE);
});
assertEquals(BLOCK_TYPE, block.type);
assertEquals(
'Expecting no warnings when defining and creating a simple block.',
warnings.length, 0);
// TODO: asserts
} finally {
block.dispose();
workspace.dispose();
delete Blockly.Blocks[BLOCK_TYPE];
}
}
/**
* Ensure a block without a type id fails with a warning, but does not block
* later definitions.
*/
function test_json_nullOrUndefinedBlockTypeId() {
var BLOCK_TYPE1 = 'test_json_before_bad_blocks';
var BLOCK_TYPE2 = 'test_json_after_bad_blocks';
assertUndefined(Blockly.Blocks[BLOCK_TYPE1]);
assertUndefined(Blockly.Blocks[BLOCK_TYPE2]);
var blockTypeCount = Object.keys(Blockly.Blocks).length;
try {
var warnings = captureWarnings(function() {
Blockly.defineBlocksWithJsonArray([
{"type": BLOCK_TYPE1},
{"type": undefined},
{"type": null},
{"type": BLOCK_TYPE2}]);
});
assertNotNullNorUndefined('Block before bad blocks should be defined.',
Blockly.Blocks[BLOCK_TYPE1]);
assertNotNullNorUndefined('Block after bad blocks should be defined.',
Blockly.Blocks[BLOCK_TYPE2]);
assertEquals(Object.keys(Blockly.Blocks).length, blockTypeCount + 2);
assertEquals(
'Expecting 2 warnings, one for each bad block.', warnings.length, 2);
} finally {
delete Blockly.Blocks[BLOCK_TYPE1];
delete Blockly.Blocks[BLOCK_TYPE2];
}
}
/** Ensure message0 creates an input. */
function test_json_message0() {
var BLOCK_TYPE = 'test_json_message0';
var MESSAGE0 = 'message0';
var workspace = new Blockly.Workspace();
var block;
try {
Blockly.defineBlocksWithJsonArray([{
"type": BLOCK_TYPE,
"message0": MESSAGE0
}]);
block = new Blockly.Block(workspace, BLOCK_TYPE);
assertEquals(1, block.inputList.length);
assertEquals(1, block.inputList[0].fieldRow.length);
var textField = block.inputList[0].fieldRow[0];
assertEquals(Blockly.FieldLabel, textField.constructor);
assertEquals(MESSAGE0, textField.getText());
} finally {
block && block.dispose();
workspace.dispose();
delete Blockly.Blocks[BLOCK_TYPE];
}
}
/** Ensure message1 creates a new input. */
function test_json_message1() {
var BLOCK_TYPE = 'test_json_message1';
var MESSAGE0 = 'message0';
var MESSAGE1 = 'message1';
var workspace = new Blockly.Workspace();
var block;
try {
Blockly.defineBlocksWithJsonArray([{
"type": BLOCK_TYPE,
"message0": MESSAGE0,
"message1": MESSAGE1
}]);
block = new Blockly.Block(workspace, BLOCK_TYPE);
assertEquals(2, block.inputList.length);
assertEquals(1, block.inputList[0].fieldRow.length);
var textField = block.inputList[0].fieldRow[0];
assertEquals(Blockly.FieldLabel, textField.constructor);
assertEquals(MESSAGE0, textField.getText());
assertEquals(1, block.inputList[1].fieldRow.length);
var textField = block.inputList[1].fieldRow[0];
assertEquals(Blockly.FieldLabel, textField.constructor);
assertEquals(MESSAGE1, textField.getText());
} finally {
block && block.dispose();
workspace.dispose();
delete Blockly.Blocks[BLOCK_TYPE];
}
}
/** Ensure message string is dereferenced. */
function test_json_message0_i18n() {
var BLOCK_TYPE = 'test_json_message0_i18n';
var MESSAGE0 = '%{BKY_MESSAGE}';
var MESSAGE = 'message';
Blockly.Msg['MESSAGE'] = MESSAGE;
var workspace = new Blockly.Workspace();
var block;
try {
Blockly.defineBlocksWithJsonArray([{
"type": BLOCK_TYPE,
"message0": MESSAGE0
}]);
block = new Blockly.Block(workspace, BLOCK_TYPE);
assertEquals(1, block.inputList.length);
assertEquals(1, block.inputList[0].fieldRow.length);
var textField = block.inputList[0].fieldRow[0];
assertEquals(Blockly.FieldLabel, textField.constructor);
assertEquals(MESSAGE, textField.getText());
} finally {
block && block.dispose(); // Disposes of textField, too.
workspace.dispose();
delete Blockly.Blocks[BLOCK_TYPE];
delete Blockly.Msg['MESSAGE'];
}
}
function test_json_dropdown() {
var BLOCK_TYPE = 'test_json_dropdown';
var FIELD_NAME = 'FIELD_NAME';
var LABEL0 = 'LABEL0';
var VALUE0 = 'VALUE0';
var LABEL1 = 'LABEL1';
var VALUE1 = 'VALUE1';
var workspace = new Blockly.Workspace();
var block;
try {
Blockly.defineBlocksWithJsonArray([{
"type": BLOCK_TYPE,
"message0": "%1",
"args0": [
{
"type": "field_dropdown",
"name": FIELD_NAME,
"options": [
[LABEL0, VALUE0],
[LABEL1, VALUE1]
]
}
]
}]);
block = new Blockly.Block(workspace, BLOCK_TYPE);
assertEquals(1, block.inputList.length);
assertEquals(1, block.inputList[0].fieldRow.length);
var dropdown = block.inputList[0].fieldRow[0];
assertEquals(dropdown, block.getField(FIELD_NAME));
assertEquals(Blockly.FieldDropdown, dropdown.constructor);
assertEquals(VALUE0, dropdown.getValue());
var options = dropdown.getOptions();
assertEquals(LABEL0, options[0][0]);
assertEquals(VALUE0, options[0][1]);
assertEquals(LABEL1, options[1][0]);
assertEquals(VALUE1, options[1][1]);
} finally {
block && block.dispose(); // Disposes of dropdown, too.
workspace.dispose();
delete Blockly.Blocks[BLOCK_TYPE];
}
}
function test_json_dropdown_image() {
var BLOCK_TYPE = 'test_json_dropdown';
var FIELD_NAME = 'FIELD_NAME';
var IMAGE1_ALT_TEXT = 'Localized message.';
Blockly.Msg['ALT_TEXT'] = IMAGE1_ALT_TEXT;
var IMAGE0 = {
'width': 12,
'height': 34,
'src': 'http://image0.src',
'alt': 'IMAGE0 alt text'
};
var VALUE0 = 'VALUE0';
var IMAGE1 = {
'width': 56,
'height': 78,
'src': 'http://image1.src',
'alt': '%{BKY_ALT_TEXT}'
};
var VALUE1 = 'VALUE1';
var IMAGE2 = {
'width': 90,
'height': 123,
'src': 'http://image2.src'
};
var VALUE2 = 'VALUE2';
var workspace = new Blockly.Workspace();
var block;
try {
Blockly.defineBlocksWithJsonArray([{
"type": BLOCK_TYPE,
"message0": "%1",
"args0": [
{
"type": "field_dropdown",
"name": FIELD_NAME,
"options": [
[IMAGE0, VALUE0],
[IMAGE1, VALUE1],
[IMAGE2, VALUE2]
]
}
]
}]);
block = new Blockly.Block(workspace, BLOCK_TYPE);
assertEquals(1, block.inputList.length);
assertEquals(1, block.inputList[0].fieldRow.length);
var dropdown = block.inputList[0].fieldRow[0];
assertEquals(dropdown, block.getField(FIELD_NAME));
assertEquals(Blockly.FieldDropdown, dropdown.constructor);
assertEquals(VALUE0, dropdown.getValue());
var options = dropdown.getOptions();
var image0 = options[0][0];
assertEquals(IMAGE0.width, image0.width);
assertEquals(IMAGE0.height, image0.height);
assertEquals(IMAGE0.src, image0.src);
assertEquals(IMAGE0.alt, image0.alt);
assertEquals(VALUE0, options[0][1]);
var image1 = options[1][0];
assertEquals(IMAGE1.width, image1.width);
assertEquals(IMAGE1.height, image1.height);
assertEquals(IMAGE1.src, image1.src);
assertEquals(IMAGE1.alt, IMAGE1_ALT_TEXT); // Via Msg reference
assertEquals(VALUE1, options[1][1]);
var image2 = options[2][0];
assertEquals(IMAGE2.width, image2.width);
assertEquals(IMAGE2.height, image2.height);
assertEquals(IMAGE2.src, image2.src);
assert(image2.alt == null); // No alt specified.
assertEquals(VALUE2, options[2][1]);
} finally {
block && block.dispose(); // Disposes of dropdown, too.
workspace.dispose();
delete Blockly.Blocks[BLOCK_TYPE];
delete Blockly.Msg['ALTTEXT'];
}
}
function test_defineBlocksWithJsonArray_nullItem() {
var BLOCK_TYPE1 = 'test_block_before_null';
var BLOCK_TYPE2 = 'test_block_after_null';
assertUndefined(Blockly.Blocks[BLOCK_TYPE1]);
assertUndefined(Blockly.Blocks[BLOCK_TYPE2]);
var blockTypeCount = Object.keys(Blockly.Blocks).length;
try {
var warnings = captureWarnings(function() {
Blockly.defineBlocksWithJsonArray([
{
"type": BLOCK_TYPE1,
"message0": 'before'
},
null,
{
"type": BLOCK_TYPE2,
"message0": 'after'
}]);
});
assertNotNullNorUndefined(
'Block before null in array should be defined.',
Blockly.Blocks[BLOCK_TYPE1]);
assertNotNullNorUndefined(
'Block after null in array should be defined.',
Blockly.Blocks[BLOCK_TYPE2]);
assertEquals(Object.keys(Blockly.Blocks).length, blockTypeCount + 2);
assertEquals('Expected 1 warning for the bad block.', warnings.length, 1);
} finally {
workspace.dispose();
delete Blockly.Blocks[BLOCK_TYPE1];
delete Blockly.Blocks[BLOCK_TYPE2];
}
}
function test_defineBlocksWithJsonArray_undefinedItem() {
var BLOCK_TYPE1 = 'test_block_before_undefined';
var BLOCK_TYPE2 = 'test_block_after_undefined';
assertUndefined(Blockly.Blocks[BLOCK_TYPE1]);
assertUndefined(Blockly.Blocks[BLOCK_TYPE2]);
var blockTypeCount = Object.keys(Blockly.Blocks).length;
try {
var warnings = captureWarnings(function() {
Blockly.defineBlocksWithJsonArray([
{
"type": BLOCK_TYPE1,
"message0": 'before'
},
undefined,
{
"type": BLOCK_TYPE2,
"message0": 'after'
}]);
});
assertNotNullNorUndefined(
'Block before undefined in array should be defined.',
Blockly.Blocks[BLOCK_TYPE1]);
assertNotNullNorUndefined(
'Block after undefined in array should be defined.',
Blockly.Blocks[BLOCK_TYPE2]);
assertEquals(Object.keys(Blockly.Blocks).length, blockTypeCount + 2);
assertEquals('Expected 1 warning for the bad block.', warnings.length, 1);
} finally {
workspace.dispose();
delete Blockly.Blocks[BLOCK_TYPE1];
delete Blockly.Blocks[BLOCK_TYPE2];
}
}

View File

@@ -0,0 +1,132 @@
/**
* @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.
*/
'use strict';
function assertDimensionsMatch(toCheck, left, top, width, height) {
assertEquals('Top did not match.', top, toCheck.top);
assertEquals('Left did not match.', left, toCheck.left);
assertEquals('Width did not match.', width, toCheck.width);
assertEquals('Height did not match.', height, toCheck.height);
}
/**
* Make a mock workspace object with two properties: getBlocksBoundingBox and
* scale.
*/
function makeMockWs(scale, x, y, width, height) {
return {
getBlocksBoundingBox: function() {
return {
width: width,
height: height,
x: x,
y: y
}
},
scale: scale
};
}
// Empty workspace.
var test_GetContentDimensionsExact_empty = function() {
var ws = makeMockWs(1, 0, 0, 0, 0)
var defaultZoom = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
assertDimensionsMatch(defaultZoom, 0, 0, 0, 0);
}
var test_GetContentDimensionsExact_emptyZoomIn = function() {
var ws = makeMockWs(2, 0, 0, 0, 0)
var zoomIn = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
assertDimensionsMatch(zoomIn, 0, 0, 0, 0);
}
var test_GetContentDimensionsExact_emptyZoomOut = function() {
var ws = makeMockWs(.5, 0, 0, 0, 0)
var zoomOut = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
assertDimensionsMatch(zoomOut, 0, 0, 0, 0);
}
// Non-empty workspace, with top-left corner at ws origin.
var test_GetContentDimensionsExact_nonEmptyAtOrigin = function() {
var ws = makeMockWs(1, 0, 0, 100, 100)
var defaultZoom = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
// Pixel and ws units are the same at default zoom.
assertDimensionsMatch(defaultZoom, 0, 0, 100, 100);
}
var test_GetContentDimensionsExact_nonEmptyAtOriginZoomIn = function() {
var ws = makeMockWs(2, 0, 0, 100, 100)
var zoomIn = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
// 1 ws unit = 2 pixels at this zoom level.
assertDimensionsMatch(zoomIn, 0, 0, 200, 200);
}
var test_GetContentDimensionsExact_nonEmptyAtOriginZoomOut = function() {
var ws = makeMockWs(.5, 0, 0, 100, 100)
var zoomOut = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
// 1 ws unit = 0.5 pixels at this zoom level.
assertDimensionsMatch(zoomOut, 0, 0, 50, 50);
}
// Non-empty workspace, with top-left corner in positive ws coordinates.
var test_GetContentDimensionsExact_nonEmptyPositiveOrigin = function() {
var ws = makeMockWs(1, 10, 10, 100, 100)
var defaultZoom = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
// Pixel and ws units are the same at default zoom.
assertDimensionsMatch(defaultZoom, 10, 10, 100, 100);
}
// Changing zoom will change both width/height and origin location in pixels.
var test_GetContentDimensionsExact_nonEmptyPositiveOriginZoomIn = function() {
var ws = makeMockWs(2, 10, 10, 100, 100)
var zoomIn = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
// 1 ws unit = 2 pixels at this zoom level.
assertDimensionsMatch(zoomIn, 20, 20, 200, 200);
}
var test_GetContentDimensionsExact_nonEmptyPositiveOriginZoomOut = function() {
var ws = makeMockWs(.5, 10, 10, 100, 100)
var zoomOut = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
// 1 ws unit = 0.5 pixels at this zoom level.
assertDimensionsMatch(zoomOut, 5, 5, 50, 50);
}
// Non-empty workspace, with top-left corner in negative ws coordinates.
var test_GetContentDimensionsExact_nonEmptyNegativeOrigin = function() {
var ws = makeMockWs(1, -10, -10, 100, 100)
var defaultZoom = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
// Pixel and ws units are the same at default zoom.
assertDimensionsMatch(defaultZoom, -10, -10, 100, 100);
}
// Changing zoom will change both width/height and origin location in pixels.
var test_GetContentDimensionsExact_nonEmptyNegativeOriginZoomIn = function() {
var ws = makeMockWs(2, -10, -10, 100, 100)
var zoomIn = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
// 1 ws unit = 2 pixels at this zoom level.
assertDimensionsMatch(zoomIn, -20, -20, 200, 200);
}
var test_GetContentDimensionsExact_nonEmptyNegativeOriginZoomOut = function() {
var ws = makeMockWs(.5, -10, -10, 100, 100)
var zoomOut = Blockly.WorkspaceSvg.getContentDimensionsExact_(ws);
// 1 ws unit = 0.5 pixels at this zoom level.
assertDimensionsMatch(zoomOut, -5, -5, 50, 50);
}

View File

@@ -0,0 +1,62 @@
/**
* @license
* Blockly Tests
*
* Copyright 2012 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.
*/
'use strict';
function test_safeName() {
var varDB = new Blockly.Names('window,door');
assertEquals('SafeName empty.', 'unnamed', varDB.safeName_(''));
assertEquals('SafeName ok.', 'foobar', varDB.safeName_('foobar'));
assertEquals('SafeName number start.', 'my_9lives',
varDB.safeName_('9lives'));
assertEquals('SafeName number end.', 'lives9', varDB.safeName_('lives9'));
assertEquals('SafeName special chars.', '____', varDB.safeName_('!@#$'));
assertEquals('SafeName reserved.', 'door', varDB.safeName_('door'));
}
function test_getName() {
var varDB = new Blockly.Names('window,door');
assertEquals('Name add #1.', 'Foo_bar', varDB.getName('Foo.bar', 'var'));
assertEquals('Name get #1.', 'Foo_bar', varDB.getName('Foo.bar', 'var'));
assertEquals('Name add #2.', 'Foo_bar2', varDB.getName('Foo bar', 'var'));
assertEquals('Name get #2.', 'Foo_bar2', varDB.getName('foo BAR', 'var'));
assertEquals('Name add #3.', 'door2', varDB.getName('door', 'var'));
assertEquals('Name add #4.', 'Foo_bar3', varDB.getName('Foo.bar', 'proc'));
assertEquals('Name get #1b.', 'Foo_bar', varDB.getName('Foo.bar', 'var'));
assertEquals('Name get #4.', 'Foo_bar3', varDB.getName('Foo.bar', 'proc'));
}
function test_getDistinctName() {
var varDB = new Blockly.Names('window,door');
assertEquals('Name distinct #1.', 'Foo_bar',
varDB.getDistinctName('Foo.bar', 'var'));
assertEquals('Name distinct #2.', 'Foo_bar2',
varDB.getDistinctName('Foo.bar', 'var'));
assertEquals('Name distinct #3.', 'Foo_bar3',
varDB.getDistinctName('Foo.bar', 'proc'));
varDB.reset();
assertEquals('Name distinct #4.', 'Foo_bar',
varDB.getDistinctName('Foo.bar', 'var'));
}
function test_nameEquals() {
assertTrue('Name equals #1.', Blockly.Names.equals('Foo.bar', 'Foo.bar'));
assertFalse('Name equals #2.', Blockly.Names.equals('Foo.bar', 'Foo_bar'));
assertTrue('Name equals #3.', Blockly.Names.equals('Foo.bar', 'FOO.BAR'));
}

View File

@@ -0,0 +1,84 @@
/**
* @license
* Blockly Tests
*
* 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.
*/
/**
* @fileoverview Tests for procedures.
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
goog.require('goog.testing');
var workspace;
function proceduresTest_setUpWithMockBlocks() {
workspace = new Blockly.Workspace();
Blockly.defineBlocksWithJsonArray([{
getProcedureDef: function() {
},
'type': 'procedure_mock_block',
'message0': '%1',
'args0': [
{
'type': 'field_variable',
'name': 'NAME',
'variable': 'item'
}
]
}]);
Blockly.Blocks['procedure_mock_block'].getProcedureDef = function() {
return [this.getField('NAME').getText(), [], false];
};
}
function proceduresTest_tearDownWithMockBlocks() {
workspace.dispose();
delete Blockly.Blocks.procedures_mock_block;
}
function test_isNameUsed_NoBlocks() {
workspace = new Blockly.Workspace();
var result = Blockly.Procedures.isNameUsed('name1', workspace);
assertFalse(result);
workspace.dispose();
}
function test_isNameUsed_False() {
proceduresTest_setUpWithMockBlocks();
workspace.createVariable('name2', '', 'id2');
var block = new Blockly.Block(workspace, 'procedure_mock_block');
block.setFieldValue('id2', 'NAME');
var result = Blockly.Procedures.isNameUsed('name1', workspace);
assertFalse(result);
proceduresTest_tearDownWithMockBlocks();
}
function test_isNameUsed_True() {
proceduresTest_setUpWithMockBlocks();
workspace.createVariable('name1', '', 'id1');
var block = new Blockly.Block(workspace, 'procedure_mock_block');
block.setFieldValue('id1', 'NAME');
var result = Blockly.Procedures.isNameUsed('name1', workspace);
assertTrue(result);
proceduresTest_tearDownWithMockBlocks();
}

View File

@@ -0,0 +1,35 @@
var webdriverio = require('webdriverio');
var options = {
desiredCapabilities: {
browserName: 'chrome'
}
};
var path = process.cwd();
//TODO: change pause to waitunitl
var browser = webdriverio
.remote(options)
.init()
.url("file://" + path + "/tests/jsunit/index.html").pause(5000);
browser
.getHTML('#closureTestRunnerLog')
.then(function(result) {
// call js to parse html
var regex = /[\d]+\spassed,\s([\d]+)\sfailed./i;
var numOfFailure = regex.exec(result)[1];
var regex2 = /Unit Tests for Blockly .*]/;
var testStatus = regex2.exec(result)[0];
console.log("============Blockly Unit Test Summary=================");
console.log(testStatus);
var regex3 = /\d+ passed,\s\d+ failed/;
var detail = regex3.exec(result)[0];
console.log(detail);
console.log("============Blockly Unit Test Summary=================");
if ( parseInt(numOfFailure) !== 0) {
console.log(result);
process.exit(1);
}
})
.catch(function(err) { console.log(err); process.exit(1); });

View File

@@ -0,0 +1,163 @@
/**
* @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.
*/
/**
* @fileoverview Test utilities.
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
goog.require('goog.testing');
/**
* Check that two arrays have the same content.
* @param {!Array.<string>} array1 The first array.
* @param {!Array.<string>} array2 The second array.
*/
function isEqualArrays(array1, array2) {
assertEquals(array1.length, array2.length);
for (var i = 0; i < array1.length; i++) {
assertEquals(array1[i], array2[i]);
}
}
/**
* Creates a controlled MethodMock. Sets the expected return values and
* the parameters if any exist. Sets the method to replay.
* @param {!goog.testing.MockControl} mockControl Object that holds a set
* of mocks for this test.
* @param {!Object} scope The scope of the method to be mocked out.
* @param {!string} funcName The name of the function we're going to mock.
* @param {Array<Object>} parameters The parameters to call the mock with.
* @param {Array<!Object>} return_values The values to return when called.
* @return {!goog.testing.MockInterface} The mocked method.
*/
function setUpMockMethod(mockControl, scope, funcName, parameters,
return_values) {
var mockMethod = mockControl.createMethodMock(scope, funcName);
if (return_values) {
for (var i = 0, return_value; return_value = return_values[i]; i++) {
if (parameters && i < parameters.length) {
mockMethod(parameters[i]).$returns(return_value);
}
else {
mockMethod().$returns(return_value);
}
}
}
// If there are no return values but there are parameters, we are only
// recording specific method calls.
else if (parameters) {
for (var i = 0; i < parameters.length; i++) {
mockMethod(parameters[i]);
}
}
mockMethod.$replay();
return mockMethod;
}
/**
* Check if a variable with the given values exists.
* @param {Blockly.Workspace|Blockly.VariableMap} container The workspace or
* variableMap the checked variable belongs to.
* @param {!string} name The expected name of the variable.
* @param {!string} type The expected type of the variable.
* @param {!string} id The expected id of the variable.
*/
function checkVariableValues(container, name, type, id) {
var variable = container.getVariableById(id);
assertNotUndefined(variable);
assertEquals(name, variable.name);
assertEquals(type, variable.type);
assertEquals(id, variable.getId());
}
/**
* Create a test get_var_block.
* Will fail if get_var_block isn't defined.
* @param {!Blockly.Workspace} workspace The workspace on which to create the
* block.
* @param {!string} variable_id The id of the variable to reference.
* @return {!Blockly.Block} The created block.
*/
function createMockVarBlock(workspace, variable_id) {
if (!Blockly.Blocks['get_var_block']) {
fail();
}
// Turn off events to avoid testing XML at the same time.
Blockly.Events.disable();
var block = new Blockly.Block(workspace, 'get_var_block');
block.inputList[0].fieldRow[0].setValue(variable_id);
Blockly.Events.enable();
return block;
}
function createTwoVariablesAndBlocks(workspace) {
// Create two variables of different types.
workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type2', 'id2');
// Create blocks to refer to both of them.
createMockVarBlock(workspace, 'id1');
createMockVarBlock(workspace, 'id2');
}
function createVariableAndBlock(workspace) {
workspace.createVariable('name1', 'type1', 'id1');
createMockVarBlock(workspace, 'id1');
}
function defineGetVarBlock() {
Blockly.defineBlocksWithJsonArray([{
"type": "get_var_block",
"message0": "%1",
"args0": [
{
"type": "field_variable",
"name": "VAR",
"variableTypes": ["", "type1", "type2"]
}
]
}]);
}
function undefineGetVarBlock() {
delete Blockly.Blocks['get_var_block'];
}
/**
* Capture the strings sent to console.warn() when calling a function.
* @param {function} innerFunc The function where warnings may called.
* @return {string[]} The warning messages (only the first arguments).
*/
function captureWarnings(innerFunc) {
var msgs = [];
var nativeConsoleWarn = console.warn;
try {
console.warn = function(msg) {
msgs.push(msg);
nativeConsoleWarn.apply(console, arguments);
};
innerFunc();
} finally {
console.warn = nativeConsoleWarn;
}
return msgs;
}

View File

@@ -0,0 +1,234 @@
/**
* @license
* Visual Blocks Editor
*
* Copyright 2011 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.
*/
'use strict';
function test_genUid() {
var uuids = {};
for (var i = 0; i < 1000; i++) {
var uuid = Blockly.utils.genUid();
assertFalse('UUID different: ' + uuid, uuid in uuids);
uuids[uuid] = true;
}
}
function test_addClass() {
var p = document.createElement('p');
Blockly.utils.addClass(p, 'one');
assertEquals('Adding "one"', 'one', p.className);
Blockly.utils.addClass(p, 'one');
assertEquals('Adding duplicate "one"', 'one', p.className);
Blockly.utils.addClass(p, 'two');
assertEquals('Adding "two"', 'one two', p.className);
Blockly.utils.addClass(p, 'two');
assertEquals('Adding duplicate "two"', 'one two', p.className);
Blockly.utils.addClass(p, 'three');
assertEquals('Adding "three"', 'one two three', p.className);
}
function test_hasClass() {
var p = document.createElement('p');
p.className = ' one three two three ';
assertTrue('Has "one"', Blockly.utils.hasClass(p, 'one'));
assertTrue('Has "two"', Blockly.utils.hasClass(p, 'two'));
assertTrue('Has "three"', Blockly.utils.hasClass(p, 'three'));
assertFalse('Has no "four"', Blockly.utils.hasClass(p, 'four'));
assertFalse('Has no "t"', Blockly.utils.hasClass(p, 't'));
}
function test_removeClass() {
var p = document.createElement('p');
p.className = ' one three two three ';
Blockly.utils.removeClass(p, 'two');
assertEquals('Removing "two"', 'one three three', p.className);
Blockly.utils.removeClass(p, 'four');
assertEquals('Removing "four"', 'one three three', p.className);
Blockly.utils.removeClass(p, 'three');
assertEquals('Removing "three"', 'one', p.className);
Blockly.utils.removeClass(p, 'ne');
assertEquals('Removing "ne"', 'one', p.className);
Blockly.utils.removeClass(p, 'one');
assertEquals('Removing "one"', '', p.className);
Blockly.utils.removeClass(p, 'zero');
assertEquals('Removing "zero"', '', p.className);
}
function test_shortestStringLength() {
var len = Blockly.utils.shortestStringLength('one,two,three,four,five'.split(','));
assertEquals('Length of "one"', 3, len);
len = Blockly.utils.shortestStringLength('one,two,three,four,five,'.split(','));
assertEquals('Length of ""', 0, len);
len = Blockly.utils.shortestStringLength(['Hello World']);
assertEquals('List of one', 11, len);
len = Blockly.utils.shortestStringLength([]);
assertEquals('Empty list', 0, len);
}
function test_commonWordPrefix() {
var len = Blockly.utils.commonWordPrefix('one,two,three,four,five'.split(','));
assertEquals('No prefix', 0, len);
len = Blockly.utils.commonWordPrefix('Xone,Xtwo,Xthree,Xfour,Xfive'.split(','));
assertEquals('No word prefix', 0, len);
len = Blockly.utils.commonWordPrefix('abc de,abc de,abc de,abc de'.split(','));
assertEquals('Full equality', 6, len);
len = Blockly.utils.commonWordPrefix('abc deX,abc deY'.split(','));
assertEquals('One word prefix', 4, len);
len = Blockly.utils.commonWordPrefix('abc de,abc deY'.split(','));
assertEquals('Overflow no', 4, len);
len = Blockly.utils.commonWordPrefix('abc de,abc de Y'.split(','));
assertEquals('Overflow yes', 6, len);
len = Blockly.utils.commonWordPrefix(['Hello World']);
assertEquals('List of one', 11, len);
len = Blockly.utils.commonWordPrefix([]);
assertEquals('Empty list', 0, len);
len = Blockly.utils.commonWordPrefix('turn&nbsp;left,turn&nbsp;right'.split(','));
assertEquals('No prefix due to &amp;nbsp;', 0, len);
len = Blockly.utils.commonWordPrefix('turn\u00A0left,turn\u00A0right'.split(','));
assertEquals('No prefix due to \\u00A0', 0, len);
}
function test_commonWordSuffix() {
var len = Blockly.utils.commonWordSuffix('one,two,three,four,five'.split(','));
assertEquals('No prefix', 0, len);
len = Blockly.utils.commonWordSuffix('oneX,twoX,threeX,fourX,fiveX'.split(','));
assertEquals('No word prefix', 0, len);
len = Blockly.utils.commonWordSuffix('abc de,abc de,abc de,abc de'.split(','));
assertEquals('Full equality', 6, len);
len = Blockly.utils.commonWordSuffix('Xabc de,Yabc de'.split(','));
assertEquals('One word prefix', 3, len);
len = Blockly.utils.commonWordSuffix('abc de,Yabc de'.split(','));
assertEquals('Overflow no', 3, len);
len = Blockly.utils.commonWordSuffix('abc de,Y abc de'.split(','));
assertEquals('Overflow yes', 6, len);
len = Blockly.utils.commonWordSuffix(['Hello World']);
assertEquals('List of one', 11, len);
len = Blockly.utils.commonWordSuffix([]);
assertEquals('Empty list', 0, len);
}
function test_tokenizeInterpolation() {
var tokens = Blockly.utils.tokenizeInterpolation('');
assertArrayEquals('Null interpolation', [], tokens);
tokens = Blockly.utils.tokenizeInterpolation('Hello');
assertArrayEquals('No interpolation', ['Hello'], tokens);
tokens = Blockly.utils.tokenizeInterpolation('Hello%World');
assertArrayEquals('Unescaped %.', ['Hello%World'], tokens);
tokens = Blockly.utils.tokenizeInterpolation('Hello%%World');
assertArrayEquals('Escaped %.', ['Hello%World'], tokens);
tokens = Blockly.utils.tokenizeInterpolation('Hello %1 World');
assertArrayEquals('Interpolation.', ['Hello ', 1, ' World'], tokens);
tokens = Blockly.utils.tokenizeInterpolation('%123Hello%456World%789');
assertArrayEquals('Interpolations.', [123, 'Hello', 456, 'World', 789], tokens);
tokens = Blockly.utils.tokenizeInterpolation('%%%x%%0%00%01%');
assertArrayEquals('Torture interpolations.', ['%%x%0', 0, 1, '%'], tokens);
Blockly.Msg = Blockly.Msg || {};
Blockly.Msg.STRING_REF = 'test string';
tokens = Blockly.utils.tokenizeInterpolation('%{bky_string_ref}');
assertArrayEquals('String table reference, lowercase', ['test string'], tokens);
tokens = Blockly.utils.tokenizeInterpolation('%{BKY_STRING_REF}');
assertArrayEquals('String table reference, uppercase', ['test string'], tokens);
Blockly.Msg.WITH_PARAM = 'before %1 after';
tokens = Blockly.utils.tokenizeInterpolation('%{bky_with_param}');
assertArrayEquals('String table reference, with parameter', ['before ', 1, ' after'], tokens);
Blockly.Msg.RECURSE = 'before %{bky_string_ref} after';
tokens = Blockly.utils.tokenizeInterpolation('%{bky_recurse}');
assertArrayEquals('String table reference, with subreference', ['before test string after'], tokens);
// Error cases...
tokens = Blockly.utils.tokenizeInterpolation('%{bky_undefined}');
assertArrayEquals('Undefined string table reference', ['%{bky_undefined}'], tokens);
Blockly.Msg['1'] = 'Will not match';
tokens = Blockly.utils.tokenizeInterpolation('before %{1} after');
assertArrayEquals('Invalid initial digit in string table reference', ['before %{1} after'], tokens);
Blockly.Msg['TWO WORDS'] = 'Will not match';
tokens = Blockly.utils.tokenizeInterpolation('before %{two words} after');
assertArrayEquals('Invalid character in string table reference: space', ['before %{two words} after'], tokens);
Blockly.Msg['TWO-WORDS'] = 'Will not match';
tokens = Blockly.utils.tokenizeInterpolation('before %{two-words} after');
assertArrayEquals('Invalid character in string table reference: dash', ['before %{two-words} after'], tokens);
Blockly.Msg['TWO.WORDS'] = 'Will not match';
tokens = Blockly.utils.tokenizeInterpolation('before %{two.words} after');
assertArrayEquals('Invalid character in string table reference: period', ['before %{two.words} after'], tokens);
Blockly.Msg['AB&C'] = 'Will not match';
tokens = Blockly.utils.tokenizeInterpolation('before %{ab&c} after');
assertArrayEquals('Invalid character in string table reference: &', ['before %{ab&c} after'], tokens);
Blockly.Msg['UNCLOSED'] = 'Will not match';
tokens = Blockly.utils.tokenizeInterpolation('before %{unclosed');
assertArrayEquals('String table reference, with parameter', ['before %{unclosed'], tokens);
}
function test_replaceMessageReferences() {
Blockly.Msg = Blockly.Msg || {};
Blockly.Msg.STRING_REF = 'test string';
Blockly.Msg.SUBREF = 'subref';
Blockly.Msg.STRING_REF_WITH_ARG = 'test %1 string';
Blockly.Msg.STRING_REF_WITH_SUBREF = 'test %{bky_subref} string';
var resultString = Blockly.utils.replaceMessageReferences('');
assertEquals('Empty string produces empty string', '', resultString);
resultString = Blockly.utils.replaceMessageReferences('%%');
assertEquals('Escaped %', '%', resultString);
resultString = Blockly.utils.replaceMessageReferences('%%{bky_string_ref}');
assertEquals('Escaped %', '%{bky_string_ref}', resultString);
resultString = Blockly.utils.replaceMessageReferences('%a');
assertEquals('Unrecognized % escape code treated as literal', '%a', resultString);
resultString = Blockly.utils.replaceMessageReferences('%1');
assertEquals('Interpolation tokens ignored.', '%1', resultString);
resultString = Blockly.utils.replaceMessageReferences('%1 %2');
assertEquals('Interpolation tokens ignored.', '%1 %2', resultString);
resultString = Blockly.utils.replaceMessageReferences('before %1 after');
assertEquals('Interpolation tokens ignored.', 'before %1 after', resultString);
// Blockly.Msg.STRING_REF cases:
resultString = Blockly.utils.replaceMessageReferences('%{bky_string_ref}');
assertEquals('Message ref dereferenced.', 'test string', resultString);
resultString = Blockly.utils.replaceMessageReferences('before %{bky_string_ref} after');
assertEquals('Message ref dereferenced.', 'before test string after', resultString);
// Blockly.Msg.STRING_REF_WITH_ARG cases:
resultString = Blockly.utils.replaceMessageReferences('%{bky_string_ref_with_arg}');
assertEquals('Message ref dereferenced with argument preserved.', 'test %1 string', resultString);
resultString = Blockly.utils.replaceMessageReferences('before %{bky_string_ref_with_arg} after');
assertEquals('Message ref dereferenced with argument preserved.', 'before test %1 string after', resultString);
// Blockly.Msg.STRING_REF_WITH_SUBREF cases:
resultString = Blockly.utils.replaceMessageReferences('%{bky_string_ref_with_subref}');
assertEquals('Message ref and subref dereferenced.', 'test subref string', resultString);
resultString = Blockly.utils.replaceMessageReferences('before %{bky_string_ref_with_subref} after');
assertEquals('Message ref and subref dereferenced.', 'before test subref string after', resultString);
}

View File

@@ -0,0 +1,300 @@
/**
* @license
* Blockly Tests
*
* 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.
*/
/**
* @fileoverview Tests for variable map.
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
goog.require('goog.testing');
goog.require('goog.testing.MockControl');
var variable_map;
var mockControl_;
var workspace;
function variableMapTest_setUp() {
workspace = new Blockly.Workspace();
variable_map = new Blockly.VariableMap(workspace);
mockControl_ = new goog.testing.MockControl();
}
function variableMapTest_tearDown() {
workspace.dispose();
mockControl_.$tearDown();
variable_map = null;
}
function test_getVariable_ByNameAndType() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', 'type1', 'id1');
var var_2 = variable_map.createVariable('name2', 'type1', 'id2');
var var_3 = variable_map.createVariable('name3', 'type2', 'id3');
var result_1 = variable_map.getVariable('name1', 'type1');
var result_2 = variable_map.getVariable('name2', 'type1');
var result_3 = variable_map.getVariable('name3', 'type2');
// Searching by name + type is correct.
assertEquals(var_1, result_1);
assertEquals(var_2, result_2);
assertEquals(var_3, result_3);
// Searching only by name defaults to the '' type.
assertNull(variable_map.getVariable('name1'));
assertNull(variable_map.getVariable('name2'));
assertNull(variable_map.getVariable('name3'));
variableMapTest_tearDown();
}
function test_getVariable_NotFound() {
variableMapTest_setUp();
var result = variable_map.getVariable('name1');
assertNull(result);
variableMapTest_tearDown();
}
function test_getVariableById_Trivial() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', 'type1', 'id1');
var var_2 = variable_map.createVariable('name2', 'type1', 'id2');
var var_3 = variable_map.createVariable('name3', 'type2', 'id3');
var result_1 = variable_map.getVariableById('id1');
var result_2 = variable_map.getVariableById('id2');
var result_3 = variable_map.getVariableById('id3');
assertEquals(var_1, result_1);
assertEquals(var_2, result_2);
assertEquals(var_3, result_3);
variableMapTest_tearDown();
}
function test_getVariableById_NotFound() {
variableMapTest_setUp();
var result = variable_map.getVariableById('id1');
assertNull(result);
variableMapTest_tearDown();
}
function test_createVariableTrivial() {
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
checkVariableValues(variable_map, 'name1', 'type1', 'id1');
variableMapTest_tearDown();
}
function test_createVariableAlreadyExists() {
// Expect that when the variable already exists, the variableMap_ is unchanged.
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
// Assert there is only one variable in the variable_map.
var keys = Object.keys(variable_map.variableMap_);
assertEquals(1, keys.length);
var varMapLength = variable_map.variableMap_[keys[0]].length;
assertEquals(1, varMapLength);
variable_map.createVariable('name1', 'type1');
checkVariableValues(variable_map, 'name1', 'type1', 'id1');
// Check that the size of the variableMap_ did not change.
keys = Object.keys(variable_map.variableMap_);
assertEquals(1, keys.length);
varMapLength = variable_map.variableMap_[keys[0]].length;
assertEquals(1, varMapLength);
variableMapTest_tearDown();
}
function test_createVariableNameAlreadyExists() {
// Expect that when a variable with the same name but a different type already
// exists, the new variable is created.
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
// Assert there is only one variable in the variable_map.
var keys = Object.keys(variable_map.variableMap_);
assertEquals(1, keys.length);
var varMapLength = variable_map.variableMap_[keys[0]].length;
assertEquals(1, varMapLength);
variable_map.createVariable('name1', 'type2', 'id2');
checkVariableValues(variable_map, 'name1', 'type1', 'id1');
checkVariableValues(variable_map, 'name1', 'type2', 'id2');
// Check that the size of the variableMap_ did change.
keys = Object.keys(variable_map.variableMap_);
assertEquals(2, keys.length);
variableMapTest_tearDown();
}
function test_createVariableNullAndUndefinedType() {
variableMapTest_setUp();
variable_map.createVariable('name1', null, 'id1');
variable_map.createVariable('name2', undefined, 'id2');
checkVariableValues(variable_map, 'name1', '', 'id1');
checkVariableValues(variable_map, 'name2', '', 'id2');
variableMapTest_tearDown();
}
function test_createVariableNullId() {
variableMapTest_setUp();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '2']);
try {
variable_map.createVariable('name1', 'type1', null);
checkVariableValues(variable_map, 'name1', 'type1', '1');
} finally {
variableMapTest_tearDown();
}
}
function test_createVariableUndefinedId() {
variableMapTest_setUp();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '2']);
try {
variable_map.createVariable('name1', 'type1', undefined);
checkVariableValues(variable_map, 'name1', 'type1', '1');
} finally {
variableMapTest_tearDown();
}
}
function test_createVariableIdAlreadyExists() {
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
try {
variable_map.createVariable('name2', 'type2', 'id1');
fail();
} catch (e) {
// expected
}
variableMapTest_tearDown();
}
function test_createVariableMismatchedIdAndType() {
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
try {
variable_map.createVariable('name1', 'type2', 'id1');
fail();
} catch (e) {
// expected
}
try {
variable_map.createVariable('name1', 'type1', 'id2');
fail();
} catch (e) {
// expected
}
variableMapTest_tearDown();
}
function test_createVariableTwoSameTypes() {
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
variable_map.createVariable('name2', 'type1', 'id2');
checkVariableValues(variable_map, 'name1', 'type1', 'id1');
checkVariableValues(variable_map, 'name2', 'type1', 'id2');
variableMapTest_tearDown();
}
function test_getVariablesOfType_Trivial() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', 'type1', 'id1');
var var_2 = variable_map.createVariable('name2', 'type1', 'id2');
variable_map.createVariable('name3', 'type2', 'id3');
variable_map.createVariable('name4', 'type3', 'id4');
var result_array_1 = variable_map.getVariablesOfType('type1');
var result_array_2 = variable_map.getVariablesOfType('type5');
isEqualArrays([var_1, var_2], result_array_1);
isEqualArrays([], result_array_2);
variableMapTest_tearDown();
}
function test_getVariablesOfType_Null() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', '', 'id1');
var var_2 = variable_map.createVariable('name2', '', 'id2');
var var_3 = variable_map.createVariable('name3', '', 'id3');
variable_map.createVariable('name4', 'type1', 'id4');
var result_array = variable_map.getVariablesOfType(null);
isEqualArrays([var_1, var_2, var_3], result_array);
variableMapTest_tearDown();
}
function test_getVariablesOfType_EmptyString() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', null, 'id1');
var var_2 = variable_map.createVariable('name2', null, 'id2');
var result_array = variable_map.getVariablesOfType('');
isEqualArrays([var_1, var_2], result_array);
variableMapTest_tearDown();
}
function test_getVariablesOfType_Deleted() {
variableMapTest_setUp();
var variable = variable_map.createVariable('name1', null, 'id1');
variable_map.deleteVariable(variable);
var result_array = variable_map.getVariablesOfType('');
isEqualArrays([], result_array);
variableMapTest_tearDown();
}
function test_getVariablesOfType_DoesNotExist() {
variableMapTest_setUp();
var result_array = variable_map.getVariablesOfType('type1');
isEqualArrays([], result_array);
variableMapTest_tearDown();
}
function test_getVariableTypes_Trivial() {
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
variable_map.createVariable('name2', 'type1', 'id2');
variable_map.createVariable('name3', 'type2', 'id3');
variable_map.createVariable('name4', 'type3', 'id4');
var result_array = variable_map.getVariableTypes();
// The empty string is always an option.
isEqualArrays(['type1', 'type2', 'type3', ''], result_array);
variableMapTest_tearDown();
}
function test_getVariableTypes_None() {
variableMapTest_setUp();
// The empty string is always an option.
var result_array = variable_map.getVariableTypes();
isEqualArrays([''], result_array);
variableMapTest_tearDown();
}
function test_getAllVariables_Trivial() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', 'type1', 'id1');
var var_2 = variable_map.createVariable('name2', 'type1', 'id2');
var var_3 = variable_map.createVariable('name3', 'type2', 'id3');
var result_array = variable_map.getAllVariables();
isEqualArrays([var_1, var_2, var_3], result_array);
variableMapTest_tearDown();
}
function test_getAllVariables_None() {
variableMapTest_setUp();
var result_array = variable_map.getAllVariables();
isEqualArrays([], result_array);
variableMapTest_tearDown();
}

View File

@@ -0,0 +1,91 @@
/**
* @license
* Blockly Tests
*
* 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.
*/
/**
* @fileoverview Tests for variable model.
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
var variable;
var workspace;
function variableModelTest_setUp() {
workspace = new Blockly.Workspace();
}
function variableModelTest_tearDown() {
workspace.dispose();
variable = null;
}
/**
* These tests check the constructor of the variable model.
*/
function testInit_Trivial() {
variableModelTest_setUp();
variable = new Blockly.VariableModel(workspace, 'test', 'test_type',
'test_id');
assertEquals('test', variable.name);
assertEquals('test_type', variable.type);
assertEquals('test_id', variable.id_);
variableModelTest_tearDown();
}
function testInit_NullType() {
variableModelTest_setUp();
variable = new Blockly.VariableModel(workspace, 'test', null, 'test_id');
assertEquals('', variable.type);
variableModelTest_tearDown();
}
function testInit_UndefinedType() {
variableModelTest_setUp();
variable = new Blockly.VariableModel(workspace, 'test', undefined, 'test_id');
assertEquals('', variable.type);
variableModelTest_tearDown();
}
function testInit_NullId() {
variableModelTest_setUp();
variable = new Blockly.VariableModel(workspace, 'test', 'test_type', null);
assertEquals('test', variable.name);
assertEquals('test_type', variable.type);
assertNotNull(variable.id_);
variableModelTest_tearDown();
}
function testInit_UndefinedId() {
variableModelTest_setUp();
variable = new Blockly.VariableModel(workspace, 'test', 'test_type', undefined);
assertEquals('test', variable.name);
assertEquals('test_type', variable.type);
assertNotNull(variable.id_);
variableModelTest_tearDown();
}
function testInit_OnlyNameProvided() {
variableModelTest_setUp();
variable = new Blockly.VariableModel(workspace, 'test');
assertEquals('test', variable.name);
assertEquals('', variable.type);
assertNotNull(variable.id_);
variableModelTest_tearDown();
}

View File

@@ -0,0 +1,99 @@
/**
* @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 variable utility functions in Blockly
* @author fenichel@google.com (Rachel Fenichel)
*/
'use strict';
goog.require('goog.testing');
function variablesTest_setUp() {
defineGetVarBlock();
var workspace = new Blockly.Workspace();
workspace.createVariable('foo', 'type1', '1');
workspace.createVariable('bar', 'type1', '2');
workspace.createVariable('baz', 'type1', '3');
return workspace;
}
function variablesTest_tearDown(workspace) {
undefineGetVarBlock();
workspace.dispose();
}
function buildVariablesTest(testImpl) {
return function() {
var workspace = variablesTest_setUp();
try {
testImpl(workspace);
} finally {
variablesTest_tearDown(workspace);
}
};
}
var test_allUsedVarModels = buildVariablesTest(
function(workspace) {
createMockVarBlock(workspace, '1');
createMockVarBlock(workspace, '2');
createMockVarBlock(workspace, '3');
var result = Blockly.Variables.allUsedVarModels(workspace);
assertEquals('Expected three variables in the list of used variables',
3, result.length);
}
);
var test_allUsedVarModels_someUnused = buildVariablesTest(
function(workspace) {
createMockVarBlock(workspace, '2');
var result = Blockly.Variables.allUsedVarModels(workspace);
assertEquals('Expected one variable in the list of used variables',
1, result.length);
assertEquals('Expected variable with ID 2 in the list of used variables',
'2', result[0].getId());
}
);
var test_allUsedVarModels_varUsedTwice = buildVariablesTest(
function(workspace) {
createMockVarBlock(workspace, '2');
createMockVarBlock(workspace, '2');
var result = Blockly.Variables.allUsedVarModels(workspace);
// Using the same variable multiple times should not change the number of
// elements in the list.
assertEquals('Expected one variable in the list of used variables',
1, result.length);
assertEquals('Expected variable with ID 2 in the list of used variables',
'2', result[0].getId());
}
);
var test_allUsedVarModels_allUnused = buildVariablesTest(
function(workspace) {
var result = Blockly.Variables.allUsedVarModels(workspace);
assertEquals('Expected no variables in the list of used variables',
0, result.length);
}
);

View File

@@ -0,0 +1,153 @@
/**
* @license
* Blockly Tests
*
* 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.
*/
'use strict';
goog.require('goog.testing');
function widgetdiv_testHelper_makeBBox(left, top, width, height) {
return {
left: left,
right: left + width,
top: top,
bottom: top + height
};
}
function widgetdiv_testHelper_makeSize(width, height) {
return {
width: width,
height: height
};
}
var widgetDiv_test_viewport = widgetdiv_testHelper_makeBBox(0, 0, 1000, 1000);
var widgetDiv_test_widgetSize = widgetdiv_testHelper_makeSize(100, 100);
// Anchor is always 90 px wide and 90 px tall for this test.
var widgetDiv_test_anchorSize = 90;
function widgetdiv_testHelper_makeAnchor(left, top) {
return {
left: left,
right: left + widgetDiv_test_anchorSize,
top: top,
bottom: top + widgetDiv_test_anchorSize
};
}
function test_widgetDiv_topConflict() {
var anchorTop = 50;
// Anchor placed close to the top.
var anchorBBox = widgetdiv_testHelper_makeAnchor(500, anchorTop);
// The widget div should be placed just below the anchor.
var calculated = Blockly.WidgetDiv.calculateY_(widgetDiv_test_viewport,
anchorBBox, widgetDiv_test_widgetSize);
assertEquals(anchorTop + widgetDiv_test_anchorSize, calculated);
}
function test_widgetDiv_bottomConflict() {
var anchorTop = 900;
// Anchor placed close to the bottom.
var anchorBBox = widgetdiv_testHelper_makeAnchor(500, anchorTop);
// The widget div should be placed just above the anchor.
var calculated = Blockly.WidgetDiv.calculateY_(widgetDiv_test_viewport,
anchorBBox, widgetDiv_test_widgetSize);
assertEquals(anchorTop - widgetDiv_test_widgetSize.height, calculated);
}
function test_widgetDiv_noYConflict() {
var anchorTop = 500;
// Anchor placed in the middle.
var anchorBBox = widgetdiv_testHelper_makeAnchor(500, anchorTop);
// The widget div should be placed just below the anchor.
var calculated = Blockly.WidgetDiv.calculateY_(widgetDiv_test_viewport,
anchorBBox, widgetDiv_test_widgetSize);
assertEquals(anchorTop + widgetDiv_test_anchorSize, calculated);
}
function test_widgetDiv_leftConflict_LTR() {
var anchorLeft = 50;
// Anchor placed close to the left side.
var anchorBBox = widgetdiv_testHelper_makeAnchor(anchorLeft, 500);
// The widget div should be placed at the anchor.
var calculated = Blockly.WidgetDiv.calculateX_(widgetDiv_test_viewport,
anchorBBox, widgetDiv_test_widgetSize, false /* rtl */);
assertEquals(anchorLeft, calculated);
}
function test_widgetDiv_rightConflict_LTR() {
var anchorLeft = 950;
// Anchor placed close to the right side.
var anchorBBox = widgetdiv_testHelper_makeAnchor(anchorLeft, 500);
// The widget div should be placed as far right as possible--at the edge of
// the screen.
var calculated = Blockly.WidgetDiv.calculateX_(widgetDiv_test_viewport,
anchorBBox, widgetDiv_test_widgetSize, false /* rtl */);
assertEquals(1000 - widgetDiv_test_widgetSize.width, calculated);
}
function test_widgetDiv_noXConflict_LTR() {
var anchorLeft = 500;
// Anchor in the middle
var anchorBBox = widgetdiv_testHelper_makeAnchor(anchorLeft, 500);
// The widget div should be placed just at the left side of the anchor.
var calculated = Blockly.WidgetDiv.calculateX_(widgetDiv_test_viewport,
anchorBBox, widgetDiv_test_widgetSize, false /* rtl */);
assertEquals(anchorLeft, calculated);
}
function test_widgetDiv_leftConflict_RTL() {
var anchorLeft = 10;
// Anchor placed close to the left side.
var anchorBBox = widgetdiv_testHelper_makeAnchor(anchorLeft, 500);
// The widget div should be placed as far left as possible--at the edge of
// the screen.
var calculated = Blockly.WidgetDiv.calculateX_(widgetDiv_test_viewport,
anchorBBox, widgetDiv_test_widgetSize, true /* rtl */);
assertEquals(0, calculated);
}
function test_widgetDiv_rightConflict_RTL() {
var anchorLeft = 950;
// Anchor placed close to the right side.
var anchorBBox = widgetdiv_testHelper_makeAnchor(anchorLeft, 500);
// The widget div should be placed as far right as possible--at the edge of
// the screen.
var calculated = Blockly.WidgetDiv.calculateX_(widgetDiv_test_viewport,
anchorBBox, widgetDiv_test_widgetSize, true /* rtl */);
assertEquals(1000 - widgetDiv_test_widgetSize.width, calculated);
}
function test_widgetDiv_noXConflict_RTL() {
var anchorLeft = 500;
// anchor placed in the middle
var anchorBBox = widgetdiv_testHelper_makeAnchor(anchorLeft, 500);
// The widget div should be placed at the right side of the anchor.
var calculated = Blockly.WidgetDiv.calculateX_(widgetDiv_test_viewport,
anchorBBox, widgetDiv_test_widgetSize, true /* rtl */);
assertEquals(anchorBBox.right - widgetDiv_test_widgetSize.width, calculated);
}

View File

@@ -0,0 +1,376 @@
/**
* @license
* Blockly Tests
*
* Copyright 2012 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.
*/
'use strict';
goog.require('goog.testing');
goog.require('goog.testing.MockControl');
var workspace;
var mockControl_;
function workspaceTest_setUp() {
defineGetVarBlock();
workspace = new Blockly.Workspace();
mockControl_ = new goog.testing.MockControl();
}
function workspaceTest_tearDown() {
undefineGetVarBlock();
mockControl_.$tearDown();
workspace.dispose();
}
function test_emptyWorkspace() {
workspaceTest_setUp();
try {
assertEquals('Empty workspace (1).', 0, workspace.getTopBlocks(true).length);
assertEquals('Empty workspace (2).', 0, workspace.getTopBlocks(false).length);
assertEquals('Empty workspace (3).', 0, workspace.getAllBlocks().length);
workspace.clear();
assertEquals('Empty workspace (4).', 0, workspace.getTopBlocks(true).length);
assertEquals('Empty workspace (5).', 0, workspace.getTopBlocks(false).length);
assertEquals('Empty workspace (6).', 0, workspace.getAllBlocks().length);
} finally {
workspaceTest_tearDown();
}
}
function test_flatWorkspace() {
workspaceTest_setUp();
try {
var blockA = workspace.newBlock('');
assertEquals('One block workspace (1).', 1, workspace.getTopBlocks(true).length);
assertEquals('One block workspace (2).', 1, workspace.getTopBlocks(false).length);
assertEquals('One block workspace (3).', 1, workspace.getAllBlocks().length);
var blockB = workspace.newBlock('');
assertEquals('Two block workspace (1).', 2, workspace.getTopBlocks(true).length);
assertEquals('Two block workspace (2).', 2, workspace.getTopBlocks(false).length);
assertEquals('Two block workspace (3).', 2, workspace.getAllBlocks().length);
blockA.dispose();
assertEquals('One block workspace (4).', 1, workspace.getTopBlocks(true).length);
assertEquals('One block workspace (5).', 1, workspace.getTopBlocks(false).length);
assertEquals('One block workspace (6).', 1, workspace.getAllBlocks().length);
workspace.clear();
assertEquals('Cleared workspace (1).', 0, workspace.getTopBlocks(true).length);
assertEquals('Cleared workspace (2).', 0, workspace.getTopBlocks(false).length);
assertEquals('Cleared workspace (3).', 0, workspace.getAllBlocks().length);
} finally {
workspaceTest_tearDown();
}
}
function test_maxBlocksWorkspace() {
workspaceTest_setUp();
try {
var blockA = workspace.newBlock('');
var blockB = workspace.newBlock('');
assertEquals('Infinite capacity.', Infinity, workspace.remainingCapacity());
workspace.options.maxBlocks = 3;
assertEquals('Three capacity.', 1, workspace.remainingCapacity());
workspace.options.maxBlocks = 2;
assertEquals('Two capacity.', 0, workspace.remainingCapacity());
workspace.options.maxBlocks = 1;
assertEquals('One capacity.', -1, workspace.remainingCapacity());
workspace.options.maxBlocks = 0;
assertEquals('Zero capacity.', -2, workspace.remainingCapacity());
workspace.clear();
assertEquals('Cleared capacity.', 0, workspace.remainingCapacity());
} finally {
workspaceTest_tearDown();
}
}
function test_getWorkspaceById() {
var workspaceA = new Blockly.Workspace();
var workspaceB = new Blockly.Workspace();
try {
assertEquals('Find workspaceA.', workspaceA,
Blockly.Workspace.getById(workspaceA.id));
assertEquals('Find workspaceB.', workspaceB,
Blockly.Workspace.getById(workspaceB.id));
assertEquals('No workspace found.', null,
Blockly.Workspace.getById('I do not exist.'));
workspaceA.dispose();
assertEquals('Can\'t find workspaceA.', null,
Blockly.Workspace.getById(workspaceA.id));
assertEquals('WorkspaceB exists.', workspaceB,
Blockly.Workspace.getById(workspaceB.id));
} finally {
workspaceB.dispose();
workspaceA.dispose();
}
}
function test_getBlockById() {
workspaceTest_setUp();
try {
var blockA = workspace.newBlock('');
var blockB = workspace.newBlock('');
assertEquals('Find blockA.', blockA, workspace.getBlockById(blockA.id));
assertEquals('Find blockB.', blockB, workspace.getBlockById(blockB.id));
assertEquals('No block found.', null,
workspace.getBlockById('I do not exist.'));
blockA.dispose();
assertEquals('Can\'t find blockA.', null, workspace.getBlockById(blockA.id));
assertEquals('BlockB exists.', blockB, workspace.getBlockById(blockB.id));
workspace.clear();
assertEquals('Can\'t find blockB.', null, workspace.getBlockById(blockB.id));
} finally {
workspaceTest_tearDown();
}
}
function test_deleteVariable_InternalTrivial() {
workspaceTest_setUp();
var var_1 = workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type2', 'id2');
createMockVarBlock(workspace, 'id1');
createMockVarBlock(workspace, 'id1');
createMockVarBlock(workspace, 'id2');
var uses = workspace.getVariableUsesById(var_1.getId());
workspace.deleteVariableInternal_(var_1, uses);
var variable = workspace.getVariableById('id1');
var block_var_name = workspace.topBlocks_[0].getVarModels()[0].name;
assertNull(variable);
checkVariableValues(workspace, 'name2', 'type2', 'id2');
assertEquals('name2', block_var_name);
workspaceTest_tearDown();
}
// TODO(marisaleung): Test the alert for deleting a variable that is a procedure.
function test_addTopBlock_TrivialFlyoutIsTrue() {
workspaceTest_setUp();
var targetWorkspace = new Blockly.Workspace();
workspace.isFlyout = true;
workspace.targetWorkspace = targetWorkspace;
targetWorkspace.createVariable('name1', '', '1');
// Flyout.init usually does this binding.
workspace.variableMap_ = targetWorkspace.getVariableMap();
try {
var block = createMockVarBlock(workspace, '1');
workspace.removeTopBlock(block);
workspace.addTopBlock(block);
checkVariableValues(workspace, 'name1', '', '1');
} finally {
workspaceTest_tearDown();
// Have to dispose of the main workspace after the flyout workspace, because
// it holds the variable map.
// Normally the main workspace disposes of the flyout workspace.
targetWorkspace.dispose();
}
}
function test_clear_Trivial() {
workspaceTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type2', 'id2');
setUpMockMethod(mockControl_, Blockly.Events, 'setGroup', [true, false],
null);
try {
workspace.clear();
var topBlocks_length = workspace.topBlocks_.length;
var varMapLength = Object.keys(workspace.variableMap_.variableMap_).length;
assertEquals(0, topBlocks_length);
assertEquals(0, varMapLength);
} finally {
workspaceTest_tearDown();
}
}
function test_clear_NoVariables() {
workspaceTest_setUp();
setUpMockMethod(mockControl_, Blockly.Events, 'setGroup', [true, false],
null);
try {
workspace.clear();
var topBlocks_length = workspace.topBlocks_.length;
var varMapLength = Object.keys(workspace.variableMap_.variableMap_).length;
assertEquals(0, topBlocks_length);
assertEquals(0, varMapLength);
} finally {
workspaceTest_tearDown();
}
}
function test_renameVariable_NoReference() {
// Test renaming a variable in the simplest case: when no blocks refer to it.
workspaceTest_setUp();
var id = 'id1';
var type = 'type1';
var oldName = 'name1';
var newName = 'name2';
workspace.createVariable(oldName, type, id);
try {
workspace.renameVariableById(id, newName);
checkVariableValues(workspace, newName, type, id);
// Renaming should not have created a new variable.
assertEquals(1, workspace.getAllVariables().length);
} finally {
workspaceTest_tearDown();
}
}
function test_renameVariable_ReferenceExists() {
// Test renaming a variable when a reference to it exists.
// Expect 'renameVariable' to change oldName variable name to newName.
workspaceTest_setUp();
var newName = 'name2';
createVariableAndBlock(workspace);
workspace.renameVariableById('id1', newName);
checkVariableValues(workspace, newName, 'type1', 'id1');
// Renaming should not have created a new variable.
assertEquals(1, workspace.getAllVariables().length);
var block_var_name = workspace.topBlocks_[0].getVarModels()[0].name;
assertEquals(newName, block_var_name);
workspaceTest_tearDown();
}
function test_renameVariable_TwoVariablesSameType() {
// Expect 'renameVariable' to change oldName variable name to newName.
// Expect oldName block name to change to newName
workspaceTest_setUp();
var id1 = 'id1';
var id2 = 'id2';
var type = 'type1';
var oldName = 'name1';
var newName = 'name2';
// Create two variables of the same type.
workspace.createVariable(oldName, type, id1);
workspace.createVariable(newName, type, id2);
// Create blocks to refer to both of them.
createMockVarBlock(workspace, id1);
createMockVarBlock(workspace, id2);
workspace.renameVariableById(id1, newName);
checkVariableValues(workspace, newName, type, id2);
// The old variable should have been deleted.
var variable = workspace.getVariableById(id1);
assertNull(variable);
// There should only be one variable left.
assertEquals(1, workspace.getAllVariables().length);
// References should have the correct names.
var block_var_name_1 = workspace.topBlocks_[0].getVarModels()[0].name;
var block_var_name_2 = workspace.topBlocks_[1].getVarModels()[0].name;
assertEquals(newName, block_var_name_1);
assertEquals(newName, block_var_name_2);
workspaceTest_tearDown();
}
function test_renameVariable_TwoVariablesDifferentType() {
// Expect the rename to succeed, because variables with different types are
// allowed to have the same name.
workspaceTest_setUp();
createTwoVariablesAndBlocks(workspace);
var newName = 'name2';
workspace.renameVariableById('id1', newName);
checkVariableValues(workspace, newName, 'type1', 'id1');
checkVariableValues(workspace, newName, 'type2', 'id2');
// References shoul have the correct names.
var block_var_name_1 = workspace.topBlocks_[0].getVarModels()[0].name;
var block_var_name_2 = workspace.topBlocks_[1].getVarModels()[0].name;
assertEquals(newName, block_var_name_1);
assertEquals(newName, block_var_name_2);
workspaceTest_tearDown();
}
function test_renameVariable_OldCase() {
// Rename a variable with a single reference. Update only the capitalization.
workspaceTest_setUp();
var newName = 'Name1';
createVariableAndBlock(workspace);
workspace.renameVariableById('id1', newName);
checkVariableValues(workspace, newName, 'type1', 'id1');
var variable = workspace.getVariableById('id1');
assertNotEquals('name1', variable.name);
workspaceTest_tearDown();
}
function test_renameVariable_TwoVariablesAndOldCase() {
// Test renaming a variable to an in-use name, but with different
// capitalization. The new capitalization should apply everywhere.
// TODO (fenichel): What about different capitalization but also different
// types?
workspaceTest_setUp();
var oldName = 'name1';
var oldCase = 'Name2';
var newName = 'name2';
var id1 = 'id1';
var id2 = 'id2';
var type = 'type1';
workspace.createVariable(oldName, type, id1);
workspace.createVariable(oldCase, type, id2);
createMockVarBlock(workspace, id1);
createMockVarBlock(workspace, id2);
workspace.renameVariableById(id1, newName);
checkVariableValues(workspace, newName, type, id2);
// The old variable should have been deleted.
var variable = workspace.getVariableById(id1);
assertNull(variable);
// There should only be one variable left.
assertEquals(1, workspace.getAllVariables().length);
// Blocks should now use the new capitalization.
var block_var_name_1 = workspace.topBlocks_[0].getVarModels()[0].name;
var block_var_name_2 = workspace.topBlocks_[1].getVarModels()[0].name;
assertEquals(newName, block_var_name_1);
assertEquals(newName, block_var_name_2);
workspaceTest_tearDown();
}
function test_deleteVariableById_Trivial() {
workspaceTest_setUp();
createTwoVariablesAndBlocks(workspace);
workspace.deleteVariableById('id1');
checkVariableValues(workspace, 'name2', 'type2', 'id2');
var variable = workspace.getVariableById('id1');
var block_var_name = workspace.topBlocks_[0].getVarModels()[0].name;
assertNull(variable);
assertEquals('name2', block_var_name);
workspaceTest_tearDown();
}

View File

@@ -0,0 +1,381 @@
/**
* @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.
*/
/**
* @fileoverview Tests for Blockly.Workspace.undo.
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
goog.require('goog.events.EventHandler');
goog.require('goog.testing');
goog.require('goog.testing.events');
goog.require('goog.testing.MockControl');
var workspace;
var mockControl_;
var savedFireFunc = Blockly.Events.fire;
function temporary_fireEvent(event) {
if (!Blockly.Events.isEnabled()) {
return;
}
Blockly.Events.FIRE_QUEUE_.push(event);
Blockly.Events.fireNow_();
}
function undoRedoTest_setUp() {
defineGetVarBlock();
workspace = new Blockly.Workspace();
mockControl_ = new goog.testing.MockControl();
Blockly.Events.fire = temporary_fireEvent;
}
function undoRedoTest_tearDown() {
undefineGetVarBlock();
mockControl_.$tearDown();
workspace.dispose();
Blockly.Events.fire = savedFireFunc;
}
/**
* Check that the top block with the given index contains a variable with
* the given name.
* @param {number} blockIndex The index of the top block.
* @param {string} name The expected name of the variable in the block.
*/
function undoRedoTest_checkBlockVariableName(blockIndex, name) {
var blockVarName = workspace.topBlocks_[blockIndex].getVarModels()[0].name;
assertEquals(name, blockVarName);
}
function createTwoVarsEmptyType() {
workspace.createVariable('name1', '', 'id1');
workspace.createVariable('name2', '', 'id2');
}
function createTwoVarsDifferentTypes() {
workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type2', 'id2');
}
function test_undoCreateVariable_Trivial() {
undoRedoTest_setUp();
createTwoVarsDifferentTypes();
workspace.undo();
checkVariableValues(workspace, 'name1', 'type1', 'id1');
assertNull(workspace.getVariableById('id2'));
workspace.undo();
assertNull(workspace.getVariableById('id1'));
assertNull(workspace.getVariableById('id2'));
undoRedoTest_tearDown();
}
function test_redoAndUndoCreateVariable_Trivial() {
undoRedoTest_setUp();
createTwoVarsDifferentTypes();
workspace.undo();
workspace.undo(true);
// Expect that variable 'id2' is recreated
checkVariableValues(workspace, 'name1', 'type1', 'id1');
checkVariableValues(workspace, 'name2', 'type2', 'id2');
workspace.undo();
workspace.undo();
workspace.undo(true);
// Expect that variable 'id1' is recreated
checkVariableValues(workspace, 'name1', 'type1', 'id1');
assertNull(workspace.getVariableById('id2'));
undoRedoTest_tearDown();
}
function test_undoDeleteVariable_NoBlocks() {
undoRedoTest_setUp();
createTwoVarsDifferentTypes();
workspace.deleteVariableById('id1');
workspace.deleteVariableById('id2');
workspace.undo();
assertNull(workspace.getVariableById('id1'));
checkVariableValues(workspace, 'name2', 'type2', 'id2');
workspace.undo();
checkVariableValues(workspace, 'name1', 'type1', 'id1');
checkVariableValues(workspace, 'name2', 'type2', 'id2');
undoRedoTest_tearDown();
}
function test_undoDeleteVariable_WithBlocks() {
undoRedoTest_setUp();
createTwoVariablesAndBlocks(workspace);
workspace.deleteVariableById('id1');
workspace.deleteVariableById('id2');
workspace.undo();
undoRedoTest_checkBlockVariableName(0, 'name2');
assertNull(workspace.getVariableById('id1'));
checkVariableValues(workspace, 'name2', 'type2', 'id2');
workspace.undo();
undoRedoTest_checkBlockVariableName(0, 'name2');
undoRedoTest_checkBlockVariableName(1, 'name1');
checkVariableValues(workspace, 'name1', 'type1', 'id1');
checkVariableValues(workspace, 'name2', 'type2', 'id2');
undoRedoTest_tearDown();
}
function test_redoAndUndoDeleteVariable_NoBlocks() {
undoRedoTest_setUp();
createTwoVarsDifferentTypes();
workspace.deleteVariableById('id1');
workspace.deleteVariableById('id2');
workspace.undo();
workspace.undo(true);
// Expect that both variables are deleted
assertNull(workspace.getVariableById('id1'));
assertNull(workspace.getVariableById('id2'));
workspace.undo();
workspace.undo();
workspace.undo(true);
// Expect that variable 'id2' is recreated
assertNull(workspace.getVariableById('id1'));
checkVariableValues(workspace, 'name2', 'type2', 'id2');
undoRedoTest_tearDown();
}
function test_redoAndUndoDeleteVariable_WithBlocks() {
undoRedoTest_setUp();
createTwoVariablesAndBlocks(workspace);
workspace.deleteVariableById('id1');
workspace.deleteVariableById('id2');
workspace.undo();
workspace.undo(true);
// Expect that both variables are deleted
assertEquals(0, workspace.topBlocks_.length);
assertNull(workspace.getVariableById('id1'));
assertNull(workspace.getVariableById('id2'));
workspace.undo();
workspace.undo();
workspace.undo(true);
// Expect that variable 'id2' is recreated
undoRedoTest_checkBlockVariableName(0, 'name2');
assertNull(workspace.getVariableById('id1'));
checkVariableValues(workspace, 'name2', 'type2', 'id2');
undoRedoTest_tearDown();
}
function test_redoAndUndoDeleteVariableTwice_NoBlocks() {
undoRedoTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
workspace.deleteVariableById('id1');
workspace.deleteVariableById('id1');
// Check the undoStack only recorded one delete event.
var undoStack = workspace.undoStack_;
assertEquals('var_delete', undoStack[undoStack.length-1].type);
assertNotEquals('var_delete', undoStack[undoStack.length-2].type);
// undo delete
workspace.undo();
checkVariableValues(workspace, 'name1', 'type1', 'id1');
// redo delete
workspace.undo(true);
assertNull(workspace.getVariableById('id1'));
// redo delete, nothing should happen
workspace.undo(true);
assertNull(workspace.getVariableById('id1'));
undoRedoTest_tearDown();
}
function test_redoAndUndoDeleteVariableTwice_WithBlocks() {
undoRedoTest_setUp();
var id = 'id1';
workspace.createVariable('name1', 'type1', id);
createMockVarBlock(workspace, id);
workspace.deleteVariableById(id);
workspace.deleteVariableById(id);
// Check the undoStack only recorded one delete event.
var undoStack = workspace.undoStack_;
assertEquals('var_delete', undoStack[undoStack.length-1].type);
assertEquals('delete', undoStack[undoStack.length-2].type);
assertNotEquals('var_delete', undoStack[undoStack.length-3].type);
// undo delete
workspace.undo();
undoRedoTest_checkBlockVariableName(0, 'name1');
checkVariableValues(workspace, 'name1', 'type1', id);
// redo delete
workspace.undo(true);
assertEquals(0, workspace.topBlocks_.length);
assertNull(workspace.getVariableById(id));
// redo delete, nothing should happen
workspace.undo(true);
assertEquals(0, workspace.topBlocks_.length);
assertNull(workspace.getVariableById(id));
undoRedoTest_tearDown();
}
function test_undoRedoRenameVariable_OneExists_NoBlocks() {
undoRedoTest_setUp();
workspace.createVariable('name1', '', 'id1');
workspace.renameVariableById('id1', 'name2');
workspace.undo();
checkVariableValues(workspace, 'name1', '', 'id1');
workspace.undo(true);
checkVariableValues(workspace, 'name2', '', 'id1');
undoRedoTest_tearDown();
}
function test_undoRedoRenameVariable_OneExists_WithBlocks() {
undoRedoTest_setUp();
workspace.createVariable('name1', '', 'id1');
createMockVarBlock(workspace, 'id1');
workspace.renameVariableById('id1', 'name2');
workspace.undo();
undoRedoTest_checkBlockVariableName(0, 'name1');
checkVariableValues(workspace, 'name1', '', 'id1');
workspace.undo(true);
checkVariableValues(workspace, 'name2', '', 'id1');
undoRedoTest_checkBlockVariableName(0, 'name2');
undoRedoTest_tearDown();
}
function test_undoRedoRenameVariable_BothExist_NoBlocks() {
undoRedoTest_setUp();
createTwoVarsEmptyType();
workspace.renameVariableById('id1', 'name2');
workspace.undo();
checkVariableValues(workspace, 'name1', '', 'id1');
checkVariableValues(workspace, 'name2', '', 'id2');
workspace.undo(true);
checkVariableValues(workspace, 'name2', '', 'id2');
assertNull(workspace.getVariableById('id1'));
undoRedoTest_tearDown();
}
function test_undoRedoRenameVariable_BothExist_WithBlocks() {
undoRedoTest_setUp();
createTwoVarsEmptyType();
createMockVarBlock(workspace, 'id1');
createMockVarBlock(workspace, 'id2');
workspace.renameVariableById('id1', 'name2');
workspace.undo();
undoRedoTest_checkBlockVariableName(0, 'name1');
undoRedoTest_checkBlockVariableName(1, 'name2');
checkVariableValues(workspace, 'name1', '', 'id1');
checkVariableValues(workspace, 'name2', '', 'id2');
workspace.undo(true);
undoRedoTest_checkBlockVariableName(0, 'name2');
undoRedoTest_checkBlockVariableName(1, 'name2');
undoRedoTest_tearDown();
}
function test_undoRedoRenameVariable_BothExistCaseChange_NoBlocks() {
undoRedoTest_setUp();
createTwoVarsEmptyType();
workspace.renameVariableById('id1', 'Name2');
workspace.undo();
checkVariableValues(workspace, 'name1', '', 'id1');
checkVariableValues(workspace, 'name2', '', 'id2');
workspace.undo(true);
checkVariableValues(workspace, 'Name2', '', 'id2');
assertNull(workspace.getVariable('name1'));
undoRedoTest_tearDown();
}
function test_undoRedoRenameVariable_BothExistCaseChange_WithBlocks() {
undoRedoTest_setUp();
createTwoVarsEmptyType();
createMockVarBlock(workspace, 'id1');
createMockVarBlock(workspace, 'id2');
workspace.renameVariableById('id1', 'Name2');
workspace.undo();
undoRedoTest_checkBlockVariableName(0, 'name1');
undoRedoTest_checkBlockVariableName(1, 'name2');
checkVariableValues(workspace, 'name1', '', 'id1');
checkVariableValues(workspace, 'name2', '', 'id2');
workspace.undo(true);
checkVariableValues(workspace, 'Name2', '', 'id2');
assertNull(workspace.getVariableById('id1'));
undoRedoTest_checkBlockVariableName(0, 'Name2');
undoRedoTest_checkBlockVariableName(1, 'Name2');
undoRedoTest_tearDown();
}
function test_undoRedoRenameVariable_OnlyCaseChange_NoBlocks() {
undoRedoTest_setUp();
workspace.createVariable('name1', '', 'id1');
workspace.renameVariableById('id1', 'Name1');
workspace.undo();
checkVariableValues(workspace, 'name1', '', 'id1');
workspace.undo(true);
checkVariableValues(workspace, 'Name1', '', 'id1');
undoRedoTest_tearDown();
}
function test_undoRedoRenameVariable_OnlyCaseChange_WithBlocks() {
undoRedoTest_setUp();
workspace.createVariable('name1', '', 'id1');
createMockVarBlock(workspace, 'id1');
workspace.renameVariableById('id1', 'Name1');
workspace.undo();
undoRedoTest_checkBlockVariableName(0, 'name1');
checkVariableValues(workspace, 'name1', '', 'id1');
workspace.undo(true);
checkVariableValues(workspace, 'Name1', '', 'id1');
undoRedoTest_checkBlockVariableName(0, 'Name1');
undoRedoTest_tearDown();
}

View File

@@ -0,0 +1,402 @@
/**
* @license
* Blockly Tests
*
* Copyright 2014 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.
*/
'use strict';
goog.require('goog.testing');
goog.require('goog.testing.MockControl');
var mockControl_;
var workspace;
var XML_TEXT = ['<xml xmlns="http://www.w3.org/1999/xhtml">',
' <block type="controls_repeat_ext" inline="true" x="21" y="23">',
' <value name="TIMES">',
' <block type="math_number">',
' <field name="NUM">10</field>',
' </block>',
' </value>',
' <statement name="DO">',
' <block type="variables_set" inline="true">',
' <field name="VAR">item</field>',
' <value name="VALUE">',
' <block type="lists_create_empty"></block>',
' </value>',
' <next>',
' <block type="text_print" inline="false">',
' <value name="TEXT">',
' <block type="text">',
' <field name="TEXT">Hello</field>',
' </block>',
' </value>',
' </block>',
' </next>',
' </block>',
' </statement>',
' </block>',
'</xml>'].join('\n');
function xmlTest_setUp() {
workspace = new Blockly.Workspace();
mockControl_ = new goog.testing.MockControl();
}
function xmlTest_setUpWithMockBlocks() {
xmlTest_setUp();
Blockly.defineBlocksWithJsonArray([{
'type': 'field_variable_test_block',
'message0': '%1',
'args0': [
{
'type': 'field_variable',
'name': 'VAR',
'variable': 'item'
}
]
}]);
}
function xmlTest_tearDown() {
mockControl_.$tearDown();
workspace.dispose();
}
function xmlTest_tearDownWithMockBlocks() {
xmlTest_tearDown();
delete Blockly.Blocks.field_variable_test_block;
}
/**
* Check the values of the non variable field dom.
* @param {!Element} fieldDom The xml dom of the non variable field.
* @param {!string} name The expected name of the variable.
* @param {!string} text The expected text of the variable.
*/
function xmlTest_checkNonVariableField(fieldDom, name, text) {
assertEquals(text, fieldDom.textContent);
assertEquals(name, fieldDom.getAttribute('name'));
assertNull(fieldDom.getAttribute('id'));
assertNull(fieldDom.getAttribute('variabletype'));
}
/**
* Check the values of the variable field DOM.
* @param {!Element} fieldDom The xml dom of the variable field.
* @param {!string} name The expected name of the variable.
* @param {!string} type The expected type of the variable.
* @param {!string} id The expected id of the variable.
* @param {!string} text The expected text of the variable.
*/
function xmlTest_checkVariableFieldDomValues(fieldDom, name, type, id, text) {
assertEquals(name, fieldDom.getAttribute('name'));
assertEquals(type, fieldDom.getAttribute('variabletype'));
assertEquals(id, fieldDom.getAttribute('id'));
assertEquals(text, fieldDom.textContent);
}
/**
* Check the values of the variable DOM.
* @param {!Element} variableDom The xml dom of the variable.
* @param {!string} type The expected type of the variable.
* @param {!string} id The expected id of the variable.
* @param {!string} text The expected text of the variable.
*/
function xmlTest_checkVariableDomValues(variableDom, type, id, text) {
assertEquals(type, variableDom.getAttribute('type'));
assertEquals(id, variableDom.getAttribute('id'));
assertEquals(text, variableDom.textContent);
}
function test_textToDom() {
var dom = Blockly.Xml.textToDom(XML_TEXT);
assertEquals('XML tag', 'xml', dom.nodeName);
assertEquals('Block tags', 6, dom.getElementsByTagName('block').length);
}
function test_domToText() {
var dom = Blockly.Xml.textToDom(XML_TEXT);
var text = Blockly.Xml.domToText(dom);
assertEquals('Round trip', XML_TEXT.replace(/\s+/g, ''),
text.replace(/\s+/g, ''));
}
function test_domToWorkspace_BackwardCompatibility() {
// Expect that workspace still loads without serialized variables.
xmlTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '1']);
try {
var dom = Blockly.Xml.textToDom(
'<xml>' +
' <block type="field_variable_test_block" id="block_id">' +
' <field name="VAR">name1</field>' +
' </block>' +
'</xml>');
Blockly.Xml.domToWorkspace(dom, workspace);
assertEquals('Block count', 1, workspace.getAllBlocks().length);
checkVariableValues(workspace, 'name1', '', '1');
} finally {
xmlTest_tearDownWithMockBlocks();
}
}
function test_domToWorkspace_VariablesAtTop() {
// Expect that unused variables are preserved.
xmlTest_setUpWithMockBlocks();
try {
var dom = Blockly.Xml.textToDom(
'<xml>' +
' <variables>' +
' <variable type="type1" id="id1">name1</variable>' +
' <variable type="type2" id="id2">name2</variable>' +
' <variable type="" id="id3">name3</variable>' +
' </variables>' +
' <block type="field_variable_test_block">' +
' <field name="VAR" id="id3" variabletype="">name3</field>' +
' </block>' +
'</xml>');
Blockly.Xml.domToWorkspace(dom, workspace);
assertEquals('Block count', 1, workspace.getAllBlocks().length);
checkVariableValues(workspace, 'name1', 'type1', 'id1');
checkVariableValues(workspace, 'name2', 'type2', 'id2');
checkVariableValues(workspace, 'name3', '', 'id3');
} finally {
xmlTest_tearDownWithMockBlocks();
}
}
function test_domToWorkspace_VariablesAtTop_DuplicateVariablesTag() {
// Expect thrown Error because of duplicate 'variables' tag
xmlTest_setUpWithMockBlocks();
try {
var dom = Blockly.Xml.textToDom(
'<xml>' +
' <variables>' +
' </variables>' +
' <variables>' +
' </variables>' +
'</xml>');
Blockly.Xml.domToWorkspace(dom, workspace);
fail();
}
catch (e) {
// expected
} finally {
xmlTest_tearDownWithMockBlocks();
}
}
function test_domToWorkspace_VariablesAtTop_MissingType() {
// Expect thrown error when a variable tag is missing the type attribute.
workspace = new Blockly.Workspace();
try {
var dom = Blockly.Xml.textToDom(
'<xml>' +
' <variables>' +
' <variable id="id1">name1</variable>' +
' </variables>' +
' <block type="field_variable_test_block">' +
' <field name="VAR" id="id1" variabletype="">name3</field>' +
' </block>' +
'</xml>');
Blockly.Xml.domToWorkspace(dom, workspace);
fail();
} catch (e) {
// expected
} finally {
workspace.dispose();
}
}
function test_domToWorkspace_VariablesAtTop_MismatchBlockType() {
// Expect thrown error when the serialized type of a variable does not match
// the type of a variable field that references it.
xmlTest_setUpWithMockBlocks();
try {
var dom = Blockly.Xml.textToDom(
'<xml>' +
' <variables>' +
' <variable type="type1" id="id1">name1</variable>' +
' </variables>' +
' <block type="field_variable_test_block">' +
' <field name="VAR" id="id1" variabletype="">name1</field>' +
' </block>' +
'</xml>');
Blockly.Xml.domToWorkspace(dom, workspace);
fail();
} catch (e) {
// expected
} finally {
xmlTest_tearDownWithMockBlocks();
}
}
function test_domToPrettyText() {
var dom = Blockly.Xml.textToDom(XML_TEXT);
var text = Blockly.Xml.domToPrettyText(dom);
assertEquals('Round trip', XML_TEXT.replace(/\s+/g, ''),
text.replace(/\s+/g, ''));
}
/**
* Tests the that appendDomToWorkspace works in a headless mode.
* Also see test_appendDomToWorkspace() in workspace_svg_test.js.
*/
function test_appendDomToWorkspace() {
Blockly.Blocks.test_block = {
init: function() {
this.jsonInit({
message0: 'test',
});
}
};
try {
var dom = Blockly.Xml.textToDom(
'<xml xmlns="http://www.w3.org/1999/xhtml">' +
' <block type="test_block" inline="true" x="21" y="23">' +
' </block>' +
'</xml>');
workspace = new Blockly.Workspace();
Blockly.Xml.appendDomToWorkspace(dom, workspace);
assertEquals('Block count', 1, workspace.getAllBlocks().length);
var newBlockIds = Blockly.Xml.appendDomToWorkspace(dom, workspace);
assertEquals('Block count', 2, workspace.getAllBlocks().length);
assertEquals('Number of new block ids',1,newBlockIds.length);
} finally {
delete Blockly.Blocks.test_block;
workspace.dispose();
}
}
function test_blockToDom_fieldToDom_trivial() {
xmlTest_setUpWithMockBlocks();
// TODO (#1199): make a similar test where the variable is given a non-empty
// type.f
workspace.createVariable('name1', '', 'id1');
var block = new Blockly.Block(workspace, 'field_variable_test_block');
block.inputList[0].fieldRow[0].setValue('id1');
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
xmlTest_checkVariableFieldDomValues(resultFieldDom, 'VAR', '', 'id1',
'name1');
xmlTest_tearDownWithMockBlocks();
}
function test_blockToDom_fieldToDom_defaultCase() {
xmlTest_setUpWithMockBlocks();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '1']);
try {
workspace.createVariable('name1');
Blockly.Events.disable();
var block = new Blockly.Block(workspace, 'field_variable_test_block');
block.inputList[0].fieldRow[0].setValue('1');
Blockly.Events.enable();
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
// Expect type is '' and id is '1' since we don't specify type and id.
xmlTest_checkVariableFieldDomValues(resultFieldDom, 'VAR', '', '1', 'name1');
} finally {
xmlTest_tearDownWithMockBlocks();
}
}
function test_blockToDom_fieldToDom_notAFieldVariable() {
Blockly.defineBlocksWithJsonArray([{
"type": "field_angle_test_block",
"message0": "%1",
"args0": [
{
"type": "field_angle",
"name": "VAR",
"angle": 90
}
],
}]);
xmlTest_setUpWithMockBlocks();
var block = new Blockly.Block(workspace, 'field_angle_test_block');
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
xmlTest_checkNonVariableField(resultFieldDom, 'VAR', '90');
delete Blockly.Blocks.field_angle_block;
xmlTest_tearDownWithMockBlocks();
}
function test_variablesToDom_oneVariable() {
xmlTest_setUp();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
workspace.createVariable('name1');
var resultDom = Blockly.Xml.variablesToDom(workspace.getAllVariables());
assertEquals(1, resultDom.children.length);
var resultVariableDom = resultDom.children[0];
assertEquals('name1', resultVariableDom.textContent);
assertEquals('', resultVariableDom.getAttribute('type'));
assertEquals('1', resultVariableDom.getAttribute('id'));
xmlTest_tearDown();
}
function test_variablesToDom_twoVariables_oneBlock() {
xmlTest_setUpWithMockBlocks();
workspace.createVariable('name1', '', 'id1');
workspace.createVariable('name2', 'type2', 'id2');
// If events are enabled during block construction, it will create a default
// variable.
Blockly.Events.disable();
var block = new Blockly.Block(workspace, 'field_variable_test_block');
block.inputList[0].fieldRow[0].setValue('id1');
Blockly.Events.enable();
var resultDom = Blockly.Xml.variablesToDom(workspace.getAllVariables());
assertEquals(2, resultDom.children.length);
xmlTest_checkVariableDomValues(resultDom.children[0], '', 'id1',
'name1');
xmlTest_checkVariableDomValues(resultDom.children[1], 'type2', 'id2',
'name2');
xmlTest_tearDownWithMockBlocks();
}
function test_variablesToDom_noVariables() {
xmlTest_setUp();
workspace.createVariable('name1');
var resultDom = Blockly.Xml.variablesToDom(workspace.getAllVariables());
assertEquals(1, resultDom.children.length);
xmlTest_tearDown();
}
function test_variableFieldXml_caseSensitive() {
var id = 'testId';
var type = 'testType';
var name = 'testName';
var mockVariableModel = {
type: type,
name: name,
getId: function() {
return id;
}
};
var generatedXml =
Blockly.Variables.generateVariableFieldXmlString(mockVariableModel);
var goldenXml =
'<field name="VAR"' +
' id="' + id + '"' +
' variabletype="' + type + '"' +
'>' + name + '</field>';
assertEquals(goldenXml, generatedXml);
}