tests/touch-events-v1/submissions/Nokia/create-touch-touchlist.html
changeset 163 e5a493429528
child 195 de5bcc4200eb
equal deleted inserted replaced
162:cde6c06f4579 163:e5a493429528
       
     1 <!DOCTYPE HTML>
       
     2 <html>
       
     3 <!-- 
       
     4     Test cases for Touch Events v1 Candiate Recommendation
       
     5     http://www.w3.org/TR/2011/CR-touch-events-20111215/
       
     6 
       
     7 	These tests are based on Mozilla-Nokia-Google's single-touch tests. 
       
     8 	http://w3c-test.org/webevents/tests/touch-events-v1/submissions/Moz-Nok-Goog/single-touch.html
       
     9 
       
    10 	The primary purpose of the tests in this document is checking that the createTouch and createTouchList interfaces of the Touch Events APIs are correctly implemented.
       
    11 	Other interactions are covered in other test files.
       
    12 
       
    13     This document references Test Assertions (abbrev TA below) written by Cathy Chan
       
    14     http://www.w3.org/2010/webevents/wiki/TestAssertions
       
    15 -->
       
    16 
       
    17 <head>
       
    18 <title>Touch Events createTouch and createTouchList Interface Tests</title>
       
    19 <meta name="viewport" content="width=device-width">
       
    20 <script src="../../../testharness.js"></script>
       
    21 <script>
       
    22 	setup({explicit_done: true});
       
    23 
       
    24 	var hasIdentifiedTouch = true;
       
    25 
       
    26 	// Check a Touch object's atttributes for existence and correct type
       
    27 	// TA: 1.1.2, 1.1.3
       
    28 	function check_Touch_object (t) {
       
    29 		test(function() {
       
    30 			assert_equals(Object.prototype.toString.call(t), "[object Touch]", "touch is of type Touch");
       
    31 		}, "touch point is a Touch object");
       
    32 		[
       
    33 			["long", "identifier"],
       
    34 			["EventTarget", "target"],
       
    35 			["long", "screenX"],
       
    36 			["long", "screenY"],
       
    37 			["long", "clientX"],
       
    38 			["long", "clientY"],
       
    39 			["long", "pageX"],
       
    40 			["long", "pageY"],
       
    41 		].forEach(function(attr) {
       
    42 			var type = attr[0];
       
    43 			var name = attr[1];
       
    44 
       
    45 			// existence check
       
    46 			test(function() {
       
    47 				assert_true(name in t, name + " attribute in Touch object");
       
    48 			}, "Touch." + name + " attribute exists");
       
    49 
       
    50 			// type check
       
    51 			switch(type) {
       
    52 			case "long":
       
    53 				test(function() {
       
    54 					assert_equals(typeof t[name], "number", name + " attribute of type long");
       
    55 				}, "Touch." + name + " attribute is of type number (long)");
       
    56 				break;
       
    57 			case "EventTarget":
       
    58 				// An event target is some type of Element
       
    59 				test(function() {
       
    60 					assert_true(t[name] instanceof Element, "EventTarget must be an Element.");
       
    61 				}, "Touch." + name + " attribute is of type Element");
       
    62 				break;
       
    63 			default:
       
    64 				break;
       
    65 			}
       
    66 		}); 
       
    67 	}
       
    68 
       
    69 	// Check a TouchList object's attributes and methods for existence and proper type
       
    70 	// Also make sure all of the members of the list are Touch objects
       
    71 	// TA: 1.2.1, 1.2.2, 1.2.5, 1.2.6
       
    72 	function check_TouchList_object (tl) {
       
    73 		test(function() {
       
    74 			assert_equals(Object.prototype.toString.call(tl), "[object TouchList]", "touch list is of type TouchList");
       
    75 		}, "touch list is a TouchList object");
       
    76 		test(function() {
       
    77 			// A common failure mode (eg. WebKit bug 96294), don't report multiple errors for it
       
    78 			hasIdentifiedTouch = 'identifiedTouch' in tl;
       
    79 			assert_true(hasIdentifiedTouch);
       
    80 		}, "TouchList contains an identifiedTouch property");
       
    81 		[
       
    82 			["unsigned long", "length"],
       
    83 			["function", "item"],
       
    84 			["function", "identifiedTouch"],
       
    85 		].forEach(function(attr) {
       
    86 			var type = attr[0];
       
    87 			var name = attr[1];
       
    88 
       
    89 			if (name == "identifiedTouch" && !hasIdentifiedTouch)
       
    90 				return;
       
    91 			  
       
    92 			// existence check
       
    93 			test(function() {
       
    94 				assert_true(name in tl, name + " attribute in TouchList");
       
    95 			}, "TouchList." + name + " attribute exists");
       
    96 
       
    97 			// type check
       
    98 			switch(type) {
       
    99 			case "unsigned long":
       
   100 				test(function() {
       
   101 					assert_equals(typeof tl[name], "number", name + " attribute of type long");
       
   102 				}, "TouchList." + name + " attribute is of type number (unsigned long)");
       
   103 				break;
       
   104 			case "function":
       
   105 				test(function() {
       
   106 					assert_equals(typeof tl[name], "function", name + " attribute of type long");
       
   107 				}, "TouchList." + name + " attribute is of type function");
       
   108 				break;
       
   109 			default:
       
   110 				break;
       
   111 			}
       
   112 		}); 
       
   113 		// Each member of tl should be a proper Touch object
       
   114 		for (var i=0; i < tl.length; i++) {
       
   115 			check_Touch_object(tl.item(i));
       
   116 		}
       
   117 		// identifiedTouch should return the member Touch object with the specified identifier
       
   118 		test(function() {
       
   119 			if (hasIdentifiedTouch) {
       
   120 				for (var i=0; i < tl.length; i++) {
       
   121 					assert_equals(tl.item(i), tl.identifiedTouch(tl.item(i).identifier), "TouchList.identifiedTouch returns the correct item");
       
   122 				}
       
   123 			}
       
   124 		}, "TouchList.identifiedTouch returns the correct item");
       
   125 	}
       
   126 
       
   127     function check_touch_clientXY(touch) {
       
   128 		assert_equals(touch.clientX, touch.pageX - window.pageXOffset, "touch.clientX is touch.pageX - window.pageXOffset.");
       
   129 		assert_equals(touch.clientY, touch.pageY - window.pageYOffset, "touch.clientY is touch.pageY - window.pageYOffset.");
       
   130     }
       
   131 
       
   132 	function run() {
       
   133 		var target0 = document.getElementById("target0");
       
   134 		var touch1, touch2;
       
   135 
       
   136 		test(function() {
       
   137 			touch1 = document.createTouch(window, target0, 42, 15, 20, 35, 40);
       
   138 			assert_equals(touch1.target, target0, "touch.target is target0");
       
   139 			assert_equals(touch1.identifier, 42, "touch.identifier is requested value");
       
   140 			assert_equals(touch1.pageX, 15, "touch.pageX is requested value");
       
   141 			assert_equals(touch1.pageY, 20, "touch.pageY is requested value");
       
   142 			check_touch_clientXY(touch1);
       
   143 			assert_equals(touch1.screenX, 35, "touch.screenX is requested value");
       
   144 			assert_equals(touch1.screenY, 40, "touch.screenY is requested value");
       
   145 		}, "document.createTouch exists and creates a Touch object with requested properties");
       
   146 
       
   147 		touch2 = document.createTouch(window, target0, 44, 25, 30, 45, 50);
       
   148 
       
   149 		var touchList;
       
   150 
       
   151 		test(function() {
       
   152 			touchList = document.createTouchList(touch1);
       
   153 			assert_equals(touchList.length, 1, "touchList.length is 1");
       
   154 			assert_equals(touchList.item(0), touch1, "touchList.item(0) is input touch1");
       
   155 		}, "document.createTouchList exists and correctly creates a TouchList from a single Touch");
       
   156 
       
   157 		if (touchList)
       
   158 			check_TouchList_object(touchList);
       
   159 
       
   160 		test(function() {
       
   161 			touchList = document.createTouchList([touch1]);
       
   162 			assert_equals(touchList.length, 1, "touchList.length is 1");
       
   163 			assert_equals(touchList.item(0), touch1, "touchList.item(0) is input touch1");
       
   164 			if (touchList)
       
   165 				check_TouchList_object(touchList);
       
   166 		}, "document.createTouchList exists and correctly creates a TouchList from a Touch array of 1 item");
       
   167 		
       
   168 		test(function() {
       
   169 			touchList = document.createTouchList([touch1, touch2]);
       
   170 			assert_equals(touchList.length, 2, "touchList.length is 2");
       
   171 			assert_equals(touchList.item(0), touch1, "touchList.item(0) is input touch1");
       
   172 			assert_equals(touchList.item(1), touch2, "touchList.item(1) is input touch2");
       
   173 			if (touchList)
       
   174 				check_TouchList_object(touchList);
       
   175 		}, "document.createTouchList exists and correctly creates a TouchList from a Touch array of 2 items");
       
   176 
       
   177 		target0.innerHTML = "Test complete."
       
   178 		done();
       
   179 	}
       
   180 </script>
       
   181 <style>
       
   182 	div {
       
   183 		margin: 0em;
       
   184 		padding: 2em;
       
   185 	}
       
   186     #target0 {
       
   187 		background: yellow;
       
   188 		border: 1px solid orange;
       
   189     }
       
   190 </style>
       
   191 </head>
       
   192 <body onload="run()">
       
   193 	<h1>Touch Events: createTouch and createTouchList tests</h1>
       
   194 	<div id="target0">Please wait for test to complete...</div>
       
   195 	<div id="log"></div>
       
   196 </body>
       
   197 </html>