<?xml version="1.0"?>

<!-- ***** BEGIN LICENSE BLOCK *****
   - Version: MPL 1.1/GPL 2.0/LGPL 2.1
   -
   - The contents of this file are subject to the Mozilla Public License Version
   - 1.1 (the "License"); you may not use this file except in compliance with
   - the License. You may obtain a copy of the License at
   - http://www.mozilla.org/MPL/
   -
   - Software distributed under the License is distributed on an "AS IS" basis,
   - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   - for the specific language governing rights and limitations under the
   - License.
   -
   - The Original Code is Findbar Test code
   -
   - The Initial Developer of the Original Code is
   - Graeme McCutcheon <graememcc_firefox@graeme-online.co.uk>.
   - Portions created by the Initial Developer are Copyright (C) 2009
   - the Initial Developer. All Rights Reserved.
   -
   - Contributor(s):
   -
   -
   - Alternatively, the contents of this file may be used under the terms of
   - either the GNU General Public License Version 2 or later (the "GPL"), or
   - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
   - in which case the provisions of the GPL or the LGPL are applicable instead
   - of those above. If you wish to allow use of your version of this file only
   - under the terms of either the GPL or the LGPL, and not to allow others to
   - use your version of this file under the terms of the MPL, indicate your
   - decision by deleting the provisions above and replace them with the notice
   - and other provisions required by the GPL or the LGPL. If you do not delete
   - the provisions above, a recipient may use your version of this file under
   - the terms of any one of the MPL, the GPL or the LGPL.
   -
   - ***** END LICENSE BLOCK ***** -->

<?xml-stylesheet href="chrome://global/skin" type="text/css"?>

