summaryrefslogtreecommitdiff
path: root/dom/tests/mochitest
diff options
context:
space:
mode:
authorjanekptacijarabaci <janekptacijarabaci@seznam.cz>2017-07-05 00:34:45 +0200
committerjanekptacijarabaci <janekptacijarabaci@seznam.cz>2017-07-05 00:34:45 +0200
commit8b7b88c2028c74da3e11e9d70394d5a90e052ca5 (patch)
tree2796b62bdbc8adbfd82094f52197b874f4fb044d /dom/tests/mochitest
parent629935a9079a68d0a5eb0eff11ff24534e1625e6 (diff)
downloadpalemoon-gre-8b7b88c2028c74da3e11e9d70394d5a90e052ca5.tar.gz
DOM - implement selection events (base)
Diffstat (limited to 'dom/tests/mochitest')
-rw-r--r--dom/tests/mochitest/general/frameSelectEvents.html263
-rw-r--r--dom/tests/mochitest/general/mochitest.ini3
-rw-r--r--dom/tests/mochitest/general/test_selectevents.html32
3 files changed, 298 insertions, 0 deletions
diff --git a/dom/tests/mochitest/general/frameSelectEvents.html b/dom/tests/mochitest/general/frameSelectEvents.html
new file mode 100644
index 000000000..e88c847c2
--- /dev/null
+++ b/dom/tests/mochitest/general/frameSelectEvents.html
@@ -0,0 +1,263 @@
+<!doctype html>
+<html>
+ <head>
+ <title>Testing Selection Events</title>
+ <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+ </head>
+
+ <body>
+ <div id="normal">
+ <span id="inner">A bunch of text in a span inside of a div which should be selected</span>
+ </div>
+
+ <div id="ce">
+ This is a random block of text
+ </div>
+
+ <script>
+ // Call the testing methods from the parent window
+ var is = parent.is;
+ var ok = parent.ok;
+
+ // spin() spins the event loop for two cycles, giving time for
+ // selectionchange events to be fired, and handled by our listeners.
+ function spin() {
+ return new Promise(function(a) {
+ parent.SimpleTest.executeSoon(function() {
+ parent.SimpleTest.executeSoon(a)
+ });
+ });
+ }
+
+ // The main test
+ parent.add_task(function *() {
+ yield spin();
+
+ var selectstart = 0;
+ var selectionchange = 0;
+ var cancel = false;
+ var selectstartTarget = null;
+
+ document.addEventListener('selectstart', function(aEvent) {
+ console.log("originaltarget", aEvent.originalTarget, "new", selectstartTarget);
+ is(aEvent.originalTarget, selectstartTarget,
+ "The original target of selectstart");
+ selectstartTarget = null;
+
+ console.log(selectstart);
+ selectstart++;
+
+ if (cancel) {
+ aEvent.preventDefault();
+ }
+ });
+ document.addEventListener('selectionchange', function(aEvent) {
+ is(aEvent.originalTarget, document,
+ "The original target of selectionchange should be the document");
+ console.log(selectionchange);
+ selectionchange++;
+ });
+
+ function elt(aId) { return document.getElementById(aId); }
+ function reset() { selectstart = 0; selectionchange = 0; cancel = false; }
+
+ function* mouseAction(aElement, aOffset, aType, aSelStart, aSelChng) {
+ if (aType == "click") { // You can simulate a click event by sending undefined
+ aType = undefined;
+ }
+ synthesizeMouse(aElement, aOffset, 10, { type: aType });
+ yield spin();
+ is(selectstart, aSelStart,
+ "SelStart Mouse Action (" + aOffset + " - " + aType + ")");
+ is(selectionchange, aSelChng,
+ "SelChng Mouse Action (" + aOffset + " - " + aType + ")");
+ reset();
+ }
+
+ function* keyAction(aKey, aShift, aAccel, aSelStart, aSelChng) {
+ synthesizeKey(aKey, { shiftKey: aShift, accelKey: aAccel });
+ yield spin();
+ is(selectstart, aSelStart,
+ "SelStart Key Action (" + aKey + " - " + aShift + " - " + aAccel + ")");
+ is(selectionchange, aSelChng,
+ "SelChng Key Action (" + aKey + " - " + aShift + " - " + aAccel + ")");
+ reset();
+ }
+
+ function* contentEditableAction(aElement, aContentEditable,
+ aSelStart, aSelChng,
+ aISelChng, aTSelChng)
+ {
+ aElement.setAttribute("contenteditable",
+ aContentEditable ? "true" : "false");
+ yield spin();
+
+ is(selectstart, aSelStart,
+ "SelStart ContentEditable Action");
+ is(selectionchange, aSelChng,
+ "SelStart ContentEditable Action");
+ is(inputSelectionchange, aISelChng || 0,
+ "SelStart ContentEditable Action");
+ is(textareaSelectionchange, aTSelChng || 0,
+ "SelStart ContentEditable Action");
+ reset();
+ }
+
+ var selection = document.getSelection();
+ function isCollapsed() { is(selection.isCollapsed, true, "Selection is collapsed"); }
+ function isNotCollapsed() { is(selection.isCollapsed, false, "Selection is not collapsed"); }
+
+ // Make sure setting the element to contentEditable doesn't cause any selectionchange events
+ yield* contentEditableAction(elt("ce"), true, 0, 0, 0, 0);
+
+ // Make sure setting the element to not be contentEditable doesn't cause any selectionchange events
+ yield* contentEditableAction(elt("ce"), false, 0, 0, 0, 0);
+
+ // Now make the div contentEditable and proceed with the test
+ yield* contentEditableAction(elt("ce"), true, 0, 0, 0, 0);
+
+ // Focus the contenteditable text
+ yield* mouseAction(elt("ce"), 100, "click", 0, 1);
+ isCollapsed();
+
+ // Move the selection to the right, this should only fire selectstart once
+ selectstartTarget = elt("ce").firstChild;
+ yield* keyAction("VK_RIGHT", true, false, 1, 1);
+ isNotCollapsed();
+ yield* keyAction("VK_RIGHT", true, false, 0, 1);
+ isNotCollapsed();
+
+ // Move it back so that the selection is empty again
+ yield* keyAction("VK_LEFT", true, false, 0, 1);
+ isNotCollapsed();
+ yield* keyAction("VK_LEFT", true, false, 0, 1);
+ isCollapsed();
+
+ // Going from empty to non-empty should fire selectstart again
+ selectstartTarget = elt("ce").firstChild;
+ yield* keyAction("VK_LEFT", true, false, 1, 1);
+ isNotCollapsed();
+
+ function* mouseMoves(aElement, aTarget) {
+ // Select a region
+ yield* mouseAction(aElement, 50, "mousedown", 0, 1);
+ isCollapsed();
+
+ selectstartTarget = aTarget;
+ yield* mouseAction(aElement, 100, "mousemove", 1, 1);
+ isNotCollapsed();
+
+ // Moving it more shouldn't trigger a start (move back to empty)
+ yield* mouseAction(aElement, 75, "mousemove", 0, 1);
+ isNotCollapsed();
+ yield* mouseAction(aElement, 50, "mousemove", 0, 1);
+ isCollapsed();
+
+ // Wiggling the mouse a little such that it doesn't select any
+ // characters shouldn't trigger a selection
+ yield* mouseAction(aElement, 49, "mousemove", 0, 0);
+ isCollapsed();
+
+ // Moving the mouse again from an empty selection should trigger a
+ // selectstart
+ selectstartTarget = aTarget;
+ yield* mouseAction(aElement, 25, "mousemove", 1, 1);
+ isNotCollapsed();
+
+ // Releasing the mouse shouldn't do anything
+ yield* mouseAction(aElement, 25, "mouseup", 0, 0);
+ isNotCollapsed();
+
+ // And neither should moving your mouse around when the mouse
+ // button isn't pressed
+ yield* mouseAction(aElement, 50, "mousemove", 0, 0);
+ isNotCollapsed();
+
+ // Clicking in an random location should move the selection, but not perform a
+ // selectstart
+ yield* mouseAction(aElement, 50, "click", 0, 1);
+ isCollapsed();
+
+ // Clicking there again should do nothing
+ yield* mouseAction(aElement, 50, "click", 0, 0);
+ isCollapsed();
+
+ // Selecting a region, and canceling the selectstart should mean that the
+ // selection remains collapsed
+ yield* mouseAction(aElement, 75, "mousedown", 0, 1);
+ isCollapsed();
+ cancel = true;
+ selectstartTarget = aTarget;
+ yield* mouseAction(aElement, 100, "mousemove", 1, 1);
+ isCollapsed();
+ yield* mouseAction(aElement, 100, "mouseup", 0, 0);
+ isCollapsed();
+ }
+
+ // Should work both on normal
+ yield* mouseMoves(elt("inner"), elt("inner").firstChild);
+ // and contenteditable fields
+ yield* mouseMoves(elt("ce"), elt("ce").firstChild);
+ // and fields with elements in them
+ yield* mouseMoves(elt("normal"), elt("inner").firstChild);
+
+ yield* mouseAction(elt("inner"), 50, "click", 0, 1);
+ isCollapsed();
+
+ reset();
+ // Select all should fire both selectstart and change
+ selectstartTarget = document.body;
+ yield* keyAction("A", false, true, 1, 1);
+ isNotCollapsed();
+
+ // Clear the selection
+ yield* mouseAction(elt("inner"), 50, "click", 0, 1);
+ isCollapsed();
+
+ // Even if we already have a selection
+ yield* mouseAction(elt("inner"), 75, "mousedown", 0, 1);
+ isCollapsed();
+ selectstartTarget = elt("inner").firstChild;
+ yield* mouseAction(elt("inner"), 100, "mousemove", 1, 1);
+ isNotCollapsed();
+ yield* mouseAction(elt("inner"), 100, "mouseup", 0, 0);
+ isNotCollapsed();
+
+ selectstartTarget = document.body;
+ yield* keyAction("A", false, true, 1, 1);
+ isNotCollapsed();
+
+ // Clear the selection
+ yield* mouseAction(elt("inner"), 50, "click", 0, 1);
+ isCollapsed();
+
+ // Make sure that a synthesized selection change doesn't fire selectstart
+ var s = document.getSelection();
+ s.removeAllRanges();
+ yield spin();
+ is(selectstart, 0, "Synthesized range removals shouldn't fire selectstart");
+ is(selectionchange, 1, "Synthesized range removals should change selectionchange");
+ reset();
+ isCollapsed();
+
+ var range = document.createRange();
+ range.selectNode(elt("inner"));
+ s.addRange(range);
+ yield spin();
+ is(selectstart, 0, "Synthesized range additions shouldn't fire selectstart");
+ is(selectionchange, 1, "Synthesized range additions should change selectionchange");
+ reset();
+ isNotCollapsed();
+
+ // Change the range, without replacing
+ range.selectNode(elt("ce"));
+ yield spin();
+ is(selectstart, 0, "Synthesized range mutations shouldn't fire selectstart");
+ is(selectionchange, 1, "Synthesized range mutations should change selectionchange");
+ reset();
+ isNotCollapsed();
+ });
+ </script>
+ </body>
+</html>
diff --git a/dom/tests/mochitest/general/mochitest.ini b/dom/tests/mochitest/general/mochitest.ini
index d9410a303..f5c7a328b 100644
--- a/dom/tests/mochitest/general/mochitest.ini
+++ b/dom/tests/mochitest/general/mochitest.ini
@@ -37,6 +37,7 @@ support-files =
resource_timing.js
navigation_timing.html
test_bug1012662_common.js
+ frameSelectEvents.html
[test_497898.html]
skip-if = ((buildapp == 'mulet' || buildapp == 'b2g') && toolkit != 'gonk') || toolkit == 'android' #Bug 931116, b2g desktop specific, initial triage
@@ -99,3 +100,5 @@ skip-if = (buildapp == 'b2g' && toolkit != 'gonk') #Bug 931116, b2g desktop spec
skip-if = buildapp == 'b2g' || buildapp == 'mulet'
[test_bug1012662_editor.html]
[test_bug1012662_noeditor.html]
+[test_selectevents.html]
+skip-if = buildapp == 'b2g' || buildapp == 'mulet' # Mouse doesn't select in the same way on b2g
diff --git a/dom/tests/mochitest/general/test_selectevents.html b/dom/tests/mochitest/general/test_selectevents.html
new file mode 100644
index 000000000..d510d19d0
--- /dev/null
+++ b/dom/tests/mochitest/general/test_selectevents.html
@@ -0,0 +1,32 @@
+<!doctype html>
+<html>
+ <head>
+ <title>Testing Selection Events</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+ </head>
+
+ <body>
+ <iframe width="500"></iframe>
+ <script>
+ add_task(function* () {
+ // Push the correct preferences for the test
+ yield new Promise((done) => {
+ SpecialPowers.pushPrefEnv({'set': [['dom.select_events.enabled', true]]}, done);
+ });
+
+ // Start the actual test
+ yield new Promise((done) => {
+ var iframe = document.querySelector('iframe');
+ iframe.addEventListener('load', done);
+ iframe.setAttribute('src', 'frameSelectEvents.html');
+ });
+
+ // The child iframe will call add_task before we reach this point,
+ // and will handle the rest of the test.
+ });
+ </script>
+ </body>
+</html>