Bug 650241 - Location returned by accessibles sometimes incorrect when page zoomed, r=marcoz
authorAlexander Surkov <surkov.alexander@gmail.com>
Wed, 22 Feb 2012 11:07:43 +0900
changeset 90245 715b6b383b4df854aacb069405cd81d102f5101e
parent 90244 40d9e3a8a4cfa519950411ca45812491a515c6a0
child 90246 1acd05deef4fe337245e0a603716a0b651646178
push idunknown
push userunknown
push dateunknown
reviewersmarcoz
bugs650241
milestone13.0a1
Bug 650241 - Location returned by accessibles sometimes incorrect when page zoomed, r=marcoz
accessible/src/base/nsAccessible.cpp
accessible/tests/mochitest/Makefile.in
accessible/tests/mochitest/bounds/Makefile.in
accessible/tests/mochitest/bounds/test_zoom.html
accessible/tests/mochitest/browser.js
accessible/tests/mochitest/layout.js
--- a/accessible/src/base/nsAccessible.cpp
+++ b/accessible/src/base/nsAccessible.cpp
@@ -1055,17 +1055,18 @@ nsAccessible::GetBounds(PRInt32* aX, PRI
 
   nsPresContext* presContext = presShell->GetPresContext();
   *aX = presContext->AppUnitsToDevPixels(unionRectTwips.x);
   *aY = presContext->AppUnitsToDevPixels(unionRectTwips.y);
   *aWidth = presContext->AppUnitsToDevPixels(unionRectTwips.width);
   *aHeight = presContext->AppUnitsToDevPixels(unionRectTwips.height);
 
   // We have the union of the rectangle, now we need to put it in absolute screen coords
-  nsIntRect orgRectPixels = boundingFrame->GetScreenRectExternal();
+  nsIntRect orgRectPixels = boundingFrame->GetScreenRectInAppUnits().
+    ToNearestPixels(presContext->AppUnitsPerDevPixel());
   *aX += orgRectPixels.x;
   *aY += orgRectPixels.y;
 
   return NS_OK;
 }
 
 // helpers
 
--- a/accessible/tests/mochitest/Makefile.in
+++ b/accessible/tests/mochitest/Makefile.in
@@ -40,16 +40,17 @@ DEPTH		= ../../..
 topsrcdir	= @top_srcdir@
 srcdir		= @srcdir@
 VPATH		= @srcdir@
 relativesrcdir  = accessible
 
 DIRS	= \
   actions \
   attributes \
+  bounds \
   editabletext \
   elm \
   events \
   focus \
   hittest \
   hyperlink \
   hypertext \
   name \
new file mode 100644
--- /dev/null
+++ b/accessible/tests/mochitest/bounds/Makefile.in
@@ -0,0 +1,53 @@
+#
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is mozilla.org code.
+#
+# The Initial Developer of the Original Code is
+# Mozilla Foundation.
+# Portions created by the Initial Developer are Copyright (C) 2012
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#   Alexander Surkov <surkov.alexander@gmail.com> (original author)
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either of the GNU General Public License Version 2 or later (the "GPL"),
+# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+DEPTH		= ../../../..
+topsrcdir	= @top_srcdir@
+srcdir		= @srcdir@
+VPATH		= @srcdir@
+relativesrcdir  = accessible/bounds
+
+include $(DEPTH)/config/autoconf.mk
+include $(topsrcdir)/config/rules.mk
+
+_TEST_FILES =\
+		test_zoom.html \
+		$(NULL)
+
+libs:: $(_TEST_FILES)
+	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/a11y/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/accessible/tests/mochitest/bounds/test_zoom.html
@@ -0,0 +1,74 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Accessible boundaries when page is zoomed</title>
+  <link rel="stylesheet" type="text/css"
+        href="chrome://mochikit/content/tests/SimpleTest/test.css" />
+
+  <script type="application/javascript"
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+
+  <script type="application/javascript"
+          src="../common.js"></script>
+  <script type="application/javascript"
+          src="../role.js"></script>
+  <script type="application/javascript"
+          src="../layout.js"></script>
+  <script type="application/javascript"
+          src="../browser.js"></script>
+
+  <script type="application/javascript">
+
+    ////////////////////////////////////////////////////////////////////////////
+    // If your patch changes UI or default styles then please tweek the numbers.
+    // Accessibility exposes coordinates and size in device pixels while DOM
+    // operates with CSS pixels. Thus it seems we don't have another way for
+    // accessibility testing rather than numbers hardcode.
+    ////////////////////////////////////////////////////////////////////////////
+
+    function doTest()
+    {
+      var [outerX, outerY] = getBounds(getAccessible(currentTabDocument()).parent);
+
+      var p1 = currentTabDocument().body.firstElementChild;
+      var p2 = currentTabDocument().body.lastElementChild;
+
+      var deltaX = 8, deltaY = 8, indentY = 16, w = 584, h = (WIN ? 20 : 19);
+      testBounds(p1, outerX + deltaX, outerY + deltaY, w, h);
+      testBounds(p2, outerX + deltaX, outerY + deltaY + h + indentY, w, h);
+
+      var docShell = currentTabWindow().
+        QueryInterface(Components.interfaces.nsIInterfaceRequestor).
+        getInterface(Components.interfaces.nsIWebNavigation).
+        QueryInterface(Components.interfaces.nsIDocShell);
+      var docViewer = docShell.contentViewer.
+        QueryInterface(Components.interfaces.nsIMarkupDocumentViewer);
+      docViewer.fullZoom = 2.0;
+
+      testBounds(p1, outerX + deltaX * 2, outerY + deltaY * 2, w * 2, h * 2);
+      testBounds(p2, outerX + deltaX * 2, outerY + (deltaY + h + indentY) * 2,
+                 w * 2, h * 2);
+
+      closeBrowserWindow();
+      SimpleTest.finish();
+    }
+
+    SimpleTest.waitForExplicitFinish();
+    openBrowserWindow(doTest,
+                      "data:text/html,<html><body><p>para 1</p><p>para 2</p></body></html>",
+                      { left: 0, top: 0, width: 600, height: 600 });
+  </script>
+</head>
+<body>
+
+  <a target="_blank"
+     href="https://bugzilla.mozilla.org/show_bug.cgi?id=650241"
+     title="Location returned by accessibles incorrect when page zoomed">
+    Mozilla Bug 650241
+  </a>
+  <p id="display"></p>
+  <div id="content" style="display: none"></div>
+  <pre id="test">
+  </pre>
+</body>
+</html>
--- a/accessible/tests/mochitest/browser.js
+++ b/accessible/tests/mochitest/browser.js
@@ -1,15 +1,16 @@
 /**
  * Load the browser with the given url and then invokes the given function.
  */
