Bug 587317: Fix test_offsets to succeed on all platforms including D2D. r=dbaron
authorRobert O'Callahan <robert@ocallahan.org>
Mon, 16 Aug 2010 09:15:03 +0200
changeset 50659 66415a668c19b5c4c576d9c5d26e7001c419d43c
parent 50658 5783772f2fe745470dcea065e61ed1039362d6f6
child 50660 3fc41668b25c0c748671964328c5310df4377eda
push idunknown
push userunknown
push dateunknown
reviewersdbaron
bugs587317
milestone2.0b4pre
Bug 587317: Fix test_offsets to succeed on all platforms including D2D. r=dbaron
dom/tests/mochitest/general/test_offsets.js
--- a/dom/tests/mochitest/general/test_offsets.js
+++ b/dom/tests/mochitest/general/test_offsets.js
@@ -21,16 +21,32 @@ function testElements(baseid, callback)
   div.scrollLeft = 10;
   div.scrollTop = 10;
   is(element.scrollLeft, 0, element.id + " scrollLeft after nonscroll");
   is(element.scrollTop, 0, element.id + " scrollTop after nonscroll");
 
   callback();
 }
 
+function toNearestAppunit(v)
+{
+  // 60 appunits per CSS pixel; round result to the nearest appunit
+  return Math.round(v*60)/60;
+}
+
+function floorToNearestAppunit(v)
+{
+  return Math.floor(toNearestAppunit(v));
+}
+
+function isEqualAppunits(a, b, msg)
+{
+  is(toNearestAppunit(a), toNearestAppunit(b), msg);
+}
+
 function testElement(element)
 {
   var offsetParent = element.getAttribute("_offsetParent");
   offsetParent = $(offsetParent == "null" ? null: (offsetParent ? offsetParent : "body"));
 
   var borderLeft = gcs(element, "borderLeftWidth");
   var borderTop = gcs(element, "borderTopWidth");
   var borderRight = gcs(element, "borderRightWidth");
@@ -46,20 +62,21 @@ function testElement(element)
     checkOffsetState(element, -10000, -10000,
                               borderLeft + paddingLeft + width + paddingRight + borderRight,
                               borderTop + paddingTop + height + paddingBottom + borderBottom,
                               offsetParent, element.id);
 
   var scrollWidth, scrollHeight, clientWidth, clientHeight;
   if (element.id == "scrollbox") {
     var lastchild = $("lastline");
-    scrollWidth = Math.floor(lastchild.getBoundingClientRect().width) + paddingLeft + paddingRight;
-    var contentsHeight = element.lastChild.getBoundingClientRect().bottom -
-        element.firstChild.getBoundingClientRect().top;
-    scrollHeight = Math.floor(contentsHeight) + paddingTop + paddingBottom;
+    scrollWidth = floorToNearestAppunit(lastchild.getBoundingClientRect().width + paddingLeft + paddingRight);
+    var top = element.firstChild.getBoundingClientRect().top;
+    var bottom = element.lastChild.getBoundingClientRect().bottom;
+    var contentsHeight = bottom - top;
+    scrollHeight = floorToNearestAppunit(contentsHeight + paddingTop + paddingBottom);
     clientWidth = paddingLeft + width + paddingRight - scrollbarWidth;
     clientHeight = paddingTop + height + paddingBottom - scrollbarHeight;
   }
   else {
     // XXXndeakin note that Mozilla adds borders here, although the spec does not
     scrollWidth = paddingLeft + width + paddingRight + borderLeft + borderRight;
     scrollHeight = paddingTop + height + paddingBottom + borderTop + borderBottom;
     clientWidth = paddingLeft + width + paddingRight;
@@ -72,43 +89,35 @@ function testElement(element)
     checkScrollState(element, 0, 0, scrollWidth, scrollHeight, element.id);
 
   if (element instanceof SVGElement)
     checkClientState(element, 0, 0, 0, 0, element.id);
   else
     checkClientState(element, borderLeft, borderTop, clientWidth, clientHeight, element.id);
 
   var boundingrect = element.getBoundingClientRect();
-  is(Math.round(boundingrect.width), borderLeft + paddingLeft + width + paddingRight + borderRight,
+  isEqualAppunits(boundingrect.width, borderLeft + paddingLeft + width + paddingRight + borderRight,
      element.id + " bounding rect width");
-  is(Math.round(boundingrect.height), borderTop + paddingTop + height + paddingBottom + borderBottom,
+  isEqualAppunits(boundingrect.height, borderTop + paddingTop + height + paddingBottom + borderBottom,
      element.id + " bounding rect height");
-  is(Math.round(boundingrect.right - boundingrect.left),
-     borderLeft + paddingLeft + width + paddingRight + borderRight,
+  isEqualAppunits(boundingrect.right - boundingrect.left, boundingrect.width,
      element.id + " bounding rect right");
-  is(Math.round(boundingrect.bottom - boundingrect.top),
-     borderTop + paddingTop + height + paddingBottom + borderBottom,
+  isEqualAppunits(boundingrect.bottom - boundingrect.top, boundingrect.height,
      element.id + " bounding rect bottom");
 
   var rects = element.getClientRects();
   if (element.id == "input-displaynone" || element.id == "nonappended") {
     is(rects.length, 0, element.id + " getClientRects empty");
   }
   else {
     is(rects[0].left, boundingrect.left, element.id + " getClientRects left");
     is(rects[0].top, boundingrect.top, element.id + " getClientRects top");
     is(rects[0].right, boundingrect.right, element.id + " getClientRects right");
     is(rects[0].bottom, boundingrect.bottom, element.id + " getClientRects bottom");
   }
-
-  var root = document.documentElement;
-  gPreviousRight = Math.round(boundingrect.right) -
-                   gcs(root, "paddingLeft") - gcs(root, "borderLeftWidth");
-  gPreviousBottom = Math.round(boundingrect.bottom) -
-                   gcs(root, "paddingTop") - gcs(root, "borderTopWidth");
 }
 
 function checkScrolledElement(element, child)
 {
   var elemrect = element.getBoundingClientRect();
   var childrect = child.getBoundingClientRect();
 
   var topdiff = childrect.top - elemrect.top;
@@ -154,17 +163,17 @@ function checkScrollState(element, left,
 function checkClientState(element, left, top, width, height, testname)
 {
   checkCoords(element, "client", left, top, width, height, testname);
 }
 
 function checkCoord(element, type, val, testname)
 {
   if (val != -10000)
-    is(element[type], val, testname + " " + type);
+    is(element[type], Math.round(val), testname + " " + type);
 }
 
 function checkCoords(element, type, left, top, width, height, testname)
 {
   checkCoord(element, type + "Left", left, testname);
   checkCoord(element, type + "Top", top, testname);
   checkCoord(element, type + "Width", width, testname);
   checkCoord(element, type + "Height", height, testname);
@@ -183,12 +192,11 @@ function checkCoords(element, type, left
        element.id + " has non-zero " + type + " width and height");
 }
 
 function gcs(element, prop)
 {
   var propVal = (element instanceof SVGElement && (prop == "width" || prop == "height")) ?
                    element.getAttribute(prop) : getComputedStyle(element, "")[prop];
   if (propVal == "auto")
-    propVal = 0;
-  var propValFloat = parseFloat(propVal);
-  return (isNaN(propValFloat) ? propVal : Math.round(propValFloat));
+    return 0;
+  return parseFloat(propVal);
 }