toolkit/mozapps/extensions/test/browser/browser_select_selection.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/browser/browser_select_selection.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,268 @@
     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 the selection part of the post-app-update dialog
     1.9 +
    1.10 +var gProvider;
    1.11 +var gWin;
    1.12 +
    1.13 +const PROFILE = AddonManager.SCOPE_PROFILE;
    1.14 +const USER = AddonManager.SCOPE_USER;
    1.15 +const APP = AddonManager.SCOPE_APPLICATION;
    1.16 +const SYSTEM = AddonManager.SCOPE_SYSTEM;
    1.17 +const DIST = -1;
    1.18 +
    1.19 +// The matrix of testcases for the selection part of the UI
    1.20 +// Note that the isActive flag has the value it had when the previous version
    1.21 +// of the application ran with this add-on.
    1.22 +var ADDONS = [
    1.23 +  //userDisabled   wasAppDisabled isAppDisabled  isActive  hasUpdate  autoUpdate  scope    defaultKeep  position  keepString           disableString
    1.24 +  [false,          true,          false,         false,    false,     true,       PROFILE, true,        42,       "enabled",           ""],               // 0
    1.25 +  [false,          true,          false,         false,    true,      true,       PROFILE, true,        43,       "enabled",           ""],               // 1
    1.26 +  [false,          true,          false,         false,    true,      false,      PROFILE, true,        52,       "unneededupdate",    ""],               // 2
    1.27 +  [false,          false,         false,         true,     false,     true,       PROFILE, true,        53,       "",                  "disabled"],       // 3
    1.28 +  [false,          false,         false,         true,     true,      true,       PROFILE, true,        54,       "",                  "disabled"],       // 4
    1.29 +  [false,          false,         false,         true,     true,      false,      PROFILE, true,        55,       "unneededupdate",    "disabled"],       // 5
    1.30 +  [false,          true,          true,          false,    false,     true,       PROFILE, true,        56,       "incompatible",      ""],               // 6
    1.31 +  [false,          true,          true,          false,    true,      true,       PROFILE, true,        57,       "autoupdate",        ""],               // 7
    1.32 +  [false,          true,          true,          false,    true,      false,      PROFILE, true,        58,       "neededupdate",      ""],               // 8
    1.33 +  [false,          false,         true,          true,     false,     true,       PROFILE, true,        59,       "incompatible",      "disabled"],       // 9
    1.34 +  [false,          true,          true,          true,     true,      true,       PROFILE, true,        44,       "autoupdate",        "disabled"],       // 10
    1.35 +  [false,          true,          true,          true,     true,      false,      PROFILE, true,        45,       "neededupdate",      "disabled"],       // 11
    1.36 +  [true,           false,         false,         false,    false,     true,       PROFILE, false,       46,       "enabled",           ""],               // 12
    1.37 +  [true,           false,         false,         false,    true,      true,       PROFILE, false,       47,       "enabled",           ""],               // 13
    1.38 +  [true,           false,         false,         false,    true,      false,      PROFILE, false,       48,       "unneededupdate",    ""],               // 14
    1.39 +
    1.40 +  // userDisabled and isActive cannot be true on startup
    1.41 +
    1.42 +  [true,           true,          true,          false,    false,     true,       PROFILE, false,       49,       "incompatible",      ""],               // 15
    1.43 +  [true,           true,          true,          false,    true,      true,       PROFILE, false,       50,       "autoupdate",        ""],               // 16
    1.44 +  [true,           true,          true,          false,    true,      false,      PROFILE, false,       51,       "neededupdate",      ""],               // 17
    1.45 +
    1.46 +  // userDisabled and isActive cannot be true on startup
    1.47 +
    1.48 +  // Being in a different scope should make little difference except no updates are possible so don't exhaustively test each
    1.49 +  [false,          false,         false,         true,     true,      false,      USER,    false,       0,        "",                  "disabled"],       // 18
    1.50 +  [true,           true,          false,         false,    true,      false,      USER,    false,       1,        "enabled",           ""],               // 19
    1.51 +  [false,          true,          true,          true,     true,      false,      USER,    false,       2,        "incompatible",      "disabled"],       // 20
    1.52 +  [true,           true,          true,          false,    true,      false,      USER,    false,       3,        "incompatible",      ""],               // 21
    1.53 +  [false,          false,         false,         true,     true,      false,      SYSTEM,  false,       4,        "",                  "disabled"],       // 22
    1.54 +  [true,           true,          false,         false,    true,      false,      SYSTEM,  false,       5,        "enabled",           ""],               // 23
    1.55 +  [false,          true,          true,          true,     true,      false,      SYSTEM,  false,       6,        "incompatible",      "disabled"],       // 24
    1.56 +  [true,           true,          true,          false,    true,      false,      SYSTEM,  false,       7,        "incompatible",      ""],               // 25
    1.57 +  [false,          false,         false,         true,     true,      false,      APP,     false,       8,        "",                  "disabled"],       // 26
    1.58 +  [true,           true,          false,         false,    true,      false,      APP,     false,       9,        "enabled",           ""],               // 27
    1.59 +  [false,          true,          true,          true,     true,      false,      APP,     false,       10,       "incompatible",      "disabled"],       // 28
    1.60 +  [true,           true,          true,          false,    true,      false,      APP,     false,       11,       "incompatible",      ""],               // 29
    1.61 +];
    1.62 +
    1.63 +function waitForView(aView, aCallback) {
    1.64 +  var view = gWin.document.getElementById(aView);
    1.65 +  if (view.parentNode.selectedPanel == view) {
    1.66 +    aCallback();
    1.67 +    return;
    1.68 +  }
    1.69 +
    1.70 +  view.addEventListener("ViewChanged", function() {
    1.71 +    view.removeEventListener("ViewChanged", arguments.callee, false);
    1.72 +    aCallback();
    1.73 +  }, false);
    1.74 +}
    1.75 +
    1.76 +function getString(aName) {
    1.77 +  if (!aName)
    1.78 +    return "";
    1.79 +
    1.80 +  var strings = Services.strings.createBundle("chrome://mozapps/locale/extensions/selectAddons.properties");
    1.81 +  return strings.GetStringFromName("action." + aName);
    1.82 +}
    1.83 +
    1.84 +function getSourceString(aSource) {
    1.85 +  if (!aSource)
    1.86 +    return "";
    1.87 +
    1.88 +  var strings = Services.strings.createBundle("chrome://mozapps/locale/extensions/selectAddons.properties");
    1.89 +  switch (aSource) {
    1.90 +    case PROFILE:
    1.91 +      return strings.GetStringFromName("source.profile");
    1.92 +    case DIST:
    1.93 +      return strings.GetStringFromName("source.bundled");
    1.94 +    default:
    1.95 +      return strings.GetStringFromName("source.other");
    1.96 +  }
    1.97 +}
    1.98 +
    1.99 +function test() {
   1.100 +  waitForExplicitFinish();
   1.101 +
   1.102 +  gProvider = new MockProvider();
   1.103 +
   1.104 +  // Set prefs for Distributed Extension Source tests.
   1.105 +  Services.prefs.setBoolPref("extensions.installedDistroAddon.test3@tests.mozilla.org", true);
   1.106 +  Services.prefs.setBoolPref("extensions.installedDistroAddon.test12@tests.mozilla.org", true);
   1.107 +  Services.prefs.setBoolPref("extensions.installedDistroAddon.test15@tests.mozilla.org", true);
   1.108 +
   1.109 +  for (let pos in ADDONS) {
   1.110 +    let addonItem = ADDONS[pos];
   1.111 +    let addon = new MockAddon("test" + pos + "@tests.mozilla.org",
   1.112 +                              "Test Add-on " + pos, "extension");
   1.113 +    addon.version = "1.0";
   1.114 +    addon.userDisabled = addonItem[0];
   1.115 +    addon.appDisabled = addonItem[1];
   1.116 +    addon.isActive = addonItem[3];
   1.117 +    addon.applyBackgroundUpdates = addonItem[5] ? AddonManager.AUTOUPDATE_ENABLE
   1.118 +                                             : AddonManager.AUTOUPDATE_DISABLE;
   1.119 +    addon.scope = addonItem[6];
   1.120 +
   1.121 +    // Remove the upgrade permission from non-profile add-ons
   1.122 +    if (addon.scope != AddonManager.SCOPE_PROFILE)
   1.123 +      addon._permissions -= AddonManager.PERM_CAN_UPGRADE;
   1.124 +
   1.125 +    addon.findUpdates = function(aListener, aReason, aAppVersion, aPlatformVersion) {
   1.126 +      addon.appDisabled = addonItem[2];
   1.127 +      addon.isActive = addon.shouldBeActive;
   1.128 +
   1.129 +      if (addonItem[4]) {
   1.130 +        var newAddon = new MockAddon(this.id, this.name, "extension");
   1.131 +        newAddon.version = "2.0";
   1.132 +        var install = new MockInstall(this.name, this.type, newAddon);
   1.133 +        install.existingAddon = this;
   1.134 +        aListener.onUpdateAvailable(this, install);
   1.135 +      }
   1.136 +
   1.137 +      aListener.onUpdateFinished(this, AddonManager.UPDATE_STATUS_NO_ERROR);
   1.138 +    };
   1.139 +
   1.140 +    gProvider.addAddon(addon);
   1.141 +  }
   1.142 +
   1.143 +  gWin = Services.ww.openWindow(null,
   1.144 +                                "chrome://mozapps/content/extensions/selectAddons.xul",
   1.145 +                                "",
   1.146 +                                "chrome,centerscreen,dialog,titlebar",
   1.147 +                                null);
   1.148 +  waitForFocus(function() {
   1.149 +    waitForView("select", run_next_test);
   1.150 +  }, gWin);
   1.151 +}
   1.152 +
   1.153 +function end_test() {
   1.154 +  gWin.close();
   1.155 +  finish();
   1.156 +}
   1.157 +
   1.158 +// Minimal test for the checking UI
   1.159 +add_test(function checking_test() {
   1.160 +  // By the time we're here the progress bar should be full
   1.161 +  var progress = gWin.document.getElementById("checking-progress");
   1.162 +  is(progress.mode, "determined", "Should be a determined progress bar");
   1.163 +  is(progress.value, progress.max, "Should be at full progress");
   1.164 +
   1.165 +  run_next_test();
   1.166 +});
   1.167 +
   1.168 +// Tests that the selection UI behaves correctly
   1.169 +add_test(function selection_test() {
   1.170 +  function check_state() {
   1.171 +    var str = addon[keep.checked ? 9 : 10];
   1.172 +    var expected = getString(str);
   1.173 +    var showCheckbox = str == "neededupdate" || str == "unneededupdate";
   1.174 +    is(action.textContent, expected, "Action message should have the right text");
   1.175 +    is(!is_hidden(update), showCheckbox, "Checkbox should have the right visibility");
   1.176 +    is(is_hidden(action), showCheckbox, "Message should have the right visibility");
   1.177 +    if (showCheckbox)
   1.178 +      ok(update.checked, "Optional update checkbox should be checked");
   1.179 +
   1.180 +    if (keep.checked) {
   1.181 +      is(row.hasAttribute("active"), !addon[2] || hasUpdate,
   1.182 +       "Add-on will be active if it isn't appDisabled or an update is available");
   1.183 +
   1.184 +      if (showCheckbox) {
   1.185 +        info("Flipping update checkbox");
   1.186 +        EventUtils.synthesizeMouseAtCenter(update, { }, gWin);
   1.187 +        is(row.hasAttribute("active"), str == "unneededupdate",
   1.188 +           "If the optional update isn't needed then the add-on will still be active");
   1.189 +
   1.190 +        info("Flipping update checkbox");
   1.191 +        EventUtils.synthesizeMouseAtCenter(update, { }, gWin);
   1.192 +        is(row.hasAttribute("active"), !addon[2] || hasUpdate,
   1.193 +         "Add-on will be active if it isn't appDisabled or an update is available");
   1.194 +      }
   1.195 +    }
   1.196 +    else {
   1.197 +      ok(!row.hasAttribute("active"), "Add-on won't be active when not keeping");
   1.198 +
   1.199 +      if (showCheckbox) {
   1.200 +        info("Flipping update checkbox");
   1.201 +        EventUtils.synthesizeMouseAtCenter(update, { }, gWin);
   1.202 +        ok(!row.hasAttribute("active"),
   1.203 +           "Unchecking the update checkbox shouldn't make the add-on active");
   1.204 +
   1.205 +        info("Flipping update checkbox");
   1.206 +        EventUtils.synthesizeMouseAtCenter(update, { }, gWin);
   1.207 +        ok(!row.hasAttribute("active"),
   1.208 +           "Re-checking the update checkbox shouldn't make the add-on active");
   1.209 +      }
   1.210 +    }
   1.211 +  }
   1.212 +
   1.213 +  is(gWin.document.getElementById("view-deck").selectedPanel.id, "select",
   1.214 +     "Should be on the right view");
   1.215 +
   1.216 +  var pos = 0;
   1.217 +  var scrollbox = gWin.document.getElementById("select-scrollbox");
   1.218 +  var scrollBoxObject = scrollbox.boxObject.QueryInterface(Ci.nsIScrollBoxObject);
   1.219 +  for (var row = gWin.document.getElementById("select-rows").firstChild; row; row = row.nextSibling) {
   1.220 +    // Ignore separators but increase the position by a large amount so we
   1.221 +    // can verify they were in the right place
   1.222 +    if (row.localName == "separator") {
   1.223 +      pos += 30;
   1.224 +      continue;
   1.225 +    }
   1.226 +
   1.227 +    is(row._addon.type, "extension", "Should only be listing extensions");
   1.228 +
   1.229 +    // Ignore non-test add-ons that may be present
   1.230 +    if (row.id.substr(-18) != "@tests.mozilla.org")
   1.231 +      continue;
   1.232 +
   1.233 +    var id = parseInt(row.id.substring(4, row.id.length - 18));
   1.234 +    var addon = ADDONS[id];
   1.235 +
   1.236 +    info("Testing add-on " + id);
   1.237 +    scrollBoxObject.ensureElementIsVisible(row);
   1.238 +    var keep = gWin.document.getAnonymousElementByAttribute(row, "anonid", "keep");
   1.239 +    var action = gWin.document.getAnonymousElementByAttribute(row, "class", "addon-action-message");
   1.240 +    var update = gWin.document.getAnonymousElementByAttribute(row, "anonid", "update");
   1.241 +    var source = gWin.document.getAnonymousElementByAttribute(row, "class", "addon-source");
   1.242 +
   1.243 +    if (id == 3 || id == 12 || id == 15) {
   1.244 +      // Distro Installed To Profile
   1.245 +      is(source.textContent, getSourceString(DIST), "Source message should have the right text for Distributed Addons");
   1.246 +    } else {
   1.247 +      is(source.textContent, getSourceString(addon[6]), "Source message should have the right text");
   1.248 +    }
   1.249 +
   1.250 +    // Non-profile add-ons don't appear to have updates since we won't install
   1.251 +    // them
   1.252 +    var hasUpdate = addon[4] && addon[6] == PROFILE;
   1.253 +
   1.254 +    is(pos, addon[8], "Should have been in the right position");
   1.255 +    is(keep.checked, addon[7], "Keep checkbox should be in the right state");
   1.256 +
   1.257 +    check_state();
   1.258 +
   1.259 +    info("Flipping keep");
   1.260 +    EventUtils.synthesizeMouseAtCenter(keep, { }, gWin);
   1.261 +    is(keep.checked, !addon[7], "Keep checkbox should be in the right state");
   1.262 +
   1.263 +    check_state();
   1.264 +
   1.265 +    pos++;
   1.266 +  }
   1.267 +
   1.268 +  is(pos, 60, "Should have seen the right number of add-ons");
   1.269 +
   1.270 +  run_next_test();
   1.271 +});

mercurial