dom/inputmethod/mochitest/test_basic.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/inputmethod/mochitest/test_basic.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,195 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=932145
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Basic test for InputMethod API.</title>
    1.11 +  <script type="application/javascript;version=1.7" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <script type="application/javascript;version=1.7" src="inputmethod_common.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.14 +</head>
    1.15 +<body>
    1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=932145">Mozilla Bug 932145</a>
    1.17 +<p id="display"></p>
    1.18 +<pre id="test">
    1.19 +<script class="testbody" type="application/javascript;version=1.7">
    1.20 +
    1.21 +// The input context.
    1.22 +var gContext = null;
    1.23 +
    1.24 +inputmethod_setup(function() {
    1.25 +  runTest();
    1.26 +});
    1.27 +
    1.28 +function runTest() {
    1.29 +  let im = navigator.mozInputMethod;
    1.30 +
    1.31 +  im.oninputcontextchange = function() {
    1.32 +    ok(true, 'inputcontextchange event was fired.');
    1.33 +    im.oninputcontextchange = null;
    1.34 +
    1.35 +    gContext = im.inputcontext;
    1.36 +    if (!gContext) {
    1.37 +      ok(false, 'Should have a non-null inputcontext.');
    1.38 +      inputmethod_cleanup();
    1.39 +      return;
    1.40 +    }
    1.41 +
    1.42 +    todo_is(gContext.type, 'input', 'The input context type should match.');
    1.43 +    is(gContext.inputType, 'text', 'The inputType should match.');
    1.44 +    is(gContext.inputMode, 'verbatim', 'The input mode should match.');
    1.45 +    is(gContext.lang, 'zh', 'The language should match.');
    1.46 +    is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yuan',
    1.47 +       'Should get the text around the cursor.');
    1.48 +
    1.49 +    test_setSelectionRange();
    1.50 +  };
    1.51 +
    1.52 +  // Set current page as an input method.
    1.53 +  SpecialPowers.wrap(im).setActive(true);
    1.54 +
    1.55 +  let iframe = document.createElement('iframe');
    1.56 +  iframe.src = 'file_test_app.html';
    1.57 +  iframe.setAttribute('mozbrowser', true);
    1.58 +  document.body.appendChild(iframe);
    1.59 +}
    1.60 +
    1.61 +function test_setSelectionRange() {
    1.62 +  // Move cursor position to 2.
    1.63 +  gContext.setSelectionRange(2, 0).then(function() {
    1.64 +    is(gContext.selectionStart, 2, 'selectionStart was set successfully.');
    1.65 +    is(gContext.selectionEnd, 2, 'selectionEnd was set successfully.');
    1.66 +    test_sendKey();
    1.67 +  }, function(e) {
    1.68 +    ok(false, 'setSelectionRange failed:' + e.name);
    1.69 +    inputmethod_cleanup();
    1.70 +  });
    1.71 +}
    1.72 +
    1.73 +function test_sendKey() {
    1.74 +  // Add '-' to current cursor posistion and move the cursor position to 3.
    1.75 +  gContext.sendKey(0, '-'.charCodeAt(0), 0).then(function() {
    1.76 +    is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yu-an',
    1.77 +       'sendKey should changed the input field correctly.');
    1.78 +    test_deleteSurroundingText();
    1.79 +  }, function(e) {
    1.80 +    ok(false, 'sendKey failed:' + e.name);
    1.81 +    inputmethod_cleanup();
    1.82 +  });
    1.83 +}
    1.84 +
    1.85 +function test_deleteSurroundingText() {
    1.86 +  // Remove one character before current cursor position and move the cursor
    1.87 +  // position back to 2.
    1.88 +  gContext.deleteSurroundingText(-1, 1).then(function() {
    1.89 +    ok(true, 'deleteSurroundingText finished');
    1.90 +    is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yuan',
    1.91 +       'deleteSurroundingText should changed the input field correctly.');
    1.92 +    test_replaceSurroundingText();
    1.93 +  }, function(e) {
    1.94 +    ok(false, 'deleteSurroundingText failed:' + e.name);
    1.95 +    inputmethod_cleanup();
    1.96 +  });
    1.97 +}
    1.98 +
    1.99 +function test_replaceSurroundingText() {
   1.100 +  // Replace 'Yuan' with 'Xulei'.
   1.101 +  gContext.replaceSurroundingText('Xulei', -2, 4).then(function() {
   1.102 +    ok(true, 'replaceSurroundingText finished');
   1.103 +    is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Xulei',
   1.104 +       'replaceSurroundingText changed the input field correctly.');
   1.105 +    test_setComposition();
   1.106 +  }, function(e) {
   1.107 +    ok(false, 'replaceSurroundingText failed: ' + e.name);
   1.108 +    inputmethod_cleanup();
   1.109 +  });
   1.110 +}
   1.111 +
   1.112 +function test_setComposition() {
   1.113 +  gContext.setComposition('XXX').then(function() {
   1.114 +    ok(true, 'setComposition finished');
   1.115 +    test_endComposition();
   1.116 +  }, function(e) {
   1.117 +    ok(false, 'setComposition failed: ' + e.name);
   1.118 +    inputmethod_cleanup();
   1.119 +  });
   1.120 +}
   1.121 +
   1.122 +function test_endComposition() {
   1.123 +  gContext.endComposition('2013').then(function() {
   1.124 +    is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Xulei2013',
   1.125 +       'endComposition changed the input field correctly.');
   1.126 +    test_onSelectionChange();
   1.127 +  }, function (e) {
   1.128 +    ok(false, 'endComposition failed: ' + e.name);
   1.129 +    inputmethod_cleanup();
   1.130 +  });
   1.131 +}
   1.132 +
   1.133 +function test_onSelectionChange() {
   1.134 +  var sccTimeout = setTimeout(function() {
   1.135 +    ok(false, 'selectionchange event not fired');
   1.136 +    cleanup(true);
   1.137 +  }, 3000);
   1.138 +
   1.139 +  function cleanup(failed) {
   1.140 +    gContext.onselectionchange = null;
   1.141 +    clearTimeout(sccTimeout);
   1.142 +    if (failed) {
   1.143 +      inputmethod_cleanup();
   1.144 +    }
   1.145 +    else {
   1.146 +      test_onSurroundingTextChange();
   1.147 +    }
   1.148 +  }
   1.149 +
   1.150 +  gContext.onselectionchange = function() {
   1.151 +    ok(true, 'onselectionchange fired');
   1.152 +    cleanup();
   1.153 +  };
   1.154 +
   1.155 +  gContext.sendKey(0, 'j'.charCodeAt(0), 0).then(function() {
   1.156 +    // do nothing and wait for onselectionchange event
   1.157 +  }, function(e) {
   1.158 +    ok(false, 'sendKey failed: ' + e.name);
   1.159 +    cleanup(true);
   1.160 +  });
   1.161 +}
   1.162 +
   1.163 +function test_onSurroundingTextChange() {
   1.164 +  var sccTimeout = setTimeout(function() {
   1.165 +    ok(false, 'surroundingtextchange event not fired');
   1.166 +    cleanup(true);
   1.167 +  }, 3000);
   1.168 +
   1.169 +  function cleanup(failed) {
   1.170 +    gContext.onsurroundingtextchange = null;
   1.171 +    clearTimeout(sccTimeout);
   1.172 +    if (failed) {
   1.173 +      inputmethod_cleanup();
   1.174 +    }
   1.175 +    else {
   1.176 +      // in case we want more tests leave this
   1.177 +      inputmethod_cleanup();
   1.178 +    }
   1.179 +  }
   1.180 +
   1.181 +  gContext.onsurroundingtextchange = function() {
   1.182 +    ok(true, 'onsurroundingtextchange fired');
   1.183 +    cleanup();
   1.184 +  };
   1.185 +
   1.186 +  gContext.sendKey(0, 'j'.charCodeAt(0), 0).then(function() {
   1.187 +    // do nothing and wait for onselectionchange event
   1.188 +  }, function(e) {
   1.189 +    ok(false, 'sendKey failed: ' + e.name);
   1.190 +    cleanup(true);
   1.191 +  });
   1.192 +}
   1.193 +
   1.194 +</script>
   1.195 +</pre>
   1.196 +</body>
   1.197 +</html>
   1.198 +

mercurial