Bug 590294: Test app that uses new viewport and displayport APIs.
authorChris Jones <jones.chris.g@gmail.com>
Fri, 03 Sep 2010 15:10:46 -0500
changeset 54086 0ef3696734aefaf9541dc549a2e4cee205f2a179
parent 54085 15302bb0843b7108a2cb3e522fc01ac8441884c2
child 54087 178f9db99c93c0644d52ebd126dae26943b9e1d9
push idunknown
push userunknown
push dateunknown
bugs590294
milestone2.0b6pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 590294: Test app that uses new viewport and displayport APIs.
layout/ipc/jar.mn
layout/ipc/test-ipcbrowser-chrome.js
layout/ipc/test-ipcbrowser-content.js
layout/ipc/test-ipcbrowser.xul
new file mode 100644
--- /dev/null
+++ b/layout/ipc/jar.mn
@@ -0,0 +1,4 @@
+toolkit.jar:
+        content/global/test-ipcbrowser.xul (test-ipcbrowser.xul)
+        content/global/test-ipcbrowser-chrome.js (test-ipcbrowser-chrome.js)
+        content/global/test-ipcbrowser-content.js (test-ipcbrowser-content.js)
new file mode 100644
--- /dev/null
+++ b/layout/ipc/test-ipcbrowser-chrome.js
@@ -0,0 +1,115 @@
+function init() {
+    messageManager.loadFrameScript(
+        "chrome://global/content/test-ipcbrowser-content.js", true
+    );
+}
+
+function browser() {
+    return document.getElementById("content");
+}
+
+function frameLoader() {
+    return browser().QueryInterface(Components.interfaces.nsIFrameLoaderOwner)
+                    .frameLoader;
+}
+
+// Functions affecting the content window.
+
+function loadURL(url) {
+    browser().setAttribute('src', url);
+}
+
+function scrollContentBy(dx, dy) {
+    messageManager.sendAsyncMessage("scrollBy",
+                                    { dx: dx, dy: dy });
+
+}
+
+function scrollContentTo(x, y) {
+    messageManager.sendAsyncMessage("scrollTo",
+                                    { x: x, y: y });
+}
+
+function setContentViewport(w, h) {
+    messageManager.sendAsyncMessage("setViewport",
+                                    { w: w, h: h });
+}
+
+function setContentDisplayPort(x, y, w, h) {
+    messageManager.sendAsyncMessage("setDisplayPort",
+                                    { x: x, y: y, w: w, h: h });
+}
+
+function setContentResolution(xres, yres) {
+    messageManager.sendAsyncMessage("setResolution",
+                                    { xres: xres, yres: yres });
+}
+
+// Functions affecting <browser>.
+
+function scrollViewportBy(dx, dy) {
+    frameLoader().scrollViewportBy(dx, dy);
+}
+
+function scrollViewportTo(x, y) {
+    frameLoader().scrollViewportTo(x, y);
+}
+
+function setViewportScale(xs, ys) {
+    frameLoader().setViewportScale(xs, ys);
+}
+
+var kDelayMs = 100;
+var kDurationMs = 250;
+var scrolling = false;
+function startAnimatedScrollBy(dx, dy) {
+    if (scrolling)
+        throw "don't interrupt me!";
+
+    scrolling = true;
+
+    var start = mozAnimationStartTime;
+    var end = start + kDurationMs;
+    // |- k| so that we do something in first invocation of nudge()
+    var prevNow = start - 20;
+    var accumDx = 0, accumDy = 0;
+
+    var sentScrollBy = false;
+    function nudgeScroll(now) {
+        var ddx = dx * (now - prevNow) / kDurationMs;
+        var ddy = dy * (now - prevNow) / kDurationMs;
+
+        ddx = Math.min(dx - accumDx, ddx);
+        ddy = Math.min(dy - accumDy, ddy);
+        accumDx += ddx;
+        accumDy += ddy;
+
+        frameLoader().scrollViewportBy(ddx, ddy);
+
+        if (!sentScrollBy && 100 <= (now - start)) {
+            messageManager.sendAsyncMessage("scrollBy",
+                                            { dx: dx, dy: dy });
+            sentScrollBy = true;
+        }
+
+        if (now >= end || (accumDx >= dx && accumDy >= dy)) {
+            var fixupDx = Math.max(dx - accumDx, 0);
+            var fixupDy = Math.max(dy - accumDy, 0);
+            frameLoader().scrollViewportBy(fixupDx, fixupDy);
+
+            scrolling = false;
+            removeEventListener("MozBeforePaint", nudgeScroll, false);
+        }
+        else {
+            mozRequestAnimationFrame();
+        }
+
+        prevNow = now;
+    }
+
+    nudgeScroll(start);
+    addEventListener("MozBeforePaint",
+                     function (e) { nudgeScroll(e.timeStamp); },
+                     false);
+    mozRequestAnimationFrame();
+}
new file mode 100644
--- /dev/null
+++ b/layout/ipc/test-ipcbrowser-content.js
@@ -0,0 +1,59 @@
+function windowUtils() {
+    return content.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
+               .getInterface(Components.interfaces.nsIDOMWindowUtils);
+}
+
+function recvSetViewport(w, h) {
+
+    dump("setting viewport to "+ w +"x"+ h +"\n");
+
+    windowUtils().setCSSViewport(w, h);
+}
+
+function recvSetDisplayPort(x, y, w, h) {
+
+    dump("setting displayPort to <"+ x +", "+ y +", "+ w +", "+ h +">\n");
+
+    windowUtils().setDisplayPort(x, y, w, h);
+}
+
+function recvSetResolution(xres, yres) {
+
+    dump("setting xres="+ xres +" yres="+ yres +"\n");
+
+    windowUtils().setResolution(xres, yres);
+}
+
+function recvScrollBy(dx, dy) {
+    content.scrollBy(dx, dy);
+}
+
+function recvScrollTo(x, y) {
+    content.scrollTo(x, y);
+}
+
+addMessageListener(
+    "setViewport",
+    function (m) { recvSetViewport(m.json.w, m.json.h); }
+);
+
+addMessageListener(
+    "setDisplayPort",
+    function (m) { recvSetDisplayPort(m.json.x, m.json.y,
+                                      m.json.w, m.json.h); }
+);
+
+addMessageListener(
+    "setResolution",
+    function (m) { recvSetResolution(m.json.xres, m.json.yres); }
+);
+
+addMessageListener(
+    "scrollBy",
+    function(m) { recvScrollBy(m.json.dx, m.json.dy); }
+);
+
+addMessageListener(
+    "scrollTo",
+    function(m) { recvScrollTo(m.json.x, m.json.y); }
+);
new file mode 100644
--- /dev/null
+++ b/layout/ipc/test-ipcbrowser.xul
@@ -0,0 +1,74 @@
+<?xml version="1.0"?>
+<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
+<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
+        width="800" height="800" orient="vertical"
+        onload="init()">
+  <script src="chrome://global/content/test-ipcbrowser-chrome.js"/>
+
+  <toolbar id="controls">
+    <toolbarbutton label="Back"/>
+    <toolbarbutton label="Forward"/>
+    <textbox onchange="loadURL(this.value)" flex="1"/>
+  </toolbar>
+  <toolbar>
+    <textbox id="scbDx" flex="1" value="0"/>
+    <textbox id="scbDy" flex="1" value="100"/>
+    <toolbarbutton
+       onclick="scrollContentBy(document.getElementById('scbDx').value,
+                                document.getElementById('scbDy').value);"
+       label="scrollBy"/>
+    <textbox id="sctX" flex="1" value="200"/>
+    <textbox id="sctY" flex="1" value="300"/>
+    <toolbarbutton
+       onclick="scrollContentTo(document.getElementById('sctX').value,
+                                document.getElementById('sctY').value);"
+       label="scrollTo"/>
+  </toolbar>
+  <toolbar>
+    <textbox id="vW" flex="1" value="600"/>
+    <textbox id="vH" flex="1" value="400"/>
+    <toolbarbutton
+       onclick="setContentViewport(document.getElementById('vW').value,
+                                   document.getElementById('vH').value);"
+       label="setViewport"/>
+    <textbox id="dX" flex="1" value="0"/>
+    <textbox id="dY" flex="1" value="0"/>
+    <textbox id="dW" flex="1" value="600"/>
+    <textbox id="dH" flex="1" value="600"/>
+    <toolbarbutton
+       onclick="setContentDisplayPort(document.getElementById('dX').value,
+                                      document.getElementById('dY').value,
+                                      document.getElementById('dW').value,
+                                      document.getElementById('dH').value);"
+       label="setDisplayPort"/>
+    <textbox id="xR" flex="1" value="2.0"/>
+    <textbox id="yR" flex="1" value="2.0"/>
+    <toolbarbutton
+       onclick="setContentResolution(document.getElementById('xR').value,
+                                     document.getElementById('yR').value);"
+       label="setResolution"/>
+  </toolbar>
+  <toolbar>
+    <textbox id="vsbX" flex="1" value="0"/>
+    <textbox id="vsbY" flex="1" value="100"/>
+    <toolbarbutton
+       onclick="scrollViewportBy(document.getElementById('vsbX').value,
+                                 document.getElementById('vsbY').value);"
+       label="scrollViewportBy"/>
+    <textbox id="vstX" flex="1" value="200"/>
+    <textbox id="vstY" flex="1" value="300"/>
+    <toolbarbutton
+       onclick="scrollViewportTo(document.getElementById('vstX').value,
+                                 document.getElementById('vstY').value);"
+       label="scrollViewportTo"/>
+    <textbox id="vsX" flex="1" value="2.0"/>
+    <textbox id="vsY" flex="1" value="2.0"/>
+    <toolbarbutton
+       onclick="setViewportScale(document.getElementById('vsX').value,
+                                 document.getElementById('vsY').value);"
+       label="setViewportScale"/>
+  </toolbar>
+
+  <browser type="content" src="http://people.mozilla.com/~cjones/anim.html" flex="1" id="content"
+           remote="true"/>
+</window>