toolkit/components/downloads/test/browser/browser_nsIDownloadManagerUI.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/downloads/test/browser/browser_nsIDownloadManagerUI.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     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 +let Cr = Components.results;
     1.8 +
     1.9 +function test_visibility_open()
    1.10 +{
    1.11 +  var dmui = Cc["@mozilla.org/download-manager-ui;1"].
    1.12 +             getService(Ci.nsIDownloadManagerUI);
    1.13 +  is(dmui.visible, true,
    1.14 +     "nsIDownloadManagerUI indicates that the UI is visible");
    1.15 +}
    1.16 +
    1.17 +function test_getAttention_opened()
    1.18 +{
    1.19 +  var dmui = Cc["@mozilla.org/download-manager-ui;1"].
    1.20 +             getService(Ci.nsIDownloadManagerUI);
    1.21 +
    1.22 +  // switch focus to this window
    1.23 +  window.focus();
    1.24 +
    1.25 +  dmui.getAttention();
    1.26 +  is(dmui.visible, true,
    1.27 +     "nsIDownloadManagerUI indicates that the UI is visible");
    1.28 +}
    1.29 +
    1.30 +function test_visibility_closed(aWin)
    1.31 +{
    1.32 +  var dmui = Cc["@mozilla.org/download-manager-ui;1"].
    1.33 +             getService(Ci.nsIDownloadManagerUI);
    1.34 +  aWin.close();
    1.35 +  is(dmui.visible, false,
    1.36 +     "nsIDownloadManagerUI indicates that the UI is not visible");
    1.37 +}
    1.38 +
    1.39 +function test_getAttention_closed()
    1.40 +{
    1.41 +  var dmui = Cc["@mozilla.org/download-manager-ui;1"].
    1.42 +             getService(Ci.nsIDownloadManagerUI);
    1.43 +
    1.44 +  var exceptionCaught = false;
    1.45 +  try {
    1.46 +    dmui.getAttention();
    1.47 +  } catch (e) {
    1.48 +    is(e.result, Cr.NS_ERROR_UNEXPECTED,
    1.49 +       "Proper exception was caught");
    1.50 +    exceptionCaught = true;
    1.51 +  } finally {
    1.52 +    is(exceptionCaught, true,
    1.53 +       "Exception was caught, as expected");
    1.54 +  }
    1.55 +}
    1.56 +
    1.57 +var testFuncs = [
    1.58 +    test_visibility_open
    1.59 +  , test_getAttention_opened
    1.60 +  , test_visibility_closed /* all tests after this *must* expect there to be
    1.61 +                              no open window, otherwise they will fail! */
    1.62 +  , test_getAttention_closed
    1.63 +];
    1.64 +
    1.65 +function test()
    1.66 +{
    1.67 +  try {
    1.68 +    if (Services.prefs.getBoolPref("browser.download.useJSTransfer")) {
    1.69 +      return;
    1.70 +    }
    1.71 +  } catch (ex) { }
    1.72 +
    1.73 +  var dm = Cc["@mozilla.org/download-manager;1"].
    1.74 +           getService(Ci.nsIDownloadManager);
    1.75 +  var db = dm.DBConnection;
    1.76 +
    1.77 +  // First, we populate the database with some fake data
    1.78 +  db.executeSimpleSQL("DELETE FROM moz_downloads");
    1.79 +
    1.80 +  // See if the DM is already open, and if it is, close it!
    1.81 +  var win = Services.wm.getMostRecentWindow("Download:Manager");
    1.82 +  if (win)
    1.83 +    win.close();
    1.84 +
    1.85 +  // Ensure that the download manager callbacks and nsIDownloadManagerUI always
    1.86 +  // use the window UI instead of the panel in the browser's window.
    1.87 +  Services.prefs.setBoolPref("browser.download.useToolkitUI", true);
    1.88 +  registerCleanupFunction(function() {
    1.89 +    Services.prefs.clearUserPref("browser.download.useToolkitUI");
    1.90 +  });
    1.91 +
    1.92 +  // OK, now that all the data is in, let's pull up the UI
    1.93 +  Cc["@mozilla.org/download-manager-ui;1"].
    1.94 +  getService(Ci.nsIDownloadManagerUI).show();
    1.95 +
    1.96 +  // The window doesn't open once we call show, so we need to wait a little bit
    1.97 +  function finishUp() {
    1.98 +    var win = Services.wm.getMostRecentWindow("Download:Manager");
    1.99 +
   1.100 +    // Now we can run our tests
   1.101 +    for each (var t in testFuncs)
   1.102 +      t(win);
   1.103 +
   1.104 +    finish();
   1.105 +  }
   1.106 +  
   1.107 +  waitForExplicitFinish();
   1.108 +  window.setTimeout(finishUp, 1000);
   1.109 +}

mercurial