toolkit/mozapps/extensions/test/xpcshell/test_backgroundupdate.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_backgroundupdate.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     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 +// This verifies that background updates & notifications work as expected
     1.9 +
    1.10 +// The test extension uses an insecure update url.
    1.11 +Services.prefs.setBoolPref(PREF_EM_CHECK_UPDATE_SECURITY, false);
    1.12 +
    1.13 +Components.utils.import("resource://testing-common/httpd.js");
    1.14 +var testserver = new HttpServer();
    1.15 +testserver.start(-1);
    1.16 +gPort = testserver.identity.primaryPort;
    1.17 +const profileDir = gProfD.clone();
    1.18 +profileDir.append("extensions");
    1.19 +
    1.20 +// register static files with server and interpolate port numbers in them
    1.21 +mapFile("/data/test_backgroundupdate.rdf", testserver);
    1.22 +
    1.23 +function run_test() {
    1.24 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
    1.25 +
    1.26 +  testserver.registerDirectory("/addons/", do_get_file("addons"));
    1.27 +
    1.28 +  startupManager();
    1.29 +
    1.30 +  do_test_pending();
    1.31 +  run_test_1();
    1.32 +}
    1.33 +
    1.34 +function end_test() {
    1.35 +  testserver.stop(do_test_finished);
    1.36 +}
    1.37 +
    1.38 +// Verify that with no add-ons installed the background update notifications get
    1.39 +// called
    1.40 +function run_test_1() {
    1.41 +  AddonManager.getAddonsByTypes(["extension", "theme", "locale"], function(aAddons) {
    1.42 +    do_check_eq(aAddons.length, 0);
    1.43 +
    1.44 +    Services.obs.addObserver(function() {
    1.45 +      Services.obs.removeObserver(arguments.callee, "addons-background-update-complete");
    1.46 +
    1.47 +      do_execute_soon(run_test_2);
    1.48 +    }, "addons-background-update-complete", false);
    1.49 +
    1.50 +    AddonManagerPrivate.backgroundUpdateCheck();
    1.51 +  });
    1.52 +}
    1.53 +
    1.54 +// Verify that with two add-ons installed both of which claim to have updates
    1.55 +// available we get the notification after both updates attempted to start
    1.56 +function run_test_2() {
    1.57 +  writeInstallRDFForExtension({
    1.58 +    id: "addon1@tests.mozilla.org",
    1.59 +    version: "1.0",
    1.60 +    updateURL: "http://localhost:" + gPort + "/data/test_backgroundupdate.rdf",
    1.61 +    targetApplications: [{
    1.62 +      id: "xpcshell@tests.mozilla.org",
    1.63 +      minVersion: "1",
    1.64 +      maxVersion: "1"
    1.65 +    }],
    1.66 +    name: "Test Addon 1",
    1.67 +  }, profileDir);
    1.68 +
    1.69 +  writeInstallRDFForExtension({
    1.70 +    id: "addon2@tests.mozilla.org",
    1.71 +    version: "1.0",
    1.72 +    updateURL: "http://localhost:" + gPort + "/data/test_backgroundupdate.rdf",
    1.73 +    targetApplications: [{
    1.74 +      id: "xpcshell@tests.mozilla.org",
    1.75 +      minVersion: "1",
    1.76 +      maxVersion: "1"
    1.77 +    }],
    1.78 +    name: "Test Addon 2",
    1.79 +  }, profileDir);
    1.80 +
    1.81 +  writeInstallRDFForExtension({
    1.82 +    id: "addon3@tests.mozilla.org",
    1.83 +    version: "1.0",
    1.84 +    targetApplications: [{
    1.85 +      id: "xpcshell@tests.mozilla.org",
    1.86 +      minVersion: "1",
    1.87 +      maxVersion: "1"
    1.88 +    }],
    1.89 +    name: "Test Addon 3",
    1.90 +  }, profileDir);
    1.91 +
    1.92 +  // Background update uses a different pref, if set
    1.93 +  Services.prefs.setCharPref("extensions.update.background.url",
    1.94 +                             "http://localhost:" + gPort +"/data/test_backgroundupdate.rdf");
    1.95 +  restartManager();
    1.96 +
    1.97 +  // Do hotfix checks
    1.98 +  Services.prefs.setCharPref("extensions.hotfix.id", "hotfix@tests.mozilla.org");
    1.99 +  Services.prefs.setCharPref("extensions.hotfix.url", "http://localhost:" + gPort + "/missing.rdf");
   1.100 +
   1.101 +  let installCount = 0;
   1.102 +  let completeCount = 0;
   1.103 +  let sawCompleteNotification = false;
   1.104 +
   1.105 +  Services.obs.addObserver(function() {
   1.106 +    Services.obs.removeObserver(arguments.callee, "addons-background-update-complete");
   1.107 +
   1.108 +    do_check_eq(installCount, 3);
   1.109 +    sawCompleteNotification = true;
   1.110 +  }, "addons-background-update-complete", false);
   1.111 +
   1.112 +  AddonManager.addInstallListener({
   1.113 +    onNewInstall: function(aInstall) {
   1.114 +      installCount++;
   1.115 +    },
   1.116 +
   1.117 +    onDownloadFailed: function(aInstall) {
   1.118 +      completeCount++;
   1.119 +      if (completeCount == 3) {
   1.120 +        do_check_true(sawCompleteNotification);
   1.121 +        end_test();
   1.122 +      }
   1.123 +    }
   1.124 +  });
   1.125 +
   1.126 +  AddonManagerPrivate.backgroundUpdateCheck();
   1.127 +}

mercurial