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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 /**
     6   Make sure that the download manager service is given a chance to cancel the
     7   private browsing mode transition.
     8 **/
    10 const Cm = Components.manager;
    12 const kPromptServiceUUID = "{6cc9c9fe-bc0b-432b-a410-253ef8bcc699}";
    13 const kPromptServiceContractID = "@mozilla.org/embedcomp/prompt-service;1";
    15 // Save original prompt service factory
    16 const kPromptServiceFactory = Cm.getClassObject(Cc[kPromptServiceContractID],
    17                                                 Ci.nsIFactory);
    19 let fakePromptServiceFactory = {
    20   createInstance: function(aOuter, aIid) {
    21     if (aOuter != null)
    22       throw Cr.NS_ERROR_NO_AGGREGATION;
    23     return promptService.QueryInterface(aIid);
    24   }
    25 };
    27 let promptService = {
    28   _buttonChoice: 0,
    29   _called: false,
    30   wasCalled: function() {
    31     let called = this._called;
    32     this._called = false;
    33     return called;
    34   },
    35   sayCancel: function() {
    36     this._buttonChoice = 1;
    37     this._called = false;
    38   },
    39   sayProceed: function() {
    40     this._buttonChoice = 0;
    41     this._called = false;
    42   },
    43   QueryInterface: function(aIID) {
    44     if (aIID.equals(Ci.nsIPromptService) ||
    45         aIID.equals(Ci.nsISupports)) {
    46       return this;
    47     }
    48     throw Cr.NS_ERROR_NO_INTERFACE;
    49   },
    50   confirmEx: function(parent, title, text, buttonFlags,
    51                       button0Title, button1Title, button2Title,
    52                       checkMsg, checkState) {
    53     this._called = true;
    54     return this._buttonChoice;
    55   }
    56 };
    58 Cm.QueryInterface(Ci.nsIComponentRegistrar)
    59   .registerFactory(Components.ID(kPromptServiceUUID), "Prompt Service",
    60                    kPromptServiceContractID, fakePromptServiceFactory);
    62 this.__defineGetter__("dm", function() {
    63   delete this.dm;
    64   return this.dm = Cc["@mozilla.org/download-manager;1"].
    65                    getService(Ci.nsIDownloadManager);
    66 });
    68 function trigger_pb_cleanup(expected)
    69 {
    70   var obs = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
    71   var cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
    72   cancel.data = false;
    73   obs.notifyObservers(cancel, "last-pb-context-exiting", null);
    74   do_check_eq(expected, cancel.data);
    75   if (!expected)
    76     obs.notifyObservers(cancel, "last-pb-context-exited", null);
    77 }
    79 function run_test() {
    80   if (oldDownloadManagerDisabled()) {
    81     return;
    82   }
    84   function finishTest() {
    85     // Cancel Download-G
    86     dlG.cancel();
    87     dlG.remove();
    88     dm.cleanUp();
    89     dm.cleanUpPrivate();
    90     do_check_eq(dm.activeDownloadCount, 0);
    91     do_check_eq(dm.activePrivateDownloadCount, 0);
    93     dm.removeListener(listener);
    94     httpserv.stop(do_test_finished);
    96     // Unregister the factory so we do not leak
    97     Cm.QueryInterface(Ci.nsIComponentRegistrar)
    98       .unregisterFactory(Components.ID(kPromptServiceUUID),
    99                          fakePromptServiceFactory);
   101     // Restore the original factory
   102     Cm.QueryInterface(Ci.nsIComponentRegistrar)
   103       .registerFactory(Components.ID(kPromptServiceUUID), "Prompt Service",
   104                        kPromptServiceContractID, kPromptServiceFactory);
   105   }
   107   do_test_pending();
   108   let httpserv = new HttpServer();
   109   httpserv.registerDirectory("/file/", do_get_cwd());
   110   httpserv.registerPathHandler("/noresume", function (meta, response) {
   111     response.setHeader("Content-Type", "text/html", false);
   112     response.setHeader("Accept-Ranges", "none", false);
   113     response.write("foo");
   114   });
   115   httpserv.start(-1);
   117   let tmpDir = Cc["@mozilla.org/file/directory_service;1"].
   118                getService(Ci.nsIProperties).
   119                get("TmpD", Ci.nsIFile);
   121   // make sure we're starting with an empty DB
   122   do_check_eq(dm.activeDownloadCount, 0);
   124   let listener = {
   125     onDownloadStateChange: function(aState, aDownload)
   126     {
   127       switch (aDownload.state) {
   128         case dm.DOWNLOAD_QUEUED:
   129         case dm.DOWNLOAD_DOWNLOADING:
   130           if (aDownload.targetFile.equals(dlD.targetFile)) {
   131             // Sanity check: Download-D must not be resumable
   132             do_check_false(dlD.resumable);
   134             // Cancel the transition
   135             promptService.sayCancel();
   136             trigger_pb_cleanup(true);
   137             do_check_true(promptService.wasCalled());
   138             do_check_eq(dm.activePrivateDownloadCount, 1);
   140             promptService.sayProceed();
   141             trigger_pb_cleanup(false);
   142             do_check_true(promptService.wasCalled());
   143             do_check_eq(dm.activePrivateDownloadCount, 0);
   144             do_check_eq(dlD.state, dm.DOWNLOAD_CANCELED);
   146             // Create Download-E
   147             dlE = addDownload(httpserv, {
   148               isPrivate: true,
   149               targetFile: fileE,
   150               sourceURI: downloadESource,
   151               downloadName: downloadEName
   152             });
   154             // Wait for Download-E to start
   155           } else if (aDownload.targetFile.equals(dlE.targetFile)) {
   156             // Sanity check: Download-E must be resumable
   157             do_check_true(dlE.resumable);
   159             promptService.sayCancel();
   160             trigger_pb_cleanup(true);
   161             do_check_true(promptService.wasCalled());
   162             do_check_eq(dm.activePrivateDownloadCount, 1);
   164             promptService.sayProceed();
   165             trigger_pb_cleanup(false);
   166             do_check_true(promptService.wasCalled());
   167             do_check_eq(dm.activePrivateDownloadCount, 0);
   168             do_check_eq(dlE.state, dm.DOWNLOAD_CANCELED);
   170             // Create Download-F
   171             dlF = addDownload(httpserv, {
   172               isPrivate: true,
   173               targetFile: fileF,
   174               sourceURI: downloadFSource,
   175               downloadName: downloadFName
   176             });
   178             // Wait for Download-F to start
   179           } else if (aDownload.targetFile.equals(dlF.targetFile)) {
   180             // Sanity check: Download-F must be resumable
   181             do_check_true(dlF.resumable);
   182             dlF.pause();
   184           } else if (aDownload.targetFile.equals(dlG.targetFile)) {
   185             // Sanity check: Download-G must not be resumable
   186             do_check_false(dlG.resumable);
   188             promptService.sayCancel();
   189             trigger_pb_cleanup(false);
   190             do_check_false(promptService.wasCalled());
   191             do_check_eq(dm.activeDownloadCount, 1);
   192             do_check_eq(dlG.state, dm.DOWNLOAD_DOWNLOADING);
   193             finishTest();
   194           }
   195           break;
   197         case dm.DOWNLOAD_PAUSED:
   198           if (aDownload.targetFile.equals(dlF.targetFile)) {
   199             promptService.sayProceed();
   200             trigger_pb_cleanup(false);
   201             do_check_true(promptService.wasCalled());
   202             do_check_eq(dm.activePrivateDownloadCount, 0);
   203             do_check_eq(dlF.state, dm.DOWNLOAD_CANCELED);
   205             // Create Download-G
   206             dlG = addDownload(httpserv, {
   207               isPrivate: false,
   208               targetFile: fileG,
   209               sourceURI: downloadGSource,
   210               downloadName: downloadGName
   211             });
   213             // Wait for Download-G to start
   214           }
   215           break;
   216       }
   217     },
   218     onStateChange: function(a, b, c, d, e) { },
   219     onProgressChange: function(a, b, c, d, e, f, g) { },
   220     onSecurityChange: function(a, b, c, d) { }
   221   };
   223   dm.addPrivacyAwareListener(listener);
   225   const PORT = httpserv.identity.primaryPort;
   227   // properties of Download-D
   228   const downloadDSource = "http://localhost:" + PORT + "/noresume";
   229   const downloadDDest = "download-file-D";
   230   const downloadDName = "download-D";
   232   // properties of Download-E
   233   const downloadESource = "http://localhost:" + PORT + "/file/head_download_manager.js";
   234   const downloadEDest = "download-file-E";
   235   const downloadEName = "download-E";
   237   // properties of Download-F
   238   const downloadFSource = "http://localhost:" + PORT + "/file/head_download_manager.js";
   239   const downloadFDest = "download-file-F";
   240   const downloadFName = "download-F";
   242   // properties of Download-G
   243   const downloadGSource = "http://localhost:" + PORT + "/noresume";
   244   const downloadGDest = "download-file-G";
   245   const downloadGName = "download-G";
   247   // Create all target files
   248   let fileD = tmpDir.clone();
   249   fileD.append(downloadDDest);
   250   fileD.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   251   let fileE = tmpDir.clone();
   252   fileE.append(downloadEDest);
   253   fileE.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   254   let fileF = tmpDir.clone();
   255   fileF.append(downloadFDest);
   256   fileF.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   257   let fileG = tmpDir.clone();
   258   fileG.append(downloadGDest);
   259   fileG.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   261   // Create Download-D
   262   let dlD = addDownload(httpserv, {
   263     isPrivate: true,
   264     targetFile: fileD,
   265     sourceURI: downloadDSource,
   266     downloadName: downloadDName
   267   });
   269   let dlE, dlF, dlG;
   271   // wait for Download-D to start
   272 }

mercurial