editor/libeditor/tests/test_composition_event_created_in_chrome.html
author Aryeh Gregor <ayg@aryeh.name>
Tue, 23 Aug 2016 16:01:28 +0300
changeset 432282 1334f6bb2ea03f9449dbc2319e1add4a9f972cd5
parent 201605 c2c4c70d0be36493b7736d4981b47e3c3fb9bac4
child 451341 2ab49d2ea8cb3c2884039dd05fb4a4671f172145
permissions -rw-r--r--
Bug 1271120 - Port test_composition_event_created_in_chrome.html from chrome to plain; r=masayuki MozReview-Commit-ID: 5K2qv7JXAT4

<!doctype html>
<html>

<head>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">

  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
</head>

<body>

<input id="input">

<script type="application/javascript">

// In nsEditorEventListener, when listening event is not created with proper
// event interface, it asserts the fact.
SimpleTest.waitForExplicitFinish();

var gInputElement = document.getElementById("input");

function getEditorIMESupport(aInputElement)
{
  var editableElement = SpecialPowers.wrap(aInputElement).QueryInterface(SpecialPowers.Ci.nsIDOMNSEditableElement);
  ok(editableElement, "The input element doesn't have nsIDOMNSEditableElement interface");
  ok(editableElement.editor, "There is no editor for the input element");
  var editorIMESupport = SpecialPowers.wrap(editableElement).editor.QueryInterface(SpecialPowers.Ci.nsIEditorIMESupport);
  ok(editorIMESupport, "The input element doesn't have nsIEditorIMESupport interface");
  return editorIMESupport;
}

var gEditorIMESupport;

function testNotGenerateCompositionByCreatedEvents(aEventInterface)
{
  var compositionEvent = document.createEvent(aEventInterface);
  if (compositionEvent.initCompositionEvent) {
    compositionEvent.initCompositionEvent("compositionstart", true, true, window, "", "");
  } else if (compositionEvent.initMouseEvent) {
    compositionEvent.initMouseEvent("compositionstart", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  }
  gInputElement.dispatchEvent(compositionEvent);
  ok(!gEditorIMESupport.composing, "Composition shouldn't be started with a created compositionstart event (" + aEventInterface + ")");

  compositionEvent = document.createEvent(aEventInterface);
  if (compositionEvent.initCompositionEvent) {
    compositionEvent.initCompositionEvent("compositionupdate", true, false, window, "abc", "");
  } else if (compositionEvent.initMouseEvent) {
    compositionEvent.initMouseEvent("compositionupdate", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  }
  gInputElement.dispatchEvent(compositionEvent);
  ok(!gEditorIMESupport.composing, "Composition shouldn't be started with a created compositionupdate event (" + aEventInterface + ")");
  is(gInputElement.value, "", "Input element shouldn't be modified with a created compositionupdate event (" + aEventInterface + ")");

  compositionEvent = document.createEvent(aEventInterface);
  if (compositionEvent.initCompositionEvent) {
    compositionEvent.initCompositionEvent("compositionend", true, false, window, "abc", "");
  } else if (compositionEvent.initMouseEvent) {
    compositionEvent.initMouseEvent("compositionend", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  }
  gInputElement.dispatchEvent(compositionEvent);
  ok(!gEditorIMESupport.composing, "Composition shouldn't be committed with a created compositionend event (" + aEventInterface + ")");
  is(gInputElement.value, "", "Input element shouldn't be committed with a created compositionend event (" + aEventInterface + ")");
}

function doTests()
{
  gInputElement.focus();
  gEditorIMESupport = getEditorIMESupport(gInputElement);

  testNotGenerateCompositionByCreatedEvents("CompositionEvent");
  testNotGenerateCompositionByCreatedEvents("MouseEvent");

  SimpleTest.finish();
}

SimpleTest.waitForFocus(doTests);

</script>
</body>
</html>