<!doctype html>
<html style="ime-mode: disabled;">
<head>
  <meta charset="utf-8">
  <title>Test for IME state management on focus move in parent process</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <script src="file_ime_state_test_helper.js"></script>
  <script src="file_test_ime_state_on_focus_move.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body style="ime-mode: disabled;">
<div style="ime-mode: disabled;"></div>
<script>
"use strict";

/* import-globals-from file_ime_state_test_helper.js */
/* import-globals-from file_test_ime_state_on_focus_move.js */

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  const tipWrapper = new TIPWrapper(window);
  ok(tipWrapper.isAvailable(), "TextInputProcessor should've been initialized");

  const container = document.querySelector("div");
  async function runIMEStateOnFocusMoveTests(aDescription) {
    {
      const runnerAndChecker = new IMEStateWhenNoActiveElementTester(aDescription);
      const expectedData = await runnerAndChecker.run(document);
      runnerAndChecker.check(expectedData);
    }
    for (let index = 0; index < IMEStateOnFocusMoveTester.numberOfTests; ++index) {
      const runnerAndChecker = new IMEStateOnFocusMoveTester(aDescription, index);
      const expectedData = await runnerAndChecker.prepareToRun(container);
      runnerAndChecker.prepareToCheck(expectedData, tipWrapper);
      await runnerAndChecker.run();
      runnerAndChecker.check(expectedData);
      if (runnerAndChecker.canTestOpenCloseState(expectedData)) {
        for (const defaultOpenState of [false, true]) {
          const expectedOpenStateData =
            await runnerAndChecker.prepareToRunOpenCloseTest(container);
          runnerAndChecker.prepareToCheckOpenCloseTest(
            defaultOpenState,
            expectedOpenStateData
          );
          await runnerAndChecker.runOpenCloseTest();
          runnerAndChecker.checkOpenCloseTest(expectedOpenStateData);
        }
      }
      runnerAndChecker.destroy();
    }
  }

  // test for normal contents.
  await runIMEStateOnFocusMoveTests("in non-editable container");

  // test for contentEditable="true"
  container.setAttribute("contenteditable", "true");
  await runIMEStateOnFocusMoveTests("in div[contenteditable]");

  // test for contentEditable="false"
  container.setAttribute("contenteditable", "false");
  await runIMEStateOnFocusMoveTests('in div[contenteditable="false"]');

  // test for removing contentEditable
  container.setAttribute("contenteditable", "true");
  container.focus();
  await new Promise(resolve =>
    requestAnimationFrame(
      () => requestAnimationFrame(resolve)
    )
  );
  container.removeAttribute("contenteditable");
  await runIMEStateOnFocusMoveTests("after removing contenteditable from the container");

  // test designMode
  document.designMode = "on";
  await runIMEStateOnFocusMoveTests('in designMode="on"');
  document.designMode = "off";
  await runIMEStateOnFocusMoveTests('in designMode="off"');

  tipWrapper.destroy();

  SimpleTest.finish();
});
</script>
</body>
</html>
