feat(web/engine): replaces basic keystroke test

This commit is contained in:
jahorton 2020-05-01 11:46:15 +07:00
parent 564df4e68a
commit 7462f1707e
2 changed files with 55 additions and 33 deletions

View file

@ -0,0 +1,55 @@
var assert = require('chai').assert;
let fs = require('fs');
let vm = require('vm');
let KeyboardProcessor = require('../../dist');
let KMWRecorder = require('../../../tools/recorder/dist/nodeProctor');
// Required initialization setup.
global.com = KeyboardProcessor.com; // exports all keyboard-processor namespacing.
describe('Engine - Basic Simulation', function() {
let testJSONtext = fs.readFileSync('../tests/resources/json/engine_tests/basic_lao_simulation.json');
// Common test suite setup.
let testSuite = new KMWRecorder.KeyboardTest(JSON.parse(testJSONtext));
var keyboard;
let device = {
formFactor: 'desktop',
OS: 'windows',
browser: 'native'
}
before(function() {
// -- START: Standard Recorder-based unit test loading boilerplate --
// Load the keyboard. We'll need a KeyboardProcessor instance as an intermediary.
let kp = new KeyboardProcessor();
// These two lines will load a keyboard from its file; headless-mode `registerKeyboard` will
// automatically set the keyboard as active.
var script = new vm.Script(fs.readFileSync('../tests/' + testSuite.keyboard.filename));
script.runInThisContext();
keyboard = kp.activeKeyboard;
assert.equal(keyboard.id, "Keyboard_" + testSuite.keyboard.id);
// -- END: Standard Recorder-based unit test loading boilerplate --
// This part provides extra assurance that the keyboard properly loaded.
assert.equal(keyboard.id, "Keyboard_lao_2008_basic");
});
// Converts each test set into its own Mocha-level test.
for(let set of testSuite.inputTestSets) {
let proctor = new KMWRecorder.NodeProctor(keyboard, device, assert.equal);
if(!proctor.compatibleWithSuite(testSuite)) {
it.skip(set.toTestName() + " - Cannot run this test suite on Node.");
} else {
it(set.toTestName(), function() {
// Refresh the proctor instance at runtime.
let proctor = new KMWRecorder.NodeProctor(keyboard, device, assert.equal);
set.test(proctor);
});
}
}
});

View file

@ -42,37 +42,4 @@ describe('KeyboardProcessor', function() {
assert.equal('Keyboard_khmer_angkor', kp.activeKeyboard.id, 'Unexpected keyboard id found after script load');
});
});
describe('processKeystroke', function() {
it('has simple output, no context', function() {
let kp = new KeyboardProcessor();
// These two lines will load a keyboard from its file; headless-mode `registerKeyboard` will
// automatically set the keyboard as active.
var script = new vm.Script(fs.readFileSync('../tests/resources/keyboards/khmer_angkor.js'));
script.runInThisContext();
// Fetch a key from the keyboard's layout.
let defaultLayer = kp.activeKeyboard.layout('desktop').getLayer('default');
assert.isNotNull(defaultLayer);
let keyS = defaultLayer.getKey('K_S');
assert.isNotNull(keyS);
//Get the default synthetic key event for said key.
let mock = new com.keyman.text.Mock();
let keyEvent = keyS.constructKeyEvent(kp, mock,
new com.keyman.text.EngineDeviceSpec('chrome', 'desktop', 'windows', false)
);
assert.isNotNull(keyEvent);
let ruleBehavior = kp.processKeystroke(keyEvent, mock);
assert.isNotNull(ruleBehavior);
assert.isNotNull(ruleBehavior.transcription);
assert.equal(mock.getText(), 'ស', 'Unexpected output from key event');
let transform = ruleBehavior.transcription.transform;
assert.isNotNull(transform);
})
});
});