Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / events / touch / resources / compositor-touch-hit-rects.js
blobafca5dbc50b20cc3d2936750c18775d9ae08ba9d
1 function listener() {
4 function log(msg) {
5 var span = document.createElement("span");
6 document.getElementById("console").appendChild(span);
7 span.innerHTML = msg + '<br />';
10 function nameForNode(node) {
11 if (!node)
12 return "[unknown-node]";
13 var name = node.nodeName;
14 if (node.id)
15 name += '#' + node.id;
16 return name;
19 function sortRects(a, b) {
20 return a.layerRelativeRect.top - b.layerRelativeRect.top
21 || a.layerRelativeRect.left - b.layerRelativeRect.left
22 || a.layerRelativeRect.width - b.layerRelativeRect.width
23 || a.layerRelativeRect.height - b.layerRelativeRect.right
24 || nameForNode(a.layerAssociatedNode).localeCompare(nameForNode(b.layerAssociatedNode))
25 || a.layerType.localeCompare(b.layerType);
28 var preRunHandlerForTest = {};
30 function testElement(element) {
31 element.addEventListener('touchstart', listener, false);
33 // Run any test-specific handler AFTER adding the touch event listener
34 // (which itself causes rects to be recomputed).
35 if (element.id in preRunHandlerForTest)
36 preRunHandlerForTest[element.id](element);
38 logRects(element.id);
40 // If we're running manually, leave the handlers in place so the user
41 // can use dev tools 'show potential scroll bottlenecks' for visualization.
42 if (window.internals)
43 element.removeEventListener('touchstart', listener, false);
46 function logRects(testName, opt_noOverlay) {
47 if (!window.internals) {
48 log(testName + ': not run');
49 return;
52 var rects = window.internals.touchEventTargetLayerRects(document);
53 if (rects.length == 0)
54 log(testName + ': no rects');
56 var sortedRects = new Array();
57 for ( var i = 0; i < rects.length; ++i)
58 sortedRects[i] = rects[i];
59 sortedRects.sort(sortRects);
60 for ( var i = 0; i < sortedRects.length; ++i) {
61 var node = sortedRects[i].layerAssociatedNode;
62 var r = sortedRects[i].layerRelativeRect;
63 var nameSuffix = "";
64 if (sortedRects[i].layerType)
65 nameSuffix += " " + sortedRects[i].layerType;
66 var offsetX = sortedRects[i].associatedNodeOffsetX;
67 var offsetY = sortedRects[i].associatedNodeOffsetY;
68 if (offsetX || offsetY)
69 nameSuffix += "[" + offsetX + "," + offsetY + "]"
70 log(testName + ": " + nameForNode(node) + nameSuffix + " ("
71 + r.left + ", " + r.top + ", " + r.width + ", " + r.height + ")");
73 if (visualize && node && !opt_noOverlay && window.location.hash != '#nooverlay') {
74 var patch = document.createElement("div");
75 patch.className = "overlay generated display-when-done";
76 patch.style.left = r.left + "px";
77 patch.style.top = r.top + "px";
78 patch.style.width = r.width + "px";
79 patch.style.height = r.height + "px";
81 if (node === document) {
82 patch.style.position = "absolute";
83 document.body.appendChild(patch);
84 } else {
85 // Use a zero-size container to avoid changing the position of
86 // the existing elements.
87 var container = document.createElement("div");
88 container.className = "overlay-container generated";
89 patch.style.position = "relative";
90 node.appendChild(container);
91 var x = -offsetX;
92 var y = -offsetY;
93 if (container.offsetParent != node) {
94 // Assume container.offsetParent == node.offsetParent
95 y += node.offsetTop - container.offsetTop;
96 x += node.offsetLeft - container.offsetLeft;
98 if (x || y) {
99 container.style.top = y + "px";
100 container.style.left = x + "px";
102 container.classList.add("display-when-done");
103 container.appendChild(patch);
108 log('');
111 function checkForRectUpdate(expectUpdate, operation) {
112 if (window.internals)
113 var oldCount = window.internals
114 .touchEventTargetLayerRectsUpdateCount(document);
116 operation();
118 if (window.internals) {
119 var newCount = window.internals
120 .touchEventTargetLayerRectsUpdateCount(document);
121 if ((oldCount != newCount) != !!expectUpdate)
122 log('FAIL: ' + (expectUpdate ? 'rects not updated' : 'rects updated unexpectedly'));
126 // Set this to true in order to visualize the results in an image.
127 // Elements that are expected to be included in hit rects have a red border.
128 // The actual hit rects are in a green tranlucent overlay.
129 var visualize = false;
131 if (window.testRunner) {
132 if (visualize)
133 window.testRunner.dumpAsTextWithPixelResults();
134 else
135 window.testRunner.dumpAsText();
136 document.documentElement.setAttribute('dumpRenderTree', 'true');
137 } else {
138 // Note, this test can be run interactively in content-shell with
139 // --expose-internals-for-testing. In that case we almost certainly
140 // want to visualize the results.
141 visualize = true;
144 if (window.internals) {
145 window.internals.settings.setMockScrollbarsEnabled(true);
148 window.onload = function() {
149 // Run each general test case.
150 var tests = document.querySelectorAll('.testcase');
151 for ( var i = 0; i < tests.length; i++) {
152 // Force a compositing update before testing each case to ensure that
153 // any subsequent touch rect updates are actually done because of
154 // the event handler changes in the test itself.
155 if (window.internals)
156 window.internals.forceCompositingUpdate(document);
157 testElement(tests[i]);
160 if (window.additionalTests)
161 additionalTests();
163 if (!visualize && window.internals) {
164 var testContainer = document.getElementById("tests");
165 testContainer.parentNode.removeChild(testContainer);
168 document.documentElement.setAttribute('done', 'true');