toolkit/mozapps/downloads/tests/chrome/test_retention_is_0_closes.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <?xml version="1.0"?>
     2 <!-- This Source Code Form is subject to the terms of the Mozilla Public
     3    - License, v. 2.0. If a copy of the MPL was not distributed with this
     4    - file, You can obtain one at http://mozilla.org/MPL/2.0/.  -->
     5 <!--
     6  * This tests that the download manager UI closes upon download completion when
     7  * browser.download.manager.retention is set to 0.  This test was added in bug
     8  * 413093.
     9 -->
    11 <window title="Download Manager Test"
    12         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    13         onload="test();">
    15   <script type="application/javascript"
    16           src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    17   <script type="application/javascript"
    18           src="utils.js"/>
    20   <script type="application/javascript">
    21   <![CDATA[
    23 function setPref(aDoTest)
    24 {
    25   let prefs = Cc["@mozilla.org/preferences-service;1"].
    26               getService(Ci.nsIPrefBranch);
    28   // If we're testing, set retention to auto-remove and auto-close
    29   prefs.setIntPref("browser.download.manager.retention", aDoTest ? 0 : 2);
    30   prefs.setBoolPref("browser.download.manager.closeWhenDone", aDoTest);
    31 }
    33 function bug413093obs()
    34 {
    35   this.mDownload = null;
    36   this.wasPaused = false;
    37 }
    38 bug413093obs.prototype = {
    39   observe: function(aSubject, aTopic, aData)
    40   {
    41     if ("domwindowopened" == aTopic) {
    42       // If we opened before we were paused, we need to set up our proper state
    43       // We also should not try to resume (we weren't paused!)
    44       if (!this.wasPaused) {
    45         dump("domwindowopened callback - not pausing or resuming\n");
    46         this.wasPaused = true;
    47         return;
    48       }
    50       dump("domwindowopened callback - resuming download\n");
    52       // Resume the download now that UI is up and running
    53       let dm = Cc["@mozilla.org/download-manager;1"].
    54                 getService(Ci.nsIDownloadManager);
    55       dm.resumeDownload(this.mDownload.id);
    56     }
    57     else if("domwindowclosed" == aTopic) {
    58       let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
    59                getService(Ci.nsIWindowWatcher);
    60       ww.unregisterNotification(this);
    62       // Let the UI finish doing whatever it needs to do
    63       SimpleTest.executeSoon(function() {
    64         setPref(false);
    66         let dmui = Cc["@mozilla.org/download-manager-ui;1"].
    67                    getService(Ci.nsIDownloadManagerUI);
    68         ok(!dmui.visible, "Download Manager UI is not showing");
    70         // reset the changed config values to default value
    71         let prefs = Cc["@mozilla.org/preferences-service;1"].
    72                     getService(Ci.nsIPrefService);
    73         prefs.clearUserPref("browser.download.manager.retention");
    74         prefs.clearUserPref("browser.download.manager.closeWhenDone");
    76         SimpleTest.finish();
    77       });
    78     }
    79   },
    81   onDownloadStateChange: function(aOldState, aDownload)
    82   {
    83     if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING &&
    84         !this.wasPaused) {
    85       dump("onDownloadStateChange - pausing download\n");
    86       this.wasPaused = true;
    88       // Pause the download until the UI shows up
    89       let dm = Cc["@mozilla.org/download-manager;1"].
    90                 getService(Ci.nsIDownloadManager);
    91       dm.pauseDownload(aDownload.id);
    92     }
    94     if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
    95       aDownload.targetFile.remove(false);
    97       let dm = Cc["@mozilla.org/download-manager;1"].
    98                 getService(Ci.nsIDownloadManager);
    99       dm.removeListener(this);
   101       // If this test is going to pass, we'll get a domwindowclosed notification
   102     }
   103   },
   104   onStateChange: function(a, b, c, d, e) { },
   105   onProgressChange: function(a, b, c, d, e, f, g) { },
   106   onSecurityChange: function(a, b, c, d) { },
   107 };
   108 function test()
   109 {
   110   function addDownload() {
   111     function createURI(aObj) {
   112       let ios = Cc["@mozilla.org/network/io-service;1"].
   113                 getService(Ci.nsIIOService);
   114       return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) :
   115                                             ios.newURI(aObj, null, null);
   116     }
   118     const nsIWBP = Ci.nsIWebBrowserPersist;
   119     let persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
   120                   .createInstance(Ci.nsIWebBrowserPersist);
   121     persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
   122                            nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
   123                            nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
   125     let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
   126                  getService(Ci.nsIProperties);
   127     let destFile = dirSvc.get("ProfD", Ci.nsIFile);
   128     destFile.append("download.result");
   129     if (destFile.exists())
   130       destFile.remove(false);
   132     let src = createURI("http://example.com/httpd.js");
   133     let target = createURI(destFile);
   134     let tr = Cc["@mozilla.org/transfer;1"].createInstance(Ci.nsITransfer);
   135     tr.init(src, target, "test download", null, Math.round(Date.now() * 1000),
   136             null, persist, false);
   137     persist.progressListener = tr;
   138     persist.saveURI(src, null, null, null, null, destFile);
   139   }
   141   // First, we clear out the database
   142   let dm = Cc["@mozilla.org/download-manager;1"].
   143            getService(Ci.nsIDownloadManager);
   144   dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads");
   146   // See if the DM is already open, and if it is, close it!
   147   let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
   148            getService(Ci.nsIWindowMediator);
   149   let win = wm.getMostRecentWindow("Download:Manager");
   150   if (win)
   151     win.close();
   153   let obs = new bug413093obs();
   154   dm.addListener(obs);
   155   setPref(true);
   157   // Start the test when the download manager window loads
   158   let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
   159            getService(Ci.nsIWindowWatcher);
   160   ww.registerNotification(obs);
   162   addDownload();
   164   SimpleTest.waitForExplicitFinish();
   165 }
   167   ]]>
   168   </script>
   170   <body xmlns="http://www.w3.org/1999/xhtml">
   171     <p id="display"></p>
   172     <div id="content" style="display:none;"></div>
   173     <pre id="test"></pre>
   174   </body>
   175 </window>

mercurial