Bug 1299053 - Refactor test/browser/browser_ext_getViews draft
authorRob Wu <rob@robwu.nl>
Sun, 23 Apr 2017 12:44:59 +0200
changeset 577556 8d371d922bbaa18f28f513c615507f236309d767
parent 577555 d0679fd0f0bd427941cde134c90ce90d38bbb13e
child 577557 7d7b67e26ca85508933b0ea2777c0e387782085d
push id58717
push userbmo:rob@robwu.nl
push dateSun, 14 May 2017 22:37:15 +0000
bugs1299053
milestone55.0a1
Bug 1299053 - Refactor test/browser/browser_ext_getViews I want to add more tests, based on tabId. Adding more positional arguments makes the code less readable, so I moved the check for windowId to a separate method. MozReview-Commit-ID: 2aTtMftoCZX
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,27 +9,22 @@ 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 windowId = args[0];
       let counts = {
-        "background": 0,
-        "tab": 0,
-        "popup": 0,
-        "kind": 0,
-        "window": 0,
+        background: 0,
+        tab: 0,
+        popup: 0,
+        kind: 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");
@@ -43,16 +38,20 @@ function genericChecker() {
       if (background) {
         browser.runtime.getBackgroundPage().then(view => {
           browser.test.assertEq(background, view, "runtime.getBackgroundPage() is correct");
           browser.test.sendMessage("counts", counts);
         });
       } else {
         browser.test.sendMessage("counts", counts);
       }
+    } else if (msg == kind + "-getViews-with-filter") {
+      let filter = args[0];
+      let count = browser.extension.getViews(filter).length;
+      browser.test.sendMessage("getViews-count", count);
     } else if (msg == kind + "-open-tab") {
       browser.tabs.create({windowId: args[0], url: browser.runtime.getURL("tab.html")});
     } else if (msg == kind + "-close-tab") {
       browser.tabs.query({
         windowId: args[0],
       }, tabs => {
         let tab = tabs.find(tab => tab.url.indexOf("tab.html") != -1);
         browser.tabs.remove(tab.id, () => {
@@ -109,36 +108,43 @@ add_task(async function() {
   let winId1 = windowTracker.getId(win1);
   let winId2 = windowTracker.getId(win2);
 
   async function openTab(winId) {
     extension.sendMessage("background-open-tab", winId);
     await extension.awaitMessage("tab-ready");
   }
 
-  async function checkViews(kind, tabCount, popupCount, kindCount, windowId = undefined, windowCount = 0) {
-    extension.sendMessage(kind + "-check-views", windowId);
+  async function checkViews(kind, tabCount, popupCount, kindCount) {
+    extension.sendMessage(kind + "-check-views");
     let counts = await 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");
+  }
+
+  async function checkViewsWithFilter(filter, expectedCount) {
+    extension.sendMessage("background-getViews-with-filter", filter);
+    let count = await extension.awaitMessage("getViews-count");
+    is(count, expectedCount, `count for ${JSON.stringify(filter)} correct`);
   }
 
   await checkViews("background", 0, 0, 0);
 
   await openTab(winId1);
 
-  await checkViews("background", 1, 0, 0, winId1, 1);
+  await checkViews("background", 1, 0, 0);
   await checkViews("tab", 1, 0, 1);
+  await checkViewsWithFilter({windowId: winId1}, 1);
 
   await openTab(winId2);
 
-  await checkViews("background", 2, 0, 0, winId2, 1);
+  await checkViews("background", 2, 0, 0);
+  await checkViewsWithFilter({windowId: winId2}, 1);
 
   async function triggerPopup(win, callback) {
     await clickBrowserAction(extension, win);
     await awaitExtensionPanel(extension, win);
 
     await extension.awaitMessage("popup-ready");
 
     await callback();
@@ -148,28 +154,31 @@ add_task(async 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.
   await new Promise(resolve => win1.setTimeout(resolve, 10));
 
   await triggerPopup(win1, async function() {
-    await checkViews("background", 2, 1, 0, winId1, 2);
+    await checkViews("background", 2, 1, 0);
     await checkViews("popup", 2, 1, 1);
+    await checkViewsWithFilter({windowId: winId1}, 2);
   });
 
   await triggerPopup(win2, async function() {
-    await checkViews("background", 2, 1, 0, winId2, 2);
+    await checkViews("background", 2, 1, 0);
     await checkViews("popup", 2, 1, 1);
+    await checkViewsWithFilter({windowId: winId2}, 2);
   });
 
   info("checking counts after popups");
 
-  await checkViews("background", 2, 0, 0, winId1, 1);
+  await checkViews("background", 2, 0, 0);
+  await checkViewsWithFilter({windowId: winId1}, 1);
 
   info("closing one tab");
 
   extension.sendMessage("background-close-tab", winId1);
   await extension.awaitMessage("closed");
 
   info("one tab closed, one remains");