layout/generic/test/test_selection_preventDefault.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/generic/test/test_selection_preventDefault.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,174 @@
     1.4 +<!DOCTYPE>
     1.5 +<html>
     1.6 +<head>
     1.7 +<title>selection preventDefault test</title>
     1.8 +<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
    1.10 +<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
    1.11 +
    1.12 +<style type="text/css">
    1.13 +  #fixedDiv1 {
    1.14 +    position: fixed;
    1.15 +    right: 0;
    1.16 +    overflow: scroll;
    1.17 +    width: 200px;
    1.18 +    top: 0;
    1.19 +  }
    1.20 +  input {
    1.21 +    font-size: 16px;
    1.22 +    height: 16px;
    1.23 +    width: 80px;
    1.24 +    margin: 0;
    1.25 +    padding: 0;
    1.26 +  }
    1.27 +</style>
    1.28 +
    1.29 +</head>
    1.30 +<body>
    1.31 +<input id="input" type="text" value="iiiiiiiii iiiiiiiii iiiiiiiii">
    1.32 +<div id="fixedDiv1" class="testingDiv">
    1.33 +dddddd dddddd dddddd
    1.34 +</div>
    1.35 +<pre id="test">
    1.36 +<script class="testbody" type="text/javascript">
    1.37 +
    1.38 +var fixedDiv1 = document.getElementById("fixedDiv1");
    1.39 +var input = document.getElementById("input");
    1.40 +
    1.41 +function test()
    1.42 +{
    1.43 +  function getSelectionForEditor(aEditorElement)
    1.44 +  {
    1.45 +    const nsIDOMNSEditableElement =
    1.46 +      Components.interfaces.nsIDOMNSEditableElement;
    1.47 +    return aEditorElement.QueryInterface(nsIDOMNSEditableElement).editor.selection;
    1.48 +  }
    1.49 +
    1.50 +  function clear()
    1.51 +  {
    1.52 +    var sel = window.getSelection();
    1.53 +    if (sel.rangeCount > 0)
    1.54 +      sel.collapseToEnd();
    1.55 +    sel = getSelectionForEditor(input);
    1.56 +    if (sel.rangeCount > 0)
    1.57 +      sel.collapseToEnd();
    1.58 +  }
    1.59 +
    1.60 +  const kFalse = 0;
    1.61 +  const kTrue  = 1;
    1.62 +  const kToDo  = 2;
    1.63 +
    1.64 +  function check(aFixedDiv1ShouldBeSelected,
    1.65 +                 aInputShouldBeSelected,
    1.66 +                 aTestingDescription)
    1.67 +  {
    1.68 +    function checkCharacter(aSelectedText,
    1.69 +                            aShouldBeIncludedCharacter,
    1.70 +                            aSouldBeSelected,
    1.71 +                            aElementName)
    1.72 +    {
    1.73 +      var boolvalue = aSouldBeSelected & kTrue;
    1.74 +      var f = aSouldBeSelected & kToDo ? todo : ok;
    1.75 +      var str = aSelectedText.replace('\n', '\\n');
    1.76 +      if (boolvalue) {
    1.77 +        f(aSelectedText.indexOf(aShouldBeIncludedCharacter) >= 0,
    1.78 +          "The contents of " + aElementName +
    1.79 +          " aren't selected (" + aTestingDescription +
    1.80 +          "): Selected String: \"" + str + "\"");
    1.81 +      } else {
    1.82 +        f(aSelectedText.indexOf(aShouldBeIncludedCharacter) < 0,
    1.83 +          "The contents of " + aElementName +
    1.84 +          " are selected (" + aTestingDescription +
    1.85 +          "): Selected String: \"" + str + "\"");
    1.86 +      }
    1.87 +    }
    1.88 +
    1.89 +    var sel = window.getSelection().toString();
    1.90 +    checkCharacter(sel, "d", aFixedDiv1ShouldBeSelected, "fixedDiv1");
    1.91 +
    1.92 +    // input contents must not be included on the parent
    1.93 +    // selection.
    1.94 +    checkCharacter(sel, "i", false, "input (checking on parent)");
    1.95 +
    1.96 +    var selInput = getSelectionForEditor(input).toString();
    1.97 +    checkCharacter(selInput, "i", aInputShouldBeSelected, "input");
    1.98 +  }
    1.99 +
   1.100 +  function eventHandler(evt) {
   1.101 +    evt.preventDefault();
   1.102 +  }
   1.103 +
   1.104 +  // prevent default action on mousedown should prevent selection
   1.105 +  fixedDiv1.addEventListener("mousedown", eventHandler);
   1.106 +  synthesizeMouse(fixedDiv1, 30, 5, { type: "mousedown" });
   1.107 +  synthesizeMouse(fixedDiv1, 40, 5, { type: "mousemove" });
   1.108 +  synthesizeMouse(fixedDiv1, 40, 5, { type: "mouseup" });
   1.109 +  check(kFalse, kFalse, "fixedDiv1-fixedDiv1-mousedown");
   1.110 +  clear();
   1.111 +
   1.112 +  input.addEventListener("mousedown", eventHandler);
   1.113 +  synthesizeMouse(input, 20, 5, { type: "mousedown" });
   1.114 +  synthesizeMouse(input, 40, 5, { type: "mousemove" });
   1.115 +  synthesizeMouse(input, 40, 5, { type: "mouseup" });
   1.116 +  check(kFalse, kFalse, "input-input-mousedown");
   1.117 +  clear();
   1.118 +
   1.119 +  // clean up mousedown listener
   1.120 +  [fixedDiv1, input].forEach(function(element) {
   1.121 +     element.removeEventListener("mousedown", eventHandler);
   1.122 +  });
   1.123 +
   1.124 +  // prevent default action on mouseup should not affect the selection state
   1.125 +  fixedDiv1.addEventListener("mouseup", eventHandler);
   1.126 +  synthesizeMouse(fixedDiv1, 30, 5, { type: "mousedown" });
   1.127 +  synthesizeMouse(fixedDiv1, 40, 5, { type: "mousemove" });
   1.128 +  synthesizeMouse(fixedDiv1, 40, 5, { type: "mouseup" });
   1.129 +  check(kTrue, kFalse, "fixedDiv1-fixedDiv1-mouseup");
   1.130 +  clear();
   1.131 +
   1.132 +  input.addEventListener("mouseup", eventHandler);
   1.133 +  synthesizeMouse(input, 20, 5, { type: "mousedown" });
   1.134 +  synthesizeMouse(input, 40, 5, { type: "mousemove" });
   1.135 +  synthesizeMouse(input, 40, 5, { type: "mouseup" });
   1.136 +  check(kFalse, kTrue, "input-input-mouseup");
   1.137 +  clear();
   1.138 +
   1.139 +  [fixedDiv1, input].forEach(function(element) {
   1.140 +     element.removeEventListener("mouseup", eventHandler);
   1.141 +  });
   1.142 +
   1.143 +  // touchmove event should not affect the selection state
   1.144 +  synthesizeTouch(fixedDiv1, 30, 5, { type: "touchstart" });
   1.145 +  synthesizeTouch(fixedDiv1, 40, 5, { type: "touchmove" });
   1.146 +  check(kFalse, kFalse, "fixedDiv1-fixedDiv1-touchmove");
   1.147 +  synthesizeTouch(fixedDiv1, 40, 5, { type: "touchend" });
   1.148 +  clear();
   1.149 +
   1.150 +  synthesizeTouch(input, 20, 5, { type: "touchstart" });
   1.151 +  synthesizeTouch(input, 40, 5, { type: "touchmove" });
   1.152 +  check(kFalse, kFalse, "input-input-touchmove");
   1.153 +  synthesizeTouch(input, 40, 5, { type: "touchend" });
   1.154 +  clear();
   1.155 +
   1.156 +  fixedDiv1.addEventListener("touchmove", eventHandler);
   1.157 +  synthesizeTouch(fixedDiv1, 30, 5, { type: "touchstart" });
   1.158 +  synthesizeTouch(fixedDiv1, 40, 5, { type: "touchmove" });
   1.159 +  check(kFalse, kFalse, "fixedDiv1-fixedDiv1-touchmove-preventDefault");
   1.160 +  synthesizeTouch(fixedDiv1, 40, 5, { type: "touchend" });
   1.161 +  clear();
   1.162 +
   1.163 +  input.addEventListener("touchmove", eventHandler);
   1.164 +  synthesizeTouch(input, 20, 5, { type: "touchstart" });
   1.165 +  synthesizeTouch(input, 40, 5, { type: "touchmove" });
   1.166 +  check(kFalse, kFalse, "input-input-touchmove-preventDefault");
   1.167 +  synthesizeTouch(input, 40, 5, { type: "touchend" });
   1.168 +  clear();
   1.169 +
   1.170 +  SimpleTest.finish();
   1.171 +}
   1.172 +window.onload = function() { setTimeout(test, 0); };
   1.173 +SimpleTest.waitForExplicitFinish();
   1.174 +</script>
   1.175 +</pre>
   1.176 +</body>
   1.177 +</html>

mercurial