Files
firefox/mobile/android/tests/browser/robocop/robocop_input.html
T
Jim Chen 504ef9a3da Bug 1384829 - 4. Add test case to testInputConnection; r=esawin
Add a test case to testInputConnection that makes sure GeckoEditable's
Editable interface still behaves correctly even after disconnecting from
Gecko due to a blur.

MozReview-Commit-ID: 7Z6Kpv2tpRy
2017-09-28 00:14:19 -04:00

263 lines
8.9 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Robocop Input</title>
</head>
<body>
<p>Input: <input id="input" type="text"></p>
<p>Text area: <textarea id="text-area"></textarea></p>
<p>Content editable: <div id="content-editable" contentEditable="true"></div></p>
<p>Design mode: <iframe id="design-mode" srcdoc="<html><body></body></html>"></iframe></p>
<p>Resetting input: <input id="resetting-input" type="text"></p>
<p>Hiding input: <input id="hiding-input" type="text"></p>
<script type="application/javascript" src="robocop_head.js"></script>
<script type="application/javascript">
let input = document.getElementById("input");
let textArea = document.getElementById("text-area");
let contentEditable = document.getElementById("content-editable");
let designMode = document.getElementById("design-mode");
try {
designMode.contentDocument.designMode = "on";
} catch (e) {
// Setting designMode above sometimes fails, so try again later.
setTimeout(function() { designMode.contentDocument.designMode = "on" }, 0);
}
// Add key listeners to trigger dummy key synthesis.
input.addEventListener("keydown", function() {});
textArea.addEventListener("keydown", function() {});
contentEditable.addEventListener("keydown", function() {});
// An input that resets the editor on every input by resetting the value property.
let resetting_input = document.getElementById("resetting-input");
resetting_input.addEventListener("input", function() {
this.value = this.value;
});
// An input that hides on input.
let hiding_input = document.getElementById("hiding-input");
hiding_input.addEventListener("keydown", function(e) {
if (e.key === "!") { // '!' key event as sent by testInputConnection.java.
this.value = "";
this.style.display = "none";
}
});
let getEditor, setValue, setSelection;
let events_log;
function log_event(event) {
switch (event.type) {
case "compositionstart":
events_log += "<";
break;
case "compositionupdate":
events_log += "=";
break;
case "compositionend":
events_log += ">";
break;
case "selectionchange":
events_log += "|";
break;
default:
events_log += "?";
break;
}
}
let key_log;
function get_key_metastate(event) {
return (event.ctrlKey ? "C" : "c") +
(event.altKey ? "A" : "a") +
(event.shiftKey ? "S" : "s") +
(event.metaKey ? "M" : "m");
}
function log_key(event) {
switch (event.type) {
case "keydown":
case "keypress":
case "keyup":
key_log += `${event.type}:${event.key},${get_key_metastate(event)};`;
break;
default:
key_log += "unknown;";
break;
}
}
function get_event_target() {
var editor = getEditor();
var parent = SpecialPowers.unwrap(editor.rootElement.parentElement);
if (parent instanceof HTMLInputElement || parent instanceof HTMLTextAreaElement) {
// "selectionchange" is only dispatched to the element itself,
// so use the element as the target instead of using the document.
return parent;
}
return editor.document;
}
let test = {
focus_input: function(value) {
getEditor = function() {
return SpecialPowers.wrap(input).QueryInterface(
SpecialPowers.Ci.nsIDOMNSEditableElement).editor;
};
setValue = function(val) {
input.value = val;
};
setSelection = function(pos) {
input.setSelectionRange(pos, pos);
};
setValue(value);
input.focus();
},
focus_text_area: function(value) {
getEditor = function() {
return SpecialPowers.wrap(textArea).QueryInterface(
SpecialPowers.Ci.nsIDOMNSEditableElement).editor;
};
setValue = function(val) {
textArea.value = val;
};
setSelection = function(pos) {
textArea.setSelectionRange(pos, pos);
};
setValue(value);
textArea.focus();
},
focus_content_editable: function(value) {
getEditor = function() {
return SpecialPowers.wrap(window).QueryInterface(
SpecialPowers.Ci.nsIInterfaceRequestor).getInterface(
SpecialPowers.Ci.nsIWebNavigation).QueryInterface(
SpecialPowers.Ci.nsIDocShell).editor;
};
setValue = function(val) {
// eslint-disable-next-line no-unsanitized/property
contentEditable.innerHTML = val;
};
setSelection = function(pos) {
window.getSelection().collapse(contentEditable.firstChild, pos);
};
setValue(value);
contentEditable.focus();
},
focus_design_mode: function(value) {
getEditor = function() {
return SpecialPowers.wrap(designMode.contentWindow).QueryInterface(
SpecialPowers.Ci.nsIInterfaceRequestor).getInterface(
SpecialPowers.Ci.nsIWebNavigation).QueryInterface(
SpecialPowers.Ci.nsIDocShell).editor;
};
setValue = function(val) {
// eslint-disable-next-line no-unsanitized/property
designMode.contentDocument.body.innerHTML = val;
};
setSelection = function(pos) {
designMode.contentWindow.getSelection().collapse(
designMode.contentDocument.body.firstChild, pos);
};
setValue(value);
designMode.contentWindow.focus();
designMode.contentDocument.body.focus();
},
test_reflush_changes: function() {
do_check_true(getEditor().composing);
// Ending the composition then setting the input value triggers the bug.
getEditor().forceCompositionEnd();
setValue("good"); // Value that testInputConnection.java expects.
setSelection(4);
},
test_set_selection: function() {
do_check_true(getEditor().composing);
// Ending the composition then setting the selection triggers the bug.
getEditor().forceCompositionEnd();
setSelection(3); // Offsets that testInputConnection.java expects.
},
start_events_log: function() {
// Reset the log
events_log = "";
let target = get_event_target();
target.addEventListener("compositionstart", log_event);
target.addEventListener("compositionupdate", log_event);
target.addEventListener("compositionend", log_event);
target.addEventListener("selectionchange", log_event);
},
end_events_log: function() {
java.asyncCall("setEventsLog", events_log);
let target = get_event_target();
target.removeEventListener("compositionstart", log_event);
target.removeEventListener("compositionupdate", log_event);
target.removeEventListener("compositionend", log_event);
target.removeEventListener("selectionchange", log_event);
},
start_key_log: function() {
// Reset the log
key_log = "";
let target = get_event_target();
target.addEventListener("keydown", log_key);
target.addEventListener("keypress", log_key);
target.addEventListener("keyup", log_key);
},
end_key_log: function() {
java.asyncCall("setKeyLog", key_log);
let target = get_event_target();
target.removeEventListener("keydown", log_key);
target.removeEventListener("keypress", log_key);
target.removeEventListener("keyup", log_key);
},
test_bug1123514: function() {
document.activeElement.addEventListener("input", function() {
// Only works on input and textarea.
if (this.value === "b") {
this.value = "abc";
}
}, {once: true});
},
focus_resetting_input: function(val) {
resetting_input.value = val;
resetting_input.focus();
},
focus_hiding_input: function(val) {
hiding_input.value = val;
hiding_input.style.display = "";
hiding_input.focus();
},
blur_hiding_input: function() {
hiding_input.blur();
},
finish_test: function() {
java.disconnect();
},
};
var java = new JavaBridge(test);
</script>
</body>
</html>