Bug 1190322 - Test coverage for |extension| extension API, r?kmag draft
authorbsilverberg <bsilverberg@mozilla.com>
Wed, 06 Apr 2016 13:24:12 -0400
changeset 349404 a92a58d86638d65c51adcb83a5b266fa98b4eac0
parent 349384 e847cfcb315f511f4928b03fd47dcf57aad05e1e
child 518090 d02a928d5f1150172f2526a84ee75635692e06e6
push id15076
push userbmo:bob.silverberg@gmail.com
push dateMon, 11 Apr 2016 13:53:32 +0000
reviewerskmag
bugs1190322
milestone48.0a1
Bug 1190322 - Test coverage for |extension| extension API, r?kmag Add coverage for: * The |getViews| API method with a |type| property. * The |getViews| API method with a |windowId| property. MozReview-Commit-ID: Evoi8au0wzW
browser/components/extensions/test/browser/browser_ext_getViews.js
--- a/browser/components/extensions/test/browser/browser_ext_getViews.js
+++ b/browser/components/extensions/test/browser/browser_ext_getViews.js
@@ -9,22 +9,31 @@ function genericChecker() {
     kind = "popup";
   } else if (path.indexOf("tab") != -1) {
     kind = "tab";
   }
   window.kind = kind;
 
   browser.test.onMessage.addListener((msg, ...args) => {
     if (msg == kind + "-check-views") {
-      let views = browser.extension.getViews();
+      let windowId = args[0];
       let counts = {
         "background": 0,
         "tab": 0,
         "popup": 0,
+        "kind": 0,
+        "window": 0,
       };
+      if (Number.isInteger(windowId)) {
+        counts.window = browser.extension.getViews({windowId: windowId}).length;
+      }
+      if (kind !== "background") {
+        counts.kind = browser.extension.getViews({type: kind}).length;
+      }
+      let views = browser.extension.getViews();
       let background;
       for (let i = 0; i < views.length; i++) {
         let view = views[i];
         browser.test.assertTrue(view.kind in counts, "view type is valid");
         counts[view.kind]++;
         if (view.kind == "background") {
           browser.test.assertTrue(view === browser.extension.getBackgroundPage(),
                                   "background page is correct");
@@ -100,34 +109,36 @@ add_task(function* () {
   let winId1 = WindowManager.getId(win1);
   let winId2 = WindowManager.getId(win2);
 
   function* openTab(winId) {
     extension.sendMessage("background-open-tab", winId);
     yield extension.awaitMessage("tab-ready");
   }
 
-  function* checkViews(kind, tabCount, popupCount) {
-    extension.sendMessage(kind + "-check-views");
+  function* checkViews(kind, tabCount, popupCount, kindCount, windowId = undefined, windowCount = 0) {
+    extension.sendMessage(kind + "-check-views", windowId);
     let counts = yield extension.awaitMessage("counts");
     is(counts.background, 1, "background count correct");
     is(counts.tab, tabCount, "tab count correct");
     is(counts.popup, popupCount, "popup count correct");
+    is(counts.kind, kindCount, "count for type correct");
+    is(counts.window, windowCount, "count for window correct");
   }
 
-  yield checkViews("background", 0, 0);
+  yield checkViews("background", 0, 0, 0);
 
   yield openTab(winId1);
 
-  yield checkViews("background", 1, 0);
-  yield checkViews("tab", 1, 0);
+  yield checkViews("background", 1, 0, 0, winId1, 1);
+  yield checkViews("tab", 1, 0, 1);
 
   yield openTab(winId2);
 
-  yield checkViews("background", 2, 0);
+  yield checkViews("background", 2, 0, 0, winId2, 1);
 
   function* triggerPopup(win, callback) {
     yield clickBrowserAction(extension, win);
 
     yield extension.awaitMessage("popup-ready");
 
     yield callback();
 
@@ -136,51 +147,51 @@ add_task(function* () {
 
   // The popup occasionally closes prematurely if we open it immediately here.
   // I'm not sure what causes it to close (it's something internal, and seems to
   // be focus-related, but it's not caused by JS calling hidePopup), but even a
   // short timeout seems to consistently fix it.
   yield new Promise(resolve => win1.setTimeout(resolve, 10));
 
   yield triggerPopup(win1, function* () {
-    yield checkViews("background", 2, 1);
-    yield checkViews("popup", 2, 1);
+    yield checkViews("background", 2, 1, 0, winId1, 2);
+    yield checkViews("popup", 2, 1, 1);
   });
 
   yield triggerPopup(win2, function* () {
-    yield checkViews("background", 2, 1);
-    yield checkViews("popup", 2, 1);
+    yield checkViews("background", 2, 1, 0, winId2, 2);
+    yield checkViews("popup", 2, 1, 1);
   });
 
   info("checking counts after popups");
 
-  yield checkViews("background", 2, 0);
+  yield checkViews("background", 2, 0, 0, winId1, 1);
 
   info("closing one tab");
 
   extension.sendMessage("background-close-tab", winId1);
   yield extension.awaitMessage("closed");
 
   info("one tab closed, one remains");
 
-  yield checkViews("background", 1, 0);
+  yield checkViews("background", 1, 0, 0);
 
   info("opening win1 popup");
 
   yield triggerPopup(win1, function* () {
-    yield checkViews("background", 1, 1);
-    yield checkViews("tab", 1, 1);
-    yield checkViews("popup", 1, 1);
+    yield checkViews("background", 1, 1, 0);
+    yield checkViews("tab", 1, 1, 1);
+    yield checkViews("popup", 1, 1, 1);
   });
 
   info("opening win2 popup");
 
   yield triggerPopup(win2, function* () {
-    yield checkViews("background", 1, 1);
-    yield checkViews("tab", 1, 1);
-    yield checkViews("popup", 1, 1);
+    yield checkViews("background", 1, 1, 0);
+    yield checkViews("tab", 1, 1, 1);
+    yield checkViews("popup", 1, 1, 1);
   });
 
   yield extension.unload();
 
   yield BrowserTestUtils.closeWindow(win1);
   yield BrowserTestUtils.closeWindow(win2);
 });