toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository_cache.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository_cache.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,735 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.6 + */
     1.7 +
     1.8 +// Tests caching in AddonRepository.jsm
     1.9 +
    1.10 +Components.utils.import("resource://gre/modules/addons/AddonRepository.jsm");
    1.11 +
    1.12 +Components.utils.import("resource://testing-common/httpd.js");
    1.13 +let gServer;
    1.14 +
    1.15 +const PORT      = 4444;
    1.16 +const BASE_URL  = "http://localhost:" + PORT;
    1.17 +
    1.18 +const PREF_GETADDONS_CACHE_ENABLED = "extensions.getAddons.cache.enabled";
    1.19 +const PREF_GETADDONS_CACHE_TYPES   = "extensions.getAddons.cache.types";
    1.20 +const GETADDONS_RESULTS            = BASE_URL + "/data/test_AddonRepository_cache.xml";
    1.21 +const GETADDONS_EMPTY              = BASE_URL + "/data/test_AddonRepository_empty.xml";
    1.22 +const GETADDONS_FAILED             = BASE_URL + "/data/test_AddonRepository_failed.xml";
    1.23 +
    1.24 +const FILE_DATABASE = "addons.json";
    1.25 +const ADDON_NAMES = ["test_AddonRepository_1",
    1.26 +                     "test_AddonRepository_2",
    1.27 +                     "test_AddonRepository_3"];
    1.28 +const ADDON_IDS = ADDON_NAMES.map(function(aName) aName + "@tests.mozilla.org");
    1.29 +const ADDON_FILES = ADDON_NAMES.map(do_get_addon);
    1.30 +
    1.31 +const PREF_ADDON0_CACHE_ENABLED = "extensions." + ADDON_IDS[0] + ".getAddons.cache.enabled";
    1.32 +const PREF_ADDON1_CACHE_ENABLED = "extensions." + ADDON_IDS[1] + ".getAddons.cache.enabled";
    1.33 +
    1.34 +// Properties of an individual add-on that should be checked
    1.35 +// Note: size and updateDate are checked separately
    1.36 +const ADDON_PROPERTIES = ["id", "type", "name", "version", "creator",
    1.37 +                          "developers", "translators", "contributors",
    1.38 +                          "description", "fullDescription",
    1.39 +                          "developerComments", "eula", "iconURL", "icons",
    1.40 +                          "screenshots", "homepageURL", "supportURL",
    1.41 +                          "optionsURL", "aboutURL", "contributionURL",
    1.42 +                          "contributionAmount", "averageRating", "reviewCount",
    1.43 +                          "reviewURL", "totalDownloads", "weeklyDownloads",
    1.44 +                          "dailyUsers", "sourceURI", "repositoryStatus",
    1.45 +                          "compatibilityOverrides"];
    1.46 +
    1.47 +// The size and updateDate properties are annoying to test for XPI add-ons.
    1.48 +// However, since we only care about whether the repository value vs. the
    1.49 +// XPI value is used, we can just test if the property value matches
    1.50 +// the repository value
    1.51 +const REPOSITORY_SIZE       = 9;
    1.52 +const REPOSITORY_UPDATEDATE = 9;
    1.53 +
    1.54 +// Get the URI of a subfile locating directly in the folder of
    1.55 +// the add-on corresponding to the specified id
    1.56 +function get_subfile_uri(aId, aFilename) {
    1.57 +  let file = gProfD.clone();
    1.58 +  file.append("extensions");
    1.59 +  return do_get_addon_root_uri(file, aId) + aFilename;
    1.60 +}
    1.61 +
    1.62 +
    1.63 +// Expected repository add-ons
    1.64 +const REPOSITORY_ADDONS = [{
    1.65 +  id:                     ADDON_IDS[0],
    1.66 +  type:                   "extension",
    1.67 +  name:                   "Repo Add-on 1",
    1.68 +  version:                "2.1",
    1.69 +  creator:                {
    1.70 +                            name: "Repo Add-on 1 - Creator",
    1.71 +                            url:  BASE_URL + "/repo/1/creator.html"
    1.72 +                          },
    1.73 +  developers:             [{
    1.74 +                            name: "Repo Add-on 1 - First Developer",
    1.75 +                            url:  BASE_URL + "/repo/1/firstDeveloper.html"
    1.76 +                          }, {
    1.77 +                            name: "Repo Add-on 1 - Second Developer",
    1.78 +                            url:  BASE_URL + "/repo/1/secondDeveloper.html"
    1.79 +                          }],
    1.80 +  description:            "Repo Add-on 1 - Description\nSecond line",
    1.81 +  fullDescription:        "Repo Add-on 1 - Full Description & some extra",
    1.82 +  developerComments:      "Repo Add-on 1\nDeveloper Comments",
    1.83 +  eula:                   "Repo Add-on 1 - EULA",
    1.84 +  iconURL:                BASE_URL + "/repo/1/icon.png",
    1.85 +  icons:                  { "32": BASE_URL + "/repo/1/icon.png" },
    1.86 +  homepageURL:            BASE_URL + "/repo/1/homepage.html",
    1.87 +  supportURL:             BASE_URL + "/repo/1/support.html",
    1.88 +  contributionURL:        BASE_URL + "/repo/1/meetDevelopers.html",
    1.89 +  contributionAmount:     "$11.11",
    1.90 +  averageRating:          1,
    1.91 +  reviewCount:            1111,
    1.92 +  reviewURL:              BASE_URL + "/repo/1/review.html",
    1.93 +  totalDownloads:         2221,
    1.94 +  weeklyDownloads:        3331,
    1.95 +  dailyUsers:             4441,
    1.96 +  sourceURI:              BASE_URL + "/repo/1/install.xpi",
    1.97 +  repositoryStatus:       4,
    1.98 +  compatibilityOverrides: [{
    1.99 +                            type: "incompatible",
   1.100 +                            minVersion: 0.1,
   1.101 +                            maxVersion: 0.2,
   1.102 +                            appID: "xpcshell@tests.mozilla.org",
   1.103 +                            appMinVersion: 3.0,
   1.104 +                            appMaxVersion: 4.0
   1.105 +                          }, {
   1.106 +                            type: "incompatible",
   1.107 +                            minVersion: 0.2,
   1.108 +                            maxVersion: 0.3,
   1.109 +                            appID: "xpcshell@tests.mozilla.org",
   1.110 +                            appMinVersion: 5.0,
   1.111 +                            appMaxVersion: 6.0
   1.112 +                          }]
   1.113 +}, {
   1.114 +  id:                     ADDON_IDS[1],
   1.115 +  type:                   "theme",
   1.116 +  name:                   "Repo Add-on 2",
   1.117 +  version:                "2.2",
   1.118 +  creator:                {
   1.119 +                            name: "Repo Add-on 2 - Creator",
   1.120 +                            url:  BASE_URL + "/repo/2/creator.html"
   1.121 +                          },
   1.122 +  developers:             [{
   1.123 +                            name: "Repo Add-on 2 - First Developer",
   1.124 +                            url:  BASE_URL + "/repo/2/firstDeveloper.html"
   1.125 +                          }, {
   1.126 +                            name: "Repo Add-on 2 - Second Developer",
   1.127 +                            url:  BASE_URL + "/repo/2/secondDeveloper.html"
   1.128 +                          }],
   1.129 +  description:            "Repo Add-on 2 - Description",
   1.130 +  fullDescription:        "Repo Add-on 2 - Full Description",
   1.131 +  developerComments:      "Repo Add-on 2 - Developer Comments",
   1.132 +  eula:                   "Repo Add-on 2 - EULA",
   1.133 +  iconURL:                BASE_URL + "/repo/2/icon.png",
   1.134 +  icons:                  { "32": BASE_URL + "/repo/2/icon.png" },
   1.135 +  screenshots:            [{
   1.136 +                            url:          BASE_URL + "/repo/2/firstFull.png",
   1.137 +                            thumbnailURL: BASE_URL + "/repo/2/firstThumbnail.png",
   1.138 +                            caption:      "Repo Add-on 2 - First Caption"
   1.139 +                          } , {
   1.140 +                            url:          BASE_URL + "/repo/2/secondFull.png",
   1.141 +                            thumbnailURL: BASE_URL + "/repo/2/secondThumbnail.png",
   1.142 +                            caption:      "Repo Add-on 2 - Second Caption"
   1.143 +                          }],
   1.144 +  homepageURL:            BASE_URL + "/repo/2/homepage.html",
   1.145 +  supportURL:             BASE_URL + "/repo/2/support.html",
   1.146 +  contributionURL:        BASE_URL + "/repo/2/meetDevelopers.html",
   1.147 +  contributionAmount:     null,
   1.148 +  averageRating:          2,
   1.149 +  reviewCount:            1112,
   1.150 +  reviewURL:              BASE_URL + "/repo/2/review.html",
   1.151 +  totalDownloads:         2222,
   1.152 +  weeklyDownloads:        3332,
   1.153 +  dailyUsers:             4442,
   1.154 +  sourceURI:              BASE_URL + "/repo/2/install.xpi",
   1.155 +  repositoryStatus:       9
   1.156 +}, {
   1.157 +  id:                     ADDON_IDS[2],
   1.158 +  name:                   "Repo Add-on 3",
   1.159 +  version:                "2.3",
   1.160 +  iconURL:                BASE_URL + "/repo/3/icon.png",
   1.161 +  icons:                  { "32": BASE_URL + "/repo/3/icon.png" },
   1.162 +  screenshots:            [{
   1.163 +                            url:          BASE_URL + "/repo/3/firstFull.png",
   1.164 +                            thumbnailURL: BASE_URL + "/repo/3/firstThumbnail.png",
   1.165 +                            caption:      "Repo Add-on 3 - First Caption"
   1.166 +                          } , {
   1.167 +                            url:          BASE_URL + "/repo/3/secondFull.png",
   1.168 +                            thumbnailURL: BASE_URL + "/repo/3/secondThumbnail.png",
   1.169 +                            caption:      "Repo Add-on 3 - Second Caption"
   1.170 +                          }]
   1.171 +}];
   1.172 +
   1.173 +
   1.174 +// Expected add-ons when not using cache
   1.175 +const WITHOUT_CACHE = [{
   1.176 +  id:                     ADDON_IDS[0],
   1.177 +  type:                   "extension",
   1.178 +  name:                   "XPI Add-on 1",
   1.179 +  version:                "1.1",
   1.180 +  creator:                { name: "XPI Add-on 1 - Creator" },
   1.181 +  developers:             [{ name: "XPI Add-on 1 - First Developer" },
   1.182 +                           { name: "XPI Add-on 1 - Second Developer" }],
   1.183 +  translators:            [{ name: "XPI Add-on 1 - First Translator" },
   1.184 +                           { name: "XPI Add-on 1 - Second Translator" }],
   1.185 +  contributors:           [{ name: "XPI Add-on 1 - First Contributor" },
   1.186 +                           { name: "XPI Add-on 1 - Second Contributor" }],
   1.187 +  description:            "XPI Add-on 1 - Description",
   1.188 +  iconURL:                BASE_URL + "/xpi/1/icon.png",
   1.189 +  icons:                  { "32": BASE_URL + "/xpi/1/icon.png" },
   1.190 +  homepageURL:            BASE_URL + "/xpi/1/homepage.html",
   1.191 +  optionsURL:             BASE_URL + "/xpi/1/options.html",
   1.192 +  aboutURL:               BASE_URL + "/xpi/1/about.html",
   1.193 +  sourceURI:              NetUtil.newURI(ADDON_FILES[0]).spec
   1.194 +}, {
   1.195 +  id:                     ADDON_IDS[1],
   1.196 +  type:                   "theme",
   1.197 +  name:                   "XPI Add-on 2",
   1.198 +  version:                "1.2",
   1.199 +  sourceURI:              NetUtil.newURI(ADDON_FILES[1]).spec,
   1.200 +  icons:                  {}
   1.201 +}, {
   1.202 +  id:                     ADDON_IDS[2],
   1.203 +  type:                   "theme",
   1.204 +  name:                   "XPI Add-on 3",
   1.205 +  version:                "1.3",
   1.206 +  get iconURL () {
   1.207 +    return get_subfile_uri(ADDON_IDS[2], "icon.png");
   1.208 +  },
   1.209 +  get icons () {
   1.210 +    return { "32": get_subfile_uri(ADDON_IDS[2], "icon.png") };
   1.211 +  },
   1.212 +  screenshots:            [{ get url () { return get_subfile_uri(ADDON_IDS[2], "preview.png"); } }],
   1.213 +  sourceURI:              NetUtil.newURI(ADDON_FILES[2]).spec
   1.214 +}];
   1.215 +
   1.216 +
   1.217 +// Expected add-ons when using cache
   1.218 +const WITH_CACHE = [{
   1.219 +  id:                     ADDON_IDS[0],
   1.220 +  type:                   "extension",
   1.221 +  name:                   "XPI Add-on 1",
   1.222 +  version:                "1.1",
   1.223 +  creator:                {
   1.224 +                            name: "Repo Add-on 1 - Creator",
   1.225 +                            url:  BASE_URL + "/repo/1/creator.html"
   1.226 +                          },
   1.227 +  developers:             [{ name: "XPI Add-on 1 - First Developer" },
   1.228 +                           { name: "XPI Add-on 1 - Second Developer" }],
   1.229 +  translators:            [{ name: "XPI Add-on 1 - First Translator" },
   1.230 +                           { name: "XPI Add-on 1 - Second Translator" }],
   1.231 +  contributors:           [{ name: "XPI Add-on 1 - First Contributor" },
   1.232 +                           { name: "XPI Add-on 1 - Second Contributor" }],
   1.233 +  description:            "XPI Add-on 1 - Description",
   1.234 +  fullDescription:        "Repo Add-on 1 - Full Description & some extra",
   1.235 +  developerComments:      "Repo Add-on 1\nDeveloper Comments",
   1.236 +  eula:                   "Repo Add-on 1 - EULA",
   1.237 +  iconURL:                BASE_URL + "/xpi/1/icon.png",
   1.238 +  icons:                  { "32": BASE_URL + "/xpi/1/icon.png" },
   1.239 +  homepageURL:            BASE_URL + "/xpi/1/homepage.html",
   1.240 +  supportURL:             BASE_URL + "/repo/1/support.html",
   1.241 +  optionsURL:             BASE_URL + "/xpi/1/options.html",
   1.242 +  aboutURL:               BASE_URL + "/xpi/1/about.html",
   1.243 +  contributionURL:        BASE_URL + "/repo/1/meetDevelopers.html",
   1.244 +  contributionAmount:     "$11.11",
   1.245 +  averageRating:          1,
   1.246 +  reviewCount:            1111,
   1.247 +  reviewURL:              BASE_URL + "/repo/1/review.html",
   1.248 +  totalDownloads:         2221,
   1.249 +  weeklyDownloads:        3331,
   1.250 +  dailyUsers:             4441,
   1.251 +  sourceURI:              NetUtil.newURI(ADDON_FILES[0]).spec,
   1.252 +  repositoryStatus:       4,
   1.253 +  compatibilityOverrides: [{
   1.254 +                            type: "incompatible",
   1.255 +                            minVersion: 0.1,
   1.256 +                            maxVersion: 0.2,
   1.257 +                            appID: "xpcshell@tests.mozilla.org",
   1.258 +                            appMinVersion: 3.0,
   1.259 +                            appMaxVersion: 4.0
   1.260 +                          }, {
   1.261 +                            type: "incompatible",
   1.262 +                            minVersion: 0.2,
   1.263 +                            maxVersion: 0.3,
   1.264 +                            appID: "xpcshell@tests.mozilla.org",
   1.265 +                            appMinVersion: 5.0,
   1.266 +                            appMaxVersion: 6.0
   1.267 +                          }]
   1.268 +}, {
   1.269 +  id:                     ADDON_IDS[1],
   1.270 +  type:                   "theme",
   1.271 +  name:                   "XPI Add-on 2",
   1.272 +  version:                "1.2",
   1.273 +  creator:                {
   1.274 +                            name: "Repo Add-on 2 - Creator",
   1.275 +                            url:  BASE_URL + "/repo/2/creator.html"
   1.276 +                          },
   1.277 +  developers:             [{
   1.278 +                            name: "Repo Add-on 2 - First Developer",
   1.279 +                            url:  BASE_URL + "/repo/2/firstDeveloper.html"
   1.280 +                          }, {
   1.281 +                            name: "Repo Add-on 2 - Second Developer",
   1.282 +                            url:  BASE_URL + "/repo/2/secondDeveloper.html"
   1.283 +                          }],
   1.284 +  description:            "Repo Add-on 2 - Description",
   1.285 +  fullDescription:        "Repo Add-on 2 - Full Description",
   1.286 +  developerComments:      "Repo Add-on 2 - Developer Comments",
   1.287 +  eula:                   "Repo Add-on 2 - EULA",
   1.288 +  iconURL:                BASE_URL + "/repo/2/icon.png",
   1.289 +  icons:                  { "32": BASE_URL + "/repo/2/icon.png" },
   1.290 +  screenshots:            [{
   1.291 +                            url:          BASE_URL + "/repo/2/firstFull.png",
   1.292 +                            thumbnailURL: BASE_URL + "/repo/2/firstThumbnail.png",
   1.293 +                            caption:      "Repo Add-on 2 - First Caption"
   1.294 +                          } , {
   1.295 +                            url:          BASE_URL + "/repo/2/secondFull.png",
   1.296 +                            thumbnailURL: BASE_URL + "/repo/2/secondThumbnail.png",
   1.297 +                            caption:      "Repo Add-on 2 - Second Caption"
   1.298 +                          }],
   1.299 +  homepageURL:            BASE_URL + "/repo/2/homepage.html",
   1.300 +  supportURL:             BASE_URL + "/repo/2/support.html",
   1.301 +  contributionURL:        BASE_URL + "/repo/2/meetDevelopers.html",
   1.302 +  contributionAmount:     null,
   1.303 +  averageRating:          2,
   1.304 +  reviewCount:            1112,
   1.305 +  reviewURL:              BASE_URL + "/repo/2/review.html",
   1.306 +  totalDownloads:         2222,
   1.307 +  weeklyDownloads:        3332,
   1.308 +  dailyUsers:             4442,
   1.309 +  sourceURI:              NetUtil.newURI(ADDON_FILES[1]).spec,
   1.310 +  repositoryStatus:       9
   1.311 +}, {
   1.312 +  id:                     ADDON_IDS[2],
   1.313 +  type:                   "theme",
   1.314 +  name:                   "XPI Add-on 3",
   1.315 +  version:                "1.3",
   1.316 +  get iconURL () {
   1.317 +    return get_subfile_uri(ADDON_IDS[2], "icon.png");
   1.318 +  },
   1.319 +  get icons () {
   1.320 +    return { "32": get_subfile_uri(ADDON_IDS[2], "icon.png") };
   1.321 +  },
   1.322 +  screenshots:            [{
   1.323 +                            url:          BASE_URL + "/repo/3/firstFull.png",
   1.324 +                            thumbnailURL: BASE_URL + "/repo/3/firstThumbnail.png",
   1.325 +                            caption:      "Repo Add-on 3 - First Caption"
   1.326 +                          } , {
   1.327 +                            url:          BASE_URL + "/repo/3/secondFull.png",
   1.328 +                            thumbnailURL: BASE_URL + "/repo/3/secondThumbnail.png",
   1.329 +                            caption:      "Repo Add-on 3 - Second Caption"
   1.330 +                          }],
   1.331 +  sourceURI:              NetUtil.newURI(ADDON_FILES[2]).spec
   1.332 +}];
   1.333 +
   1.334 +// Expected add-ons when using cache
   1.335 +const WITH_EXTENSION_CACHE = [{
   1.336 +  id:                     ADDON_IDS[0],
   1.337 +  type:                   "extension",
   1.338 +  name:                   "XPI Add-on 1",
   1.339 +  version:                "1.1",
   1.340 +  creator:                {
   1.341 +                            name: "Repo Add-on 1 - Creator",
   1.342 +                            url:  BASE_URL + "/repo/1/creator.html"
   1.343 +                          },
   1.344 +  developers:             [{ name: "XPI Add-on 1 - First Developer" },
   1.345 +                           { name: "XPI Add-on 1 - Second Developer" }],
   1.346 +  translators:            [{ name: "XPI Add-on 1 - First Translator" },
   1.347 +                           { name: "XPI Add-on 1 - Second Translator" }],
   1.348 +  contributors:           [{ name: "XPI Add-on 1 - First Contributor" },
   1.349 +                           { name: "XPI Add-on 1 - Second Contributor" }],
   1.350 +  description:            "XPI Add-on 1 - Description",
   1.351 +  fullDescription:        "Repo Add-on 1 - Full Description & some extra",
   1.352 +  developerComments:      "Repo Add-on 1\nDeveloper Comments",
   1.353 +  eula:                   "Repo Add-on 1 - EULA",
   1.354 +  iconURL:                BASE_URL + "/xpi/1/icon.png",
   1.355 +  icons:                  { "32": BASE_URL + "/xpi/1/icon.png" },
   1.356 +  homepageURL:            BASE_URL + "/xpi/1/homepage.html",
   1.357 +  supportURL:             BASE_URL + "/repo/1/support.html",
   1.358 +  optionsURL:             BASE_URL + "/xpi/1/options.html",
   1.359 +  aboutURL:               BASE_URL + "/xpi/1/about.html",
   1.360 +  contributionURL:        BASE_URL + "/repo/1/meetDevelopers.html",
   1.361 +  contributionAmount:     "$11.11",
   1.362 +  averageRating:          1,
   1.363 +  reviewCount:            1111,
   1.364 +  reviewURL:              BASE_URL + "/repo/1/review.html",
   1.365 +  totalDownloads:         2221,
   1.366 +  weeklyDownloads:        3331,
   1.367 +  dailyUsers:             4441,
   1.368 +  sourceURI:              NetUtil.newURI(ADDON_FILES[0]).spec,
   1.369 +  repositoryStatus:       4,
   1.370 +  compatibilityOverrides: [{
   1.371 +                            type: "incompatible",
   1.372 +                            minVersion: 0.1,
   1.373 +                            maxVersion: 0.2,
   1.374 +                            appID: "xpcshell@tests.mozilla.org",
   1.375 +                            appMinVersion: 3.0,
   1.376 +                            appMaxVersion: 4.0
   1.377 +                          }, {
   1.378 +                            type: "incompatible",
   1.379 +                            minVersion: 0.2,
   1.380 +                            maxVersion: 0.3,
   1.381 +                            appID: "xpcshell@tests.mozilla.org",
   1.382 +                            appMinVersion: 5.0,
   1.383 +                            appMaxVersion: 6.0
   1.384 +                          }]
   1.385 +}, {
   1.386 +  id:                     ADDON_IDS[1],
   1.387 +  type:                   "theme",
   1.388 +  name:                   "XPI Add-on 2",
   1.389 +  version:                "1.2",
   1.390 +  sourceURI:              NetUtil.newURI(ADDON_FILES[1]).spec,
   1.391 +  icons:                  {}
   1.392 +}, {
   1.393 +  id:                     ADDON_IDS[2],
   1.394 +  type:                   "theme",
   1.395 +  name:                   "XPI Add-on 3",
   1.396 +  version:                "1.3",
   1.397 +  get iconURL () {
   1.398 +    return get_subfile_uri(ADDON_IDS[2], "icon.png");
   1.399 +  },
   1.400 +  get icons () {
   1.401 +    return { "32": get_subfile_uri(ADDON_IDS[2], "icon.png") };
   1.402 +  },
   1.403 +  screenshots:            [{ get url () { return get_subfile_uri(ADDON_IDS[2], "preview.png"); } }],
   1.404 +  sourceURI:              NetUtil.newURI(ADDON_FILES[2]).spec
   1.405 +}];
   1.406 +
   1.407 +
   1.408 +/*
   1.409 + * Trigger an AddonManager background update check
   1.410 + *
   1.411 + * @return Promise{null}
   1.412 + *         Resolves when the background update notification is received
   1.413 + */
   1.414 +function trigger_background_update() {
   1.415 +  return new Promise((resolve, reject) => {
   1.416 +    Services.obs.addObserver({
   1.417 +      observe: function(aSubject, aTopic, aData) {
   1.418 +        do_print("Observed " + aTopic);
   1.419 +        Services.obs.removeObserver(this, "addons-background-update-complete");
   1.420 +        resolve();
   1.421 +      }
   1.422 +    }, "addons-background-update-complete", false);
   1.423 +
   1.424 +    gInternalManager.notify(null);
   1.425 +  });
   1.426 +}
   1.427 +
   1.428 +let gDBFile = gProfD.clone();
   1.429 +gDBFile.append(FILE_DATABASE);
   1.430 +
   1.431 +/*
   1.432 + * Check the actual add-on results against the expected add-on results
   1.433 + *
   1.434 + * @param  aActualAddons
   1.435 + *         The array of actual add-ons to check
   1.436 + * @param  aExpectedAddons
   1.437 + *         The array of expected add-ons to check against
   1.438 + * @param  aFromRepository
   1.439 + *         An optional boolean representing if the add-ons are from
   1.440 + *         the repository
   1.441 + */
   1.442 +function check_results(aActualAddons, aExpectedAddons, aFromRepository) {
   1.443 +  aFromRepository = !!aFromRepository;
   1.444 +
   1.445 +  do_check_addons(aActualAddons, aExpectedAddons, ADDON_PROPERTIES);
   1.446 +
   1.447 +  // Separately test size and updateDate (they should only be equal to the
   1.448 +  // REPOSITORY values if they are from the repository)
   1.449 +  aActualAddons.forEach(function(aActualAddon) {
   1.450 +    if (aActualAddon.size)
   1.451 +      do_check_eq(aActualAddon.size === REPOSITORY_SIZE, aFromRepository);
   1.452 +
   1.453 +    if (aActualAddon.updateDate) {
   1.454 +      let time = aActualAddon.updateDate.getTime();
   1.455 +      do_check_eq(time === 1000 * REPOSITORY_UPDATEDATE, aFromRepository);
   1.456 +    }
   1.457 +  });
   1.458 +}
   1.459 +
   1.460 +/*
   1.461 + * Check the add-ons in the cache. This function also tests
   1.462 + * AddonRepository.getCachedAddonByID()
   1.463 + *
   1.464 + * @param  aExpectedToFind
   1.465 + *         An array of booleans representing which REPOSITORY_ADDONS are
   1.466 + *         expected to be found in the cache
   1.467 + * @param  aExpectedImmediately
   1.468 + *         A boolean representing if results from the cache are expected
   1.469 + *         immediately. Results are not immediate if the cache has not been
   1.470 + *         initialized yet.
   1.471 + * @return Promise{null}
   1.472 + *         Resolves once the checks are complete
   1.473 + */
   1.474 +function check_cache(aExpectedToFind, aExpectedImmediately) {
   1.475 +  do_check_eq(aExpectedToFind.length, REPOSITORY_ADDONS.length);
   1.476 +
   1.477 +  let lookups = [];
   1.478 +
   1.479 +  for (let i = 0 ; i < REPOSITORY_ADDONS.length ; i++) {
   1.480 +    lookups.push(new Promise((resolve, reject) => {
   1.481 +      let immediatelyFound = true;
   1.482 +      let expected = aExpectedToFind[i] ? REPOSITORY_ADDONS[i] : null;
   1.483 +      // can't Promise-wrap this because we're also testing whether the callback is
   1.484 +      // sync or async
   1.485 +      AddonRepository.getCachedAddonByID(REPOSITORY_ADDONS[i].id, function(aAddon) {
   1.486 +        do_check_eq(immediatelyFound, aExpectedImmediately);
   1.487 +        if (expected == null)
   1.488 +          do_check_eq(aAddon, null);
   1.489 +        else
   1.490 +          check_results([aAddon], [expected], true);
   1.491 +        resolve();
   1.492 +      });
   1.493 +      immediatelyFound = false;
   1.494 +    }));
   1.495 +  }
   1.496 +  return Promise.all(lookups);
   1.497 +}
   1.498 +
   1.499 +/*
   1.500 + * Task to check an initialized cache by checking the cache, then restarting the
   1.501 + * manager, and checking the cache. This checks that the cache is consistent
   1.502 + * across manager restarts.
   1.503 + *
   1.504 + * @param  aExpectedToFind
   1.505 + *         An array of booleans representing which REPOSITORY_ADDONS are
   1.506 + *         expected to be found in the cache
   1.507 + */
   1.508 +function* check_initialized_cache(aExpectedToFind) {
   1.509 +  yield check_cache(aExpectedToFind, true);
   1.510 +  yield promiseRestartManager();
   1.511 +
   1.512 +  // If cache is disabled, then expect results immediately
   1.513 +  let cacheEnabled = Services.prefs.getBoolPref(PREF_GETADDONS_CACHE_ENABLED);
   1.514 +  yield check_cache(aExpectedToFind, !cacheEnabled);
   1.515 +}
   1.516 +
   1.517 +function run_test() {
   1.518 +  run_next_test();
   1.519 +}
   1.520 +
   1.521 +add_task(function* setup() {
   1.522 +  // Setup for test
   1.523 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
   1.524 +
   1.525 +  startupManager();
   1.526 +
   1.527 +  // Install XPI add-ons
   1.528 +  yield promiseInstallAllFiles(ADDON_FILES);
   1.529 +  yield promiseRestartManager();
   1.530 +
   1.531 +  gServer = new HttpServer();
   1.532 +  gServer.registerDirectory("/data/", do_get_file("data"));
   1.533 +  gServer.start(PORT);
   1.534 +});
   1.535 +
   1.536 +// Tests AddonRepository.cacheEnabled
   1.537 +add_task(function* run_test_1() {
   1.538 +  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
   1.539 +  do_check_false(AddonRepository.cacheEnabled);
   1.540 +  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
   1.541 +  do_check_true(AddonRepository.cacheEnabled);
   1.542 +});
   1.543 +
   1.544 +// Tests that the cache and database begin as empty
   1.545 +add_task(function* run_test_2() {
   1.546 +  do_check_false(gDBFile.exists());
   1.547 +  yield check_cache([false, false, false], false);
   1.548 +  yield AddonRepository.flush();
   1.549 +});
   1.550 +
   1.551 +// Tests repopulateCache when the search fails
   1.552 +add_task(function* run_test_3() {
   1.553 +  do_check_true(gDBFile.exists());
   1.554 +  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
   1.555 +  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_FAILED);
   1.556 +
   1.557 +  yield new Promise((resolve, reject) =>
   1.558 +    AddonRepository.repopulateCache(ADDON_IDS, resolve));
   1.559 +  yield check_initialized_cache([false, false, false]);
   1.560 +});
   1.561 +
   1.562 +// Tests repopulateCache when search returns no results
   1.563 +add_task(function* run_test_4() {
   1.564 +  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_EMPTY);
   1.565 +
   1.566 +  yield new Promise((resolve, reject) =>
   1.567 +    AddonRepository.repopulateCache(ADDON_IDS, resolve));
   1.568 +  yield check_initialized_cache([false, false, false]);
   1.569 +});
   1.570 +
   1.571 +// Tests repopulateCache when search returns results
   1.572 +add_task(function* run_test_5() {
   1.573 +  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);
   1.574 +
   1.575 +  yield new Promise((resolve, reject) =>
   1.576 +    AddonRepository.repopulateCache(ADDON_IDS, resolve));
   1.577 +  yield check_initialized_cache([true, true, true]);
   1.578 +});
   1.579 +
   1.580 +// Tests repopulateCache when caching is disabled for a single add-on
   1.581 +add_task(function* run_test_5_1() {
   1.582 +  Services.prefs.setBoolPref(PREF_ADDON0_CACHE_ENABLED, false);
   1.583 +
   1.584 +  yield new Promise((resolve, reject) =>
   1.585 +    AddonRepository.repopulateCache(ADDON_IDS, resolve));
   1.586 +
   1.587 +  // Reset pref for next test
   1.588 +  Services.prefs.setBoolPref(PREF_ADDON0_CACHE_ENABLED, true);
   1.589 +
   1.590 +  yield check_initialized_cache([false, true, true]);
   1.591 +});
   1.592 +
   1.593 +// Tests repopulateCache when caching is disabled
   1.594 +add_task(function* run_test_6() {
   1.595 +  do_check_true(gDBFile.exists());
   1.596 +  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
   1.597 +
   1.598 +  yield new Promise((resolve, reject) =>
   1.599 +    AddonRepository.repopulateCache(ADDON_IDS, resolve));
   1.600 +  // Database should have been deleted
   1.601 +  do_check_false(gDBFile.exists());
   1.602 +
   1.603 +  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
   1.604 +  yield check_cache([false, false, false], false);
   1.605 +  yield AddonRepository.flush();
   1.606 +});
   1.607 +
   1.608 +// Tests cacheAddons when the search fails
   1.609 +add_task(function* run_test_7() {
   1.610 +  do_check_true(gDBFile.exists());
   1.611 +  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_FAILED);
   1.612 +
   1.613 +  yield new Promise((resolve, reject) =>
   1.614 +    AddonRepository.cacheAddons(ADDON_IDS, resolve));
   1.615 +  yield check_initialized_cache([false, false, false]);
   1.616 +});
   1.617 +
   1.618 +// Tests cacheAddons when the search returns no results
   1.619 +add_task(function* run_test_8() {
   1.620 +  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_EMPTY);
   1.621 +
   1.622 +  yield new Promise((resolve, reject) =>
   1.623 +    AddonRepository.cacheAddons(ADDON_IDS, resolve));
   1.624 +  yield check_initialized_cache([false, false, false]);
   1.625 +});
   1.626 +
   1.627 +// Tests cacheAddons for a single add-on when search returns results
   1.628 +add_task(function* run_test_9() {
   1.629 +  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);
   1.630 +
   1.631 +  yield new Promise((resolve, reject) =>
   1.632 +    AddonRepository.cacheAddons([ADDON_IDS[0]], resolve));
   1.633 +  yield check_initialized_cache([true, false, false]);
   1.634 +});
   1.635 +
   1.636 +// Tests cacheAddons when caching is disabled for a single add-on
   1.637 +add_task(function* run_test_9_1() {
   1.638 +  Services.prefs.setBoolPref(PREF_ADDON1_CACHE_ENABLED, false);
   1.639 +
   1.640 +  yield new Promise((resolve, reject) =>
   1.641 +    AddonRepository.cacheAddons(ADDON_IDS, resolve));
   1.642 +
   1.643 +  // Reset pref for next test
   1.644 +  Services.prefs.setBoolPref(PREF_ADDON1_CACHE_ENABLED, true);
   1.645 +
   1.646 +  yield check_initialized_cache([true, false, true]);
   1.647 +});
   1.648 +
   1.649 +// Tests cacheAddons for multiple add-ons, some already in the cache,
   1.650 +add_task(function* run_test_10() {
   1.651 +  yield new Promise((resolve, reject) =>
   1.652 +    AddonRepository.cacheAddons(ADDON_IDS, resolve));
   1.653 +  yield check_initialized_cache([true, true, true]);
   1.654 +});
   1.655 +
   1.656 +// Tests cacheAddons when caching is disabled
   1.657 +add_task(function* run_test_11() {
   1.658 +  do_check_true(gDBFile.exists());
   1.659 +  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
   1.660 +
   1.661 +  yield new Promise((resolve, reject) =>
   1.662 +    AddonRepository.cacheAddons(ADDON_IDS, resolve));
   1.663 +  do_check_true(gDBFile.exists());
   1.664 +
   1.665 +  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
   1.666 +  yield check_initialized_cache([true, true, true]);
   1.667 +});
   1.668 +
   1.669 +// Tests that XPI add-ons do not use any of the repository properties if
   1.670 +// caching is disabled, even if there are repository properties available
   1.671 +add_task(function* run_test_12() {
   1.672 +  do_check_true(gDBFile.exists());
   1.673 +  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
   1.674 +  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);
   1.675 +
   1.676 +  let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
   1.677 +  check_results(aAddons, WITHOUT_CACHE);
   1.678 +});
   1.679 +
   1.680 +// Tests that a background update with caching disabled deletes the add-ons
   1.681 +// database, and that XPI add-ons still do not use any of repository properties
   1.682 +add_task(function* run_test_13() {
   1.683 +  do_check_true(gDBFile.exists());
   1.684 +  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS_PERFORMANCE, GETADDONS_EMPTY);
   1.685 +
   1.686 +  yield trigger_background_update();
   1.687 +  // Database should have been deleted
   1.688 +  do_check_false(gDBFile.exists());
   1.689 +
   1.690 +  let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
   1.691 +  check_results(aAddons, WITHOUT_CACHE);
   1.692 +});
   1.693 +
   1.694 +// Tests that the XPI add-ons have the correct properties if caching is
   1.695 +// enabled but has no information
   1.696 +add_task(function* run_test_14() {
   1.697 +  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
   1.698 +
   1.699 +  yield trigger_background_update();
   1.700 +  yield AddonRepository.flush();
   1.701 +  do_check_true(gDBFile.exists());
   1.702 +
   1.703 +  let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
   1.704 +  check_results(aAddons, WITHOUT_CACHE);
   1.705 +});
   1.706 +
   1.707 +// Tests that the XPI add-ons correctly use the repository properties when
   1.708 +// caching is enabled and the repository information is available
   1.709 +add_task(function* run_test_15() {
   1.710 +  Services.prefs.setCharPref(PREF_GETADDONS_BYIDS_PERFORMANCE, GETADDONS_RESULTS);
   1.711 +
   1.712 +  yield trigger_background_update();
   1.713 +  let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
   1.714 +  check_results(aAddons, WITH_CACHE);
   1.715 +});
   1.716 +
   1.717 +// Tests that restarting the manager does not change the checked properties
   1.718 +// on the XPI add-ons (repository properties still exist and are still properly
   1.719 +// used)
   1.720 +add_task(function* run_test_16() {
   1.721 +  yield promiseRestartManager();
   1.722 +
   1.723 +  let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
   1.724 +  check_results(aAddons, WITH_CACHE);
   1.725 +});
   1.726 +
   1.727 +// Tests that setting a list of types to cache works
   1.728 +add_task(function* run_test_17() {
   1.729 +  Services.prefs.setCharPref(PREF_GETADDONS_CACHE_TYPES, "foo,bar,extension,baz");
   1.730 +
   1.731 +  yield trigger_background_update();
   1.732 +  let aAddons = yield promiseAddonsByIDs(ADDON_IDS);
   1.733 +  check_results(aAddons, WITH_EXTENSION_CACHE);
   1.734 +});
   1.735 +
   1.736 +add_task(function* end_test() {
   1.737 +  yield new Promise((resolve, reject) => gServer.stop(resolve));
   1.738 +});

mercurial