michael@0: var checkClipRegion, checkClipRegionForFrame, checkClipRegionNoBounds; michael@0: michael@0: (function() { michael@0: var windowFrameX, windowFrameY; michael@0: michael@0: // Import test API michael@0: var is = window.opener.is; michael@0: var ok = window.opener.ok; michael@0: var todo = window.opener.todo; michael@0: var finish = window.opener.SimpleTest.finish; michael@0: window.onerror = function (event) { window.opener.onerror(event); window.close(); }; michael@0: michael@0: function dumpRect(r) { michael@0: return "{" + r.join(",") + "}"; michael@0: } michael@0: michael@0: function dumpRegion(rects) { michael@0: var s = []; michael@0: for (var i = 0; i < rects.length; ++i) { michael@0: var r = rects[i]; michael@0: s.push(dumpRect(r)); michael@0: } michael@0: return s.join(", "); michael@0: } michael@0: michael@0: function generateSpan(coords) { michael@0: coords.sort(function(a,b) { return a - b; }); michael@0: var result = [coords[0]]; michael@0: for (var i = 1; i < coords.length; ++i) { michael@0: if (coords[i] != coords[i - 1]) { michael@0: result.push(coords[i]); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: function containsRect(r1, r2) { michael@0: return r1[0] <= r2[0] && r1[2] >= r2[2] && michael@0: r1[1] <= r2[1] && r1[3] >= r2[3]; michael@0: } michael@0: michael@0: function subtractRect(r1, r2, rlist) { michael@0: var spanX = generateSpan([r1[0], r1[2], r2[0], r2[2]]); michael@0: var spanY = generateSpan([r1[1], r1[3], r2[1], r2[3]]); michael@0: for (var i = 1; i < spanX.length; ++i) { michael@0: for (var j = 1; j < spanY.length; ++j) { michael@0: var subrect = [spanX[i - 1], spanY[j - 1], spanX[i], spanY[j]]; michael@0: if (containsRect(r1, subrect) && !containsRect(r2, subrect)) { michael@0: rlist.push(subrect); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: function regionContainsRect(rs, r) { michael@0: var rectList = [r]; michael@0: for (var i = 0; i < rs.length; ++i) { michael@0: var newList = []; michael@0: for (var j = 0; j < rectList.length; ++j) { michael@0: subtractRect(rectList[j], rs[i], newList); michael@0: } michael@0: if (newList.length == 0) michael@0: return true; michael@0: rectList = newList; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: function regionContains(r1s, r2s) { michael@0: for (var i = 0; i < r2s.length; ++i) { michael@0: if (!regionContainsRect(r1s, r2s[i])) michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: function equalRegions(r1s, r2s) { michael@0: return regionContains(r1s, r2s) && regionContains(r2s, r1s); michael@0: } michael@0: michael@0: // Checks that a plugin's clip region equals the specified rectangle list. michael@0: // The rectangles are given relative to the plugin's top-left. They are in michael@0: // [left, top, right, bottom] format. michael@0: function checkClipRegionWithDoc(doc, offsetX, offsetY, id, rects, checkBounds) { michael@0: var p = doc.getElementById(id); michael@0: var bounds = p.getBoundingClientRect(); michael@0: var pX = p.getEdge(0); michael@0: var pY = p.getEdge(1); michael@0: michael@0: if (checkBounds) { michael@0: var pWidth = p.getEdge(2) - pX; michael@0: var pHeight = p.getEdge(3) - pY; michael@0: michael@0: is(pX, windowFrameX + bounds.left + offsetX, id + " plugin X"); michael@0: is(pY, windowFrameY + bounds.top + offsetY, id + " plugin Y"); michael@0: is(pWidth, bounds.width, id + " plugin width"); michael@0: is(pHeight, bounds.height, id + " plugin height"); michael@0: } michael@0: michael@0: // Now check clip region. 'rects' is relative to the plugin's top-left. michael@0: var clipRects = []; michael@0: var n = p.getClipRegionRectCount(); michael@0: for (var i = 0; i < n; ++i) { michael@0: // Convert the clip rect to be relative to the plugin's top-left. michael@0: clipRects[i] = [ michael@0: p.getClipRegionRectEdge(i, 0) - pX, michael@0: p.getClipRegionRectEdge(i, 1) - pY, michael@0: p.getClipRegionRectEdge(i, 2) - pX, michael@0: p.getClipRegionRectEdge(i, 3) - pY michael@0: ]; michael@0: } michael@0: michael@0: ok(equalRegions(clipRects, rects), "Matching regions for '" + id + michael@0: "': expected " + dumpRegion(rects) + ", got " + dumpRegion(clipRects)); michael@0: } michael@0: michael@0: checkClipRegion = function checkClipRegion(id, rects) { michael@0: checkClipRegionWithDoc(document, 0, 0, id, rects, true); michael@0: } michael@0: michael@0: checkClipRegionForFrame = function checkClipRegionForFrame(fid, id, rects) { michael@0: var f = document.getElementById(fid); michael@0: var bounds = f.getBoundingClientRect(); michael@0: checkClipRegionWithDoc(f.contentDocument, bounds.left, bounds.top, id, rects, true); michael@0: } michael@0: michael@0: checkClipRegionNoBounds = function checkClipRegionNoBounds(id, rects) { michael@0: checkClipRegionWithDoc(document, 0, 0, id, rects, false); michael@0: } michael@0: michael@0: function loaded() { michael@0: var h1 = document.getElementById("h1"); michael@0: var h2 = document.getElementById("h2"); michael@0: var hwidth = h2.boxObject.screenX - h1.boxObject.screenX; michael@0: if (hwidth != 100) { michael@0: // Maybe it's a DPI issue michael@0: todo(false, "Unexpected DPI?"); michael@0: finish(); michael@0: window.close(); michael@0: return; michael@0: } michael@0: michael@0: if (!document.getElementById("p1").identifierToStringTest) { michael@0: todo(false, "Test plugin not available"); michael@0: finish(); michael@0: window.close(); michael@0: return; michael@0: } michael@0: michael@0: var bounds = h1.getBoundingClientRect(); michael@0: windowFrameX = h1.boxObject.screenX - bounds.left - window.screenX; michael@0: windowFrameY = h1.boxObject.screenY - bounds.top - window.screenY; michael@0: michael@0: // Run actual test code michael@0: runTests(); michael@0: } michael@0: michael@0: // Need to run 'loaded' after painting is unsuppressed, or we'll set clip michael@0: // regions to empty. The timeout must be non-zero on X11 so that michael@0: // gtk_container_idle_sizer runs after the GtkSocket gets the plug_window. michael@0: window.addEventListener("load", michael@0: function () { setTimeout(loaded, 1000); }, false); michael@0: })();