Bug 1478669 - Fix addon data parsing for mac, r?aswan draft
authorShane Caraveo <scaraveo@mozilla.com>
Thu, 26 Jul 2018 12:42:06 -0300
changeset 823111 ca48c14ba740dd89f643f00aa5bbc5a5359e4ca5
parent 822981 4e6486b672b32aba075b704c6b1e41e8ccf7a135
push id117572
push usermixedpuppy@gmail.com
push dateThu, 26 Jul 2018 15:44:10 +0000
reviewersaswan
bugs1478669
milestone63.0a1
Bug 1478669 - Fix addon data parsing for mac, r?aswan AMO returns "mac" but the parsing ended up using "darwin". To test I also hack up the test environment appInfo, since it sets the platform to XPCShell. MozReview-Commit-ID: KxGVBpFazwC
toolkit/mozapps/extensions/internal/AddonRepository.jsm
toolkit/mozapps/extensions/test/xpcshell/data/test_AddonRepository_getAddonsByIDs.json
toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository.js
--- a/toolkit/mozapps/extensions/internal/AddonRepository.jsm
+++ b/toolkit/mozapps/extensions/internal/AddonRepository.jsm
@@ -572,22 +572,28 @@ var AddonRepository = {
    *
    * @param  aEntry
    *         An entry from the AMO search API to parse.
    * @return Result object containing the parsed AddonSearchResult
    */
   _parseAddon(aEntry) {
     let addon = new AddonSearchResult(aEntry.guid);
 
+    // AMO uses "mac", Firefox uses "darwin"
+    let platform = Services.appinfo.OS.toLowerCase();
+    if (platform === "darwin") {
+      platform = "mac";
+    }
+
     addon.name = aEntry.name;
     if (typeof aEntry.current_version == "object") {
       addon.version = String(aEntry.current_version.version);
       if (Array.isArray(aEntry.current_version.files)) {
         for (let file of aEntry.current_version.files) {
-          if (file.platform == "all" || file.platform == Services.appinfo.OS.toLowerCase()) {
+          if (file.platform == "all" || file.platform == platform) {
             if (file.url) {
               addon.sourceURI = NetUtil.newURI(file.url);
             }
             break;
           }
         }
       }
     }
--- a/toolkit/mozapps/extensions/test/xpcshell/data/test_AddonRepository_getAddonsByIDs.json
+++ b/toolkit/mozapps/extensions/test/xpcshell/data/test_AddonRepository_getAddonsByIDs.json
@@ -8,16 +8,21 @@
         {
             "name": "PASS",
             "type": "extension",
             "guid": "test1@tests.mozilla.org",
             "current_version": {
                 "version": "1.1",
                 "files": [
                     {
+                        "platform": "mac",
+                        "url": "http://example.com/addons/test_AddonRepository_2.xpi",
+                        "size": 5555
+                    },
+                    {
                         "platform": "all",
                         "url": "http://example.com/addons/test_AddonRepository_2.xpi",
                         "size": 5555
                     }
                 ]
             },
             "authors": [
                 {
--- a/toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository.js
@@ -10,16 +10,31 @@ var gServer = AddonTestUtils.createHttpS
 
 const PREF_GETADDONS_BROWSEADDONS        = "extensions.getAddons.browseAddons";
 const PREF_GETADDONS_BROWSESEARCHRESULTS = "extensions.getAddons.search.browseURL";
 
 const PORT          = gServer.identity.primaryPort;
 const BASE_URL      = "http://example.com";
 const DEFAULT_URL   = "about:blank";
 
+function getAppInfoName() {
+  switch (AppConstants.platform) {
+    case "macosx":
+      return "Darwin";
+    case "win":
+      return "Windows";
+    case "linux":
+      return "Linux";
+    default:
+      return "XPCShell";
+  }
+}
+
+let appInfoName = getAppInfoName();
+
 const ADDONS = [
   {
     id: "test_AddonRepository_1@tests.mozilla.org",
     version: "1.1",
     bootstrap: true,
 
     name: "XPI Add-on 1",
     description: "XPI Add-on 1 - Description",
@@ -130,21 +145,21 @@ var GET_RESULTS = [{
   icons:                  {}
 }];
 
 // Values for testing AddonRepository.getAddonsByIDs()
 var GET_TEST = {
   preference:       PREF_GETADDONS_BYIDS,
   preferenceValue:  BASE_URL + "/%OS%/%VERSION%/%IDS%",
   failedIDs:      ["test1@tests.mozilla.org"],
-  failedURL:        "/XPCShell/1/test1%40tests.mozilla.org",
+  failedURL:        `/${appInfoName}/1/test1%40tests.mozilla.org`,
   successfulIDs:  ["test1@tests.mozilla.org",
                      "{00000000-1111-2222-3333-444444444444}",
                      "test_AddonRepository_1@tests.mozilla.org"],
-  successfulURL:    "/XPCShell/1/test1%40tests.mozilla.org%2C" +
+  successfulURL:    `/${appInfoName}/1/test1%40tests.mozilla.org%2C` +
                     "%7B00000000-1111-2222-3333-444444444444%7D%2C" +
                     "test_AddonRepository_1%40tests.mozilla.org"
 };
 
 // Test that actual results and expected results are equal
 function check_results(aActualAddons, aExpectedAddons) {
   do_check_addons(aActualAddons, aExpectedAddons, ADDON_PROPERTIES);
 
@@ -156,17 +171,19 @@ function check_results(aActualAddons, aE
     if (aActualAddon.name != "PASS")
       do_throw(aActualAddon.id + " - invalid add-on name " + aActualAddon.name);
 
   });
 }
 
 add_task(async function setup() {
   // Setup for test
-  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
+  createAppInfo("xpcshell@tests.mozilla.org", appInfoName, "1", "1.9");
+  // Fixup appinfo.OS
+  gAppInfo.OS = appInfoName;
 
   let xpis = ADDONS.map(addon => createTempXPIFile(addon));
 
   // Register other add-on XPI files
   gServer.registerFile(INSTALL_URL2, xpis[1]);
   gServer.registerFile(INSTALL_URL3, xpis[2]);
 
   // Register files used to test search failure
@@ -201,17 +218,17 @@ add_task(async function test_1() {
     });
   }
 
   var urlTests = [{
     preferenceValue:  BASE_URL,
     expectedURL:      BASE_URL
   }, {
     preferenceValue:  BASE_URL + "/%OS%/%VERSION%",
-    expectedURL:      BASE_URL + "/XPCShell/1"
+    expectedURL:      BASE_URL + `/${appInfoName}/1`
   }];
 
   // Extra tests for AddonRepository.getSearchURL();
   var searchURLTests = [{
     searchTerms:      "test",
     preferenceValue:  BASE_URL + "/search?q=%TERMS%",
     expectedURL:      BASE_URL + "/search?q=test"
   }, {