Bug 1384964 - Fix browserAction.setTitle/getTitle on Android for tabId 0. r=mixedpuppy
authorLuca Greco <lgreco@mozilla.com>
Thu, 27 Jul 2017 20:59:38 +0200
changeset 420644 5c6a8f30184cbb1dc12be2cadf66d65db2518321
parent 420643 60076d86bb5cf9a03b8eff93931523e718e1ccea
child 420645 efc1451e4b0b694c33f32d6f0bc7911afa0e837c
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1384964
milestone56.0a1
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 1384964 - Fix browserAction.setTitle/getTitle on Android for tabId 0. r=mixedpuppy MozReview-Commit-ID: AOdxP0btwYT
mobile/android/components/extensions/ext-browserAction.js
mobile/android/components/extensions/test/mochitest/test_ext_browserAction_getTitle_setTitle.html
--- a/mobile/android/components/extensions/ext-browserAction.js
+++ b/mobile/android/components/extensions/ext-browserAction.js
@@ -140,36 +140,43 @@ this.browserAction = class extends Exten
       browserActionMap.delete(extension);
     }
   }
 
   getAPI(context) {
     const {extension} = context;
     const {tabManager} = extension;
 
+    function getTab(tabId) {
+      if (tabId !== null) {
+        return tabTracker.getTab(tabId);
+      }
+      return null;
+    }
+
     return {
       browserAction: {
         onClicked: new EventManager(context, "browserAction.onClicked", fire => {
           let listener = (event, tab) => {
             fire.async(tabManager.convert(tab));
           };
           browserActionMap.get(extension).on("click", listener);
           return () => {
             browserActionMap.get(extension).off("click", listener);
           };
         }).api(),
 
         setTitle: function(details) {
           let {tabId, title} = details;
-          let tab = tabId ? tabTracker.getTab(tabId) : null;
+          let tab = getTab(tabId);
           browserActionMap.get(extension).setProperty(tab, "name", title);
         },
 
         getTitle: function(details) {
           let {tabId} = details;
-          let tab = tabId ? tabTracker.getTab(tabId) : null;
+          let tab = getTab(tabId);
           let title = browserActionMap.get(extension).getProperty(tab, "name");
           return Promise.resolve(title);
         },
       },
     };
   }
 };
--- a/mobile/android/components/extensions/test/mochitest/test_ext_browserAction_getTitle_setTitle.html
+++ b/mobile/android/components/extensions/test/mochitest/test_ext_browserAction_getTitle_setTitle.html
@@ -44,16 +44,28 @@ add_task(async function test_setTitle_an
       // Set the title for the new tab and test that getTitle returns the correct title.
       await browser.browserAction.setTitle({tabId: tab.id, title});
       tabTitle = await browser.browserAction.getTitle({tabId: tab.id});
       browser.test.assertEq(title, tabTitle, "Expected the new tab title to be returned");
 
       return tab;
     }
 
+    // Test on tabId 0
+    // Test that the default title is returned before the title is set for the tab.
+    let tabTitle = await browser.browserAction.getTitle({tabId: 0});
+    browser.test.assertEq("Browser Action", tabTitle,
+                          "Expected the default title to be returned for tabId 0");
+
+    // Set the title for the new tab and test that getTitle returns the correct title.
+    await browser.browserAction.setTitle({tabId: 0, title: "tab 0"});
+    tabTitle = await browser.browserAction.getTitle({tabId: 0});
+    browser.test.assertEq("tab 0", tabTitle,
+                          "Expected the new tab title to be returned for tabId 0");
+
     // Create and test 3 new tabs.
     let tab1 = await createAndTestNewTab("tab 1", "about:blank");
     let tab2 = await createAndTestNewTab("tab 2", "about:blank");
     let tab3 = await createAndTestNewTab("tab 3", "about:blank");
 
     // Test the default title again.
     let title = await browser.browserAction.getTitle({});
     browser.test.assertEq("Browser Action", title, "Expected the default title to be returned");