/*global mocha requireApp suite test assert setup suiteTeardown */
window.assert = chai.assert;
/**
* This is a set of tests that test mozInputMethod
*/
suite('Input Method', function() {
this.timeout(10000);
var container = document.getElementById('workspace');
/**
* Send a set of characters to the current inputcontext
* @param {Array} chars Array of characters
* @param {Boolean} expected Expected value of the field after input
* @param {Function} next Callback
*/
function sendKeys(chars, expected, next) {
var completed = 0;
var icc = navigator.mozInputMethod.inputcontext;
if (!icc)
return;
chars.forEach(function(c) {
var sk;
if (typeof c === 'string') {
sk = icc.sendKey(0, c.charCodeAt(0), 0);
}
else {
sk = icc.sendKey(c, 0, 0);
}
sk.then(function() {
if (++completed === chars.length) {
navigator.mozInputMethod.inputcontext.getText().then(function(v) {
assert.equal(expected, v);
next();
}, function(err) {
assert.strictEqual(true, err);
});
}
}, function(err) {
assert.strictEqual(true, err);
});
});
}
/**
* Listens once to an inputcontextchange and then discards the event
* @param {Boolean} needIc Specify whether NULL for the inputcontext is OK
* @param {Function} callback Callback with 1 arg that holds inputcontext
*/
function onIcc(needIc, callback) {
navigator.mozInputMethod.addEventListener('inputcontextchange', function onicc() {
if (!navigator.mozInputMethod.inputcontext && needIc) {
return;
}
navigator.mozInputMethod.removeEventListener('inputcontextchange', onicc);
callback(navigator.mozInputMethod.inputcontext);
});
}
setup(function(next) {
container.innerHTML = '';
// no active context? go along
if (!navigator.mozInputMethod.inputcontext) {
return next();
}
// otherwise wait for the blur() action
onIcc(false, function() {
next();
});
});
suiteTeardown(function() {
container.innerHTML = '';
});
suite('InputContextChange events', function() {
test('Event fires on textbox focus', function(next) {
onIcc(true, function(ic) {
assert.equal(ic.inputType, 'text');
next();
});
container.innerHTML = '';
document.querySelector('#test').focus();
});
test('Event fires on textarea focus', function(next) {
onIcc(true, function(ic) {
assert.equal(ic.inputType, 'textarea');
next();
});
container.innerHTML = '';
document.querySelector('#test2').focus();
});
test('Event fires on contenteditable focus', function(next) {
onIcc(true, function(ic) {
assert.equal(ic.inputType,
'textarea');
next();
});
container.innerHTML = '
Hi
';
document.querySelector('#test3').focus();
});
test('Event fires on designMode=on focus', function(next) {
onIcc(true, function(ic) {
assert.equal(ic.inputType, 'textarea');
next();
});
container.innerHTML = '';
var iframe = container.querySelector('iframe');
iframe.contentDocument.designMode = 'on';
iframe.focus();
});
test('Event fires on iframe body contenteditable focus', function(next) {
// contentEditable
onIcc(true, function(ic) {
assert.equal(ic.inputType, 'textarea');
next();
});
container.innerHTML = '';
var iframe = container.querySelector('iframe');
iframe.contentDocument.body.setAttribute('contenteditable', true);
iframe.focus();
});
test('No event on type="range"', function(next) {
var fired = false;
onIcc(true, function() {
fired = true;
});
container.innerHTML = '';
document.querySelector('input[type=range]').focus();
setTimeout(function() {
assert.equal(fired, false);
next();
}, 150);
});
test('Focus event on type="date"', function(next) {
container.innerHTML = '';
var el = document.querySelector('#date1');
el.onfocus = function() {
next();
};
el.focus();
});
test('Focus event on type="time"', function(next) {
container.innerHTML = '';
var el = document.querySelector('#time1');
el.onfocus = function() {
next();
};
el.focus();
});
test('Event fires on textbox blur', function(next) {
container.innerHTML = '';
var el = document.querySelector('#test');
el.focus();
setTimeout(function() {
onIcc(false, function(ic) {
assert.equal(ic, null);
next();
});
el.blur();
}, 100);
});
test('Event fires on textarea blur', function(next) {
container.innerHTML = '';
var el = document.querySelector('#test2');
el.focus();
setTimeout(function() {
onIcc(false, function(ic) {
assert.equal(ic, null);
next();
});
el.blur();
}, 100);
});
test('Event fires on contenteditable blur', function(next) {
container.innerHTML = '