toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 let Cu = Components.utils;
michael@0 6 Cu.import("resource://gre/modules/DownloadUtils.jsm");
michael@0 7
michael@0 8 const gDecimalSymbol = Number(5.4).toLocaleString().match(/\D/);
michael@0 9 function _(str) {
michael@0 10 return str.replace(".", gDecimalSymbol, "g");
michael@0 11 }
michael@0 12
michael@0 13 function testConvertByteUnits(aBytes, aValue, aUnit)
michael@0 14 {
michael@0 15 let [value, unit] = DownloadUtils.convertByteUnits(aBytes);
michael@0 16 do_check_eq(value, aValue);
michael@0 17 do_check_eq(unit, aUnit);
michael@0 18 }
michael@0 19
michael@0 20 function testTransferTotal(aCurrBytes, aMaxBytes, aTransfer)
michael@0 21 {
michael@0 22 let transfer = DownloadUtils.getTransferTotal(aCurrBytes, aMaxBytes);
michael@0 23 do_check_eq(transfer, aTransfer);
michael@0 24 }
michael@0 25
michael@0 26 // Get the em-dash character because typing it directly here doesn't work :(
michael@0 27 let gDash = DownloadUtils.getDownloadStatus(0)[0].match(/remaining (.) 0 bytes/)[1];
michael@0 28
michael@0 29 let gVals = [0, 100, 2345, 55555, 982341, 23194134, 1482, 58, 9921949201, 13498132, Infinity];
michael@0 30
michael@0 31 function testStatus(aFunc, aCurr, aMore, aRate, aTest)
michael@0 32 {
michael@0 33 dump("Status Test: " + [aCurr, aMore, aRate, aTest] + "\n");
michael@0 34 let curr = gVals[aCurr];
michael@0 35 let max = curr + gVals[aMore];
michael@0 36 let speed = gVals[aRate];
michael@0 37
michael@0 38 let [status, last] = aFunc(curr, max, speed);
michael@0 39
michael@0 40 if (0) {
michael@0 41 dump("testStatus(" + aCurr + ", " + aMore + ", " + aRate + ", [\"" +
michael@0 42 status.replace(gDash, "--") + "\", " + last.toFixed(3) + "]);\n");
michael@0 43 }
michael@0 44
michael@0 45 // Make sure the status text matches
michael@0 46 do_check_eq(status, _(aTest[0].replace(/--/, gDash)));
michael@0 47
michael@0 48 // Make sure the lastSeconds matches
michael@0 49 if (last == Infinity)
michael@0 50 do_check_eq(last, aTest[1]);
michael@0 51 else
michael@0 52 do_check_true(Math.abs(last - aTest[1]) < .1);
michael@0 53 }
michael@0 54
michael@0 55 function testURI(aURI, aDisp, aHost)
michael@0 56 {
michael@0 57 dump("URI Test: " + [aURI, aDisp, aHost] + "\n");
michael@0 58
michael@0 59 let [disp, host] = DownloadUtils.getURIHost(aURI);
michael@0 60
michael@0 61 // Make sure we have the right display host and full host
michael@0 62 do_check_eq(disp, aDisp);
michael@0 63 do_check_eq(host, aHost);
michael@0 64 }
michael@0 65
michael@0 66
michael@0 67 function testGetReadableDates(aDate, aCompactValue)
michael@0 68 {
michael@0 69 const now = new Date(2000, 11, 31, 11, 59, 59);
michael@0 70
michael@0 71 let [dateCompact] = DownloadUtils.getReadableDates(aDate, now);
michael@0 72 do_check_eq(dateCompact, aCompactValue);
michael@0 73 }
michael@0 74
michael@0 75 function testAllGetReadableDates()
michael@0 76 {
michael@0 77 // This test cannot depend on the current date and time, or the date format.
michael@0 78 // It depends on being run with the English localization, however.
michael@0 79 const today_11_30 = new Date(2000, 11, 31, 11, 30, 15);
michael@0 80 const today_12_30 = new Date(2000, 11, 31, 12, 30, 15);
michael@0 81 const yesterday_11_30 = new Date(2000, 11, 30, 11, 30, 15);
michael@0 82 const yesterday_12_30 = new Date(2000, 11, 30, 12, 30, 15);
michael@0 83 const twodaysago = new Date(2000, 11, 29, 11, 30, 15);
michael@0 84 const sixdaysago = new Date(2000, 11, 25, 11, 30, 15);
michael@0 85 const sevendaysago = new Date(2000, 11, 24, 11, 30, 15);
michael@0 86
michael@0 87 let dts = Components.classes["@mozilla.org/intl/scriptabledateformat;1"].
michael@0 88 getService(Components.interfaces.nsIScriptableDateFormat);
michael@0 89
michael@0 90 testGetReadableDates(today_11_30, dts.FormatTime("", dts.timeFormatNoSeconds,
michael@0 91 11, 30, 0));
michael@0 92 testGetReadableDates(today_12_30, dts.FormatTime("", dts.timeFormatNoSeconds,
michael@0 93 12, 30, 0));
michael@0 94 testGetReadableDates(yesterday_11_30, "Yesterday");
michael@0 95 testGetReadableDates(yesterday_12_30, "Yesterday");
michael@0 96 testGetReadableDates(twodaysago, twodaysago.toLocaleFormat("%A"));
michael@0 97 testGetReadableDates(sixdaysago, sixdaysago.toLocaleFormat("%A"));
michael@0 98 testGetReadableDates(sevendaysago, sevendaysago.toLocaleFormat("%B") + " " +
michael@0 99 sevendaysago.toLocaleFormat("%d"));
michael@0 100
michael@0 101 let [, dateTimeFull] = DownloadUtils.getReadableDates(today_11_30);
michael@0 102 do_check_eq(dateTimeFull, dts.FormatDateTime("", dts.dateFormatLong,
michael@0 103 dts.timeFormatNoSeconds,
michael@0 104 2000, 12, 31, 11, 30, 0));
michael@0 105 }
michael@0 106
michael@0 107 function run_test()
michael@0 108 {
michael@0 109 testConvertByteUnits(-1, "-1", "bytes");
michael@0 110 testConvertByteUnits(1, _("1"), "bytes");
michael@0 111 testConvertByteUnits(42, _("42"), "bytes");
michael@0 112 testConvertByteUnits(123, _("123"), "bytes");
michael@0 113 testConvertByteUnits(1024, _("1.0"), "KB");
michael@0 114 testConvertByteUnits(8888, _("8.7"), "KB");
michael@0 115 testConvertByteUnits(59283, _("57.9"), "KB");
michael@0 116 testConvertByteUnits(640000, _("625"), "KB");
michael@0 117 testConvertByteUnits(1048576, _("1.0"), "MB");
michael@0 118 testConvertByteUnits(307232768, _("293"), "MB");
michael@0 119 testConvertByteUnits(1073741824, _("1.0"), "GB");
michael@0 120
michael@0 121 testTransferTotal(1, 1, _("1 of 1 bytes"));
michael@0 122 testTransferTotal(234, 4924, _("234 bytes of 4.8 KB"));
michael@0 123 testTransferTotal(94923, 233923, _("92.7 of 228 KB"));
michael@0 124 testTransferTotal(4924, 94923, _("4.8 of 92.7 KB"));
michael@0 125 testTransferTotal(2342, 294960345, _("2.3 KB of 281 MB"));
michael@0 126 testTransferTotal(234, undefined, _("234 bytes"));
michael@0 127 testTransferTotal(4889023, undefined, _("4.7 MB"));
michael@0 128
michael@0 129 if (0) {
michael@0 130 // Help find some interesting test cases
michael@0 131 let r = function() Math.floor(Math.random() * 10);
michael@0 132 for (let i = 0; i < 100; i++) {
michael@0 133 testStatus(r(), r(), r());
michael@0 134 }
michael@0 135 }
michael@0 136
michael@0 137 // First, test with rates, via getDownloadStatus...
michael@0 138 let statusFunc = DownloadUtils.getDownloadStatus.bind(DownloadUtils);
michael@0 139
michael@0 140 testStatus(statusFunc, 2, 1, 7, ["A few seconds remaining -- 2.3 of 2.4 KB (58 bytes/sec)", 1.724]);
michael@0 141 testStatus(statusFunc, 1, 2, 6, ["A few seconds remaining -- 100 bytes of 2.4 KB (1.4 KB/sec)", 1.582]);
michael@0 142 testStatus(statusFunc, 4, 3, 9, ["A few seconds remaining -- 959 KB of 1.0 MB (12.9 MB/sec)", 0.004]);
michael@0 143 testStatus(statusFunc, 2, 3, 8, ["A few seconds remaining -- 2.3 of 56.5 KB (9.2 GB/sec)", 0.000]);
michael@0 144
michael@0 145 testStatus(statusFunc, 8, 4, 3, ["17 seconds remaining -- 9.2 of 9.2 GB (54.3 KB/sec)", 17.682]);
michael@0 146 testStatus(statusFunc, 1, 3, 2, ["23 seconds remaining -- 100 bytes of 54.4 KB (2.3 KB/sec)", 23.691]);
michael@0 147 testStatus(statusFunc, 9, 3, 2, ["23 seconds remaining -- 12.9 of 12.9 MB (2.3 KB/sec)", 23.691]);
michael@0 148 testStatus(statusFunc, 5, 6, 7, ["25 seconds remaining -- 22.1 of 22.1 MB (58 bytes/sec)", 25.552]);
michael@0 149
michael@0 150 testStatus(statusFunc, 3, 9, 3, ["4 minutes remaining -- 54.3 KB of 12.9 MB (54.3 KB/sec)", 242.969]);
michael@0 151 testStatus(statusFunc, 2, 3, 1, ["9 minutes remaining -- 2.3 of 56.5 KB (100 bytes/sec)", 555.550]);
michael@0 152 testStatus(statusFunc, 4, 3, 7, ["15 minutes remaining -- 959 KB of 1.0 MB (58 bytes/sec)", 957.845]);
michael@0 153 testStatus(statusFunc, 5, 3, 7, ["15 minutes remaining -- 22.1 of 22.2 MB (58 bytes/sec)", 957.845]);
michael@0 154
michael@0 155 testStatus(statusFunc, 1, 9, 2, ["1 hour, 35 minutes remaining -- 100 bytes of 12.9 MB (2.3 KB/sec)", 5756.133]);
michael@0 156 testStatus(statusFunc, 2, 9, 6, ["2 hours, 31 minutes remaining -- 2.3 KB of 12.9 MB (1.4 KB/sec)", 9108.051]);
michael@0 157 testStatus(statusFunc, 2, 4, 1, ["2 hours, 43 minutes remaining -- 2.3 of 962 KB (100 bytes/sec)", 9823.410]);
michael@0 158 testStatus(statusFunc, 6, 4, 7, ["4 hours, 42 minutes remaining -- 1.4 of 961 KB (58 bytes/sec)", 16936.914]);
michael@0 159
michael@0 160 testStatus(statusFunc, 6, 9, 1, ["1 day, 13 hours remaining -- 1.4 KB of 12.9 MB (100 bytes/sec)", 134981.320]);
michael@0 161 testStatus(statusFunc, 3, 8, 3, ["2 days, 1 hour remaining -- 54.3 KB of 9.2 GB (54.3 KB/sec)", 178596.872]);
michael@0 162 testStatus(statusFunc, 1, 8, 6, ["77 days, 11 hours remaining -- 100 bytes of 9.2 GB (1.4 KB/sec)", 6694972.470]);
michael@0 163 testStatus(statusFunc, 6, 8, 7, ["1979 days, 22 hours remaining -- 1.4 KB of 9.2 GB (58 bytes/sec)", 171068089.672]);
michael@0 164
michael@0 165 testStatus(statusFunc, 0, 0, 5, ["Unknown time remaining -- 0 of 0 bytes (22.1 MB/sec)", Infinity]);
michael@0 166 testStatus(statusFunc, 0, 6, 0, ["Unknown time remaining -- 0 bytes of 1.4 KB (0 bytes/sec)", Infinity]);
michael@0 167 testStatus(statusFunc, 6, 6, 0, ["Unknown time remaining -- 1.4 of 2.9 KB (0 bytes/sec)", Infinity]);
michael@0 168 testStatus(statusFunc, 8, 5, 0, ["Unknown time remaining -- 9.2 of 9.3 GB (0 bytes/sec)", Infinity]);
michael@0 169
michael@0 170 // With rate equal to Infinity
michael@0 171 testStatus(statusFunc, 0, 0, 10, ["Unknown time remaining -- 0 of 0 bytes (Really fast)", Infinity]);
michael@0 172 testStatus(statusFunc, 1, 2, 10, ["A few seconds remaining -- 100 bytes of 2.4 KB (Really fast)", 0]);
michael@0 173
michael@0 174 // Now test without rates, via getDownloadStatusNoRate.
michael@0 175 statusFunc = DownloadUtils.getDownloadStatusNoRate.bind(DownloadUtils);
michael@0 176
michael@0 177 testStatus(statusFunc, 2, 1, 7, ["A few seconds remaining -- 2.3 of 2.4 KB", 1.724]);
michael@0 178 testStatus(statusFunc, 1, 2, 6, ["A few seconds remaining -- 100 bytes of 2.4 KB", 1.582]);
michael@0 179 testStatus(statusFunc, 4, 3, 9, ["A few seconds remaining -- 959 KB of 1.0 MB", 0.004]);
michael@0 180 testStatus(statusFunc, 2, 3, 8, ["A few seconds remaining -- 2.3 of 56.5 KB", 0.000]);
michael@0 181
michael@0 182 testStatus(statusFunc, 8, 4, 3, ["17 seconds remaining -- 9.2 of 9.2 GB", 17.682]);
michael@0 183 testStatus(statusFunc, 1, 3, 2, ["23 seconds remaining -- 100 bytes of 54.4 KB", 23.691]);
michael@0 184 testStatus(statusFunc, 9, 3, 2, ["23 seconds remaining -- 12.9 of 12.9 MB", 23.691]);
michael@0 185 testStatus(statusFunc, 5, 6, 7, ["25 seconds remaining -- 22.1 of 22.1 MB", 25.552]);
michael@0 186
michael@0 187 testStatus(statusFunc, 3, 9, 3, ["4 minutes remaining -- 54.3 KB of 12.9 MB", 242.969]);
michael@0 188 testStatus(statusFunc, 2, 3, 1, ["9 minutes remaining -- 2.3 of 56.5 KB", 555.550]);
michael@0 189 testStatus(statusFunc, 4, 3, 7, ["15 minutes remaining -- 959 KB of 1.0 MB", 957.845]);
michael@0 190 testStatus(statusFunc, 5, 3, 7, ["15 minutes remaining -- 22.1 of 22.2 MB", 957.845]);
michael@0 191
michael@0 192 testStatus(statusFunc, 1, 9, 2, ["1 hour, 35 minutes remaining -- 100 bytes of 12.9 MB", 5756.133]);
michael@0 193 testStatus(statusFunc, 2, 9, 6, ["2 hours, 31 minutes remaining -- 2.3 KB of 12.9 MB", 9108.051]);
michael@0 194 testStatus(statusFunc, 2, 4, 1, ["2 hours, 43 minutes remaining -- 2.3 of 962 KB", 9823.410]);
michael@0 195 testStatus(statusFunc, 6, 4, 7, ["4 hours, 42 minutes remaining -- 1.4 of 961 KB", 16936.914]);
michael@0 196
michael@0 197 testStatus(statusFunc, 6, 9, 1, ["1 day, 13 hours remaining -- 1.4 KB of 12.9 MB", 134981.320]);
michael@0 198 testStatus(statusFunc, 3, 8, 3, ["2 days, 1 hour remaining -- 54.3 KB of 9.2 GB", 178596.872]);
michael@0 199 testStatus(statusFunc, 1, 8, 6, ["77 days, 11 hours remaining -- 100 bytes of 9.2 GB", 6694972.470]);
michael@0 200 testStatus(statusFunc, 6, 8, 7, ["1979 days, 22 hours remaining -- 1.4 KB of 9.2 GB", 171068089.672]);
michael@0 201
michael@0 202 testStatus(statusFunc, 0, 0, 5, ["Unknown time remaining -- 0 of 0 bytes", Infinity]);
michael@0 203 testStatus(statusFunc, 0, 6, 0, ["Unknown time remaining -- 0 bytes of 1.4 KB", Infinity]);
michael@0 204 testStatus(statusFunc, 6, 6, 0, ["Unknown time remaining -- 1.4 of 2.9 KB", Infinity]);
michael@0 205 testStatus(statusFunc, 8, 5, 0, ["Unknown time remaining -- 9.2 of 9.3 GB", Infinity]);
michael@0 206
michael@0 207 testURI("http://www.mozilla.org/", "mozilla.org", "www.mozilla.org");
michael@0 208 testURI("http://www.city.mikasa.hokkaido.jp/", "city.mikasa.hokkaido.jp", "www.city.mikasa.hokkaido.jp");
michael@0 209 testURI("data:text/html,Hello World", "data resource", "data resource");
michael@0 210 testURI("jar:http://www.mozilla.com/file!/magic", "mozilla.com", "www.mozilla.com");
michael@0 211 testURI("file:///C:/Cool/Stuff/", "local file", "local file");
michael@0 212 testURI("moz-icon:file:///test.extension", "moz-icon resource", "moz-icon resource");
michael@0 213 testURI("about:config", "about resource", "about resource");
michael@0 214
michael@0 215 testAllGetReadableDates();
michael@0 216 }

mercurial