Skip to content

Truly independent web browser

LibWeb: Import Touch and TouchEvent constructor coverage

Andreas Kling120f6f73a233
diff --git a/Tests/LibWeb/Text/expected/wpt-import/touch-events/touch-touchevent-constructor.txt b/Tests/LibWeb/Text/expected/wpt-import/touch-events/touch-touchevent-constructor.txt
new file mode 100644
index 00000000000..70ae9ad6989
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/touch-events/touch-touchevent-constructor.txt
@@ -0,0 +1,10 @@
+Harness status: OK
+
+Found 5 tests
+
+5 Fail
+Fail Touch constructor with insufficient properties
+Fail Touch constructor with non-EventTarget target
+Fail Touch constructor exists and creates a Touch object with minimum properties
+Fail Touch constructor exists and creates a Touch object with requested properties
+Fail TouchEvent constructor exists and creates a TouchEvent object with requested properties
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/touch-events/support/touch.js b/Tests/LibWeb/Text/input/wpt-import/touch-events/support/touch.js
new file mode 100644
index 00000000000..af0fefb6700
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/touch-events/support/touch.js
@@ -0,0 +1,117 @@
+// Check a Touch object's attributes for existence and correct type
+// TA: 1.1.2, 1.1.3
+function check_Touch_object(t) {
+ assert_equals(Object.prototype.toString.call(t), "[object Touch]", "touch is of type Touch");
+ [
+ ["long", "identifier"],
+ ["EventTarget", "target"],
+ ["long", "screenX"],
+ ["long", "screenY"],
+ ["long", "clientX"],
+ ["long", "clientY"],
+ ["long", "pageX"],
+ ["long", "pageY"],
+ ["long", "radiusX"],
+ ["long", "radiusY"],
+ ["long", "rotationAngle"],
+ ["long", "force"],
+ ].forEach(function(attr) {
+ var type = attr[0];
+ var name = attr[1];
+
+ // existence check
+ assert_true(name in t, name + " attribute in Touch object");
+
+ // type check
+ switch (type) {
+ case "long":
+ assert_equals(typeof t[name], "number", name + " attribute of type long");
+ break;
+ case "EventTarget":
+ // An event target is some type of Element
+ assert_true(t[name] instanceof Element, "EventTarget must be an Element.");
+ break;
+ default:
+ break;
+ }
+ });
+}
+
+// Check a TouchList object's attributes and methods for existence and proper type
+// Also make sure all of the members of the list are Touch objects
+// TA: 1.2.1, 1.2.2, 1.2.5, 1.2.6
+function check_TouchList_object(tl) {
+ assert_equals(Object.prototype.toString.call(tl), "[object TouchList]", "touch list is of type TouchList");
+ [
+ ["unsigned long", "length"],
+ ["function", "item"],
+ ].forEach(function(attr) {
+ var type = attr[0];
+ var name = attr[1];
+
+ // existence check
+ assert_true(name in tl, name + " attribute in TouchList");
+
+ // type check
+ switch (type) {
+ case "unsigned long":
+ assert_equals(typeof tl[name], "number", name + " attribute of type long");
+ break;
+ case "function":
+ assert_equals(typeof tl[name], "function", name + " attribute of type function");
+ break;
+ default:
+ break;
+ }
+ });
+ // Each member of tl should be a proper Touch object
+ for (var i = 0; i < tl.length; i++) {
+ check_Touch_object(tl.item(i));
+ }
+ // TouchList.item(x) should return null if x is >= TouchList.length
+ var t = tl.item(tl.length);
+ assert_equals(t, null, "TouchList.item returns null if the index is >= the length of the list");
+}
+
+// Check a TouchEvent event's attributes for existence and proper type
+// Also check that each of the event's TouchList objects are valid
+// TA: 1.{3,4,5}.1.1, 1.{3,4,5}.1.2
+function check_TouchEvent(ev) {
+ assert_true(ev instanceof TouchEvent, ev.type + " event is a TouchEvent event");
+ [
+ ["TouchList", "touches"],
+ ["TouchList", "targetTouches"],
+ ["TouchList", "changedTouches"],
+ ["boolean", "altKey"],
+ ["boolean", "metaKey"],
+ ["boolean", "ctrlKey"],
+ ["boolean", "shiftKey"],
+ ].forEach(function(attr) {
+ var type = attr[0];
+ var name = attr[1];
+ // existence check
+ assert_true(name in ev, name + " attribute in " + ev.type + " event");
+ // type check
+ switch (type) {
+ case "boolean":
+ assert_equals(typeof ev[name], "boolean", name + " attribute of type boolean");
+ break;
+ case "TouchList":
+ assert_equals(Object.prototype.toString.call(ev[name]), "[object TouchList]", name + " attribute of type TouchList");
+ break;
+ default:
+ break;
+ }
+ });
+}
+
+// This chromium-specific helper is a no-op to other user-agents. It can be used
+// to ensure that chromium's input-handling compositor thread is ready before
+// touch-related test logic proceeds.
+// TODO(crbug.com/41481669): This shouldn't be necessary if the test harness
+// deferred running the tests until after paint holding.
+async function waitTillReadyForTouchInput() {
+ const animation =
+ document.body.animate({ opacity: [ 0, 1 ] }, {duration: 1 });
+ return animation.finished;
+}
diff --git a/Tests/LibWeb/Text/input/wpt-import/touch-events/touch-touchevent-constructor.html b/Tests/LibWeb/Text/input/wpt-import/touch-events/touch-touchevent-constructor.html
new file mode 100644
index 00000000000..96b7d4231e3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/touch-events/touch-touchevent-constructor.html
@@ -0,0 +1,145 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+<title>Touch and TouchEvent Constructor Tests</title>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="support/touch.js"></script>
+</head>
+<body>
+<div id="target0"></div>
+<script>
+test(function() {
+ var testIdentifier = 0;
+ var testTarget = document.getElementById('target0');
+
+ assert_throws_js(TypeError, function() {new Touch();}, "Touch constructor with no argument");
+ assert_throws_js(TypeError, function() {new Touch(null);}, "Touch constructor with null argument");
+ assert_throws_js(TypeError, function() {new Touch(undefined);}, "Touch constructor with undefined argument");
+ assert_throws_js(TypeError, function() {new Touch({});}, "Touch constructor with empty object");
+ assert_throws_js(TypeError, function() {new Touch({
+ identifier: testIdentifier
+ });}, "Touch constructor with only identifier");
+ assert_throws_js(TypeError, function() {new Touch({
+ target: testTarget
+ });}, "Touch constructor with only target");
+}, "Touch constructor with insufficient properties");
+
+test(function() {
+ var testIdentifier = 0;
+ var testTarget = document.getElementById('target0');
+
+ assert_throws_js(TypeError, function() {new Touch({
+ identifier: testIdentifier,
+ target: null
+ });}, "Touch constructor with null target");
+ assert_throws_js(TypeError, function() {new Touch({
+ identifier: testIdentifier,
+ target: undefined
+ });}, "Touch constructor with undefined target");
+ assert_throws_js(TypeError, function() {new Touch({
+ identifier: testIdentifier,
+ target: location
+ });}, "Touch constructor with Location target");
+}, "Touch constructor with non-EventTarget target");
+
+test(function() {
+ var testIdentifier = 74;
+ var testTarget = document.getElementById('target0');
+ var approxEpsilon = 0.00001;
+
+ var touch1 = new Touch({
+ identifier: testIdentifier,
+ target: testTarget,
+ });
+ check_Touch_object(touch1);
+ assert_equals(touch1.target, testTarget, "touch.target is requested value");
+ assert_equals(touch1.identifier, testIdentifier, "touch.identifier is requested value");
+ assert_approx_equals(touch1.pageX, 0, approxEpsilon, "touch.pageX is default value");
+ assert_approx_equals(touch1.pageY, 0, approxEpsilon, "touch.pageY is default value");
+ assert_approx_equals(touch1.screenX, 0, approxEpsilon, "touch.screenX is default value");
+ assert_approx_equals(touch1.screenY, 0, approxEpsilon, "touch.screenY is default value");
+ assert_approx_equals(touch1.clientX, 0, approxEpsilon, "touch.clientX is default value.");
+ assert_approx_equals(touch1.clientY, 0, approxEpsilon, "touch.clientY is default value.");
+}, "Touch constructor exists and creates a Touch object with minimum properties");
+
+test(function() {
+ var testIdentifier = 42;
+ var testTarget = document.getElementById('target0');
+ var testPageX = 15;
+ var testPageY = 20.2;
+ var testScreenX = 35.34;
+ var testScreenY = 40.56;
+ var testClientX = 10.175;
+ var testClientY = 5;
+ var approxEpsilon = 0.00001;
+
+ var touch1 = new Touch({
+ identifier: testIdentifier,
+ target: testTarget,
+ pageX: testPageX,
+ pageY: testPageY,
+ screenX: testScreenX,
+ screenY: testScreenY,
+ clientX: testClientX,
+ clientY: testClientY,
+ });
+ check_Touch_object(touch1);
+ assert_equals(touch1.identifier, testIdentifier, "touch.identifier is requested value");
+ assert_equals(touch1.target, testTarget, "touch.target is requested value");
+ assert_approx_equals(touch1.pageX, testPageX, approxEpsilon, "touch.pageX is requested value");
+ assert_approx_equals(touch1.pageY, testPageY, approxEpsilon, "touch.pageY is requested value");
+ assert_approx_equals(touch1.screenX, testScreenX, approxEpsilon, "touch.screenX is requested value");
+ assert_approx_equals(touch1.screenY, testScreenY, approxEpsilon, "touch.screenY is requested value");
+ assert_approx_equals(touch1.clientX, testClientX, approxEpsilon, "touch.clientX is requested value.");
+ assert_approx_equals(touch1.clientY, testClientY, approxEpsilon, "touch.clientY is requested value.");
+}, "Touch constructor exists and creates a Touch object with requested properties");
+
+
+test(function() {
+ var testTarget = document.getElementById('target0');
+
+ var touch1 = new Touch({
+ identifier: 45,
+ target: testTarget,
+ pageX: 45,
+ pageY: 50,
+ screenX: 65,
+ screenY: 60,
+ clientX: 70,
+ clientY: 75,
+ });
+ var touch2 = new Touch({
+ identifier: 52,
+ target: testTarget,
+ pageX: 15,
+ pageY: 20,
+ screenX: 15,
+ screenY: 20,
+ clientX: 15,
+ clientY: 20,
+ });
+
+ var touchEvent1 = new TouchEvent("ontouchstart", {
+ touches: [touch1, touch2],
+ targetTouches: [touch1],
+ altKey: true,
+ metaKey: false,
+ });
+
+ check_TouchEvent(touchEvent1);
+ assert_equals(touchEvent1.type, "ontouchstart", "touchEvent.type is requested value");
+ assert_equals(touchEvent1.touches.length, 2, "touchEvent.touches.length is requested value");
+ assert_equals(touchEvent1.touches[0], touch1, "touchEvent.touches[0] is requested value");
+ assert_equals(touchEvent1.touches[1], touch2, "touchEvent.touches[1] is requested value");
+ assert_equals(touchEvent1.targetTouches.length, 1, "touchEvent.targetTouches.length is requested value");
+ assert_equals(touchEvent1.targetTouches[0], touch1, "touchEvent.targetTouches[0] is requested value");
+ assert_equals(touchEvent1.changedTouches.length, 0, "touchEvent.changedTouches.length is requested value");
+ assert_equals(touchEvent1.altKey, true, "touchEvent.altKey is requested value");
+ assert_equals(touchEvent1.metaKey, false, "touchEvent.metaKey is requested value");
+ assert_equals(touchEvent1.ctrlKey, false, "touchEvent.ctrlKey is requested value");
+ assert_equals(touchEvent1.shiftKey, false, "touchEvent.shiftKey is requested value.");
+}, "TouchEvent constructor exists and creates a TouchEvent object with requested properties");
+</script>
+</body>
+</html>