Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / fast / events / touch / resources / compositor-touch-hit-rects.js
1 function listener() {
2 }
3
4 function log(msg) {
5     var span = document.createElement("span");
6     document.getElementById("console").appendChild(span);
7     span.innerHTML = msg + '<br />';
8 }
9
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;
17 }
18
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);
26 }
27
28 var preRunHandlerForTest = {};
29
30 function testElement(element) {
31     element.addEventListener('touchstart', listener, false);
32
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);
37
38     logRects(element.id);
39
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);
44 }
45
46 function logRects(testName, opt_noOverlay) {
47     if (!window.internals) {
48         log(testName + ': not run');
49         return;
50     }
51
52     var rects = window.internals.touchEventTargetLayerRects(document);
53     if (rects.length == 0)
54         log(testName + ': no rects');
55
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 + ")");
72
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";
80
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;
97                 }
98                 if (x || y) {
99                     container.style.top = y + "px";
100                     container.style.left = x + "px";
101                 }
102                 container.classList.add("display-when-done");
103                 container.appendChild(patch);
104             }
105         }
106     }
107
108     log('');
109 }
110
111 function checkForRectUpdate(expectUpdate, operation) {
112     if (window.internals)
113         var oldCount = window.internals
114                 .touchEventTargetLayerRectsUpdateCount(document);
115
116     operation();
117
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'));
123     }
124 }
125
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;
130
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;
142 }
143
144 if (window.internals) {
145     window.internals.settings.setMockScrollbarsEnabled(true);
146     window.internals.settings.setForceCompositingMode(true);
147 }
148
149 window.onload = function() {
150     // Run each general test case.
151     var tests = document.querySelectorAll('.testcase');
152     for ( var i = 0; i < tests.length; i++) {
153         // Force a compositing update before testing each case to ensure that
154         // any subsequent touch rect updates are actually done because of
155         // the event handler changes in the test itself.
156         if (window.internals)
157             window.internals.forceCompositingUpdate(document);
158         testElement(tests[i]);
159     }
160
161     if (window.additionalTests)
162         additionalTests();
163
164     if (!visualize && window.internals) {
165         var testContainer = document.getElementById("tests");
166         testContainer.parentNode.removeChild(testContainer);
167     }
168
169     document.documentElement.setAttribute('done', 'true');
170 };