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