Files
palemoon27/mobile/android/base/tests/roboextender/testSelectionHandler.html
T

392 lines
14 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<html>
<head>
<title>Automated Text Selection tests for Mobile</title>
<meta name="viewport" content="initial-scale=1.0"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<script type="application/javascript" src="SelectionUtils.js"></script>
<script type="application/javascript;version=1.8">
// Name of this test.
const TYPE_NAME = "Robocop:testSelectionHandler";
const DIV_POINT_TEXT = "Under";
const INPUT_TEXT = "Text for select all in an <input>";
const TEXTAREA_TEXT = "Text for select all in a <textarea>";
const READONLY_INPUT_TEXT = "readOnly text";
/* =================================================================================
*
* Start of all text selection tests, check initialization state.
*
*/
function startTests() {
testSelectAllDivs().
then(testSelectDivAtPoint).
then(testSelectInput).
then(testSelectTextarea).
then(testReadonlyInput).
then(testCloseSelection).
then(testStartSelectionFail).
then(testAttachCaret).
then(testAttachCaretFail).
then(finishTests, function(err) {
ok(false, "Error in selection test " + err);
finishTests();
});
}
/* =================================================================================
*
* "Select all" text selection tests, for <div> (non-editable) fields.
*
*/
function testSelectAllDivs() {
let sh = getSelectionHandler();
let selDiv = document.getElementById("selDiv");
let nonSelDiv = document.getElementById("nonSelDiv");
// Check the initial state of the selection handler, and selectable/non-selectable <div>s.
return Promise.all([
ok(!sh.isSelectionActive(), "Selection should not be active at start of testSelectAllDivs"),
ok(sh.canSelect(selDiv), "Can select selectable <div>"),
ok(!sh.canSelect(nonSelDiv), "Can't select non-selectable <div>"),
]).then(function() {
// Select all on a non-editable text node selects all the text in the page.
let startSelectionResult = sh.startSelection(selDiv);
let selection = sh._getSelection();
return Promise.all([
is(startSelectionResult, sh.ERROR_NONE,
"startSelection() should have completed successfully"),
ok(sh.isSelectionActive(), "Selection should be active now"),
is(selection.anchorNode, document.documentElement, "Anchor Node should be start of document"),
is(selection.anchorOffset, 0, "Anchor offset should be 0"),
is(selection.focusNode, document.body.lastChild, "Focus node should be lastChild of document"),
is(selection.focusOffset, document.body.lastChild.textContent.length, "Focus offset should be it's length"),
]);
});
}
/* =================================================================================
*
* "Select word-at-point" text selection test, for <div> (non-editable) field.
* "collapseToStart" test closes selection (Bug 864589).
*
*/
function testSelectDivAtPoint() {
let sh = getSelectionHandler();
let selDiv = document.getElementById("selDiv");
// Select word at point in <div>
let rect = selDiv.getBoundingClientRect();
let startSelectionResult = sh.startSelection(selDiv, {
mode: sh.SELECT_AT_POINT,
x: rect.left + 1,
y: rect.top + 1
});
let selection = sh._getSelection();
// Check the state of the selection handler after selecting at a point.
return Promise.all([
ok(sh.isSelectionActive(), "Selection should be active at start of testSelectDivAtPoint"),
is(startSelectionResult, sh.ERROR_NONE,
"startSelection() should have completed successfully"),
is(selection.toString(), DIV_POINT_TEXT, "The first word in the <div> was selected"),
]).then(function() {
// Check the state of the selection handler after collapsing a selection.
selection.collapseToStart();
return Promise.all([
ok(selection.collapsed, "Selection should be collapsed"),
ok(!sh.isSelectionActive(), "Selection should not be active"),
]);
});
}
/* =================================================================================
*
* "Select all" text selection test, for <input> (editable) field.
*
*/
function testSelectInput() {
let sh = getSelectionHandler();
let inputNode = document.getElementById("inputNode");
inputNode.value = INPUT_TEXT;
// Test that calling startSelection with an input selects all the text in the input.
return Promise.all([
ok(!sh.isSelectionActive(), "Selection should not be active at start of testSelectInput"),
ok(sh.canSelect(inputNode), "Can select selectable <input>"),
]).then(function() {
// Check the state of the selection handler after calling startSelection on it.
let startSelectionResult = sh.startSelection(inputNode);
let selection = sh._getSelection();
return Promise.all([
is(startSelectionResult, sh.ERROR_NONE,
"startSelection() should have completed successfully"),
ok(sh.isSelectionActive(), "Selection should be active"),
ok((sh._targetElement instanceof Ci.nsIDOMNSEditableElement), "Selected element is editable"),
is(selection.toString(), INPUT_TEXT, "All text in the <input> was selected"),
]);
});
}
/* =================================================================================
*
* "Select all" text selection test, for <textarea> (editable) field.
*
*/
function testSelectTextarea() {
let sh = getSelectionHandler();
let textareaNode = document.getElementById("textareaNode");
textareaNode.value = TEXTAREA_TEXT;
// Change (still-active) selection from previous <input> field to <textarea>
let startSelectionResult = sh.startSelection(textareaNode);
let selection = sh._getSelection();
return Promise.all([
ok(sh.isSelectionActive(), "Selection should be active at start of testSelectTextarea"),
is(startSelectionResult, sh.ERROR_NONE,
"startSelection() should have completed successfully"),
ok((sh._targetElement instanceof Ci.nsIDOMHTMLTextAreaElement), "Selected element is editable, and a <textarea>"),
is(selection.toString(), TEXTAREA_TEXT, "All text in the <textarea> was selected"),
]).then(function() {
// Collpase the selection to close it again.
selection.collapseToStart();
return Promise.all([
ok(!sh.isSelectionActive(), "Selection should not be active"),
]);
});
}
/* =================================================================================
*
* Ensure that readonly inputs aren't editable, and not subject to
* SelectionHandler actions such as "cut".
*
*/
function testReadonlyInput() {
let sh = getSelectionHandler();
let readOnlyNode = document.getElementById("readOnlyTextInput");
readOnlyNode.value = READONLY_INPUT_TEXT;
return Promise.all([
ok(!sh.isSelectionActive(), "Selection should not be active at start of testReadonlyInput"),
]).then(function() {
return Promise.all([
ok(!sh.isElementEditableText(readOnlyNode), "Selected element should be readOnly (not editable)"),
]);
});
}
/* =================================================================================
*
* Various text selection tests to end active selections, including:
* 1.) Clicking outside the selection.
* 2.) SelectionEnd or Tab:Selected messages from java.
*
*/
function testCloseSelection() {
let sh = getSelectionHandler();
let inputNode = document.getElementById("inputNode");
let browserApp = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp;
inputNode.value = INPUT_TEXT;
// Check the initial state of the selection handler.
return Promise.all([
ok(!sh.isSelectionActive(), "Selection should not be active at start of testCloseSelection"),
// Various ways to close an active selection.
]).then(function() {
sh.startSelection(inputNode);
Services.obs.notifyObservers(null, "TextSelection:End", {});
return ok(!sh.isSelectionActive(), "TextSelection:End should close active selection");
}).then(function() {
sh.startSelection(inputNode);
Services.obs.notifyObservers(null, "Tab:Selected", browserApp.selectedTab.id);
return ok(!sh.isSelectionActive(), "Tab:Selected should close active selection");
}).then(function() {
sh.startSelection(inputNode);
sh.handleEvent({ type: "pagehide", originalTarget: {} });
return ok(sh.isSelectionActive(), "unrelated pagehide should not close active selection");
}).then(function() {
sh.handleEvent({ type: "pagehide", originalTarget: document });
return ok(!sh.isSelectionActive(), "pagehide should close active selection");
}).then(function() {
sh.startSelection(inputNode);
sh.handleEvent({ type: "blur" });
return ok(!sh.isSelectionActive(), "blur should close active selection");
});
}
/* =================================================================================
*
* Various text selection tests to ensure we fail certain startSelection() requests.
*
*/
function testStartSelectionFail() {
let sh = getSelectionHandler();
return Promise.all([
ok(!sh.isSelectionActive(),
"Selection should not be active at start of testStartSelectionFail"),
]).then(function() {
// We cannot perform an invalid selection request.
let element = document.getElementById("inputNode");
let rect = element.getBoundingClientRect();
let startSelectionResult = sh.startSelection(element, {
mode: "fooMode",
x: rect.left + 1,
y: rect.top + 1
});
return Promise.all([
is(startSelectionResult, sh.START_ERROR_INVALID_MODE,
"startSelection() should have failed predictably."),
ok(!sh.isSelectionActive(), "We cannot select text with a bad mode request."),
]);
}).then(function() {
// Select all on a Button should fail.
let element = document.getElementById("inputButton");
let startSelectionResult = sh.startSelection(element);
return Promise.all([
is(startSelectionResult, sh.START_ERROR_NONTEXT_INPUT,
"startSelection() should have failed predictably."),
ok(!sh.isSelectionActive(), "We cannot select text in an input Button."),
]);
}).then(function() {
// We cannot Select Word where no point exists.
let element = document.getElementById("inputNode");
let rect = element.getBoundingClientRect();
let startSelectionResult = sh.startSelection(element, {
mode: sh.SELECT_AT_POINT,
x: -1000,
y: -1000
});
return Promise.all([
is(startSelectionResult, sh.START_ERROR_SELECT_WORD_FAILED,
"startSelection() should have failed predictably."),
ok(!sh.isSelectionActive(), "We cannot select text at a bad location request."),
]);
});
}
/* =================================================================================
*
* Test to ensure we can attach a Caret to an input field.
*
*/
function testAttachCaret() {
let sh = getSelectionHandler();
return Promise.all([
ok(!sh.isSelectionActive(), "Selection should not be active at start of testAttachCaret"),
]).then(function() {
let element = document.getElementById("inputNode");
element.value = INPUT_TEXT;
let attachCaretResult = sh.attachCaret(element);
return Promise.all([
is(attachCaretResult, sh.ERROR_NONE,
"attachCaret() should have completed successfully"),
]);
}).then(function() {
Services.obs.notifyObservers(null, "TextSelection:End", {});
return Promise.all([
ok(!sh.isSelectionActive(), "Selection should not be active at end of testAttachCaret"),
]);
});
}
/* =================================================================================
*
* Test to ensure we fail certain attachCaret() requests.
*
*/
function testAttachCaretFail() {
let sh = getSelectionHandler();
return Promise.all([
is(sh._activeType, sh.TYPE_NONE,
"Selection should not be active at start of testAttachCaretFail."),
]).then(function() {
// We cannot attach Caret into disabled input.
let element = document.getElementById("inputDisabled");
element.value = INPUT_TEXT;
let attachCaretResult = sh.attachCaret(element);
return Promise.all([
is(attachCaretResult, sh.ATTACH_ERROR_INCOMPATIBLE,
"attachCaret() should have failed predictably."),
is(sh._activeType, sh.TYPE_NONE,
"Selection should not be active at end of testAttachCaretFail."),
]);
});
}
</script>
</head>
<body onload="startTests();">
<div id="selDiv">Under sufficiently extreme conditions, quarks may become
deconfined and exist as free particles. In the course of asymptotic freedom,
the strong interaction becomes weaker at higher temperatures. Eventually,
color confinement would be lost and an extremely hot plasma of freely moving
quarks and gluons would be formed. This theoretical phase of matter is called
quark-gluon plasma.[81] The exact conditions needed to give rise to this state
are unknown and have been the subject of a great deal of speculation and
experimentation. A recent estimate puts the needed temperature at
(1.90±0.02)×1012 Kelvin. While a state of entirely free quarks and gluons has
never been achieved (despite numerous attempts by CERN in the 1980s and 1990s),
recent experiments at the Relativistic Heavy Ion Collider have yielded evidence
for liquid-like quark matter exhibiting "nearly perfect" fluid motion.</div><br>
<div id="nonSelDiv" style="-moz-user-select: none;">Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Proin in blandit magna, non porttitor augue.
Nam in neque sagittis, varius augue at, ornare velit. Vestibulum eget nisl
congue odio molestie scelerisque. Pellentesque ut augue orci. In hac habitasse
platea dictumst. Sed placerat tellus quis lacus condimentum, quis luctus elit
pellentesque. Mauris cursus neque diam, sit amet gravida quam porta ac.
Aliquam aliquam feugiat vestibulum. Proin commodo nulla ligula, in bibendum
massa euismod a. Ut ac lobortis dui. Ut id augue id arcu ornare suscipit eu
ornare lorem. Pellentesque nec dictum ante. Nam quis ligula ultricies, auctor
nunc vel, fringilla turpis. Nulla lacinia, leo ut egestas hendrerit, risus
ligula interdum enim, vel varius libero sem ut ligula.</div><br>
<input id="inputNode" type="text"><br>
<textarea id="textareaNode"></textarea><br>
<input id="readOnlyTextInput" type="text" readonly><br>
<input id="inputButton" type="button" value="Click me"><br>
<input id="inputDisabled" type="text" disabled><br>
</body>
</html>