2017-11-16 11:21:51 -05:00
|
|
|
const test = require('tap').test;
|
|
|
|
|
|
|
|
const Blocks = require('../../src/engine/blocks');
|
|
|
|
const BlockUtility = require('../../src/engine/block-utility');
|
|
|
|
const Event = require('../../src/blocks/scratch3_event');
|
|
|
|
const Runtime = require('../../src/engine/runtime');
|
|
|
|
const Target = require('../../src/engine/target');
|
|
|
|
const Thread = require('../../src/engine/thread');
|
2017-12-04 18:01:29 -05:00
|
|
|
const Variable = require('../../src/engine/variable');
|
2017-11-16 11:21:51 -05:00
|
|
|
|
|
|
|
test('#760 - broadcastAndWait', t => {
|
|
|
|
const broadcastAndWaitBlock = {
|
|
|
|
id: 'broadcastAndWaitBlock',
|
|
|
|
fields: {
|
|
|
|
BROADCAST_OPTION: {
|
2017-11-29 10:43:07 -05:00
|
|
|
id: 'testBroadcastID',
|
2017-11-16 11:21:51 -05:00
|
|
|
value: 'message'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
inputs: Object,
|
|
|
|
block: 'fakeBlock',
|
|
|
|
opcode: 'event_broadcastandwait',
|
|
|
|
next: null,
|
|
|
|
parent: null,
|
|
|
|
shadow: false,
|
|
|
|
topLevel: true,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
};
|
|
|
|
const receiveMessageBlock = {
|
|
|
|
id: 'receiveMessageBlock',
|
|
|
|
fields: {
|
|
|
|
BROADCAST_OPTION: {
|
2017-11-29 10:43:07 -05:00
|
|
|
id: 'testBroadcastID',
|
2017-11-16 11:21:51 -05:00
|
|
|
value: 'message'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
inputs: Object,
|
|
|
|
block: 'fakeBlock',
|
|
|
|
opcode: 'event_whenbroadcastreceived',
|
|
|
|
next: null,
|
|
|
|
parent: null,
|
|
|
|
shadow: false,
|
|
|
|
topLevel: true,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
const rt = new Runtime();
|
|
|
|
const e = new Event(rt);
|
|
|
|
const b = new Blocks();
|
|
|
|
b.createBlock(broadcastAndWaitBlock);
|
|
|
|
b.createBlock(receiveMessageBlock);
|
|
|
|
const tgt = new Target(rt, b);
|
2017-11-29 10:43:07 -05:00
|
|
|
tgt.isStage = true;
|
2017-12-04 18:01:29 -05:00
|
|
|
tgt.createVariable('testBroadcastID', 'message', Variable.BROADCAST_MESSAGE_TYPE);
|
2019-01-14 11:48:11 -05:00
|
|
|
|
|
|
|
rt.addTarget(tgt);
|
2017-11-16 11:21:51 -05:00
|
|
|
|
|
|
|
let th = rt._pushThread('broadcastAndWaitBlock', t);
|
|
|
|
const util = new BlockUtility();
|
|
|
|
util.sequencer = rt.sequencer;
|
|
|
|
util.thread = th;
|
2017-11-29 10:43:07 -05:00
|
|
|
util.runtime = rt;
|
2017-11-16 11:21:51 -05:00
|
|
|
|
|
|
|
// creates threads
|
2017-11-29 10:43:07 -05:00
|
|
|
e.broadcastAndWait({BROADCAST_OPTION: {id: 'testBroadcastID', name: 'message'}}, util);
|
2017-11-16 11:21:51 -05:00
|
|
|
t.strictEqual(rt.threads.length, 2);
|
|
|
|
t.strictEqual(rt.threads[1].topBlock, 'receiveMessageBlock');
|
|
|
|
// yields when some thread is active
|
|
|
|
t.strictEqual(th.status, Thread.STATUS_YIELD);
|
|
|
|
th.status = Thread.STATUS_RUNNING;
|
2017-12-04 18:01:29 -05:00
|
|
|
e.broadcastAndWait({BROADCAST_OPTION: {id: 'testBroadcastID', name: 'message'}}, util);
|
2017-11-16 11:21:51 -05:00
|
|
|
t.strictEqual(th.status, Thread.STATUS_YIELD);
|
|
|
|
// does not yield once all threads are done
|
|
|
|
th.status = Thread.STATUS_RUNNING;
|
|
|
|
rt.threads[1].status = Thread.STATUS_DONE;
|
2018-10-05 16:34:01 -04:00
|
|
|
rt.threads.splice(1, 1);
|
2017-12-04 18:01:29 -05:00
|
|
|
e.broadcastAndWait({BROADCAST_OPTION: {id: 'testBroadcastID', name: 'message'}}, util);
|
2017-11-16 11:21:51 -05:00
|
|
|
t.strictEqual(th.status, Thread.STATUS_RUNNING);
|
|
|
|
|
|
|
|
// restarts done threads that are in runtime threads
|
|
|
|
th = rt._pushThread('broadcastAndWaitBlock', tgt);
|
|
|
|
util.thread = th;
|
2017-11-29 10:43:07 -05:00
|
|
|
e.broadcastAndWait({BROADCAST_OPTION: {id: 'testBroadcastID', name: 'message'}}, util);
|
2017-11-16 11:21:51 -05:00
|
|
|
t.strictEqual(rt.threads.length, 3);
|
2018-10-05 16:34:01 -04:00
|
|
|
t.strictEqual(rt.threads[2].status, Thread.STATUS_RUNNING);
|
2017-11-16 11:21:51 -05:00
|
|
|
t.strictEqual(th.status, Thread.STATUS_YIELD);
|
|
|
|
// yields when some restarted thread is active
|
|
|
|
th.status = Thread.STATUS_RUNNING;
|
2017-11-29 10:43:07 -05:00
|
|
|
e.broadcastAndWait({BROADCAST_OPTION: {id: 'testBroadcastID', name: 'message'}}, util);
|
2017-11-16 11:21:51 -05:00
|
|
|
t.strictEqual(th.status, Thread.STATUS_YIELD);
|
|
|
|
// does not yield once all threads are done
|
|
|
|
th.status = Thread.STATUS_RUNNING;
|
2018-10-05 16:34:01 -04:00
|
|
|
rt.threads[2].status = Thread.STATUS_DONE;
|
|
|
|
rt.threads.splice(2, 1);
|
2017-11-29 10:43:07 -05:00
|
|
|
e.broadcastAndWait({BROADCAST_OPTION: {id: 'testBroadcastID', name: 'message'}}, util);
|
2017-11-16 11:21:51 -05:00
|
|
|
t.strictEqual(th.status, Thread.STATUS_RUNNING);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2018-07-05 09:42:00 -04:00
|
|
|
|
|
|
|
test('When > hat - loudness', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
rt.audioEngine = {getLoudness: () => 10};
|
|
|
|
const e = new Event(rt);
|
|
|
|
const args = {
|
|
|
|
WHENGREATERTHANMENU: 'LOUDNESS',
|
|
|
|
VALUE: '11'
|
|
|
|
};
|
|
|
|
t.equal(e.hatGreaterThanPredicate(args), false);
|
|
|
|
args.VALUE = '5';
|
|
|
|
t.equal(e.hatGreaterThanPredicate(args), true);
|
|
|
|
t.end();
|
|
|
|
});
|