michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This file tests the download manager backend michael@0: michael@0: const nsIDownloadManager = Ci.nsIDownloadManager; michael@0: const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager); michael@0: michael@0: function test_get_download_empty_queue() michael@0: { michael@0: try { michael@0: dm.getDownload(0); michael@0: do_throw("Hey! We expect to get an excpetion with this!"); michael@0: } catch(e) { michael@0: do_check_eq(Components.lastResult, Cr.NS_ERROR_NOT_AVAILABLE); michael@0: } michael@0: } michael@0: michael@0: function test_connection() michael@0: { michael@0: print("*** DOWNLOAD MANAGER TEST - test_connection"); michael@0: var ds = dm.DBConnection; michael@0: michael@0: do_check_true(ds.connectionReady); michael@0: michael@0: do_check_true(ds.tableExists("moz_downloads")); michael@0: } michael@0: michael@0: function test_count_empty_queue() michael@0: { michael@0: print("*** DOWNLOAD MANAGER TEST - test_count_empty_queue"); michael@0: do_check_eq(0, dm.activeDownloadCount); michael@0: michael@0: do_check_false(dm.activeDownloads.hasMoreElements()); michael@0: } michael@0: michael@0: function test_canCleanUp_empty_queue() michael@0: { michael@0: print("*** DOWNLOAD MANAGER TEST - test_canCleanUp_empty_queue"); michael@0: do_check_false(dm.canCleanUp); michael@0: } michael@0: michael@0: function test_pauseDownload_empty_queue() michael@0: { michael@0: print("*** DOWNLOAD MANAGER TEST - test_pauseDownload_empty_queue"); michael@0: try { michael@0: dm.pauseDownload(0); michael@0: do_throw("This should not be reached"); michael@0: } catch (e) { michael@0: do_check_eq(Cr.NS_ERROR_FAILURE, e.result); michael@0: } michael@0: } michael@0: michael@0: function test_resumeDownload_empty_queue() michael@0: { michael@0: print("*** DOWNLOAD MANAGER TEST - test_resumeDownload_empty_queue"); michael@0: try { michael@0: dm.resumeDownload(0); michael@0: do_throw("This should not be reached"); michael@0: } catch (e) { michael@0: do_check_eq(Cr.NS_ERROR_FAILURE, e.result); michael@0: } michael@0: } michael@0: michael@0: function test_addDownload_normal() michael@0: { michael@0: print("*** DOWNLOAD MANAGER TEST - Testing normal download adding"); michael@0: addDownload(httpserv); michael@0: } michael@0: michael@0: function test_addDownload_cancel() michael@0: { michael@0: print("*** DOWNLOAD MANAGER TEST - Testing download cancel"); michael@0: var dl = addDownload(httpserv); michael@0: michael@0: dm.cancelDownload(dl.id); michael@0: michael@0: do_check_eq(nsIDownloadManager.DOWNLOAD_CANCELED, dl.state); michael@0: } michael@0: michael@0: // This test is actually ran by the observer michael@0: function test_dm_getDownload(aDl) michael@0: { michael@0: // this will get it from the database michael@0: var dl = dm.getDownload(aDl.id); michael@0: michael@0: do_check_eq(aDl.displayName, dl.displayName); michael@0: } michael@0: michael@0: var tests = [test_get_download_empty_queue, test_connection, michael@0: test_count_empty_queue, test_canCleanUp_empty_queue, michael@0: test_pauseDownload_empty_queue, test_resumeDownload_empty_queue, michael@0: test_addDownload_normal, test_addDownload_cancel]; michael@0: michael@0: var httpserv = null; michael@0: function run_test() michael@0: { michael@0: if (oldDownloadManagerDisabled()) { michael@0: return; michael@0: } michael@0: michael@0: httpserv = new HttpServer(); michael@0: httpserv.registerDirectory("/", do_get_cwd()); michael@0: httpserv.start(-1); michael@0: michael@0: // our download listener michael@0: var listener = { michael@0: // this listener checks to ensure activeDownloadCount is correct. michael@0: onDownloadStateChange: function(aState, aDownload) michael@0: { michael@0: do_check_eq(gDownloadCount, dm.activeDownloadCount); michael@0: }, michael@0: onStateChange: function(a, b, c, d, e) { }, michael@0: onProgressChange: function(a, b, c, d, e, f, g) { }, michael@0: onSecurityChange: function(a, b, c, d) { } michael@0: }; michael@0: dm.addListener(listener); michael@0: dm.addListener(getDownloadListener()); michael@0: michael@0: var observer = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: var dl = aSubject.QueryInterface(Ci.nsIDownload); michael@0: do_check_eq(nsIDownloadManager.DOWNLOAD_CANCELED, dl.state); michael@0: do_check_true(dm.canCleanUp); michael@0: test_dm_getDownload(dl); michael@0: } michael@0: }; michael@0: var os = Cc["@mozilla.org/observer-service;1"] michael@0: .getService(Ci.nsIObserverService); michael@0: os.addObserver(observer, "dl-cancel", false); michael@0: michael@0: for (var i = 0; i < tests.length; i++) michael@0: tests[i](); michael@0: }