forked from jmoenig/Snap
-
Notifications
You must be signed in to change notification settings - Fork 6
/
extensions.spec.js
432 lines (389 loc) · 17.1 KB
/
extensions.spec.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*globals driver, assert */
describe('extensions', function() {
describe('event handlers', function() {
let CallCounterMetaExt, CallPromiseMetaExt;
before(async () => {
await driver.reset();
const {Extension, utils} = driver.globals();
CallCounterMetaExt = function(method) {
class CallCounterExt extends Extension {
constructor() {
super('CallCounter_' + method);
this.callCount = 0;
}
};
CallCounterExt.prototype[method] = function() {
console.log(this);
return this.callCount++;
};
return CallCounterExt;
}
CallPromiseMetaExt = function(method) {
class CallCounterExt extends Extension {
constructor() {
super('CallPromise_' + method);
this._deferred = utils.defer();
this.promise = this._deferred.promise;
}
};
CallCounterExt.prototype[method] = function() {
return this._deferred.resolve();
};
return CallCounterExt;
}
});
describe('onNewProject', function() {
it('should call function on new project', function(done) {
driver.reset();
TestExtension.prototype.onNewProject = () => {
delete TestExtension.prototype.onNewProject;
done();
};
});
});
describe('onOpenRole', function() {
it('should call on new project if loaded after load', async function() {
// This checks the async issue where the extension loads slower than
// initializing the project
const {NetsBloxExtensions} = driver.globals();
NetsBloxExtensions.register(CallCounterMetaExt('onOpenRole'));
const extension = await driver.expect(
() => NetsBloxExtensions.registry[0],
'Extension not loaded.'
);
assert.equal(extension.callCount, 1);
});
it('should call function on switch roles', async function() {
const {utils} = driver.globals();
await driver.newRole('testRole');
const deferred = utils.defer();
TestExtension.prototype.onOpenRole = () => {
delete TestExtension.prototype.onOpenRole;
deferred.resolve();
};
await driver.moveToRole('testRole');
await deferred.promise;
});
});
describe('onRunScripts', function() {
it('should call function on green flag', async function() {
const {utils} = driver.globals();
const deferred = utils.defer();
TestExtension.prototype.onRunScripts = () => {
delete TestExtension.prototype.onRunScripts;
deferred.resolve();
};
driver.click(driver.ide().controlBar.startButton);
await deferred.promise;
});
});
describe('onStopAllScripts', function() {
it('should call function on stop button', async function() {
const {utils} = driver.globals();
const deferred = utils.defer();
TestExtension.prototype.onStopAllScripts = () => {
delete TestExtension.prototype.onStopAllScripts;
deferred.resolve();
};
driver.click(driver.ide().controlBar.stopButton);
await deferred.promise;
});
});
describe('onPauseAll', function() {
it('should call function on pause button', async function() {
const {utils} = driver.globals();
let deferred = utils.defer();
TestExtension.prototype.onPauseAll = () => {
delete TestExtension.prototype.onPauseAll;
deferred.resolve();
};
driver.click(driver.ide().controlBar.pauseButton);
await deferred.promise;
});
it('should call function on pause block', async function() {
driver.reset();
driver.selectTab('scripts');
const {utils, Point} = driver.globals();
deferred = utils.defer();
TestExtension.prototype.onPauseAll = () => {
delete TestExtension.prototype.onPauseAll;
deferred.resolve();
};
let block = await driver.addBlock("doPauseAll", new Point(300, 300));
await driver.sleep(100);
driver.click(block);
await deferred.promise;
});
});
describe('onResumeAll', function() {
it('should call function on resume button', async function() {
driver.reset();
driver.selectTab('scripts');
const {utils, Point} = driver.globals();
const deferred = utils.defer();
let block = await driver.addBlock("doPauseAll", new Point(300, 300));
await driver.sleep(100);
driver.click(block);
await driver.sleep(100);
TestExtension.prototype.onResumeAll = () => {
delete TestExtension.prototype.onResumeAll;
deferred.resolve();
};
driver.click(driver.ide().controlBar.pauseButton);
await deferred.promise;
});
});
describe('onNewSprite', function() {
it('should call function on new sprite button', async function() {
const {utils} = driver.globals();
const deferred = utils.defer();
TestExtension.prototype.onNewSprite = () => {
delete TestExtension.prototype.onNewSprite;
deferred.resolve();
};
driver.click(driver.ide().corralBar.children[0]);
await deferred.promise;
});
});
describe('onRenameSprite', function() {
it('should call function on rename sprite', async function() {
const {utils, SnapActions} = driver.globals();
const deferred = utils.defer();
TestExtension.prototype.onRenameSprite = (spriteId, name) => {
delete TestExtension.prototype.onRenameSprite;
deferred.resolve(name);
};
SnapActions.renameSprite(driver.ide().currentSprite, "newName");
let name = await deferred.promise;
assert(name == "newName");
});
});
describe('onSetStageSize', function() {
it('should call function on pause button', async function() {
const {utils} = driver.globals();
const deferred = utils.defer();
TestExtension.prototype.onSetStageSize = (w, h) => {
delete TestExtension.prototype.onSetStageSize;
deferred.resolve([w, h]);
};
driver.click(driver.ide().controlBar.settingsButton);
driver.click(driver.dialog().children[4]);
driver.click(driver.dialog().allEntryFields()[0]);
driver.keys("123");
driver.click(driver.dialog().allEntryFields()[1]);
driver.keys("456");
driver.dialog().ok();
let [w, h] = await deferred.promise;
assert(w == 123 && h == 456);
});
});
});
describe('TestExtension', function() {
let TestExtension;
before(() => {
driver.reset();
const {NetsBloxExtensions,Extension,Color,SpriteMorph,StageMorph} = driver.globals();
TestExtension = function() {
this.onOpenRoleCount = 0;
};
TestExtension.prototype = new Extension('TestExt');
TestExtension.prototype.getMenu = () => {
return {
'TestMenuItem': function() {},
};
};
TestExtension.prototype.getSettings = () => {
return [new Extension.ExtensionSetting(
'Test Setting',
() => {},
() => false
)];
};
TestExtension.prototype.getCategories = () => [
new Extension.Category(
'TEST!',
new Color(10, 100, 10),
)
];
TestExtension.prototype.getPalette = () => [
new Extension.PaletteCategory(
'TEST!',
[
new Extension.Palette.Block('newBlock'),
new Extension.Palette.Block('newBlock').withWatcherToggle(),
new Extension.Palette.Block('spriteBlock'),
],
SpriteMorph
),
new Extension.PaletteCategory(
'TEST!',
[
new Extension.Palette.Block('newBlock'),
new Extension.Palette.Block('newBlock').withWatcherToggle()
],
StageMorph
),
];
TestExtension.prototype.getBlocks = () => [
new Extension.Block(
'newBlock',
'reporter',
'TEST!',
'test block',
[],
() => 'This is a test.'
).for(SpriteMorph, StageMorph),
new Extension.Block(
'spriteBlock',
'reporter',
'TEST!',
'sprite-only block: %testPart2',
[],
() => 'This is another test.'
).for(SpriteMorph)
];
const {InputSlotMorph} = driver.globals();
TestExtension.prototype.getLabelParts = () => [
new Extension.LabelPart(
'testPart',
() => {
const part = new InputSlotMorph(
null, // text
false, // non-numeric
{
'this is a test': ['this is a test'],
'yet another value': ['yet another value']
},
true
);
part.setContents(['this is a test']);
return part;
}
),
new Extension.LabelPart(
'testPart2',
() => {
const part = new InputSlotMorph(
null, // text
false, // non-numeric
{
'this is a second test': ['this is a second test'],
'yet another second value': ['yet another second value']
},
true
);
part.setContents(['this is a second test']);
return part;
}
),
];
TestExtension.prototype.onOpenRole = () => {
this.onOpenRoleCount++;
console.log('On Open Role!', this.onOpenRoleCount);
};
NetsBloxExtensions.register(TestExtension);
});
it('should show extensions button', function() {
assert(driver.ide().controlBar.extensionsButton.isVisible);
});
it('should create menu', function() {
driver.click(driver.ide().controlBar.extensionsButton);
const menuItems = driver.dialog().children.map(c => c.labelString);
assert(menuItems.includes('TestExt'));
driver.dialog().destroy();
});
it('should create menu item', function() {
driver.click(driver.ide().controlBar.extensionsButton);
driver.click(driver.dialog().children[1]);
const subMenuItems = driver.dialog().children[2].children.map(c => c.labelString);
assert(subMenuItems.includes('TestMenuItem'));
driver.dialog().destroy();
});
it('should create settings', function() {
driver.click(driver.ide().controlBar.extensionsButton);
driver.click(driver.dialog().children[1]);
const subMenuItems = driver.dialog().children[2].children.map(c => c.labelString);
assert(subMenuItems.includes('Options'));
driver.click(driver.dialog().children[2].children[2]);
const optionMenuItems = driver.dialog().children[2].children[3].children.map(c => c.labelString);
assert(optionMenuItems.find(item => item && item[1] == 'Test Setting'));
driver.dialog().destroy();
});
it('should not load an extension twice', function() {
const {NetsBloxExtensions} = driver.globals();
const extCount = NetsBloxExtensions.registry.length;
NetsBloxExtensions.register(TestExtension);
assert.equal(NetsBloxExtensions.registry.length, extCount);
});
it.skip('should save required extensions in xml', function() {
});
describe('ExtensionRegistry', function() {
it('should defer registration until after initialization', function() {
const {ExtensionRegistry} = driver.globals();
const registry = new ExtensionRegistry();
let loaded = false;
function SimpleExtension() {
loaded = true;
}
registry.register(SimpleExtension);
assert(!loaded);
registry.initialize(driver.ide());
assert(loaded);
});
});
describe('palette', function() {
it('should add new category', function() {
const categories = driver.ide().categories.children.map(c => c.labelString);
assert(categories.includes('TEST!'));
});
it('should add new blocks', function() {
driver.selectCategory('TEST!');
assert.equal(
driver.palette().contents.children.length,
5
);
});
it('should show new blocks on the stage', function() {
driver.selectStage();
driver.selectCategory('TEST!');
assert.equal(
driver.palette().contents.children.length,
4
);
});
it('should show watcher toggles in palette', function() {
driver.selectCategory('TEST!');
const {ToggleMorph} = driver.globals();
const toggle = driver.palette().contents.children.find(child => child instanceof ToggleMorph);
assert(toggle);
});
it('should create toggleable watchers in palette', function() {
driver.selectCategory('TEST!');
const {ToggleMorph} = driver.globals();
const toggle = driver.palette().contents.children.find(child => child instanceof ToggleMorph);
driver.click(toggle);
});
it('should show sprite block on sprite', function() {
const {name} = driver.ide().sprites.at(1);
driver.selectSprite(name);
driver.selectCategory('TEST!');
const block = driver.palette().contents.children.find(child => child.selector === 'spriteBlock');
assert(block);
});
it('should add label part', function() {
const {name} = driver.ide().sprites.at(1);
driver.selectSprite(name);
driver.selectCategory('TEST!');
const block = driver.palette().contents.children.find(child => child.selector === 'spriteBlock');
const [inputSlot] = block.inputs();
assert(Object.keys(inputSlot.choices).includes('this is a second test'));
});
it('should hide sprite block on stage', function() {
driver.selectStage();
driver.selectCategory('TEST!');
const block = driver.palette().contents.children.find(child => child.selector === 'spriteBlock');
assert(!block);
});
});
});
});