1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/downloads/test/unit/test_download_manager.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,135 @@ 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 + 1.8 +// This file tests the download manager backend 1.9 + 1.10 +const nsIDownloadManager = Ci.nsIDownloadManager; 1.11 +const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager); 1.12 + 1.13 +function test_get_download_empty_queue() 1.14 +{ 1.15 + try { 1.16 + dm.getDownload(0); 1.17 + do_throw("Hey! We expect to get an excpetion with this!"); 1.18 + } catch(e) { 1.19 + do_check_eq(Components.lastResult, Cr.NS_ERROR_NOT_AVAILABLE); 1.20 + } 1.21 +} 1.22 + 1.23 +function test_connection() 1.24 +{ 1.25 + print("*** DOWNLOAD MANAGER TEST - test_connection"); 1.26 + var ds = dm.DBConnection; 1.27 + 1.28 + do_check_true(ds.connectionReady); 1.29 + 1.30 + do_check_true(ds.tableExists("moz_downloads")); 1.31 +} 1.32 + 1.33 +function test_count_empty_queue() 1.34 +{ 1.35 + print("*** DOWNLOAD MANAGER TEST - test_count_empty_queue"); 1.36 + do_check_eq(0, dm.activeDownloadCount); 1.37 + 1.38 + do_check_false(dm.activeDownloads.hasMoreElements()); 1.39 +} 1.40 + 1.41 +function test_canCleanUp_empty_queue() 1.42 +{ 1.43 + print("*** DOWNLOAD MANAGER TEST - test_canCleanUp_empty_queue"); 1.44 + do_check_false(dm.canCleanUp); 1.45 +} 1.46 + 1.47 +function test_pauseDownload_empty_queue() 1.48 +{ 1.49 + print("*** DOWNLOAD MANAGER TEST - test_pauseDownload_empty_queue"); 1.50 + try { 1.51 + dm.pauseDownload(0); 1.52 + do_throw("This should not be reached"); 1.53 + } catch (e) { 1.54 + do_check_eq(Cr.NS_ERROR_FAILURE, e.result); 1.55 + } 1.56 +} 1.57 + 1.58 +function test_resumeDownload_empty_queue() 1.59 +{ 1.60 + print("*** DOWNLOAD MANAGER TEST - test_resumeDownload_empty_queue"); 1.61 + try { 1.62 + dm.resumeDownload(0); 1.63 + do_throw("This should not be reached"); 1.64 + } catch (e) { 1.65 + do_check_eq(Cr.NS_ERROR_FAILURE, e.result); 1.66 + } 1.67 +} 1.68 + 1.69 +function test_addDownload_normal() 1.70 +{ 1.71 + print("*** DOWNLOAD MANAGER TEST - Testing normal download adding"); 1.72 + addDownload(httpserv); 1.73 +} 1.74 + 1.75 +function test_addDownload_cancel() 1.76 +{ 1.77 + print("*** DOWNLOAD MANAGER TEST - Testing download cancel"); 1.78 + var dl = addDownload(httpserv); 1.79 + 1.80 + dm.cancelDownload(dl.id); 1.81 + 1.82 + do_check_eq(nsIDownloadManager.DOWNLOAD_CANCELED, dl.state); 1.83 +} 1.84 + 1.85 +// This test is actually ran by the observer 1.86 +function test_dm_getDownload(aDl) 1.87 +{ 1.88 + // this will get it from the database 1.89 + var dl = dm.getDownload(aDl.id); 1.90 + 1.91 + do_check_eq(aDl.displayName, dl.displayName); 1.92 +} 1.93 + 1.94 +var tests = [test_get_download_empty_queue, test_connection, 1.95 + test_count_empty_queue, test_canCleanUp_empty_queue, 1.96 + test_pauseDownload_empty_queue, test_resumeDownload_empty_queue, 1.97 + test_addDownload_normal, test_addDownload_cancel]; 1.98 + 1.99 +var httpserv = null; 1.100 +function run_test() 1.101 +{ 1.102 + if (oldDownloadManagerDisabled()) { 1.103 + return; 1.104 + } 1.105 + 1.106 + httpserv = new HttpServer(); 1.107 + httpserv.registerDirectory("/", do_get_cwd()); 1.108 + httpserv.start(-1); 1.109 + 1.110 + // our download listener 1.111 + var listener = { 1.112 + // this listener checks to ensure activeDownloadCount is correct. 1.113 + onDownloadStateChange: function(aState, aDownload) 1.114 + { 1.115 + do_check_eq(gDownloadCount, dm.activeDownloadCount); 1.116 + }, 1.117 + onStateChange: function(a, b, c, d, e) { }, 1.118 + onProgressChange: function(a, b, c, d, e, f, g) { }, 1.119 + onSecurityChange: function(a, b, c, d) { } 1.120 + }; 1.121 + dm.addListener(listener); 1.122 + dm.addListener(getDownloadListener()); 1.123 + 1.124 + var observer = { 1.125 + observe: function(aSubject, aTopic, aData) { 1.126 + var dl = aSubject.QueryInterface(Ci.nsIDownload); 1.127 + do_check_eq(nsIDownloadManager.DOWNLOAD_CANCELED, dl.state); 1.128 + do_check_true(dm.canCleanUp); 1.129 + test_dm_getDownload(dl); 1.130 + } 1.131 + }; 1.132 + var os = Cc["@mozilla.org/observer-service;1"] 1.133 + .getService(Ci.nsIObserverService); 1.134 + os.addObserver(observer, "dl-cancel", false); 1.135 + 1.136 + for (var i = 0; i < tests.length; i++) 1.137 + tests[i](); 1.138 +}