dom/inputmethod/mochitest/test_basic.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 <!DOCTYPE HTML>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=932145
     5 -->
     6 <head>
     7   <title>Basic test for InputMethod API.</title>
     8   <script type="application/javascript;version=1.7" src="/tests/SimpleTest/SimpleTest.js"></script>
     9   <script type="application/javascript;version=1.7" src="inputmethod_common.js"></script>
    10   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    11 </head>
    12 <body>
    13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=932145">Mozilla Bug 932145</a>
    14 <p id="display"></p>
    15 <pre id="test">
    16 <script class="testbody" type="application/javascript;version=1.7">
    18 // The input context.
    19 var gContext = null;
    21 inputmethod_setup(function() {
    22   runTest();
    23 });
    25 function runTest() {
    26   let im = navigator.mozInputMethod;
    28   im.oninputcontextchange = function() {
    29     ok(true, 'inputcontextchange event was fired.');
    30     im.oninputcontextchange = null;
    32     gContext = im.inputcontext;
    33     if (!gContext) {
    34       ok(false, 'Should have a non-null inputcontext.');
    35       inputmethod_cleanup();
    36       return;
    37     }
    39     todo_is(gContext.type, 'input', 'The input context type should match.');
    40     is(gContext.inputType, 'text', 'The inputType should match.');
    41     is(gContext.inputMode, 'verbatim', 'The input mode should match.');
    42     is(gContext.lang, 'zh', 'The language should match.');
    43     is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yuan',
    44        'Should get the text around the cursor.');
    46     test_setSelectionRange();
    47   };
    49   // Set current page as an input method.
    50   SpecialPowers.wrap(im).setActive(true);
    52   let iframe = document.createElement('iframe');
    53   iframe.src = 'file_test_app.html';
    54   iframe.setAttribute('mozbrowser', true);
    55   document.body.appendChild(iframe);
    56 }
    58 function test_setSelectionRange() {
    59   // Move cursor position to 2.
    60   gContext.setSelectionRange(2, 0).then(function() {
    61     is(gContext.selectionStart, 2, 'selectionStart was set successfully.');
    62     is(gContext.selectionEnd, 2, 'selectionEnd was set successfully.');
    63     test_sendKey();
    64   }, function(e) {
    65     ok(false, 'setSelectionRange failed:' + e.name);
    66     inputmethod_cleanup();
    67   });
    68 }
    70 function test_sendKey() {
    71   // Add '-' to current cursor posistion and move the cursor position to 3.
    72   gContext.sendKey(0, '-'.charCodeAt(0), 0).then(function() {
    73     is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yu-an',
    74        'sendKey should changed the input field correctly.');
    75     test_deleteSurroundingText();
    76   }, function(e) {
    77     ok(false, 'sendKey failed:' + e.name);
    78     inputmethod_cleanup();
    79   });
    80 }
    82 function test_deleteSurroundingText() {
    83   // Remove one character before current cursor position and move the cursor
    84   // position back to 2.
    85   gContext.deleteSurroundingText(-1, 1).then(function() {
    86     ok(true, 'deleteSurroundingText finished');
    87     is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yuan',
    88        'deleteSurroundingText should changed the input field correctly.');
    89     test_replaceSurroundingText();
    90   }, function(e) {
    91     ok(false, 'deleteSurroundingText failed:' + e.name);
    92     inputmethod_cleanup();
    93   });
    94 }
    96 function test_replaceSurroundingText() {
    97   // Replace 'Yuan' with 'Xulei'.
    98   gContext.replaceSurroundingText('Xulei', -2, 4).then(function() {
    99     ok(true, 'replaceSurroundingText finished');
   100     is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Xulei',
   101        'replaceSurroundingText changed the input field correctly.');
   102     test_setComposition();
   103   }, function(e) {
   104     ok(false, 'replaceSurroundingText failed: ' + e.name);
   105     inputmethod_cleanup();
   106   });
   107 }
   109 function test_setComposition() {
   110   gContext.setComposition('XXX').then(function() {
   111     ok(true, 'setComposition finished');
   112     test_endComposition();
   113   }, function(e) {
   114     ok(false, 'setComposition failed: ' + e.name);
   115     inputmethod_cleanup();
   116   });
   117 }
   119 function test_endComposition() {
   120   gContext.endComposition('2013').then(function() {
   121     is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Xulei2013',
   122        'endComposition changed the input field correctly.');
   123     test_onSelectionChange();
   124   }, function (e) {
   125     ok(false, 'endComposition failed: ' + e.name);
   126     inputmethod_cleanup();
   127   });
   128 }
   130 function test_onSelectionChange() {
   131   var sccTimeout = setTimeout(function() {
   132     ok(false, 'selectionchange event not fired');
   133     cleanup(true);
   134   }, 3000);
   136   function cleanup(failed) {
   137     gContext.onselectionchange = null;
   138     clearTimeout(sccTimeout);
   139     if (failed) {
   140       inputmethod_cleanup();
   141     }
   142     else {
   143       test_onSurroundingTextChange();
   144     }
   145   }
   147   gContext.onselectionchange = function() {
   148     ok(true, 'onselectionchange fired');
   149     cleanup();
   150   };
   152   gContext.sendKey(0, 'j'.charCodeAt(0), 0).then(function() {
   153     // do nothing and wait for onselectionchange event
   154   }, function(e) {
   155     ok(false, 'sendKey failed: ' + e.name);
   156     cleanup(true);
   157   });
   158 }
   160 function test_onSurroundingTextChange() {
   161   var sccTimeout = setTimeout(function() {
   162     ok(false, 'surroundingtextchange event not fired');
   163     cleanup(true);
   164   }, 3000);
   166   function cleanup(failed) {
   167     gContext.onsurroundingtextchange = null;
   168     clearTimeout(sccTimeout);
   169     if (failed) {
   170       inputmethod_cleanup();
   171     }
   172     else {
   173       // in case we want more tests leave this
   174       inputmethod_cleanup();
   175     }
   176   }
   178   gContext.onsurroundingtextchange = function() {
   179     ok(true, 'onsurroundingtextchange fired');
   180     cleanup();
   181   };
   183   gContext.sendKey(0, 'j'.charCodeAt(0), 0).then(function() {
   184     // do nothing and wait for onselectionchange event
   185   }, function(e) {
   186     ok(false, 'sendKey failed: ' + e.name);
   187     cleanup(true);
   188   });
   189 }
   191 </script>
   192 </pre>
   193 </body>
   194 </html>

mercurial