Bug 1287007 - Make browser_ext_tabs_executeScript.js reliable r=billm
authorRob Wu <rob@robwu.nl>
Thu, 15 Sep 2016 15:53:03 -0700
changeset 428727 2b6e6f6614091834bcadaca619909b2a2033fa5c
parent 428726 437ef72ca3543d70dd1e106830ec5fee02e03d97
child 428728 daf166cb192a1d04c7a2703d89b5c626f356c070
push id33405
push userbcampen@mozilla.com
push dateMon, 24 Oct 2016 15:32:53 +0000
reviewersbillm
bugs1287007, 1293583
milestone52.0a1
Bug 1287007 - Make browser_ext_tabs_executeScript.js reliable r=billm The ProxyMessenger registers a listener whenever the first addon starts. Although the map does not have any listeners any more at the end of the test, the listener itself is not removed because the message-manager-close notification is not sent for them. So do not count these persistent message managers in the test. The actual message managers of interest are those associated with the (closed) tab. Note: When the test is run in isolation, it may still fail due to bugzil.la/1293583. See bug for work-around if you want to test. MozReview-Commit-ID: IiDHhmvQPcv
browser/components/extensions/test/browser/browser_ext_tabs_executeScript.js
--- a/browser/components/extensions/test/browser/browser_ext_tabs_executeScript.js
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript.js
@@ -1,17 +1,41 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
 add_task(function* testExecuteScript() {
   let {MessageChannel} = Cu.import("resource://gre/modules/MessageChannel.jsm", {});
 
-  let messageManagersSize = MessageChannel.messageManagers.size;
-  let responseManagersSize = MessageChannel.responseManagers.size;
+  function countMM(messageManagerMap) {
+    let count = 0;
+    // List of permanent message managers in the main process. We should not
+    // count them in the test because MessageChannel unsubscribes when the
+    // message manager closes, which never happens to these, of course.
+    let globalMMs = [
+      Services.mm,
+      Services.ppmm,
+      Services.ppmm.getChildAt(0),
+    ];
+    for (let mm of messageManagerMap.keys()) {
+      // Sanity check: mm is a message manager.
+      try {
+        mm.QueryInterface(Ci.nsIMessageSender);
+      } catch (e) {
+        mm.QueryInterface(Ci.nsIMessageBroadcaster);
+      }
+      if (!globalMMs.includes(mm)) {
+        ++count;
+      }
+    }
+    return count;
+  }
+
+  let messageManagersSize = countMM(MessageChannel.messageManagers);
+  let responseManagersSize = countMM(MessageChannel.responseManagers);
 
   const BASE = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/";
   const URL = BASE + "file_iframe_document.html";
   let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, URL, true);
 
   function background() {
     browser.tabs.query({active: true, currentWindow: true}).then(tabs => {
       return browser.webNavigation.getAllFrames({tabId: tabs[0].id});
@@ -198,12 +222,12 @@ add_task(function* testExecuteScript() {
   yield extension.awaitFinish("executeScript");
 
   yield extension.unload();
 
   yield BrowserTestUtils.removeTab(tab);
 
   // Make sure that we're not holding on to references to closed message
   // managers.
-  is(MessageChannel.messageManagers.size, messageManagersSize, "Message manager count");
-  is(MessageChannel.responseManagers.size, responseManagersSize, "Response manager count");
+  is(countMM(MessageChannel.messageManagers), messageManagersSize, "Message manager count");
+  is(countMM(MessageChannel.responseManagers), responseManagersSize, "Response manager count");
   is(MessageChannel.pendingResponses.size, 0, "Pending response count");
 });