forked from google/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_deserialization_test.js
122 lines (120 loc) · 3.71 KB
/
comment_deserialization_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {assert} from '../../node_modules/chai/chai.js';
import {
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
import {simulateClick} from './test_helpers/user_input.js';
suite('Comment Deserialization', function () {
setup(function () {
sharedTestSetup.call(this);
Blockly.defineBlocksWithJsonArray([
{
'type': 'empty_block',
'message0': '',
'args0': [],
},
]);
const toolboxXml = `
<xml>
<category name="test">
<block type="empty_block">
<comment pinned="true" h="80" w="160">test toolbox text</comment>
</block>
</category>
</xml>
`;
this.workspace = Blockly.inject('blocklyDiv', {
comments: true,
scrollbars: true,
trashcan: true,
maxTrashcanContents: Infinity,
toolbox: Blockly.utils.xml.textToDom(toolboxXml),
});
});
teardown(function () {
sharedTestTeardown.call(this);
});
suite('Pattern', function () {
teardown(function () {
// Delete all blocks.
this.workspace.clear();
});
function createBlock(workspace) {
const block = Blockly.Xml.domToBlock(
Blockly.utils.xml.textToDom('<block type="empty_block"/>'),
workspace,
);
block.setCommentText('test text');
return block;
}
function assertComment(workspace, text) {
// Show comment.
const block = workspace.getAllBlocks()[0];
const icon = block.getIcon(Blockly.icons.CommentIcon.TYPE);
icon.setBubbleVisible(true);
// Check comment bubble size.
const bubbleSize = icon.getBubbleSize();
assert.isNotNaN(bubbleSize.width);
assert.isNotNaN(bubbleSize.height);
assert.equal(icon.getText(), text);
}
test('Trashcan', function () {
// Create block.
this.block = createBlock(this.workspace);
// Delete block.
this.block.checkAndDelete();
assert.equal(this.workspace.getAllBlocks().length, 0);
// Open trashcan.
simulateClick(this.workspace.trashcan.svgGroup);
// Place from trashcan.
simulateClick(
this.workspace.trashcan.flyout.svgGroup_.querySelector('.blocklyPath'),
);
assert.equal(this.workspace.getAllBlocks().length, 1);
// Check comment.
assertComment(this.workspace, 'test text');
});
test('Undo', function () {
// Create block.
this.block = createBlock(this.workspace);
// Delete block.
this.block.checkAndDelete();
assert.equal(this.workspace.getAllBlocks().length, 0);
// Undo.
this.workspace.undo(false);
assert.equal(this.workspace.getAllBlocks().length, 1);
// Check comment.
assertComment(this.workspace, 'test text');
});
test('Redo', function () {
// Create block.
this.block = createBlock(this.workspace);
// Undo & undo.
this.workspace.undo(false);
this.workspace.undo(false);
assert.equal(this.workspace.getAllBlocks().length, 0);
// Redo & redo.
this.workspace.undo(true);
this.workspace.undo(true);
assert.equal(this.workspace.getAllBlocks().length, 1);
// Check comment.
assertComment(this.workspace, 'test text');
});
test('Toolbox', function () {
// Place from toolbox.
const toolbox = this.workspace.getToolbox();
simulateClick(toolbox.HtmlDiv.querySelector('.blocklyTreeRow'));
simulateClick(
toolbox.getFlyout().svgGroup_.querySelector('.blocklyPath'),
);
assert.equal(this.workspace.getAllBlocks().length, 1);
// Check comment.
assertComment(this.workspace, 'test toolbox text');
});
});
});