<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="/tests/SimpleTest/test.css" type="text/css"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        xmlns:html="http://www.w3.org/1999/xhtml"
        title="Test BackSpace/Delete Keys">
  <script type="application/javascript" src="/MochiKit/packed.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>

<script class="testbody" type="application/javascript">
<![CDATA[

function execTests() {
  var e = document.getElementById("edit");
  var doc = e.contentDocument;
  var win = e.contentWindow;
  var root = doc.documentElement;
  var editor = doc.body;
  var sel = win.getSelection();
  win.focus();

  function setupTest(html, firstChildOffsetForCaret, node) {
    // Work around bug 474255 --- we need to have nonempty content before we turn on
    // editing, or the tests below break because the editor doesn't notice when we
    // insert non-empty content using innerHTML.
    doc.designMode = 'off';
    editor.innerHTML = html;
    doc.designMode = 'on';
    var n = editor.firstChild;
    if (node) {
      n = node();
    }
    sel.collapse(n, firstChildOffsetForCaret);
  }

  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

  /* On Unix systems we have to check for a bug in Pango's handling of Thai
   * script (bug 474068 and http://bugzilla.gnome.org/show_bug.cgi?id=576156 ).
   * We assume that ctypes is available, and the library can be accessed as
   * libpango-1.0.so.0, on all systems that potentially have this problem.
   */
  var pango_todos = false;
  try {
    Components.utils.import("resource://gre/modules/ctypes.jsm");
    if (ctypes) {
      let pango = ctypes.open("libpango-1.0.so.0");
      if (pango) {
	// int pango_version();
	let pango_version = pango.declare("pango_version", ctypes.default_abi,
					  ctypes.int);
	if (pango_version) {
	  let version = pango_version();
	  // The bug was introduced in pango 1.22.0 and has not yet been fixed.
	  // version == major*10000 + minor*100 + micro, therefore:
	  if (version >= 12200) {
	    pango_todos = true;
	    todo(false, "warning: buggy version of Pango detected");
	  }
	}
      }
    }
  } catch (ex) {
  }

  // Select |is| or |todo_is| according to whether the current subtest
  // is affected by the above-mentioned Pango bug. The logic for the
  // decision is: If |pango_todos| is not true, the bug is not expected
  // to occur. If |alternate| is either undefined or equal to |expected|,
  // then the subtest should not be affected by the bug. If |value| is
  // equal to *neither* |expected| nor |alternate| then there is a
  // different problem than the known Pango bug. In all those cases
  // we want to use regular |is|.
  //
  // But if |pango_todos| is true, |alternate| is defined and not equal
  // to |expected|, and |value| is equal to one of them, then we want to
  // use |todo_is| *regardless* of which possibility |value| is equal to.
  // If |value| is equal to |alternate|, this is the bug's syndrome and
  // we obviously want a |todo_is|; if |value| is equal to |expected|,
  // the check for the buggy library is wrong (hopefully, because the
  // bug has been fixed in a new Pango version) and we want to get a
  // failure so we notice.
  function pango_todo_is(value, expected, alternate, msg) {
    if (pango_todos && alternate !== undefined && alternate !== expected &&
	(value === expected || value === alternate))
      todo_is(value, expected, msg);
    else
      is(value, expected, msg);
  }

  var eatSpace;

  function getPrefs() {
    const prefSvcContractID = "@mozilla.org/preferences-service;1";
    const prefSvcIID = Components.interfaces.nsIPrefService;
    return Components.classes[prefSvcContractID].getService(prefSvcIID)
                                                 .getBranch("layout.word_select.");
  }

  function setEatSpace(newValue) {
    getPrefs().setBoolPref("eat_space_to_next_word", newValue);
    eatSpace = newValue;
  }

  function restoreEatSpace() {
    try {
      getPrefs().clearUserPref("eat_space_to_next_word");
    } catch(ex) {}
  }

  /* The various _todo arguments below are honored only if pango_todos is true. */

  function doCommand(cmd, exc_todo) {
    if (!pango_todos) exc_todo = false;
    var controller = document.commandDispatcher.getControllerForCommand(cmd);
    if (controller) {
      try {
        controller.doCommand(cmd);
        (exc_todo ? todo : ok)
	  (true, 'doCommand(' + cmd + ') succeeded');
      } catch(ex) {
        (exc_todo ? todo : ok)
	  (false, 'exception in doCommand(' + cmd + '): ', ex.message);
      }
    }
  }

  function testRight(node, offset, offset_todo) {
    doCommand("cmd_charNext");
    var msg = "Right movement broken in \"" + editor.innerHTML + "\", offset " + offset;
    is(sel.anchorNode, node, msg);
    pango_todo_is(sel.anchorOffset, offset, offset_todo, msg);
  }

  function selErrString(dir) {
    return dir + " selection broken with eatSpace=" + eatSpace + " in \"" + editor.innerHTML + "\"";
  }

  function testWordSelRight(startNode, startOffset, endNode, endOffset) {
    doCommand("cmd_selectWordNext");
    var selRange = sel.getRangeAt(0);
    is(selRange.startContainer, startNode, selErrString("Word right"));
    is(selRange.startOffset, startOffset, selErrString("Word right"));
    is(selRange.endContainer, endNode, selErrString("Word right"));
    is(selRange.endOffset, endOffset, selErrString("Word right"));
  }

  function testDelete(node, offset, text, richtext, offset_todo, text_todo, exc_todo) {
    doCommand("cmd_deleteCharForward", exc_todo);
    var msg = "Delete broken in \"" + editor.innerHTML + "\", offset " + offset;
    if(typeof node == 'function'){
      node = node();
    }
    is(sel.anchorNode, node, msg);

    pango_todo_is(sel.anchorOffset, offset, offset_todo, msg);
    let text_result = richtext ? editor.innerHTML : editor.textContent;
    pango_todo_is(text_result, text, text_todo, msg);
  }

  function testBackspace(node, offset, text, offset_todo, text_todo) {
    doCommand("cmd_deleteCharBackward");
    var msg = "Backspace broken in \"" + editor.innerHTML + "\", offset " + offset;
    is(sel.anchorNode, node, msg);

    pango_todo_is(sel.anchorOffset, offset, offset_todo, msg);
    pango_todo_is(editor.textContent, text, text_todo, msg);
  }

  function testDeletePrevWord(node, offset, text) {
    doCommand("cmd_deleteWordBackward");
    var msg = "Delete previous word broken in \"" + editor.innerHTML + "\", offset " + offset;
    is(sel.anchorNode, node, msg);
    is(sel.anchorOffset, offset, msg);
    is(editor.textContent, text, msg);
  }

  function testDeleteNextWord(node, offset, text) {
    doCommand("cmd_deleteWordForward");
    var msg = "Delete next word broken in \"" + editor.innerHTML + "\", offset " + offset;
    is(sel.anchorNode, node, msg);
    is(sel.anchorOffset, offset, msg);
    todo_is(editor.textContent, text, msg);
  }

  // Test cell-wise deletion of Delete
  setupTest("สวัสดีพ่อแม่พี่น้อง", 0);
  testRight(editor.firstChild, 1);
  testDelete(editor.firstChild, 1, "สสดีพ่อแม่พี่น้อง");
  testRight(editor.firstChild, 2);
  testDelete(editor.firstChild, 2, "สสพ่อแม่พี่น้อง");
  testRight(editor.firstChild, 4);
  testDelete(editor.firstChild, 4, "สสพ่แม่พี่น้อง");
  testRight(editor.firstChild, 5, 7);
  testDelete(editor.firstChild, 5, "สสพ่แพี่น้อง", false, 7, "สสพ่แม่น้อง");
  testRight(editor.firstChild, 8, 9);
  testDelete(editor.firstChild, 8, "สสพ่แพี่อง", false, 9, "สสพ่แม่น้ง");
  testRight(editor.firstChild, 9, 10);
  testDelete(editor.firstChild, 9, "สสพ่แพี่อ", false, 10, "สสพ่แม่น้ง", true);

  // Test character-wise deletion of Backspace
  setupTest("สวัสดีพ่อแม่พี่น้อง", 0);
  testRight(editor.firstChild, 1);
  testBackspace(editor.firstChild, 0, "วัสดีพ่อแม่พี่น้อง");
  testRight(editor.firstChild, 2);
  testBackspace(editor.firstChild, 1, "วสดีพ่อแม่พี่น้อง");
  testRight(editor.firstChild, 2);
  testBackspace(editor.firstChild, 1, "วดีพ่อแม่พี่น้อง");
  testRight(editor.firstChild, 3);
  testBackspace(editor.firstChild, 2, "วดพ่อแม่พี่น้อง");
  testRight(editor.firstChild, 4);
  testBackspace(editor.firstChild, 3, "วดพอแม่พี่น้อง");
  testRight(editor.firstChild, 4);
  testBackspace(editor.firstChild, 3, "วดพแม่พี่น้อง");
  testRight(editor.firstChild, 4, 6);
  testBackspace(editor.firstChild, 3, "วดพม่พี่น้อง", 5, "วดพแมพี่น้อง");
  testRight(editor.firstChild, 5, 8);
  testBackspace(editor.firstChild, 4, "วดพมพี่น้อง", 7, "วดพแมพีน้อง");
  testRight(editor.firstChild, 7, 9);
  testBackspace(editor.firstChild, 6, "วดพมพีน้อง", 8, "วดพแมพีนอง");
  testRight(editor.firstChild, 8, 9);
  testBackspace(editor.firstChild, 7, "วดพมพีนอง", 8, "วดพแมพีนง");
  testRight(editor.firstChild, 8, 9);
  testBackspace(editor.firstChild, 7, "วดพมพีนง", 8, "วดพแมพีน");
  testRight(editor.firstChild, 8, 8);
  testBackspace(editor.firstChild, 7, "วดพมพีน", 7, "วดพแมพี");

  // Tests for Bug 417745

  setEatSpace(true);

  setupTest("Quick yellow fox", 0);
  testWordSelRight(editor.firstChild, 0, editor.firstChild, 6);
  testDelete(editor.firstChild, 0, "yellow fox");
  testWordSelRight(editor.firstChild, 0, editor.firstChild, 7);
  testDelete(editor.firstChild, 0, "fox");

  setEatSpace(false);

  setupTest("Quick yellow fox", 0);
  testWordSelRight(editor.firstChild, 0, editor.firstChild, 5);
  // editor converts the leading space to an &nbsp;, otherwise it
  // wouldn't show up which would confuse users
  testDelete(editor.firstChild, 0, "\u00A0yellow fox");
  testWordSelRight(editor.firstChild, 0, editor.firstChild, 7);
  testDelete(editor.firstChild, 0, "\u00A0fox");
  testWordSelRight(editor.firstChild, 0, editor.firstChild, 4);
  testDelete(editor, 0, "");

  restoreEatSpace();

  // Tests for Bug 419217

  setupTest("foo<div>bar</div>", 3);
  testDelete(function(){return editor.firstChild;}, 3, "foobar", true);

  // Tests for Bug 419406
  var s = "helloשלום";
  setupTest(s, 4);
  testRight(editor.firstChild, 5);
  testDelete(editor.firstChild, 5, "helloשלום");

  // Tests for Bug 462188
  setupTest("You should not see this text.", 29);
  testDeletePrevWord(editor.firstChild, 24, "You should not see this ");
  testDeletePrevWord(editor.firstChild, 19, "You should not see ");
  testDeletePrevWord(editor.firstChild, 15, "You should not ");
  testDeletePrevWord(editor.firstChild, 11, "You should ");
  testDeletePrevWord(editor.firstChild,  4, "You ");
  testDeletePrevWord(editor,  0, "");

  setupTest("You should not see this text.", 0);
  testDeleteNextWord(editor.firstChild, 0, "\u00A0should not see this text.");
  testDeleteNextWord(editor.firstChild, 0, "\u00A0not see this text.");
  testDeleteNextWord(editor.firstChild, 0, "\u00A0see this text.");
  testDeleteNextWord(editor.firstChild, 0, "\u00A0this text.");
  testDeleteNextWord(editor.firstChild, 0, "\u00A0text.");
  // testDeleteNextWord(editor, 0, "");

  // Tests for Bug 502259
  setupTest("<p>Bug</p>\n<p>502259</p>", 1);
  testDelete(function(){return editor.firstChild.firstChild;}, 3, "<p>Bug502259</p>", true);

  // Tests for Bug 507936
  var nodecallback = function(){return editor.firstChild.firstChild.lastChild.firstChild.lastChild;};
  setupTest("<ol><li>one<ol><li>two</li></ol></li></ol>\n<p>three</p>", 3, nodecallback);
  testDelete(nodecallback, 0, "<ol><li>one<ol><li>twothree</li></ol></li></ol>", true);

  setupTest("<ol><li>one<ol><li>two</li></ol></li></ol>\n<hr>\n<p>three</p>", 3, nodecallback);
  testDelete(nodecallback, 3, 
    "<ol><li>one<ol><li>two</li></ol></li></ol><p>three</p>", true);

  // Tests for Bug 519751
  var nodecallback = function(){return editor.firstChild.lastChild;};
  setupTest("<p>one</p><ol><li>two</li><li>three</li></ol>", 3, nodecallback);
  testDelete(nodecallback, 0, "<p>onetwo</p><ol><li>three</li></ol>", true);

  nodecallback = function(){return editor.firstChild.childNodes[1].firstChild;};
  setupTest("<ol><li>one</li><li>two</li></ol><ol><li>three</li><li>four</li></ol>", 3, nodecallback);
  testDelete(function(){return editor.firstChild.childNodes[2].firstChild;}, 
    0, "<ol><li>one</li><li>two</li><li>three</li><li>four</li></ol>", true);
  /*todo_is(false, true, 'The above testDelete should use the same nodecallback' +
    'as in the proceeding setupTest: the cursor should stay at the end of "two", while currently it is at the beginning of "three" after delete');*/

  // More Tests for Bug 507936
  nodecallback = function(){return editor.firstChild.firstChild.firstChild;}
  setupTest("<div><div>abcdef</div><div>bar</div><div>ghi</div></div>", 5, nodecallback);
  sel.extend(editor.lastChild.lastChild.lastChild, 1);
  testDelete(editor.lastChild.lastChild.lastChild, 5, "<div><div>abcdehi</div></div>", true);

  setupTest("<div><div>abcdef</div><div>ghi</div></div>", 5, nodecallback);
  sel.extend(editor.lastChild.lastChild.lastChild, 1);
  testDelete(editor.lastChild.lastChild.lastChild, 5, "<div><div>abcdehi</div></div>", true);

  nodecallback = function(){return editor.firstChild.firstChild;}
  setupTest("<div>abcdef<div><div>bar</div>ghi</div></div>", 5, nodecallback);
  sel.extend(editor.lastChild.lastChild.lastChild, 1);
  expectednodecallback = function(){return editor.lastChild.lastChild;}
  testDelete(expectednodecallback, 0, "<div>abcdehi</div>", true);

  setupTest("<div>abcdef<div>ghi</div></div>", 5, nodecallback);
  sel.extend(editor.lastChild.lastChild.lastChild, 1);
  testDelete(expectednodecallback, 0, "<div>abcdehi</div>", true);

  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
addLoadEvent(execTests);
]]>
</script>

<body  id="html_body" xmlns="http://www.w3.org/1999/xhtml">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=462188">Mozilla Bug 462188</a>
<p id="display"></p>

<pre id="test">
</pre>
<iframe id="edit" width="200" height="100" src="about:blank"/>
</body>
</window>
