1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/layout/generic/test/plugin_clipping_lib.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,163 @@ 1.4 +var checkClipRegion, checkClipRegionForFrame, checkClipRegionNoBounds; 1.5 + 1.6 +(function() { 1.7 +var windowFrameX, windowFrameY; 1.8 + 1.9 +// Import test API 1.10 +var is = window.opener.is; 1.11 +var ok = window.opener.ok; 1.12 +var todo = window.opener.todo; 1.13 +var finish = window.opener.SimpleTest.finish; 1.14 +window.onerror = function (event) { window.opener.onerror(event); window.close(); }; 1.15 + 1.16 +function dumpRect(r) { 1.17 + return "{" + r.join(",") + "}"; 1.18 +} 1.19 + 1.20 +function dumpRegion(rects) { 1.21 + var s = []; 1.22 + for (var i = 0; i < rects.length; ++i) { 1.23 + var r = rects[i]; 1.24 + s.push(dumpRect(r)); 1.25 + } 1.26 + return s.join(", "); 1.27 +} 1.28 + 1.29 +function generateSpan(coords) { 1.30 + coords.sort(function(a,b) { return a - b; }); 1.31 + var result = [coords[0]]; 1.32 + for (var i = 1; i < coords.length; ++i) { 1.33 + if (coords[i] != coords[i - 1]) { 1.34 + result.push(coords[i]); 1.35 + } 1.36 + } 1.37 + return result; 1.38 +} 1.39 + 1.40 +function containsRect(r1, r2) { 1.41 + return r1[0] <= r2[0] && r1[2] >= r2[2] && 1.42 + r1[1] <= r2[1] && r1[3] >= r2[3]; 1.43 +} 1.44 + 1.45 +function subtractRect(r1, r2, rlist) { 1.46 + var spanX = generateSpan([r1[0], r1[2], r2[0], r2[2]]); 1.47 + var spanY = generateSpan([r1[1], r1[3], r2[1], r2[3]]); 1.48 + for (var i = 1; i < spanX.length; ++i) { 1.49 + for (var j = 1; j < spanY.length; ++j) { 1.50 + var subrect = [spanX[i - 1], spanY[j - 1], spanX[i], spanY[j]]; 1.51 + if (containsRect(r1, subrect) && !containsRect(r2, subrect)) { 1.52 + rlist.push(subrect); 1.53 + } 1.54 + } 1.55 + } 1.56 +} 1.57 + 1.58 +function regionContainsRect(rs, r) { 1.59 + var rectList = [r]; 1.60 + for (var i = 0; i < rs.length; ++i) { 1.61 + var newList = []; 1.62 + for (var j = 0; j < rectList.length; ++j) { 1.63 + subtractRect(rectList[j], rs[i], newList); 1.64 + } 1.65 + if (newList.length == 0) 1.66 + return true; 1.67 + rectList = newList; 1.68 + } 1.69 + return false; 1.70 +} 1.71 + 1.72 +function regionContains(r1s, r2s) { 1.73 + for (var i = 0; i < r2s.length; ++i) { 1.74 + if (!regionContainsRect(r1s, r2s[i])) 1.75 + return false; 1.76 + } 1.77 + return true; 1.78 +} 1.79 + 1.80 +function equalRegions(r1s, r2s) { 1.81 + return regionContains(r1s, r2s) && regionContains(r2s, r1s); 1.82 +} 1.83 + 1.84 +// Checks that a plugin's clip region equals the specified rectangle list. 1.85 +// The rectangles are given relative to the plugin's top-left. They are in 1.86 +// [left, top, right, bottom] format. 1.87 +function checkClipRegionWithDoc(doc, offsetX, offsetY, id, rects, checkBounds) { 1.88 + var p = doc.getElementById(id); 1.89 + var bounds = p.getBoundingClientRect(); 1.90 + var pX = p.getEdge(0); 1.91 + var pY = p.getEdge(1); 1.92 + 1.93 + if (checkBounds) { 1.94 + var pWidth = p.getEdge(2) - pX; 1.95 + var pHeight = p.getEdge(3) - pY; 1.96 + 1.97 + is(pX, windowFrameX + bounds.left + offsetX, id + " plugin X"); 1.98 + is(pY, windowFrameY + bounds.top + offsetY, id + " plugin Y"); 1.99 + is(pWidth, bounds.width, id + " plugin width"); 1.100 + is(pHeight, bounds.height, id + " plugin height"); 1.101 + } 1.102 + 1.103 + // Now check clip region. 'rects' is relative to the plugin's top-left. 1.104 + var clipRects = []; 1.105 + var n = p.getClipRegionRectCount(); 1.106 + for (var i = 0; i < n; ++i) { 1.107 + // Convert the clip rect to be relative to the plugin's top-left. 1.108 + clipRects[i] = [ 1.109 + p.getClipRegionRectEdge(i, 0) - pX, 1.110 + p.getClipRegionRectEdge(i, 1) - pY, 1.111 + p.getClipRegionRectEdge(i, 2) - pX, 1.112 + p.getClipRegionRectEdge(i, 3) - pY 1.113 + ]; 1.114 + } 1.115 + 1.116 + ok(equalRegions(clipRects, rects), "Matching regions for '" + id + 1.117 + "': expected " + dumpRegion(rects) + ", got " + dumpRegion(clipRects)); 1.118 +} 1.119 + 1.120 +checkClipRegion = function checkClipRegion(id, rects) { 1.121 + checkClipRegionWithDoc(document, 0, 0, id, rects, true); 1.122 +} 1.123 + 1.124 +checkClipRegionForFrame = function checkClipRegionForFrame(fid, id, rects) { 1.125 + var f = document.getElementById(fid); 1.126 + var bounds = f.getBoundingClientRect(); 1.127 + checkClipRegionWithDoc(f.contentDocument, bounds.left, bounds.top, id, rects, true); 1.128 +} 1.129 + 1.130 +checkClipRegionNoBounds = function checkClipRegionNoBounds(id, rects) { 1.131 + checkClipRegionWithDoc(document, 0, 0, id, rects, false); 1.132 +} 1.133 + 1.134 +function loaded() { 1.135 + var h1 = document.getElementById("h1"); 1.136 + var h2 = document.getElementById("h2"); 1.137 + var hwidth = h2.boxObject.screenX - h1.boxObject.screenX; 1.138 + if (hwidth != 100) { 1.139 + // Maybe it's a DPI issue 1.140 + todo(false, "Unexpected DPI?"); 1.141 + finish(); 1.142 + window.close(); 1.143 + return; 1.144 + } 1.145 + 1.146 + if (!document.getElementById("p1").identifierToStringTest) { 1.147 + todo(false, "Test plugin not available"); 1.148 + finish(); 1.149 + window.close(); 1.150 + return; 1.151 + } 1.152 + 1.153 + var bounds = h1.getBoundingClientRect(); 1.154 + windowFrameX = h1.boxObject.screenX - bounds.left - window.screenX; 1.155 + windowFrameY = h1.boxObject.screenY - bounds.top - window.screenY; 1.156 + 1.157 + // Run actual test code 1.158 + runTests(); 1.159 +} 1.160 + 1.161 +// Need to run 'loaded' after painting is unsuppressed, or we'll set clip 1.162 +// regions to empty. The timeout must be non-zero on X11 so that 1.163 +// gtk_container_idle_sizer runs after the GtkSocket gets the plug_window. 1.164 +window.addEventListener("load", 1.165 + function () { setTimeout(loaded, 1000); }, false); 1.166 +})();