| const path = require('path'); |
| const test = require('tap').test; |
| const makeTestStorage = require('../fixtures/make-test-storage'); |
| const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer; |
| const VirtualMachine = require('../../src/index'); |
| const Thread = require('../../src/engine/thread'); |
| const Runtime = require('../../src/engine/runtime'); |
|
|
| const projectUri = path.resolve(__dirname, '../fixtures/timer-monitor.sb3'); |
| const project = readFileToBuffer(projectUri); |
|
|
| const checkMonitorThreadPresent = (t, threads) => { |
| t.equal(threads.length, 1); |
| const monitorThread = threads[0]; |
| t.equal(monitorThread.stackClick, false); |
| t.equal(monitorThread.updateMonitor, true); |
| t.equal(monitorThread.topBlock.toString(), 'timer'); |
| }; |
|
|
| |
| |
| |
| test('monitor thread runs every frame', t => { |
| const vm = new VirtualMachine(); |
| vm.attachStorage(makeTestStorage()); |
|
|
| |
| t.doesNotThrow(() => { |
| |
| vm.runtime.currentStepTime = Runtime.THREAD_STEP_INTERVAL; |
| vm.clear(); |
| vm.setCompatibilityMode(false); |
| vm.setTurboMode(false); |
|
|
| vm.loadProject(project).then(() => { |
| t.equal(vm.runtime.threads.length, 0); |
|
|
| vm.runtime._step(); |
| let doneThreads = vm.runtime._lastStepDoneThreads; |
| t.equal(vm.runtime.threads.length, 0); |
| t.equal(doneThreads.length, 1); |
| checkMonitorThreadPresent(t, doneThreads); |
| t.assert(doneThreads[0].status === Thread.STATUS_DONE); |
|
|
| |
| vm.runtime._step(); |
| doneThreads = vm.runtime._lastStepDoneThreads; |
| t.equal(vm.runtime.threads.length, 0); |
| t.equal(doneThreads.length, 1); |
| checkMonitorThreadPresent(t, doneThreads); |
| t.assert(doneThreads[0].status === Thread.STATUS_DONE); |
| t.end(); |
| }); |
| }); |
| }); |
|
|
| |
| |
| |
| |
| test('monitor thread not added twice', t => { |
| const vm = new VirtualMachine(); |
| vm.attachStorage(makeTestStorage()); |
|
|
| |
| t.doesNotThrow(() => { |
| |
| vm.runtime.currentStepTime = 0; |
|
|
| vm.clear(); |
| vm.setCompatibilityMode(false); |
| vm.setTurboMode(false); |
|
|
| vm.loadProject(project).then(() => { |
| t.equal(vm.runtime.threads.length, 0); |
|
|
| vm.runtime._step(); |
| let doneThreads = vm.runtime._lastStepDoneThreads; |
| t.equal(vm.runtime.threads.length, 1); |
| t.equal(doneThreads.length, 0); |
| checkMonitorThreadPresent(t, vm.runtime.threads); |
| t.assert(vm.runtime.threads[0].status === Thread.STATUS_RUNNING); |
| const prevThread = vm.runtime.threads[0]; |
|
|
| |
| vm.runtime._step(); |
| doneThreads = vm.runtime._lastStepDoneThreads; |
| t.equal(vm.runtime.threads.length, 1); |
| t.equal(doneThreads.length, 0); |
| checkMonitorThreadPresent(t, vm.runtime.threads); |
| t.equal(vm.runtime.threads[0], prevThread); |
| t.end(); |
| }); |
| }); |
| }); |
|
|