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