-function openBrowserWindow(aFunc, aURL)
+function openBrowserWindow(aFunc, aURL, aRect)
 {
   gBrowserContext.testFunc = aFunc;
   gBrowserContext.startURL = aURL;
+  gBrowserContext.browserRect = aRect;
 
   addLoadEvent(openBrowserWindowIntl);
 }
 
 /**
  * Close the browser window.
  */
 function closeBrowserWindow()
@@ -53,16 +54,24 @@ function currentBrowser()
  * Return DOM document of the current tab.
  */
 function currentTabDocument()
 {
   return currentBrowser().contentDocument;
 }
 
 /**
+ * Return window of the current tab.
+ */
+function currentTabWindow()
+{
+  return currentTabDocument().defaultView;
+}
+
+/**
  * Return browser element of the tab at the given index.
  */
 function browserAt(aIndex)
 {
   return tabBrowser().getBrowserAtIndex(aIndex);
 }
 
 /**
@@ -98,19 +107,32 @@ var gBrowserContext =
 {
   browserWnd: null,
   testFunc: null,
   startURL: ""
 };
 
 function openBrowserWindowIntl()
 {
+  var params = "chrome,all,dialog=no";
+  var rect = gBrowserContext.browserRect;
+  if (rect) {
+    if ("left" in rect)
+      params += ",left=" + rect.left;
+    if ("top" in rect)
+      params += ",top=" + rect.top;
+    if ("width" in rect)
+      params += ",width=" + rect.width;
+    if ("height" in rect)
+      params += ",height=" + rect.height;
+  }
+
   gBrowserContext.browserWnd =
     window.openDialog(Services.prefs.getCharPref("browser.chromeURL"),
-                      "_blank", "chrome,all,dialog=no",
+                      "_blank", params,
                       gBrowserContext.startURL);
 
   addA11yLoadEvent(startBrowserTests, browserWindow());
 }
 
 function startBrowserTests()
 {
   if (gBrowserContext.startURL) // wait for load
--- a/accessible/tests/mochitest/layout.js
+++ b/accessible/tests/mochitest/layout.js
@@ -51,16 +51,39 @@ function getChildAtPoint(aIdentifier, aX
       return acc.getDeepestChildAtPoint(x, y);
     return acc.getChildAtPoint(x, y);
   } catch (e) {  }
 
   return null;
 }
 
 /**
+ * Test the accessible boundaries.
+ */
+function testBounds(aID, aX, aY, aWidth, aHeight)
+{
+  var [x, y, width, height] = getBounds(aID);
+  is(x, aX, "Wrong x coordinate of " + prettyName(aID));
+  is(y, aY, "Wrong y coordinate of " + prettyName(aID));
+  is(width, aWidth, "Wrong width of " + prettyName(aID));
+  is(height, aHeight, "Wrong height of " + prettyName(aID));
+}
+
+/**
+ * Return the accessible coordinates and size relative to the screen.
+ */
+function getBounds(aID)
+{
+  var accessible = getAccessible(aID);
+  var x = {}, y = {}, width = {}, height = {};
+  accessible.getBounds(x, y, width, height);
+  return [x.value, y.value, width.value, height.value];
+}
+
+/**
  * Return DOM node coordinates relative screen as pair (x, y).
  */
 function getScreenCoords(aNode)
 {
   if (aNode instanceof nsIDOMXULElement)
     return [node.boxObject.screenX, node.boxObject.screenY];
 
   // Ugly hack until bug 486200 is fixed.