forked from google/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add initialization of blocks and event firing (google#5166)
* Change playground to use JSO system * Add tests for initialization and events * Add initialization of blocks * PR Comments
- Loading branch information
1 parent
38cddd6
commit 9138bca
Showing
6 changed files
with
252 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
goog.module('Blockly.test.jsoDeserialization'); | ||
|
||
const {assertEventFired, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers'); | ||
|
||
|
||
suite('JSO Deserialization', function() { | ||
setup(function() { | ||
sharedTestSetup.call(this); | ||
this.workspace = new Blockly.Workspace(); | ||
}); | ||
|
||
teardown(function() { | ||
workspaceTeardown.call(this, this.workspace); | ||
sharedTestTeardown.call(this); | ||
}); | ||
|
||
suite('Events', function() { | ||
test.skip('Finished loading', function() { | ||
const state = { | ||
'blocks': { | ||
'blocks': [ | ||
{ | ||
'type': 'controls_if', | ||
'id': 'testId', | ||
'x': 42, | ||
'y': 42 | ||
}, | ||
] | ||
} | ||
}; | ||
Blockly.serialization.workspaces.load(state, this.workspace); | ||
assertEventFired( | ||
this.eventsFireStub, | ||
Blockly.Events.FinishedLoading, | ||
{}, | ||
this.workspace.id); | ||
}); | ||
|
||
suite('Var create', function() { | ||
test('Just var', function() { | ||
const state = { | ||
'variables': [ | ||
{ | ||
'name': 'test', | ||
'id': 'testId', | ||
} | ||
] | ||
}; | ||
Blockly.serialization.workspaces.load(state, this.workspace); | ||
assertEventFired( | ||
this.eventsFireStub, | ||
Blockly.Events.VarCreate, | ||
{'varName': 'test', 'varId': 'testId', 'varType': ''}, | ||
this.workspace.id); | ||
}); | ||
|
||
test('Only fire one event with var and var on block', function() { | ||
const state = { | ||
'variables': [ | ||
{ | ||
'name': 'test', | ||
'id': 'testId', | ||
} | ||
], | ||
'blocks': { | ||
'blocks': [ | ||
{ | ||
'type': 'variables_get', | ||
'id': 'blockId', | ||
'x': 42, | ||
'y': 42, | ||
'fields': { | ||
'VAR': 'testId' | ||
} | ||
}, | ||
] | ||
} | ||
}; | ||
Blockly.serialization.workspaces.load(state, this.workspace); | ||
const calls = this.eventsFireStub.getCalls(); | ||
const count = calls.reduce((acc, call) => { | ||
if (call.args[0] instanceof Blockly.Events.VarCreate) { | ||
return acc + 1; | ||
} | ||
return acc; | ||
}, 0); | ||
chai.assert.equal(count, 1); | ||
assertEventFired( | ||
this.eventsFireStub, | ||
Blockly.Events.VarCreate, | ||
{'varName': 'test', 'varId': 'testId', 'varType': ''}, | ||
this.workspace.id); | ||
}); | ||
}); | ||
|
||
suite('Block create', function() { | ||
test('Simple', function() { | ||
const state = { | ||
'blocks': { | ||
'blocks': [ | ||
{ | ||
'type': 'controls_if', | ||
'id': 'testId', | ||
'x': 42, | ||
'y': 42 | ||
}, | ||
] | ||
} | ||
}; | ||
Blockly.serialization.workspaces.load(state, this.workspace); | ||
assertEventFired( | ||
this.eventsFireStub, | ||
Blockly.Events.BlockCreate, | ||
{}, | ||
this.workspace.id, | ||
'testId'); | ||
}); | ||
|
||
test('Only fire event for top block', function() { | ||
const state = { | ||
'blocks': { | ||
'blocks': [ | ||
{ | ||
'type': 'controls_if', | ||
'id': 'id1', | ||
'x': 42, | ||
'y': 42, | ||
'inputs': { | ||
'DO0': { | ||
'block': { | ||
'type': 'controls_if', | ||
'id': 'id2' | ||
} | ||
} | ||
}, | ||
'next': { | ||
'block': { | ||
'type': 'controls_if', | ||
'id': 'id3' | ||
} | ||
} | ||
}, | ||
] | ||
} | ||
}; | ||
Blockly.serialization.workspaces.load(state, this.workspace); | ||
assertEventFired( | ||
this.eventsFireStub, | ||
Blockly.Events.BlockCreate, | ||
{}, | ||
this.workspace.id, | ||
'id1'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters