gecko/dom/inputmethod/mochitest/test_basic.html

200 lines
5.7 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=932145
-->
<head>
<title>Basic test for InputMethod API.</title>
<script type="application/javascript;version=1.7" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript;version=1.7" src="inputmethod_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=932145">Mozilla Bug 932145</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">
// The input context.
var gContext = null;
inputmethod_setup(function() {
runTest();
});
function runTest() {
let im = navigator.mozInputMethod;
im.oninputcontextchange = function() {
ok(true, 'inputcontextchange event was fired.');
im.oninputcontextchange = null;
gContext = im.inputcontext;
if (!gContext) {
ok(false, 'Should have a non-null inputcontext.');
inputmethod_cleanup();
return;
}
todo_is(gContext.type, 'input', 'The input context type should match.');
is(gContext.inputType, 'text', 'The inputType should match.');
is(gContext.inputMode, 'verbatim', 'The input mode should match.');
is(gContext.lang, 'zh', 'The language should match.');
is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yuan',
'Should get the text around the cursor.');
test_setSelectionRange();
};
// Set current page as an input method.
SpecialPowers.wrap(im).setActive(true);
let iframe = document.createElement('iframe');
iframe.src = 'file_test_app.html';
iframe.setAttribute('mozbrowser', true);
document.body.appendChild(iframe);
}
function test_setSelectionRange() {
// Move cursor position to 2.
gContext.setSelectionRange(2, 0).then(function() {
is(gContext.selectionStart, 2, 'selectionStart was set successfully.');
is(gContext.selectionEnd, 2, 'selectionEnd was set successfully.');
test_sendKey();
}, function(e) {
ok(false, 'setSelectionRange failed:' + e.name);
inputmethod_cleanup();
});
}
function test_sendKey() {
// Add '-' to current cursor posistion and move the cursor position to 3.
gContext.sendKey(0, '-'.charCodeAt(0), 0).then(function() {
is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yu-an',
'sendKey should changed the input field correctly.');
test_deleteSurroundingText();
}, function(e) {
ok(false, 'sendKey failed:' + e.name);
inputmethod_cleanup();
});
}
function test_deleteSurroundingText() {
// Remove one character before current cursor position and move the cursor
// position back to 2.
gContext.deleteSurroundingText(-1, 1).then(function() {
ok(true, 'deleteSurroundingText finished');
is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yuan',
'deleteSurroundingText should changed the input field correctly.');
test_replaceSurroundingText();
}, function(e) {
ok(false, 'deleteSurroundingText failed:' + e.name);
inputmethod_cleanup();
});
}
function test_replaceSurroundingText() {
// Replace 'Yuan' with 'Xulei'.
gContext.replaceSurroundingText('Xulei', -2, 4).then(function() {
ok(true, 'replaceSurroundingText finished');
is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Xulei',
'replaceSurroundingText changed the input field correctly.');
test_setComposition();
}, function(e) {
ok(false, 'replaceSurroundingText failed: ' + e.name);
inputmethod_cleanup();
});
}
function test_setComposition() {
gContext.setComposition('XXX').then(function() {
ok(true, 'setComposition finished');
test_endComposition();
}, function(e) {
ok(false, 'setComposition failed: ' + e.name);
inputmethod_cleanup();
});
}
function test_endComposition() {
gContext.endComposition('2013').then(function() {
is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Xulei2013',
'endComposition changed the input field correctly.');
test_onSelectionChange();
}, function (e) {
ok(false, 'endComposition failed: ' + e.name);
inputmethod_cleanup();
});
}
function test_onSelectionChange() {
var sccTimeout = setTimeout(function() {
ok(false, 'selectionchange event not fired');
cleanup(true);
}, 3000);
function cleanup(failed) {
gContext.onselectionchange = null;
clearTimeout(sccTimeout);
if (failed) {
inputmethod_cleanup();
}
else {
test_onSurroundingTextChange();
}
}
gContext.onselectionchange = function(evt) {
ok(true, 'onselectionchange fired');
is(evt.detail.selectionStart, 10);
is(evt.detail.selectionEnd, 10);
ok(evt.detail.ownAction);
};
gContext.sendKey(0, 'j'.charCodeAt(0), 0).then(function() {
cleanup();
}, function(e) {
ok(false, 'sendKey failed: ' + e.name);
cleanup(true);
});
}
function test_onSurroundingTextChange() {
var sccTimeout = setTimeout(function() {
ok(false, 'surroundingtextchange event not fired');
cleanup(true);
}, 3000);
function cleanup(failed) {
gContext.onsurroundingtextchange = null;
clearTimeout(sccTimeout);
if (failed) {
inputmethod_cleanup();
}
else {
// in case we want more tests leave this
inputmethod_cleanup();
}
}
gContext.onsurroundingtextchange = function(evt) {
ok(true, 'onsurroundingtextchange fired');
is(evt.detail.beforeString, 'Xulei2013jj');
is(evt.detail.afterString, '');
ok(evt.detail.ownAction);
};
gContext.sendKey(0, 'j'.charCodeAt(0), 0).then(function() {
cleanup();
}, function(e) {
ok(false, 'sendKey failed: ' + e.name);
cleanup(true);
});
}
</script>
</pre>
</body>
</html>