toolkit/components/downloads/test/unit/test_privatebrowsing_cancel.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/downloads/test/unit/test_privatebrowsing_cancel.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,272 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 +  Make sure that the download manager service is given a chance to cancel the
    1.10 +  private browsing mode transition.
    1.11 +**/
    1.12 +
    1.13 +const Cm = Components.manager;
    1.14 +
    1.15 +const kPromptServiceUUID = "{6cc9c9fe-bc0b-432b-a410-253ef8bcc699}";
    1.16 +const kPromptServiceContractID = "@mozilla.org/embedcomp/prompt-service;1";
    1.17 +
    1.18 +// Save original prompt service factory
    1.19 +const kPromptServiceFactory = Cm.getClassObject(Cc[kPromptServiceContractID],
    1.20 +                                                Ci.nsIFactory);
    1.21 +
    1.22 +let fakePromptServiceFactory = {
    1.23 +  createInstance: function(aOuter, aIid) {
    1.24 +    if (aOuter != null)
    1.25 +      throw Cr.NS_ERROR_NO_AGGREGATION;
    1.26 +    return promptService.QueryInterface(aIid);
    1.27 +  }
    1.28 +};
    1.29 +
    1.30 +let promptService = {
    1.31 +  _buttonChoice: 0,
    1.32 +  _called: false,
    1.33 +  wasCalled: function() {
    1.34 +    let called = this._called;
    1.35 +    this._called = false;
    1.36 +    return called;
    1.37 +  },
    1.38 +  sayCancel: function() {
    1.39 +    this._buttonChoice = 1;
    1.40 +    this._called = false;
    1.41 +  },
    1.42 +  sayProceed: function() {
    1.43 +    this._buttonChoice = 0;
    1.44 +    this._called = false;
    1.45 +  },
    1.46 +  QueryInterface: function(aIID) {
    1.47 +    if (aIID.equals(Ci.nsIPromptService) ||
    1.48 +        aIID.equals(Ci.nsISupports)) {
    1.49 +      return this;
    1.50 +    }
    1.51 +    throw Cr.NS_ERROR_NO_INTERFACE;
    1.52 +  },
    1.53 +  confirmEx: function(parent, title, text, buttonFlags,
    1.54 +                      button0Title, button1Title, button2Title,
    1.55 +                      checkMsg, checkState) {
    1.56 +    this._called = true;
    1.57 +    return this._buttonChoice;
    1.58 +  }
    1.59 +};
    1.60 +
    1.61 +Cm.QueryInterface(Ci.nsIComponentRegistrar)
    1.62 +  .registerFactory(Components.ID(kPromptServiceUUID), "Prompt Service",
    1.63 +                   kPromptServiceContractID, fakePromptServiceFactory);
    1.64 +
    1.65 +this.__defineGetter__("dm", function() {
    1.66 +  delete this.dm;
    1.67 +  return this.dm = Cc["@mozilla.org/download-manager;1"].
    1.68 +                   getService(Ci.nsIDownloadManager);
    1.69 +});
    1.70 +
    1.71 +function trigger_pb_cleanup(expected)
    1.72 +{
    1.73 +  var obs = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
    1.74 +  var cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
    1.75 +  cancel.data = false;
    1.76 +  obs.notifyObservers(cancel, "last-pb-context-exiting", null);
    1.77 +  do_check_eq(expected, cancel.data);
    1.78 +  if (!expected)
    1.79 +    obs.notifyObservers(cancel, "last-pb-context-exited", null);
    1.80 +}
    1.81 +
    1.82 +function run_test() {
    1.83 +  if (oldDownloadManagerDisabled()) {
    1.84 +    return;
    1.85 +  }
    1.86 +
    1.87 +  function finishTest() {
    1.88 +    // Cancel Download-G
    1.89 +    dlG.cancel();
    1.90 +    dlG.remove();
    1.91 +    dm.cleanUp();
    1.92 +    dm.cleanUpPrivate();
    1.93 +    do_check_eq(dm.activeDownloadCount, 0);
    1.94 +    do_check_eq(dm.activePrivateDownloadCount, 0);
    1.95 +
    1.96 +    dm.removeListener(listener);
    1.97 +    httpserv.stop(do_test_finished);
    1.98 +
    1.99 +    // Unregister the factory so we do not leak
   1.100 +    Cm.QueryInterface(Ci.nsIComponentRegistrar)
   1.101 +      .unregisterFactory(Components.ID(kPromptServiceUUID),
   1.102 +                         fakePromptServiceFactory);
   1.103 +
   1.104 +    // Restore the original factory
   1.105 +    Cm.QueryInterface(Ci.nsIComponentRegistrar)
   1.106 +      .registerFactory(Components.ID(kPromptServiceUUID), "Prompt Service",
   1.107 +                       kPromptServiceContractID, kPromptServiceFactory);
   1.108 +  }
   1.109 +
   1.110 +  do_test_pending();
   1.111 +  let httpserv = new HttpServer();
   1.112 +  httpserv.registerDirectory("/file/", do_get_cwd());
   1.113 +  httpserv.registerPathHandler("/noresume", function (meta, response) {
   1.114 +    response.setHeader("Content-Type", "text/html", false);
   1.115 +    response.setHeader("Accept-Ranges", "none", false);
   1.116 +    response.write("foo");
   1.117 +  });
   1.118 +  httpserv.start(-1);
   1.119 +
   1.120 +  let tmpDir = Cc["@mozilla.org/file/directory_service;1"].
   1.121 +               getService(Ci.nsIProperties).
   1.122 +               get("TmpD", Ci.nsIFile);
   1.123 +
   1.124 +  // make sure we're starting with an empty DB
   1.125 +  do_check_eq(dm.activeDownloadCount, 0);
   1.126 +
   1.127 +  let listener = {
   1.128 +    onDownloadStateChange: function(aState, aDownload)
   1.129 +    {
   1.130 +      switch (aDownload.state) {
   1.131 +        case dm.DOWNLOAD_QUEUED:
   1.132 +        case dm.DOWNLOAD_DOWNLOADING:
   1.133 +          if (aDownload.targetFile.equals(dlD.targetFile)) {
   1.134 +            // Sanity check: Download-D must not be resumable
   1.135 +            do_check_false(dlD.resumable);
   1.136 +
   1.137 +            // Cancel the transition
   1.138 +            promptService.sayCancel();
   1.139 +            trigger_pb_cleanup(true);
   1.140 +            do_check_true(promptService.wasCalled());
   1.141 +            do_check_eq(dm.activePrivateDownloadCount, 1);
   1.142 +
   1.143 +            promptService.sayProceed();
   1.144 +            trigger_pb_cleanup(false);
   1.145 +            do_check_true(promptService.wasCalled());
   1.146 +            do_check_eq(dm.activePrivateDownloadCount, 0);
   1.147 +            do_check_eq(dlD.state, dm.DOWNLOAD_CANCELED);
   1.148 +
   1.149 +            // Create Download-E
   1.150 +            dlE = addDownload(httpserv, {
   1.151 +              isPrivate: true,
   1.152 +              targetFile: fileE,
   1.153 +              sourceURI: downloadESource,
   1.154 +              downloadName: downloadEName
   1.155 +            });
   1.156 +
   1.157 +            // Wait for Download-E to start
   1.158 +          } else if (aDownload.targetFile.equals(dlE.targetFile)) {
   1.159 +            // Sanity check: Download-E must be resumable
   1.160 +            do_check_true(dlE.resumable);
   1.161 +
   1.162 +            promptService.sayCancel();
   1.163 +            trigger_pb_cleanup(true);
   1.164 +            do_check_true(promptService.wasCalled());
   1.165 +            do_check_eq(dm.activePrivateDownloadCount, 1);
   1.166 +
   1.167 +            promptService.sayProceed();
   1.168 +            trigger_pb_cleanup(false);
   1.169 +            do_check_true(promptService.wasCalled());
   1.170 +            do_check_eq(dm.activePrivateDownloadCount, 0);
   1.171 +            do_check_eq(dlE.state, dm.DOWNLOAD_CANCELED);
   1.172 +
   1.173 +            // Create Download-F
   1.174 +            dlF = addDownload(httpserv, {
   1.175 +              isPrivate: true,
   1.176 +              targetFile: fileF,
   1.177 +              sourceURI: downloadFSource,
   1.178 +              downloadName: downloadFName
   1.179 +            });
   1.180 +
   1.181 +            // Wait for Download-F to start
   1.182 +          } else if (aDownload.targetFile.equals(dlF.targetFile)) {
   1.183 +            // Sanity check: Download-F must be resumable
   1.184 +            do_check_true(dlF.resumable);
   1.185 +            dlF.pause();
   1.186 +
   1.187 +          } else if (aDownload.targetFile.equals(dlG.targetFile)) {
   1.188 +            // Sanity check: Download-G must not be resumable
   1.189 +            do_check_false(dlG.resumable);
   1.190 +
   1.191 +            promptService.sayCancel();
   1.192 +            trigger_pb_cleanup(false);
   1.193 +            do_check_false(promptService.wasCalled());
   1.194 +            do_check_eq(dm.activeDownloadCount, 1);
   1.195 +            do_check_eq(dlG.state, dm.DOWNLOAD_DOWNLOADING);
   1.196 +            finishTest();
   1.197 +          }
   1.198 +          break;
   1.199 +
   1.200 +        case dm.DOWNLOAD_PAUSED:
   1.201 +          if (aDownload.targetFile.equals(dlF.targetFile)) {
   1.202 +            promptService.sayProceed();
   1.203 +            trigger_pb_cleanup(false);
   1.204 +            do_check_true(promptService.wasCalled());
   1.205 +            do_check_eq(dm.activePrivateDownloadCount, 0);
   1.206 +            do_check_eq(dlF.state, dm.DOWNLOAD_CANCELED);
   1.207 +
   1.208 +            // Create Download-G
   1.209 +            dlG = addDownload(httpserv, {
   1.210 +              isPrivate: false,
   1.211 +              targetFile: fileG,
   1.212 +              sourceURI: downloadGSource,
   1.213 +              downloadName: downloadGName
   1.214 +            });
   1.215 +
   1.216 +            // Wait for Download-G to start
   1.217 +          }
   1.218 +          break;
   1.219 +      }
   1.220 +    },
   1.221 +    onStateChange: function(a, b, c, d, e) { },
   1.222 +    onProgressChange: function(a, b, c, d, e, f, g) { },
   1.223 +    onSecurityChange: function(a, b, c, d) { }
   1.224 +  };
   1.225 +
   1.226 +  dm.addPrivacyAwareListener(listener);
   1.227 +
   1.228 +  const PORT = httpserv.identity.primaryPort;
   1.229 +
   1.230 +  // properties of Download-D
   1.231 +  const downloadDSource = "http://localhost:" + PORT + "/noresume";
   1.232 +  const downloadDDest = "download-file-D";
   1.233 +  const downloadDName = "download-D";
   1.234 +
   1.235 +  // properties of Download-E
   1.236 +  const downloadESource = "http://localhost:" + PORT + "/file/head_download_manager.js";
   1.237 +  const downloadEDest = "download-file-E";
   1.238 +  const downloadEName = "download-E";
   1.239 +
   1.240 +  // properties of Download-F
   1.241 +  const downloadFSource = "http://localhost:" + PORT + "/file/head_download_manager.js";
   1.242 +  const downloadFDest = "download-file-F";
   1.243 +  const downloadFName = "download-F";
   1.244 +
   1.245 +  // properties of Download-G
   1.246 +  const downloadGSource = "http://localhost:" + PORT + "/noresume";
   1.247 +  const downloadGDest = "download-file-G";
   1.248 +  const downloadGName = "download-G";
   1.249 +
   1.250 +  // Create all target files
   1.251 +  let fileD = tmpDir.clone();
   1.252 +  fileD.append(downloadDDest);
   1.253 +  fileD.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.254 +  let fileE = tmpDir.clone();
   1.255 +  fileE.append(downloadEDest);
   1.256 +  fileE.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.257 +  let fileF = tmpDir.clone();
   1.258 +  fileF.append(downloadFDest);
   1.259 +  fileF.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.260 +  let fileG = tmpDir.clone();
   1.261 +  fileG.append(downloadGDest);
   1.262 +  fileG.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.263 +
   1.264 +  // Create Download-D
   1.265 +  let dlD = addDownload(httpserv, {
   1.266 +    isPrivate: true,
   1.267 +    targetFile: fileD,
   1.268 +    sourceURI: downloadDSource,
   1.269 +    downloadName: downloadDName
   1.270 +  });
   1.271 +
   1.272 +  let dlE, dlF, dlG;
   1.273 +
   1.274 +  // wait for Download-D to start
   1.275 +}

mercurial