toolkit/mozapps/extensions/test/xpcshell/test_disable.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_disable.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,194 @@
     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 +Components.utils.import("resource://gre/modules/NetUtil.jsm");
     1.9 +
    1.10 +// This verifies that add-ons can be disabled and enabled.
    1.11 +
    1.12 +var addon1 = {
    1.13 +  id: "addon1@tests.mozilla.org",
    1.14 +  version: "1.0",
    1.15 +  name: "Test 1",
    1.16 +  optionsURL: "chrome://foo/content/options.xul",
    1.17 +  aboutURL: "chrome://foo/content/about.xul",
    1.18 +  iconURL: "chrome://foo/content/icon.png",
    1.19 +  targetApplications: [{
    1.20 +    id: "xpcshell@tests.mozilla.org",
    1.21 +    minVersion: "1",
    1.22 +    maxVersion: "1"
    1.23 +  }]
    1.24 +};
    1.25 +
    1.26 +const profileDir = gProfD.clone();
    1.27 +profileDir.append("extensions");
    1.28 +
    1.29 +var gIconURL = null;
    1.30 +
    1.31 +// Sets up the profile by installing an add-on.
    1.32 +function run_test() {
    1.33 +  do_test_pending();
    1.34 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
    1.35 +
    1.36 +  startupManager();
    1.37 +
    1.38 +  AddonManager.getAddonByID("addon1@tests.mozilla.org", callback_soon(function(a1) {
    1.39 +    do_check_eq(a1, null);
    1.40 +    do_check_not_in_crash_annotation(addon1.id, addon1.version);
    1.41 +
    1.42 +    writeInstallRDFForExtension(addon1, profileDir, addon1.id, "icon.png");
    1.43 +    gIconURL = do_get_addon_root_uri(profileDir.clone(), addon1.id) + "icon.png";
    1.44 +
    1.45 +    restartManager();
    1.46 +
    1.47 +    AddonManager.getAddonByID("addon1@tests.mozilla.org", function(newa1) {
    1.48 +      do_check_neq(newa1, null);
    1.49 +      do_check_true(newa1.isActive);
    1.50 +      do_check_false(newa1.userDisabled);
    1.51 +      do_check_eq(newa1.aboutURL, "chrome://foo/content/about.xul");
    1.52 +      do_check_eq(newa1.optionsURL, "chrome://foo/content/options.xul");
    1.53 +      do_check_eq(newa1.iconURL, "chrome://foo/content/icon.png");
    1.54 +      do_check_true(isExtensionInAddonsList(profileDir, newa1.id));
    1.55 +      do_check_true(hasFlag(newa1.permissions, AddonManager.PERM_CAN_DISABLE));
    1.56 +      do_check_false(hasFlag(newa1.permissions, AddonManager.PERM_CAN_ENABLE));
    1.57 +      do_check_eq(newa1.operationsRequiringRestart, AddonManager.OP_NEEDS_RESTART_DISABLE |
    1.58 +                                                    AddonManager.OP_NEEDS_RESTART_UNINSTALL);
    1.59 +      do_check_in_crash_annotation(addon1.id, addon1.version);
    1.60 +
    1.61 +      run_test_1();
    1.62 +    });
    1.63 +  }));
    1.64 +}
    1.65 +
    1.66 +// Disabling an add-on should work
    1.67 +function run_test_1() {
    1.68 +  prepare_test({
    1.69 +    "addon1@tests.mozilla.org": [
    1.70 +      "onDisabling"
    1.71 +    ]
    1.72 +  });
    1.73 +
    1.74 +  AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
    1.75 +    do_check_neq(a1.operationsRequiringRestart &
    1.76 +                 AddonManager.OP_NEEDS_RESTART_DISABLE, 0);
    1.77 +    a1.userDisabled = true;
    1.78 +    do_check_eq(a1.aboutURL, "chrome://foo/content/about.xul");
    1.79 +    do_check_eq(a1.optionsURL, "chrome://foo/content/options.xul");
    1.80 +    do_check_eq(a1.iconURL, "chrome://foo/content/icon.png");
    1.81 +    do_check_false(hasFlag(a1.permissions, AddonManager.PERM_CAN_DISABLE));
    1.82 +    do_check_true(hasFlag(a1.permissions, AddonManager.PERM_CAN_ENABLE));
    1.83 +    do_check_eq(a1.operationsRequiringRestart, AddonManager.OP_NEEDS_RESTART_DISABLE |
    1.84 +                                               AddonManager.OP_NEEDS_RESTART_UNINSTALL);
    1.85 +    do_check_in_crash_annotation(addon1.id, addon1.version);
    1.86 +
    1.87 +    ensure_test_completed();
    1.88 +
    1.89 +    AddonManager.getAddonsWithOperationsByTypes(null, callback_soon(function(list) {
    1.90 +      do_check_eq(list.length, 1);
    1.91 +      do_check_eq(list[0].id, "addon1@tests.mozilla.org");
    1.92 +
    1.93 +      restartManager();
    1.94 +
    1.95 +      AddonManager.getAddonByID("addon1@tests.mozilla.org", function(newa1) {
    1.96 +        do_check_neq(newa1, null);
    1.97 +        do_check_false(newa1.isActive);
    1.98 +        do_check_true(newa1.userDisabled);
    1.99 +        do_check_eq(newa1.aboutURL, null);
   1.100 +        do_check_eq(newa1.optionsURL, null);
   1.101 +        do_check_eq(newa1.iconURL, gIconURL);
   1.102 +        do_check_false(isExtensionInAddonsList(profileDir, newa1.id));
   1.103 +        do_check_false(hasFlag(newa1.permissions, AddonManager.PERM_CAN_DISABLE));
   1.104 +        do_check_true(hasFlag(newa1.permissions, AddonManager.PERM_CAN_ENABLE));
   1.105 +        do_check_eq(newa1.operationsRequiringRestart, AddonManager.OP_NEEDS_RESTART_ENABLE);
   1.106 +        do_check_not_in_crash_annotation(addon1.id, addon1.version);
   1.107 +
   1.108 +        run_test_2();
   1.109 +      });
   1.110 +    }));
   1.111 +  });
   1.112 +}
   1.113 +
   1.114 +// Enabling an add-on should work.
   1.115 +function run_test_2() {
   1.116 +  prepare_test({
   1.117 +    "addon1@tests.mozilla.org": [
   1.118 +      "onEnabling"
   1.119 +    ]
   1.120 +  });
   1.121 +
   1.122 +  AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
   1.123 +    a1.userDisabled = false;
   1.124 +    do_check_eq(a1.aboutURL, null);
   1.125 +    do_check_eq(a1.optionsURL, null);
   1.126 +    do_check_eq(a1.iconURL, gIconURL);
   1.127 +    do_check_true(hasFlag(a1.permissions, AddonManager.PERM_CAN_DISABLE));
   1.128 +    do_check_false(hasFlag(a1.permissions, AddonManager.PERM_CAN_ENABLE));
   1.129 +    do_check_eq(a1.operationsRequiringRestart, AddonManager.OP_NEEDS_RESTART_ENABLE);
   1.130 +
   1.131 +    ensure_test_completed();
   1.132 +
   1.133 +    AddonManager.getAddonsWithOperationsByTypes(null, callback_soon(function(list) {
   1.134 +      do_check_eq(list.length, 1);
   1.135 +      do_check_eq(list[0].id, "addon1@tests.mozilla.org");
   1.136 +
   1.137 +      restartManager();
   1.138 +
   1.139 +      AddonManager.getAddonByID("addon1@tests.mozilla.org", function(newa1) {
   1.140 +        do_check_neq(newa1, null);
   1.141 +        do_check_true(newa1.isActive);
   1.142 +        do_check_false(newa1.userDisabled);
   1.143 +        do_check_eq(newa1.aboutURL, "chrome://foo/content/about.xul");
   1.144 +        do_check_eq(newa1.optionsURL, "chrome://foo/content/options.xul");
   1.145 +        do_check_eq(newa1.iconURL, "chrome://foo/content/icon.png");
   1.146 +        do_check_true(isExtensionInAddonsList(profileDir, newa1.id));
   1.147 +        do_check_true(hasFlag(newa1.permissions, AddonManager.PERM_CAN_DISABLE));
   1.148 +        do_check_false(hasFlag(newa1.permissions, AddonManager.PERM_CAN_ENABLE));
   1.149 +        do_check_eq(newa1.operationsRequiringRestart, AddonManager.OP_NEEDS_RESTART_DISABLE |
   1.150 +                                                      AddonManager.OP_NEEDS_RESTART_UNINSTALL);
   1.151 +        do_check_in_crash_annotation(addon1.id, addon1.version);
   1.152 +
   1.153 +        run_test_3();
   1.154 +      });
   1.155 +    }));
   1.156 +  });
   1.157 +}
   1.158 +
   1.159 +// Disabling then enabling without restart should fire onOperationCancelled.
   1.160 +function run_test_3() {
   1.161 +  prepare_test({
   1.162 +    "addon1@tests.mozilla.org": [
   1.163 +      "onDisabling"
   1.164 +    ]
   1.165 +  });
   1.166 +
   1.167 +  AddonManager.getAddonByID("addon1@tests.mozilla.org", callback_soon(function(a1) {
   1.168 +    a1.userDisabled = true;
   1.169 +    ensure_test_completed();
   1.170 +    prepare_test({
   1.171 +      "addon1@tests.mozilla.org": [
   1.172 +        "onOperationCancelled"
   1.173 +      ]
   1.174 +    });
   1.175 +    a1.userDisabled = false;
   1.176 +    do_check_true(hasFlag(a1.permissions, AddonManager.PERM_CAN_DISABLE));
   1.177 +    do_check_false(hasFlag(a1.permissions, AddonManager.PERM_CAN_ENABLE));
   1.178 +
   1.179 +    ensure_test_completed();
   1.180 +
   1.181 +    restartManager();
   1.182 +
   1.183 +    AddonManager.getAddonByID("addon1@tests.mozilla.org", function(newa1) {
   1.184 +      do_check_neq(newa1, null);
   1.185 +      do_check_true(newa1.isActive);
   1.186 +      do_check_false(newa1.userDisabled);
   1.187 +      do_check_eq(newa1.aboutURL, "chrome://foo/content/about.xul");
   1.188 +      do_check_eq(newa1.optionsURL, "chrome://foo/content/options.xul");
   1.189 +      do_check_eq(newa1.iconURL, "chrome://foo/content/icon.png");
   1.190 +      do_check_true(isExtensionInAddonsList(profileDir, newa1.id));
   1.191 +      do_check_true(hasFlag(newa1.permissions, AddonManager.PERM_CAN_DISABLE));
   1.192 +      do_check_false(hasFlag(newa1.permissions, AddonManager.PERM_CAN_ENABLE));
   1.193 +
   1.194 +      do_execute_soon(do_test_finished);
   1.195 +    });
   1.196 +  }));
   1.197 +}

mercurial