toolkit/components/downloads/test/unit/test_download_manager.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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
michael@0 5 // This file tests the download manager backend
michael@0 6
michael@0 7 const nsIDownloadManager = Ci.nsIDownloadManager;
michael@0 8 const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager);
michael@0 9
michael@0 10 function test_get_download_empty_queue()
michael@0 11 {
michael@0 12 try {
michael@0 13 dm.getDownload(0);
michael@0 14 do_throw("Hey! We expect to get an excpetion with this!");
michael@0 15 } catch(e) {
michael@0 16 do_check_eq(Components.lastResult, Cr.NS_ERROR_NOT_AVAILABLE);
michael@0 17 }
michael@0 18 }
michael@0 19
michael@0 20 function test_connection()
michael@0 21 {
michael@0 22 print("*** DOWNLOAD MANAGER TEST - test_connection");
michael@0 23 var ds = dm.DBConnection;
michael@0 24
michael@0 25 do_check_true(ds.connectionReady);
michael@0 26
michael@0 27 do_check_true(ds.tableExists("moz_downloads"));
michael@0 28 }
michael@0 29
michael@0 30 function test_count_empty_queue()
michael@0 31 {
michael@0 32 print("*** DOWNLOAD MANAGER TEST - test_count_empty_queue");
michael@0 33 do_check_eq(0, dm.activeDownloadCount);
michael@0 34
michael@0 35 do_check_false(dm.activeDownloads.hasMoreElements());
michael@0 36 }
michael@0 37
michael@0 38 function test_canCleanUp_empty_queue()
michael@0 39 {
michael@0 40 print("*** DOWNLOAD MANAGER TEST - test_canCleanUp_empty_queue");
michael@0 41 do_check_false(dm.canCleanUp);
michael@0 42 }
michael@0 43
michael@0 44 function test_pauseDownload_empty_queue()
michael@0 45 {
michael@0 46 print("*** DOWNLOAD MANAGER TEST - test_pauseDownload_empty_queue");
michael@0 47 try {
michael@0 48 dm.pauseDownload(0);
michael@0 49 do_throw("This should not be reached");
michael@0 50 } catch (e) {
michael@0 51 do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 function test_resumeDownload_empty_queue()
michael@0 56 {
michael@0 57 print("*** DOWNLOAD MANAGER TEST - test_resumeDownload_empty_queue");
michael@0 58 try {
michael@0 59 dm.resumeDownload(0);
michael@0 60 do_throw("This should not be reached");
michael@0 61 } catch (e) {
michael@0 62 do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
michael@0 63 }
michael@0 64 }
michael@0 65
michael@0 66 function test_addDownload_normal()
michael@0 67 {
michael@0 68 print("*** DOWNLOAD MANAGER TEST - Testing normal download adding");
michael@0 69 addDownload(httpserv);
michael@0 70 }
michael@0 71
michael@0 72 function test_addDownload_cancel()
michael@0 73 {
michael@0 74 print("*** DOWNLOAD MANAGER TEST - Testing download cancel");
michael@0 75 var dl = addDownload(httpserv);
michael@0 76
michael@0 77 dm.cancelDownload(dl.id);
michael@0 78
michael@0 79 do_check_eq(nsIDownloadManager.DOWNLOAD_CANCELED, dl.state);
michael@0 80 }
michael@0 81
michael@0 82 // This test is actually ran by the observer
michael@0 83 function test_dm_getDownload(aDl)
michael@0 84 {
michael@0 85 // this will get it from the database
michael@0 86 var dl = dm.getDownload(aDl.id);
michael@0 87
michael@0 88 do_check_eq(aDl.displayName, dl.displayName);
michael@0 89 }
michael@0 90
michael@0 91 var tests = [test_get_download_empty_queue, test_connection,
michael@0 92 test_count_empty_queue, test_canCleanUp_empty_queue,
michael@0 93 test_pauseDownload_empty_queue, test_resumeDownload_empty_queue,
michael@0 94 test_addDownload_normal, test_addDownload_cancel];
michael@0 95
michael@0 96 var httpserv = null;
michael@0 97 function run_test()
michael@0 98 {
michael@0 99 if (oldDownloadManagerDisabled()) {
michael@0 100 return;
michael@0 101 }
michael@0 102
michael@0 103 httpserv = new HttpServer();
michael@0 104 httpserv.registerDirectory("/", do_get_cwd());
michael@0 105 httpserv.start(-1);
michael@0 106
michael@0 107 // our download listener
michael@0 108 var listener = {
michael@0 109 // this listener checks to ensure activeDownloadCount is correct.
michael@0 110 onDownloadStateChange: function(aState, aDownload)
michael@0 111 {
michael@0 112 do_check_eq(gDownloadCount, dm.activeDownloadCount);
michael@0 113 },
michael@0 114 onStateChange: function(a, b, c, d, e) { },
michael@0 115 onProgressChange: function(a, b, c, d, e, f, g) { },
michael@0 116 onSecurityChange: function(a, b, c, d) { }
michael@0 117 };
michael@0 118 dm.addListener(listener);
michael@0 119 dm.addListener(getDownloadListener());
michael@0 120
michael@0 121 var observer = {
michael@0 122 observe: function(aSubject, aTopic, aData) {
michael@0 123 var dl = aSubject.QueryInterface(Ci.nsIDownload);
michael@0 124 do_check_eq(nsIDownloadManager.DOWNLOAD_CANCELED, dl.state);
michael@0 125 do_check_true(dm.canCleanUp);
michael@0 126 test_dm_getDownload(dl);
michael@0 127 }
michael@0 128 };
michael@0 129 var os = Cc["@mozilla.org/observer-service;1"]
michael@0 130 .getService(Ci.nsIObserverService);
michael@0 131 os.addObserver(observer, "dl-cancel", false);
michael@0 132
michael@0 133 for (var i = 0; i < tests.length; i++)
michael@0 134 tests[i]();
michael@0 135 }

mercurial