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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/downloads/tests/chrome/test_retention_is_0_closes.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,175 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<!-- This Source Code Form is subject to the terms of the Mozilla Public
     1.6 +   - License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 +   - file, You can obtain one at http://mozilla.org/MPL/2.0/.  -->
     1.8 +<!--
     1.9 + * This tests that the download manager UI closes upon download completion when
    1.10 + * browser.download.manager.retention is set to 0.  This test was added in bug
    1.11 + * 413093.
    1.12 +-->
    1.13 +
    1.14 +<window title="Download Manager Test"
    1.15 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    1.16 +        onload="test();">
    1.17 +
    1.18 +  <script type="application/javascript"
    1.19 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.20 +  <script type="application/javascript"
    1.21 +          src="utils.js"/>
    1.22 +
    1.23 +  <script type="application/javascript">
    1.24 +  <![CDATA[
    1.25 +
    1.26 +function setPref(aDoTest)
    1.27 +{
    1.28 +  let prefs = Cc["@mozilla.org/preferences-service;1"].
    1.29 +              getService(Ci.nsIPrefBranch);
    1.30 +
    1.31 +  // If we're testing, set retention to auto-remove and auto-close
    1.32 +  prefs.setIntPref("browser.download.manager.retention", aDoTest ? 0 : 2);
    1.33 +  prefs.setBoolPref("browser.download.manager.closeWhenDone", aDoTest);
    1.34 +}
    1.35 +
    1.36 +function bug413093obs()
    1.37 +{
    1.38 +  this.mDownload = null;
    1.39 +  this.wasPaused = false;
    1.40 +}
    1.41 +bug413093obs.prototype = {
    1.42 +  observe: function(aSubject, aTopic, aData)
    1.43 +  {
    1.44 +    if ("domwindowopened" == aTopic) {
    1.45 +      // If we opened before we were paused, we need to set up our proper state
    1.46 +      // We also should not try to resume (we weren't paused!)
    1.47 +      if (!this.wasPaused) {
    1.48 +        dump("domwindowopened callback - not pausing or resuming\n");
    1.49 +        this.wasPaused = true;
    1.50 +        return;
    1.51 +      }
    1.52 +
    1.53 +      dump("domwindowopened callback - resuming download\n");
    1.54 +
    1.55 +      // Resume the download now that UI is up and running
    1.56 +      let dm = Cc["@mozilla.org/download-manager;1"].
    1.57 +                getService(Ci.nsIDownloadManager);
    1.58 +      dm.resumeDownload(this.mDownload.id);
    1.59 +    }
    1.60 +    else if("domwindowclosed" == aTopic) {
    1.61 +      let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
    1.62 +               getService(Ci.nsIWindowWatcher);
    1.63 +      ww.unregisterNotification(this);
    1.64 +
    1.65 +      // Let the UI finish doing whatever it needs to do
    1.66 +      SimpleTest.executeSoon(function() {
    1.67 +        setPref(false);
    1.68 +
    1.69 +        let dmui = Cc["@mozilla.org/download-manager-ui;1"].
    1.70 +                   getService(Ci.nsIDownloadManagerUI);
    1.71 +        ok(!dmui.visible, "Download Manager UI is not showing");
    1.72 +
    1.73 +        // reset the changed config values to default value
    1.74 +        let prefs = Cc["@mozilla.org/preferences-service;1"].
    1.75 +                    getService(Ci.nsIPrefService);
    1.76 +        prefs.clearUserPref("browser.download.manager.retention");
    1.77 +        prefs.clearUserPref("browser.download.manager.closeWhenDone");
    1.78 +
    1.79 +        SimpleTest.finish();
    1.80 +      });
    1.81 +    }
    1.82 +  },
    1.83 +
    1.84 +  onDownloadStateChange: function(aOldState, aDownload)
    1.85 +  {
    1.86 +    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING &&
    1.87 +        !this.wasPaused) {
    1.88 +      dump("onDownloadStateChange - pausing download\n");
    1.89 +      this.wasPaused = true;
    1.90 +
    1.91 +      // Pause the download until the UI shows up
    1.92 +      let dm = Cc["@mozilla.org/download-manager;1"].
    1.93 +                getService(Ci.nsIDownloadManager);
    1.94 +      dm.pauseDownload(aDownload.id);
    1.95 +    }
    1.96 +
    1.97 +    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
    1.98 +      aDownload.targetFile.remove(false);
    1.99 +
   1.100 +      let dm = Cc["@mozilla.org/download-manager;1"].
   1.101 +                getService(Ci.nsIDownloadManager);
   1.102 +      dm.removeListener(this);
   1.103 +
   1.104 +      // If this test is going to pass, we'll get a domwindowclosed notification
   1.105 +    }
   1.106 +  },
   1.107 +  onStateChange: function(a, b, c, d, e) { },
   1.108 +  onProgressChange: function(a, b, c, d, e, f, g) { },
   1.109 +  onSecurityChange: function(a, b, c, d) { },
   1.110 +};
   1.111 +function test()
   1.112 +{
   1.113 +  function addDownload() {
   1.114 +    function createURI(aObj) {
   1.115 +      let ios = Cc["@mozilla.org/network/io-service;1"].
   1.116 +                getService(Ci.nsIIOService);
   1.117 +      return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) :
   1.118 +                                            ios.newURI(aObj, null, null);
   1.119 +    }
   1.120 +
   1.121 +    const nsIWBP = Ci.nsIWebBrowserPersist;
   1.122 +    let persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
   1.123 +                  .createInstance(Ci.nsIWebBrowserPersist);
   1.124 +    persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
   1.125 +                           nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
   1.126 +                           nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
   1.127 +
   1.128 +    let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
   1.129 +                 getService(Ci.nsIProperties);
   1.130 +    let destFile = dirSvc.get("ProfD", Ci.nsIFile);
   1.131 +    destFile.append("download.result");
   1.132 +    if (destFile.exists())
   1.133 +      destFile.remove(false);
   1.134 +
   1.135 +    let src = createURI("http://example.com/httpd.js");
   1.136 +    let target = createURI(destFile);
   1.137 +    let tr = Cc["@mozilla.org/transfer;1"].createInstance(Ci.nsITransfer);
   1.138 +    tr.init(src, target, "test download", null, Math.round(Date.now() * 1000),
   1.139 +            null, persist, false);
   1.140 +    persist.progressListener = tr;
   1.141 +    persist.saveURI(src, null, null, null, null, destFile);
   1.142 +  }
   1.143 +
   1.144 +  // First, we clear out the database
   1.145 +  let dm = Cc["@mozilla.org/download-manager;1"].
   1.146 +           getService(Ci.nsIDownloadManager);
   1.147 +  dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads");
   1.148 +
   1.149 +  // See if the DM is already open, and if it is, close it!
   1.150 +  let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
   1.151 +           getService(Ci.nsIWindowMediator);
   1.152 +  let win = wm.getMostRecentWindow("Download:Manager");
   1.153 +  if (win)
   1.154 +    win.close();
   1.155 +
   1.156 +  let obs = new bug413093obs();
   1.157 +  dm.addListener(obs);
   1.158 +  setPref(true);
   1.159 +
   1.160 +  // Start the test when the download manager window loads
   1.161 +  let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
   1.162 +           getService(Ci.nsIWindowWatcher);
   1.163 +  ww.registerNotification(obs);
   1.164 +
   1.165 +  addDownload();
   1.166 +
   1.167 +  SimpleTest.waitForExplicitFinish();
   1.168 +}
   1.169 +
   1.170 +  ]]>
   1.171 +  </script>
   1.172 +
   1.173 +  <body xmlns="http://www.w3.org/1999/xhtml">
   1.174 +    <p id="display"></p>
   1.175 +    <div id="content" style="display:none;"></div>
   1.176 +    <pre id="test"></pre>
   1.177 +  </body>
   1.178 +</window>

mercurial