Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | let Cr = Components.results; |
michael@0 | 5 | |
michael@0 | 6 | function test_visibility_open() |
michael@0 | 7 | { |
michael@0 | 8 | var dmui = Cc["@mozilla.org/download-manager-ui;1"]. |
michael@0 | 9 | getService(Ci.nsIDownloadManagerUI); |
michael@0 | 10 | is(dmui.visible, true, |
michael@0 | 11 | "nsIDownloadManagerUI indicates that the UI is visible"); |
michael@0 | 12 | } |
michael@0 | 13 | |
michael@0 | 14 | function test_getAttention_opened() |
michael@0 | 15 | { |
michael@0 | 16 | var dmui = Cc["@mozilla.org/download-manager-ui;1"]. |
michael@0 | 17 | getService(Ci.nsIDownloadManagerUI); |
michael@0 | 18 | |
michael@0 | 19 | // switch focus to this window |
michael@0 | 20 | window.focus(); |
michael@0 | 21 | |
michael@0 | 22 | dmui.getAttention(); |
michael@0 | 23 | is(dmui.visible, true, |
michael@0 | 24 | "nsIDownloadManagerUI indicates that the UI is visible"); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | function test_visibility_closed(aWin) |
michael@0 | 28 | { |
michael@0 | 29 | var dmui = Cc["@mozilla.org/download-manager-ui;1"]. |
michael@0 | 30 | getService(Ci.nsIDownloadManagerUI); |
michael@0 | 31 | aWin.close(); |
michael@0 | 32 | is(dmui.visible, false, |
michael@0 | 33 | "nsIDownloadManagerUI indicates that the UI is not visible"); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | function test_getAttention_closed() |
michael@0 | 37 | { |
michael@0 | 38 | var dmui = Cc["@mozilla.org/download-manager-ui;1"]. |
michael@0 | 39 | getService(Ci.nsIDownloadManagerUI); |
michael@0 | 40 | |
michael@0 | 41 | var exceptionCaught = false; |
michael@0 | 42 | try { |
michael@0 | 43 | dmui.getAttention(); |
michael@0 | 44 | } catch (e) { |
michael@0 | 45 | is(e.result, Cr.NS_ERROR_UNEXPECTED, |
michael@0 | 46 | "Proper exception was caught"); |
michael@0 | 47 | exceptionCaught = true; |
michael@0 | 48 | } finally { |
michael@0 | 49 | is(exceptionCaught, true, |
michael@0 | 50 | "Exception was caught, as expected"); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | var testFuncs = [ |
michael@0 | 55 | test_visibility_open |
michael@0 | 56 | , test_getAttention_opened |
michael@0 | 57 | , test_visibility_closed /* all tests after this *must* expect there to be |
michael@0 | 58 | no open window, otherwise they will fail! */ |
michael@0 | 59 | , test_getAttention_closed |
michael@0 | 60 | ]; |
michael@0 | 61 | |
michael@0 | 62 | function test() |
michael@0 | 63 | { |
michael@0 | 64 | try { |
michael@0 | 65 | if (Services.prefs.getBoolPref("browser.download.useJSTransfer")) { |
michael@0 | 66 | return; |
michael@0 | 67 | } |
michael@0 | 68 | } catch (ex) { } |
michael@0 | 69 | |
michael@0 | 70 | var dm = Cc["@mozilla.org/download-manager;1"]. |
michael@0 | 71 | getService(Ci.nsIDownloadManager); |
michael@0 | 72 | var db = dm.DBConnection; |
michael@0 | 73 | |
michael@0 | 74 | // First, we populate the database with some fake data |
michael@0 | 75 | db.executeSimpleSQL("DELETE FROM moz_downloads"); |
michael@0 | 76 | |
michael@0 | 77 | // See if the DM is already open, and if it is, close it! |
michael@0 | 78 | var win = Services.wm.getMostRecentWindow("Download:Manager"); |
michael@0 | 79 | if (win) |
michael@0 | 80 | win.close(); |
michael@0 | 81 | |
michael@0 | 82 | // Ensure that the download manager callbacks and nsIDownloadManagerUI always |
michael@0 | 83 | // use the window UI instead of the panel in the browser's window. |
michael@0 | 84 | Services.prefs.setBoolPref("browser.download.useToolkitUI", true); |
michael@0 | 85 | registerCleanupFunction(function() { |
michael@0 | 86 | Services.prefs.clearUserPref("browser.download.useToolkitUI"); |
michael@0 | 87 | }); |
michael@0 | 88 | |
michael@0 | 89 | // OK, now that all the data is in, let's pull up the UI |
michael@0 | 90 | Cc["@mozilla.org/download-manager-ui;1"]. |
michael@0 | 91 | getService(Ci.nsIDownloadManagerUI).show(); |
michael@0 | 92 | |
michael@0 | 93 | // The window doesn't open once we call show, so we need to wait a little bit |
michael@0 | 94 | function finishUp() { |
michael@0 | 95 | var win = Services.wm.getMostRecentWindow("Download:Manager"); |
michael@0 | 96 | |
michael@0 | 97 | // Now we can run our tests |
michael@0 | 98 | for each (var t in testFuncs) |
michael@0 | 99 | t(win); |
michael@0 | 100 | |
michael@0 | 101 | finish(); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | waitForExplicitFinish(); |
michael@0 | 105 | window.setTimeout(finishUp, 1000); |
michael@0 | 106 | } |