<!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="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript;version=1.7" src="inputmethod_common.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/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">

SimpleTest.requestFlakyTimeout("untriaged");

// 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;
    }

    is(gContext.type, 'input', 'The input context type should match.');
    is(gContext.inputType, 'text', 'The inputType should match.');
    is(gContext.inputMode, 'verbatim', 'The inputMode should match.');
    is(gContext.lang, 'zh', 'The language should match.');
    is(gContext.text, 'Yuan', 'Should get the text.');
    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);
    console.error(e);
    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.text, 'Yu-an',
       'sendKey should changed the input field correctly.');
    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.text, 'Yuan',
       'deleteSurroundingText should changed the input field correctly.');
    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.text, 'Xulei',
       'replaceSurroundingText changed the input field correctly.');
    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.text, 'Xulei2013',
       'endComposition changed the input field correctly.');
    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.text, 'Xulei2013jj');
    is(evt.detail.textBeforeCursor, 'Xulei2013jj');
    is(evt.detail.textAfterCursor, '');
    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>