<window id="451540test"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        width="600"
        height="600"
        title="451540 test">

  <script type="application/javascript"><![CDATA[
    const Ci = Components.interfaces;
    const Cc = Components.classes;
    const Cr = Components.results;
    const SEARCH_TEXT = "minefield";

    var gFindBar = null;
    var gBrowser;

    var imports = [ "SimpleTest", "ok", "finish"];
      for each (var import in imports) {
        window[import] = window.opener.wrappedJSObject[import];
      }


    function finishTest() {
      window.close();
      SimpleTest.finish();
    }

    function onLoad() {
      var _delayedOnLoad = function() {
        gFindBar = document.getElementById("FindToolbar");
        gBrowser = document.getElementById("content");
        gBrowser.addEventListener("pageshow", onPageShow, false);
        var data = 'data:text/html,<input id="inp" type="text" />';
        data +='<textarea id="tarea"/>'
        gBrowser.loadURI(data);
      }
      let tm = Cc["@mozilla.org/thread-manager;1"].
               getService(Ci.nsIThreadManager);
      tm.mainThread.dispatch({
        run: function() _delayedOnLoad()
      }, Ci.nsIThread.DISPATCH_NORMAL);
    }

    function resetForNextTest(aElement, aText) {
      if (!aText)
        aText = SEARCH_TEXT;

      // Turn off highlighting
      var highlightButton = gFindBar.getElement("highlight");
      if (highlightButton.checked)
        highlightButton.click();

      // Initialise input
      aElement.value = aText;
      gFindBar._findField.value = SEARCH_TEXT;

      // Perform search and turn on highlighting
      gFindBar._find();
      highlightButton.click();
      aElement.focus();
    }

    function synthesizeKey(target, isKeyCode, key, ctlKey, shiftKey, metaKey) {
      try {
        var event = document.createEvent("KeyEvents");
        if (isKeyCode) {
          event.initKeyEvent("keypress", true, true, null, ctlKey, false,
                             shiftKey, metaKey, key, 0);
        } else {
          event.initKeyEvent("keypress", true, true, null, ctlKey, false,
                             shiftKey, metaKey, 0, key);
        }
        target.dispatchEvent(event);
      } catch (e) {}
    }

    function testInput(aElement, aTestTypeText) {
      // Initialise the findbar
      var matchCase = gFindBar.getElement("find-case-sensitive");
      if (matchCase.checked)
        matchCase.doCommand();

      var sendCtrl = true;
      var sendMeta = false;
      if (navigator.platform.indexOf("Mac") >= 0) {
        sendCtrl = false;
        sendMeta = true;
      }

      // First check match has been correctly highlighted
      resetForNextTest(aElement);
      if (aElement instanceof Ci.nsIDOMNSEditableElement) {
        var controller = aElement.editor.selectionController;
        var selection = controller.getSelection(controller.SELECTION_FIND);
        ok(selection.rangeCount == 1, aTestTypeText +
             " correctly highlighted match");

        // Test 2: check highlight removed when text added within the highlight
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_UP,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_LEFT,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_RIGHT,
                      false, false, false);
        synthesizeKey(aElement, false, KeyEvent.DOM_VK_A,
                      false, false, false);
        ok(selection.rangeCount == 0, aTestTypeText +
             " correctly removed highlight on text insertion");

        // Test 3: check highlighting remains when text added before highlight
        resetForNextTest(aElement);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_UP,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_LEFT,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, false, KeyEvent.DOM_VK_A,
                      false, false, false);
        ok(selection.rangeCount == 1, aTestTypeText +
             " highlight correctly remained on text insertion at start");

        //  Test 4: check highlighting remains when text added after highlight
        resetForNextTest(aElement);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_UP,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_LEFT,
                      sendCtrl, false, sendMeta);
        for (var x = 0; x < SEARCH_TEXT.length; x++)
          synthesizeKey(aElement, true, KeyEvent.DOM_VK_RIGHT,
                        false, false, false);
        synthesizeKey(aElement, false, KeyEvent.DOM_VK_A,
                      false, false, false);
        ok(selection.rangeCount == 1, aTestTypeText +
             " highlight correctly remained on text insertion at end");

        // Test 5: deleting text within the highlight
        resetForNextTest(aElement);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_UP,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_LEFT,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_RIGHT,
                      false, false, false);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_BACK_SPACE,
                      false, false, false);
        ok(selection.rangeCount == 0, aTestTypeText +
             " correctly removed highlight on text deletion");
        
        // Test 6: deleting text at end of highlight
        resetForNextTest(aElement, SEARCH_TEXT+"A");
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_UP,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_LEFT,
                      sendCtrl, false, sendMeta);
        for (var x = 0; x < aElement.value.length; x++)
          synthesizeKey(aElement, true, KeyEvent.DOM_VK_RIGHT,
                        false, false, false);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_BACK_SPACE,
                      false, false, false);
        ok(selection.rangeCount == 1, aTestTypeText +
             " highlight correctly remained on text deletion at end");

        // Test 7: deleting text at start of highlight
        resetForNextTest(aElement, "A"+SEARCH_TEXT);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_UP,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_LEFT,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_RIGHT,
                      false, false, false);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_BACK_SPACE,
                      false, false, false);
        ok(selection.rangeCount == 1, aTestTypeText +
             " highlight correctly remained on text deletion at start");

        // Test 8: deleting selection
        resetForNextTest(aElement);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_UP,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_RIGHT,
                      sendCtrl, false, sendMeta);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_LEFT,
                      false, true, false);
        synthesizeKey(aElement, true, KeyEvent.DOM_VK_LEFT,
                      false, true, false);
        synthesizeKey(aElement, false, KeyEvent.DOM_VK_X,
                      sendCtrl, false, sendMeta);
        ok(selection.rangeCount == 0, aTestTypeText +
             " correctly removed highlight on selection deletion");
      }
    }

    function onPageShow() {
      gBrowser.removeEventListener("load", onPageShow, true);
      gBrowser.contentWindow.focus();
      gFindBar.open();
      var input = gBrowser.contentDocument.getElementById("inp");
      testInput(input, "Input:");
      var textarea = gBrowser.contentDocument.getElementById("tarea");
      testInput(textarea, "Textarea:");
      finishTest();
    }

    window.opener.wrappedJSObject.SimpleTest.waitForFocus(onLoad, window);
  ]]></script>

  <browser type="content-primary" flex="1" id="content" src="about:blank"/>
  <findbar id="FindToolbar" browserid="content"/>
</window>
