Bug 702880 - Add setVisible api to mozbrowser. r=jlebar
authorDale Harvey <dale@arandomurl.com>
Sun, 27 May 2012 08:39:04 -0400
changeset 95061 4ce695c26fc563dac5efc4a4ca9e8f0f9db2c4ae
parent 95060 f41434c72cb5a9e7cd2e6ec27793642ebb269b8c
child 95062 01673165f0eef183e8a72ebada6c6caebd32c9f1
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersjlebar
bugs702880
milestone15.0a1
Bug 702880 - Add setVisible api to mozbrowser. r=jlebar
dom/base/BrowserElementChild.js
dom/base/BrowserElementParent.js
dom/tests/mochitest/browser-frame/Makefile.in
dom/tests/mochitest/browser-frame/test_browserFrame10.html
--- a/dom/base/BrowserElementChild.js
+++ b/dom/base/BrowserElementChild.js
@@ -85,16 +85,19 @@ BrowserElementChild.prototype = {
     addEventListener('DOMLinkAdded',
                      this._iconChangedHandler.bind(this),
                      /* useCapture = */ true,
                      /* wantsUntrusted = */ false);
 
     addMessageListener("browser-element-api:get-screenshot",
                        this._recvGetScreenshot.bind(this));
 
+    addMessageListener("browser-element-api:set-visible",
+                        this._recvSetVisible.bind(this));
+
     let els = Cc["@mozilla.org/eventlistenerservice;1"]
                 .getService(Ci.nsIEventListenerService);
 
     // We are using the system group for those events so if something in the
     // content called .stopPropagation() this will still be called.
     els.addSystemEventListener(global, 'keydown',
                                this._keyEventHandler.bind(this),
                                /* useCapture = */ true);
@@ -150,16 +153,23 @@ BrowserElementChild.prototype = {
     ctx.drawWindow(content, 0, 0, content.innerWidth,
                    content.innerHeight, "rgb(255,255,255)");
     sendAsyncMsg('got-screenshot', {
       id: data.json.id,
       screenshot: canvas.toDataURL("image/png")
     });
   },
 
+  _recvSetVisible: function(data) {
+    debug("Received setVisible message: (" + data.json.visible + ")");
+    if (docShell.isActive !== data.json.visible) {
+      docShell.isActive = data.json.visible;
+    }
+  },
+
   _keyEventHandler: function(e) {
     if (whitelistedEvents.indexOf(e.keyCode) != -1 && !e.defaultPrevented) {
       sendAsyncMsg('keyevent', {
         type: e.type,
         code: e.keyCode,
         charCode: e.charCode,
       });
     }
--- a/dom/base/BrowserElementParent.js
+++ b/dom/base/BrowserElementParent.js
@@ -108,16 +108,19 @@ BrowserElementParent.prototype = {
     addMessageListener("get-mozapp-manifest-url", this._sendMozAppManifestURL);
     addMessageListener("keyevent", this._fireKeyEvent);
     mm.addMessageListener('browser-element-api:got-screenshot',
                           this._recvGotScreenshot.bind(this));
 
     XPCNativeWrapper.unwrap(frameElement).getScreenshot =
       this._getScreenshot.bind(this, mm, frameElement);
 
+    XPCNativeWrapper.unwrap(frameElement).setVisible =
+      this._setVisible.bind(this, mm, frameElement);
+
     mm.loadFrameScript("chrome://global/content/BrowserElementChild.js",
                        /* allowDelayedLoad = */ true);
   },
 
   _recvHello: function(frameElement, data) {
     debug("recvHello " + frameElement);
   },
 
@@ -160,16 +163,20 @@ BrowserElementParent.prototype = {
     let id = 'req_' + this._screenshotReqCounter++;
     let req = Services.DOMRequest
       .createRequest(frameElement.ownerDocument.defaultView);
     this._screenshotListeners[id] = req;
     mm.sendAsyncMessage('browser-element-api:get-screenshot', {id: id});
     return req;
   },
 
+   _setVisible: function(mm, frameElement, visible) {
+     mm.sendAsyncMessage('browser-element-api:set-visible', {visible: visible});
+   },
+
   _fireKeyEvent: function(frameElement, data) {
     let win = frameElement.ownerDocument.defaultView;
     let evt = frameElement.ownerDocument.createEvent("KeyboardEvent");
 
     evt.initKeyEvent(data.json.type, true, true, win,
                      false, false, false, false, // modifiers
                      data.json.keyCode,
                      data.json.charCode);
--- a/dom/tests/mochitest/browser-frame/Makefile.in
+++ b/dom/tests/mochitest/browser-frame/Makefile.in
@@ -20,13 +20,14 @@ include $(topsrcdir)/config/rules.mk
 		test_browserFrame2.html \
 		test_browserFrame3.html \
 		test_browserFrame4.html \
 		test_browserFrame5.html \
 		test_browserFrame6.html \
 		test_browserFrame7.html \
 		test_browserFrame8.html \
 		test_browserFrame9.html \
+		test_browserFrame10.html \
 		test_browserFrame_keyEvents.html \
 		$(NULL)
 
 libs:: $(_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/browser-frame/test_browserFrame10.html
@@ -0,0 +1,76 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=702880
+-->
+ <head>
+  <title>Test for Bug 702880</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript" src="browserFrameHelpers.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=753595">Mozilla Bug 702880</a>
+
+<!--
+   Test the setVisible property for mozbrowser
+-->
+
+<script type="application/javascript;version=1.7">
+
+SimpleTest.waitForExplicitFinish();
+
+var iframeScript = function() {
+  content.document.addEventListener("mozvisibilitychange", function() {
+    sendAsyncMessage('test:visibilitychange', {
+      hidden: content.document.mozHidden
+    });
+  }, false);
+}
+
+function runTest() {
+
+  browserFrameHelpers.setEnabledPref(true);
+  browserFrameHelpers.addToWhitelist();
+
+  var numEvents = 0;
+  var iframe1 = document.createElement('iframe');
+  iframe1.mozbrowser = true;
+  iframe1.src = 'data:text/html,1';
+
+  document.body.appendChild(iframe1);
+
+  function recvVisibilityChanged(msg) {
+    numEvents++;
+    if (numEvents === 1) {
+      ok(true, 'iframe recieved visibility changed');
+      ok(msg.json.hidden === true, 'mozHidden attribute correctly set');
+      iframe1.setVisible(false);
+      iframe1.setVisible(true);
+    } else if (numEvents === 2) {
+      ok(msg.json.hidden === false, 'mozHidden attribute correctly set');
+      // Allow some time in case we generate too many events
+      setTimeout(function() {
+        SimpleTest.finish();
+      }, 100);
+    } else {
+      ok(false, 'Too many mozhidden events');
+    }
+  }
+
+  function iframeLoaded() {
+    var mm = SpecialPowers.getBrowserFrameMessageManager(iframe1);
+    mm.addMessageListener('test:visibilitychange', recvVisibilityChanged);
+    mm.loadFrameScript('data:,(' + iframeScript.toString() + ')();', false);
+    iframe1.setVisible(false);
+  }
+
+  iframe1.addEventListener('mozbrowserloadend', iframeLoaded);
+}
+
+addEventListener('load', function() { SimpleTest.executeSoon(runTest); });
+
+</script>
+
+</body>
+</html>