layout/generic/test/plugin_clipping_lib.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 var checkClipRegion, checkClipRegionForFrame, checkClipRegionNoBounds;
michael@0 2
michael@0 3 (function() {
michael@0 4 var windowFrameX, windowFrameY;
michael@0 5
michael@0 6 // Import test API
michael@0 7 var is = window.opener.is;
michael@0 8 var ok = window.opener.ok;
michael@0 9 var todo = window.opener.todo;
michael@0 10 var finish = window.opener.SimpleTest.finish;
michael@0 11 window.onerror = function (event) { window.opener.onerror(event); window.close(); };
michael@0 12
michael@0 13 function dumpRect(r) {
michael@0 14 return "{" + r.join(",") + "}";
michael@0 15 }
michael@0 16
michael@0 17 function dumpRegion(rects) {
michael@0 18 var s = [];
michael@0 19 for (var i = 0; i < rects.length; ++i) {
michael@0 20 var r = rects[i];
michael@0 21 s.push(dumpRect(r));
michael@0 22 }
michael@0 23 return s.join(", ");
michael@0 24 }
michael@0 25
michael@0 26 function generateSpan(coords) {
michael@0 27 coords.sort(function(a,b) { return a - b; });
michael@0 28 var result = [coords[0]];
michael@0 29 for (var i = 1; i < coords.length; ++i) {
michael@0 30 if (coords[i] != coords[i - 1]) {
michael@0 31 result.push(coords[i]);
michael@0 32 }
michael@0 33 }
michael@0 34 return result;
michael@0 35 }
michael@0 36
michael@0 37 function containsRect(r1, r2) {
michael@0 38 return r1[0] <= r2[0] && r1[2] >= r2[2] &&
michael@0 39 r1[1] <= r2[1] && r1[3] >= r2[3];
michael@0 40 }
michael@0 41
michael@0 42 function subtractRect(r1, r2, rlist) {
michael@0 43 var spanX = generateSpan([r1[0], r1[2], r2[0], r2[2]]);
michael@0 44 var spanY = generateSpan([r1[1], r1[3], r2[1], r2[3]]);
michael@0 45 for (var i = 1; i < spanX.length; ++i) {
michael@0 46 for (var j = 1; j < spanY.length; ++j) {
michael@0 47 var subrect = [spanX[i - 1], spanY[j - 1], spanX[i], spanY[j]];
michael@0 48 if (containsRect(r1, subrect) && !containsRect(r2, subrect)) {
michael@0 49 rlist.push(subrect);
michael@0 50 }
michael@0 51 }
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 function regionContainsRect(rs, r) {
michael@0 56 var rectList = [r];
michael@0 57 for (var i = 0; i < rs.length; ++i) {
michael@0 58 var newList = [];
michael@0 59 for (var j = 0; j < rectList.length; ++j) {
michael@0 60 subtractRect(rectList[j], rs[i], newList);
michael@0 61 }
michael@0 62 if (newList.length == 0)
michael@0 63 return true;
michael@0 64 rectList = newList;
michael@0 65 }
michael@0 66 return false;
michael@0 67 }
michael@0 68
michael@0 69 function regionContains(r1s, r2s) {
michael@0 70 for (var i = 0; i < r2s.length; ++i) {
michael@0 71 if (!regionContainsRect(r1s, r2s[i]))
michael@0 72 return false;
michael@0 73 }
michael@0 74 return true;
michael@0 75 }
michael@0 76
michael@0 77 function equalRegions(r1s, r2s) {
michael@0 78 return regionContains(r1s, r2s) && regionContains(r2s, r1s);
michael@0 79 }
michael@0 80
michael@0 81 // Checks that a plugin's clip region equals the specified rectangle list.
michael@0 82 // The rectangles are given relative to the plugin's top-left. They are in
michael@0 83 // [left, top, right, bottom] format.
michael@0 84 function checkClipRegionWithDoc(doc, offsetX, offsetY, id, rects, checkBounds) {
michael@0 85 var p = doc.getElementById(id);
michael@0 86 var bounds = p.getBoundingClientRect();
michael@0 87 var pX = p.getEdge(0);
michael@0 88 var pY = p.getEdge(1);
michael@0 89
michael@0 90 if (checkBounds) {
michael@0 91 var pWidth = p.getEdge(2) - pX;
michael@0 92 var pHeight = p.getEdge(3) - pY;
michael@0 93
michael@0 94 is(pX, windowFrameX + bounds.left + offsetX, id + " plugin X");
michael@0 95 is(pY, windowFrameY + bounds.top + offsetY, id + " plugin Y");
michael@0 96 is(pWidth, bounds.width, id + " plugin width");
michael@0 97 is(pHeight, bounds.height, id + " plugin height");
michael@0 98 }
michael@0 99
michael@0 100 // Now check clip region. 'rects' is relative to the plugin's top-left.
michael@0 101 var clipRects = [];
michael@0 102 var n = p.getClipRegionRectCount();
michael@0 103 for (var i = 0; i < n; ++i) {
michael@0 104 // Convert the clip rect to be relative to the plugin's top-left.
michael@0 105 clipRects[i] = [
michael@0 106 p.getClipRegionRectEdge(i, 0) - pX,
michael@0 107 p.getClipRegionRectEdge(i, 1) - pY,
michael@0 108 p.getClipRegionRectEdge(i, 2) - pX,
michael@0 109 p.getClipRegionRectEdge(i, 3) - pY
michael@0 110 ];
michael@0 111 }
michael@0 112
michael@0 113 ok(equalRegions(clipRects, rects), "Matching regions for '" + id +
michael@0 114 "': expected " + dumpRegion(rects) + ", got " + dumpRegion(clipRects));
michael@0 115 }
michael@0 116
michael@0 117 checkClipRegion = function checkClipRegion(id, rects) {
michael@0 118 checkClipRegionWithDoc(document, 0, 0, id, rects, true);
michael@0 119 }
michael@0 120
michael@0 121 checkClipRegionForFrame = function checkClipRegionForFrame(fid, id, rects) {
michael@0 122 var f = document.getElementById(fid);
michael@0 123 var bounds = f.getBoundingClientRect();
michael@0 124 checkClipRegionWithDoc(f.contentDocument, bounds.left, bounds.top, id, rects, true);
michael@0 125 }
michael@0 126
michael@0 127 checkClipRegionNoBounds = function checkClipRegionNoBounds(id, rects) {
michael@0 128 checkClipRegionWithDoc(document, 0, 0, id, rects, false);
michael@0 129 }
michael@0 130
michael@0 131 function loaded() {
michael@0 132 var h1 = document.getElementById("h1");
michael@0 133 var h2 = document.getElementById("h2");
michael@0 134 var hwidth = h2.boxObject.screenX - h1.boxObject.screenX;
michael@0 135 if (hwidth != 100) {
michael@0 136 // Maybe it's a DPI issue
michael@0 137 todo(false, "Unexpected DPI?");
michael@0 138 finish();
michael@0 139 window.close();
michael@0 140 return;
michael@0 141 }
michael@0 142
michael@0 143 if (!document.getElementById("p1").identifierToStringTest) {
michael@0 144 todo(false, "Test plugin not available");
michael@0 145 finish();
michael@0 146 window.close();
michael@0 147 return;
michael@0 148 }
michael@0 149
michael@0 150 var bounds = h1.getBoundingClientRect();
michael@0 151 windowFrameX = h1.boxObject.screenX - bounds.left - window.screenX;
michael@0 152 windowFrameY = h1.boxObject.screenY - bounds.top - window.screenY;
michael@0 153
michael@0 154 // Run actual test code
michael@0 155 runTests();
michael@0 156 }
michael@0 157
michael@0 158 // Need to run 'loaded' after painting is unsuppressed, or we'll set clip
michael@0 159 // regions to empty. The timeout must be non-zero on X11 so that
michael@0 160 // gtk_container_idle_sizer runs after the GtkSocket gets the plug_window.
michael@0 161 window.addEventListener("load",
michael@0 162 function () { setTimeout(loaded, 1000); }, false);
michael@0 163 })();

mercurial