1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,216 @@ 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 +let Cu = Components.utils; 1.9 +Cu.import("resource://gre/modules/DownloadUtils.jsm"); 1.10 + 1.11 +const gDecimalSymbol = Number(5.4).toLocaleString().match(/\D/); 1.12 +function _(str) { 1.13 + return str.replace(".", gDecimalSymbol, "g"); 1.14 +} 1.15 + 1.16 +function testConvertByteUnits(aBytes, aValue, aUnit) 1.17 +{ 1.18 + let [value, unit] = DownloadUtils.convertByteUnits(aBytes); 1.19 + do_check_eq(value, aValue); 1.20 + do_check_eq(unit, aUnit); 1.21 +} 1.22 + 1.23 +function testTransferTotal(aCurrBytes, aMaxBytes, aTransfer) 1.24 +{ 1.25 + let transfer = DownloadUtils.getTransferTotal(aCurrBytes, aMaxBytes); 1.26 + do_check_eq(transfer, aTransfer); 1.27 +} 1.28 + 1.29 +// Get the em-dash character because typing it directly here doesn't work :( 1.30 +let gDash = DownloadUtils.getDownloadStatus(0)[0].match(/remaining (.) 0 bytes/)[1]; 1.31 + 1.32 +let gVals = [0, 100, 2345, 55555, 982341, 23194134, 1482, 58, 9921949201, 13498132, Infinity]; 1.33 + 1.34 +function testStatus(aFunc, aCurr, aMore, aRate, aTest) 1.35 +{ 1.36 + dump("Status Test: " + [aCurr, aMore, aRate, aTest] + "\n"); 1.37 + let curr = gVals[aCurr]; 1.38 + let max = curr + gVals[aMore]; 1.39 + let speed = gVals[aRate]; 1.40 + 1.41 + let [status, last] = aFunc(curr, max, speed); 1.42 + 1.43 + if (0) { 1.44 + dump("testStatus(" + aCurr + ", " + aMore + ", " + aRate + ", [\"" + 1.45 + status.replace(gDash, "--") + "\", " + last.toFixed(3) + "]);\n"); 1.46 + } 1.47 + 1.48 + // Make sure the status text matches 1.49 + do_check_eq(status, _(aTest[0].replace(/--/, gDash))); 1.50 + 1.51 + // Make sure the lastSeconds matches 1.52 + if (last == Infinity) 1.53 + do_check_eq(last, aTest[1]); 1.54 + else 1.55 + do_check_true(Math.abs(last - aTest[1]) < .1); 1.56 +} 1.57 + 1.58 +function testURI(aURI, aDisp, aHost) 1.59 +{ 1.60 + dump("URI Test: " + [aURI, aDisp, aHost] + "\n"); 1.61 + 1.62 + let [disp, host] = DownloadUtils.getURIHost(aURI); 1.63 + 1.64 + // Make sure we have the right display host and full host 1.65 + do_check_eq(disp, aDisp); 1.66 + do_check_eq(host, aHost); 1.67 +} 1.68 + 1.69 + 1.70 +function testGetReadableDates(aDate, aCompactValue) 1.71 +{ 1.72 + const now = new Date(2000, 11, 31, 11, 59, 59); 1.73 + 1.74 + let [dateCompact] = DownloadUtils.getReadableDates(aDate, now); 1.75 + do_check_eq(dateCompact, aCompactValue); 1.76 +} 1.77 + 1.78 +function testAllGetReadableDates() 1.79 +{ 1.80 + // This test cannot depend on the current date and time, or the date format. 1.81 + // It depends on being run with the English localization, however. 1.82 + const today_11_30 = new Date(2000, 11, 31, 11, 30, 15); 1.83 + const today_12_30 = new Date(2000, 11, 31, 12, 30, 15); 1.84 + const yesterday_11_30 = new Date(2000, 11, 30, 11, 30, 15); 1.85 + const yesterday_12_30 = new Date(2000, 11, 30, 12, 30, 15); 1.86 + const twodaysago = new Date(2000, 11, 29, 11, 30, 15); 1.87 + const sixdaysago = new Date(2000, 11, 25, 11, 30, 15); 1.88 + const sevendaysago = new Date(2000, 11, 24, 11, 30, 15); 1.89 + 1.90 + let dts = Components.classes["@mozilla.org/intl/scriptabledateformat;1"]. 1.91 + getService(Components.interfaces.nsIScriptableDateFormat); 1.92 + 1.93 + testGetReadableDates(today_11_30, dts.FormatTime("", dts.timeFormatNoSeconds, 1.94 + 11, 30, 0)); 1.95 + testGetReadableDates(today_12_30, dts.FormatTime("", dts.timeFormatNoSeconds, 1.96 + 12, 30, 0)); 1.97 + testGetReadableDates(yesterday_11_30, "Yesterday"); 1.98 + testGetReadableDates(yesterday_12_30, "Yesterday"); 1.99 + testGetReadableDates(twodaysago, twodaysago.toLocaleFormat("%A")); 1.100 + testGetReadableDates(sixdaysago, sixdaysago.toLocaleFormat("%A")); 1.101 + testGetReadableDates(sevendaysago, sevendaysago.toLocaleFormat("%B") + " " + 1.102 + sevendaysago.toLocaleFormat("%d")); 1.103 + 1.104 + let [, dateTimeFull] = DownloadUtils.getReadableDates(today_11_30); 1.105 + do_check_eq(dateTimeFull, dts.FormatDateTime("", dts.dateFormatLong, 1.106 + dts.timeFormatNoSeconds, 1.107 + 2000, 12, 31, 11, 30, 0)); 1.108 +} 1.109 + 1.110 +function run_test() 1.111 +{ 1.112 + testConvertByteUnits(-1, "-1", "bytes"); 1.113 + testConvertByteUnits(1, _("1"), "bytes"); 1.114 + testConvertByteUnits(42, _("42"), "bytes"); 1.115 + testConvertByteUnits(123, _("123"), "bytes"); 1.116 + testConvertByteUnits(1024, _("1.0"), "KB"); 1.117 + testConvertByteUnits(8888, _("8.7"), "KB"); 1.118 + testConvertByteUnits(59283, _("57.9"), "KB"); 1.119 + testConvertByteUnits(640000, _("625"), "KB"); 1.120 + testConvertByteUnits(1048576, _("1.0"), "MB"); 1.121 + testConvertByteUnits(307232768, _("293"), "MB"); 1.122 + testConvertByteUnits(1073741824, _("1.0"), "GB"); 1.123 + 1.124 + testTransferTotal(1, 1, _("1 of 1 bytes")); 1.125 + testTransferTotal(234, 4924, _("234 bytes of 4.8 KB")); 1.126 + testTransferTotal(94923, 233923, _("92.7 of 228 KB")); 1.127 + testTransferTotal(4924, 94923, _("4.8 of 92.7 KB")); 1.128 + testTransferTotal(2342, 294960345, _("2.3 KB of 281 MB")); 1.129 + testTransferTotal(234, undefined, _("234 bytes")); 1.130 + testTransferTotal(4889023, undefined, _("4.7 MB")); 1.131 + 1.132 + if (0) { 1.133 + // Help find some interesting test cases 1.134 + let r = function() Math.floor(Math.random() * 10); 1.135 + for (let i = 0; i < 100; i++) { 1.136 + testStatus(r(), r(), r()); 1.137 + } 1.138 + } 1.139 + 1.140 + // First, test with rates, via getDownloadStatus... 1.141 + let statusFunc = DownloadUtils.getDownloadStatus.bind(DownloadUtils); 1.142 + 1.143 + testStatus(statusFunc, 2, 1, 7, ["A few seconds remaining -- 2.3 of 2.4 KB (58 bytes/sec)", 1.724]); 1.144 + testStatus(statusFunc, 1, 2, 6, ["A few seconds remaining -- 100 bytes of 2.4 KB (1.4 KB/sec)", 1.582]); 1.145 + testStatus(statusFunc, 4, 3, 9, ["A few seconds remaining -- 959 KB of 1.0 MB (12.9 MB/sec)", 0.004]); 1.146 + testStatus(statusFunc, 2, 3, 8, ["A few seconds remaining -- 2.3 of 56.5 KB (9.2 GB/sec)", 0.000]); 1.147 + 1.148 + testStatus(statusFunc, 8, 4, 3, ["17 seconds remaining -- 9.2 of 9.2 GB (54.3 KB/sec)", 17.682]); 1.149 + testStatus(statusFunc, 1, 3, 2, ["23 seconds remaining -- 100 bytes of 54.4 KB (2.3 KB/sec)", 23.691]); 1.150 + testStatus(statusFunc, 9, 3, 2, ["23 seconds remaining -- 12.9 of 12.9 MB (2.3 KB/sec)", 23.691]); 1.151 + testStatus(statusFunc, 5, 6, 7, ["25 seconds remaining -- 22.1 of 22.1 MB (58 bytes/sec)", 25.552]); 1.152 + 1.153 + testStatus(statusFunc, 3, 9, 3, ["4 minutes remaining -- 54.3 KB of 12.9 MB (54.3 KB/sec)", 242.969]); 1.154 + testStatus(statusFunc, 2, 3, 1, ["9 minutes remaining -- 2.3 of 56.5 KB (100 bytes/sec)", 555.550]); 1.155 + testStatus(statusFunc, 4, 3, 7, ["15 minutes remaining -- 959 KB of 1.0 MB (58 bytes/sec)", 957.845]); 1.156 + testStatus(statusFunc, 5, 3, 7, ["15 minutes remaining -- 22.1 of 22.2 MB (58 bytes/sec)", 957.845]); 1.157 + 1.158 + testStatus(statusFunc, 1, 9, 2, ["1 hour, 35 minutes remaining -- 100 bytes of 12.9 MB (2.3 KB/sec)", 5756.133]); 1.159 + testStatus(statusFunc, 2, 9, 6, ["2 hours, 31 minutes remaining -- 2.3 KB of 12.9 MB (1.4 KB/sec)", 9108.051]); 1.160 + testStatus(statusFunc, 2, 4, 1, ["2 hours, 43 minutes remaining -- 2.3 of 962 KB (100 bytes/sec)", 9823.410]); 1.161 + testStatus(statusFunc, 6, 4, 7, ["4 hours, 42 minutes remaining -- 1.4 of 961 KB (58 bytes/sec)", 16936.914]); 1.162 + 1.163 + testStatus(statusFunc, 6, 9, 1, ["1 day, 13 hours remaining -- 1.4 KB of 12.9 MB (100 bytes/sec)", 134981.320]); 1.164 + testStatus(statusFunc, 3, 8, 3, ["2 days, 1 hour remaining -- 54.3 KB of 9.2 GB (54.3 KB/sec)", 178596.872]); 1.165 + testStatus(statusFunc, 1, 8, 6, ["77 days, 11 hours remaining -- 100 bytes of 9.2 GB (1.4 KB/sec)", 6694972.470]); 1.166 + testStatus(statusFunc, 6, 8, 7, ["1979 days, 22 hours remaining -- 1.4 KB of 9.2 GB (58 bytes/sec)", 171068089.672]); 1.167 + 1.168 + testStatus(statusFunc, 0, 0, 5, ["Unknown time remaining -- 0 of 0 bytes (22.1 MB/sec)", Infinity]); 1.169 + testStatus(statusFunc, 0, 6, 0, ["Unknown time remaining -- 0 bytes of 1.4 KB (0 bytes/sec)", Infinity]); 1.170 + testStatus(statusFunc, 6, 6, 0, ["Unknown time remaining -- 1.4 of 2.9 KB (0 bytes/sec)", Infinity]); 1.171 + testStatus(statusFunc, 8, 5, 0, ["Unknown time remaining -- 9.2 of 9.3 GB (0 bytes/sec)", Infinity]); 1.172 + 1.173 + // With rate equal to Infinity 1.174 + testStatus(statusFunc, 0, 0, 10, ["Unknown time remaining -- 0 of 0 bytes (Really fast)", Infinity]); 1.175 + testStatus(statusFunc, 1, 2, 10, ["A few seconds remaining -- 100 bytes of 2.4 KB (Really fast)", 0]); 1.176 + 1.177 + // Now test without rates, via getDownloadStatusNoRate. 1.178 + statusFunc = DownloadUtils.getDownloadStatusNoRate.bind(DownloadUtils); 1.179 + 1.180 + testStatus(statusFunc, 2, 1, 7, ["A few seconds remaining -- 2.3 of 2.4 KB", 1.724]); 1.181 + testStatus(statusFunc, 1, 2, 6, ["A few seconds remaining -- 100 bytes of 2.4 KB", 1.582]); 1.182 + testStatus(statusFunc, 4, 3, 9, ["A few seconds remaining -- 959 KB of 1.0 MB", 0.004]); 1.183 + testStatus(statusFunc, 2, 3, 8, ["A few seconds remaining -- 2.3 of 56.5 KB", 0.000]); 1.184 + 1.185 + testStatus(statusFunc, 8, 4, 3, ["17 seconds remaining -- 9.2 of 9.2 GB", 17.682]); 1.186 + testStatus(statusFunc, 1, 3, 2, ["23 seconds remaining -- 100 bytes of 54.4 KB", 23.691]); 1.187 + testStatus(statusFunc, 9, 3, 2, ["23 seconds remaining -- 12.9 of 12.9 MB", 23.691]); 1.188 + testStatus(statusFunc, 5, 6, 7, ["25 seconds remaining -- 22.1 of 22.1 MB", 25.552]); 1.189 + 1.190 + testStatus(statusFunc, 3, 9, 3, ["4 minutes remaining -- 54.3 KB of 12.9 MB", 242.969]); 1.191 + testStatus(statusFunc, 2, 3, 1, ["9 minutes remaining -- 2.3 of 56.5 KB", 555.550]); 1.192 + testStatus(statusFunc, 4, 3, 7, ["15 minutes remaining -- 959 KB of 1.0 MB", 957.845]); 1.193 + testStatus(statusFunc, 5, 3, 7, ["15 minutes remaining -- 22.1 of 22.2 MB", 957.845]); 1.194 + 1.195 + testStatus(statusFunc, 1, 9, 2, ["1 hour, 35 minutes remaining -- 100 bytes of 12.9 MB", 5756.133]); 1.196 + testStatus(statusFunc, 2, 9, 6, ["2 hours, 31 minutes remaining -- 2.3 KB of 12.9 MB", 9108.051]); 1.197 + testStatus(statusFunc, 2, 4, 1, ["2 hours, 43 minutes remaining -- 2.3 of 962 KB", 9823.410]); 1.198 + testStatus(statusFunc, 6, 4, 7, ["4 hours, 42 minutes remaining -- 1.4 of 961 KB", 16936.914]); 1.199 + 1.200 + testStatus(statusFunc, 6, 9, 1, ["1 day, 13 hours remaining -- 1.4 KB of 12.9 MB", 134981.320]); 1.201 + testStatus(statusFunc, 3, 8, 3, ["2 days, 1 hour remaining -- 54.3 KB of 9.2 GB", 178596.872]); 1.202 + testStatus(statusFunc, 1, 8, 6, ["77 days, 11 hours remaining -- 100 bytes of 9.2 GB", 6694972.470]); 1.203 + testStatus(statusFunc, 6, 8, 7, ["1979 days, 22 hours remaining -- 1.4 KB of 9.2 GB", 171068089.672]); 1.204 + 1.205 + testStatus(statusFunc, 0, 0, 5, ["Unknown time remaining -- 0 of 0 bytes", Infinity]); 1.206 + testStatus(statusFunc, 0, 6, 0, ["Unknown time remaining -- 0 bytes of 1.4 KB", Infinity]); 1.207 + testStatus(statusFunc, 6, 6, 0, ["Unknown time remaining -- 1.4 of 2.9 KB", Infinity]); 1.208 + testStatus(statusFunc, 8, 5, 0, ["Unknown time remaining -- 9.2 of 9.3 GB", Infinity]); 1.209 + 1.210 + testURI("http://www.mozilla.org/", "mozilla.org", "www.mozilla.org"); 1.211 + testURI("http://www.city.mikasa.hokkaido.jp/", "city.mikasa.hokkaido.jp", "www.city.mikasa.hokkaido.jp"); 1.212 + testURI("data:text/html,Hello World", "data resource", "data resource"); 1.213 + testURI("jar:http://www.mozilla.com/file!/magic", "mozilla.com", "www.mozilla.com"); 1.214 + testURI("file:///C:/Cool/Stuff/", "local file", "local file"); 1.215 + testURI("moz-icon:file:///test.extension", "moz-icon resource", "moz-icon resource"); 1.216 + testURI("about:config", "about resource", "about resource"); 1.217 + 1.218 + testAllGetReadableDates(); 1.219 +}