dom/tests/mochitest/general/test_clientRects.html

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html id="d9" style="width:800px; height:1000px">
michael@0 3 <head>
michael@0 4 <title>Tests for getClientRects/getBoundingClientRect</title>
michael@0 5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
michael@0 7 </head>
michael@0 8 <body style="margin:0" onload="doTest()">
michael@0 9
michael@0 10 <script>
michael@0 11 function isWithinEps(v1, v2, eps, msg) {
michael@0 12 if (eps) {
michael@0 13 ok(Math.abs(v1 - v2) < eps, msg + " (within " + eps + "); got " + v1 + ", expected " + v2);
michael@0 14 } else {
michael@0 15 is(v1, v2, msg);
michael@0 16 }
michael@0 17 }
michael@0 18 function checkRect(clientRect, r, eps, exprMsg, restMsg) {
michael@0 19 isWithinEps(clientRect.left, r[0], eps, exprMsg + ".left" + restMsg);
michael@0 20 isWithinEps(clientRect.top, r[1], eps, exprMsg + ".top" + restMsg);
michael@0 21 isWithinEps(clientRect.right, r[2], eps, exprMsg + ".right" + restMsg);
michael@0 22 isWithinEps(clientRect.bottom, r[3], eps, exprMsg + ".bottom" + restMsg);
michael@0 23 isWithinEps(clientRect.width, r[2] - r[0], eps, exprMsg + ".width" + restMsg);
michael@0 24 isWithinEps(clientRect.height, r[3] - r[1], eps, exprMsg + ".height" + restMsg);
michael@0 25 }
michael@0 26 function doc(id) {
michael@0 27 return document.getElementById(id).contentDocument;
michael@0 28 }
michael@0 29 function checkElement(id, list, eps, doc) {
michael@0 30 var e = (doc || document).getElementById(id);
michael@0 31 var clientRects = e.getClientRects();
michael@0 32 ok(clientRects instanceof e.ownerDocument.defaultView.Array,
michael@0 33 "getClientRects retval should have Array.prototype on its proto chain");
michael@0 34 clientRects.map(function(rect) {
michael@0 35 ok(rect instanceof DOMRect, "Should have a DOMRect here");
michael@0 36 });
michael@0 37 is(clientRects.length, list.length, "getClientRects().length for element '" + id + "'");
michael@0 38 var bounds = list.length > 0 ? list[0] : [0,0,0,0];
michael@0 39 for (var i = 0; i < clientRects.length && i < list.length; ++i) {
michael@0 40 var r = list[i];
michael@0 41 r[2] += r[0];
michael@0 42 r[3] += r[1];
michael@0 43 checkRect(clientRects[i], r, eps, "getClientRects()[" + i + "]", " for element '" + id + "'");
michael@0 44 if (r[2] != r[0] && r[3] != r[1]) {
michael@0 45 bounds[0] = Math.min(bounds[0], r[0]);
michael@0 46 bounds[1] = Math.min(bounds[1], r[1]);
michael@0 47 bounds[2] = Math.max(bounds[2], r[2]);
michael@0 48 bounds[3] = Math.max(bounds[3], r[3]);
michael@0 49 }
michael@0 50 }
michael@0 51 checkRect(e.getBoundingClientRect(), bounds, eps, "getBoundingClientRect()", " for element '" + id + "'");
michael@0 52 }
michael@0 53 </script>
michael@0 54
michael@0 55 <!-- Simple case -->
michael@0 56 <div id="d1" style="position:absolute; left:50px; top:50px; width:20px; height:30px; background:pink;"></div>
michael@0 57 <!-- Multiple boxes -->
michael@0 58 <div style="position:absolute; left:50px; top:100px; width:400px; height:100px; -moz-column-count:2; -moz-column-gap:0; column-count:2; column-gap:0">
michael@0 59 <div id="d2">
michael@0 60 <div style="width:200px; height:100px; background:yellow"></div>
michael@0 61 <div style="width:200px; height:100px; background:lime"></div>
michael@0 62 </div>
michael@0 63 </div>
michael@0 64 <!-- No boxes -->
michael@0 65 <div id="d3" style="display:none"></div>
michael@0 66 <!-- Element in transform -->
michael@0 67 <div style="-moz-transform:translate(50px, 50px); transform:translate(50px,50px); position:absolute; left:0; top:200px">
michael@0 68 <div id="d4" style="width:50px; height:50px; background:blue;"></div>
michael@0 69 </div>
michael@0 70 <svg style="position:absolute; left:50px; top:300px; width:100px; height:100px;">
michael@0 71 <!-- Element in SVG foreignobject -->
michael@0 72 <foreignObject x="20" y="30" width="40" height="40">
michael@0 73 <div id="d5" style="width:40px; height:40px; background:pink;"></div>
michael@0 74 </foreignObject>
michael@0 75 <!-- SVG Element -->
michael@0 76 <circle id="s1" cx="60" cy="60" r="10" fill="yellow"/>
michael@0 77 </svg>
michael@0 78 <!-- Element in transform with bounding-box -->
michael@0 79 <div style="-moz-transform:rotate(45deg); transform:rotate(45deg); position:absolute; left:50px; top:450px; width:100px; height:100px;">
michael@0 80 <div id="d6" style="width:100px; height:100px; background:orange;"></div>
michael@0 81 </div>
michael@0 82 <!-- Element in two transforms; we should combine transforms instead of taking bounding-box twice -->
michael@0 83 <div style="-moz-transform:rotate(45deg); transform:rotate(45deg); position:absolute; left:50px; top:550px; width:100px; height:100px;">
michael@0 84 <div style="-moz-transform:rotate(-45deg); transform:rotate(-45deg); width:100px; height:100px;">
michael@0 85 <div id="d7" style="width:100px; height:100px; background:lime;"></div>
michael@0 86 </div>
michael@0 87 </div>
michael@0 88 <!-- Fixed-pos element -->
michael@0 89 <div id="d8" style="position:fixed; left:50px; top:700px; width:100px; height:100px; background:gray;"></div>
michael@0 90 <!-- Root element; see d9 -->
michael@0 91 <!-- Element in iframe -->
michael@0 92 <iframe id="f1" style="position:absolute; left:300px; top:0; width:100px; height:200px; border:none"
michael@0 93 src="data:text/html,<div id='d10' style='position:absolute; left:0; top:25px; width:100px; height:100px; background:cyan'>">
michael@0 94 </iframe>
michael@0 95 <!-- Root element in iframe -->
michael@0 96 <iframe id="f2" style="position:absolute; left:300px; top:250px; width:100px; height:200px; border:none"
michael@0 97 src="data:text/html,<html id='d11' style='width:100px; height:100px; background:magenta'>">
michael@0 98 </iframe>
michael@0 99 <!-- Fixed-pos element in iframe -->
michael@0 100 <iframe id="f3" style="position:absolute; left:300px; top:400px; border:none"
michael@0 101 src="data:text/html,<div id='d12' style='position:fixed; left:0; top:0; width:100px; height:100px;'>"></iframe>
michael@0 102
michael@0 103 <script>
michael@0 104 function doTest() {
michael@0 105 checkElement("d1", [[50,50,20,30]]);
michael@0 106 checkElement("d2", [[50,100,200,100],[250,100,200,100]]);
michael@0 107 checkElement("d3", []);
michael@0 108 checkElement("d4", [[50,250,50,50]]);
michael@0 109 checkElement("d5", [[70,330,40,40]]);
michael@0 110 checkElement("s1", [[100,350,20,20]], 0.1);
michael@0 111 var sqrt2 = Math.sqrt(2);
michael@0 112 checkElement("d6", [[100 - 50*sqrt2,500 - 50*sqrt2,100*sqrt2,100*sqrt2]], 0.1);
michael@0 113 checkElement("d7", [[50,550,100,100]]);
michael@0 114 checkElement("d8", [[50,700,100,100]]);
michael@0 115 checkElement("d9", [[0,0,800,1000]]);
michael@0 116 checkElement("d10", [[0,25,100,100]], 0, doc("f1"));
michael@0 117 checkElement("d11", [[0,0,100,100]], 0, doc("f2"));
michael@0 118 checkElement("d12", [[0,0,100,100]], 0, doc("f3"));
michael@0 119 SimpleTest.finish();
michael@0 120 }
michael@0 121 SimpleTest.waitForExplicitFinish();
michael@0 122 </script>
michael@0 123
michael@0 124 <p id="display"></p>
michael@0 125 <div id="content" style="display: none">
michael@0 126
michael@0 127 </div>
michael@0 128
michael@0 129 </body>
michael@0 130 </html>

mercurial