OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.download; | 5 package org.chromium.chrome.browser.download; |
6 | 6 |
| 7 import android.content.Context; |
| 8 import android.support.test.InstrumentationRegistry; |
7 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
8 | 10 |
9 import org.junit.Assert; | 11 import org.junit.Assert; |
10 import org.junit.Test; | 12 import org.junit.Test; |
11 import org.junit.runner.RunWith; | 13 import org.junit.runner.RunWith; |
12 | 14 |
13 import org.chromium.base.test.util.Feature; | 15 import org.chromium.base.test.util.Feature; |
14 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | 16 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| 17 import org.chromium.components.offline_items_collection.OfflineItem.Progress; |
| 18 import org.chromium.components.offline_items_collection.OfflineItemProgressUnit; |
15 | 19 |
16 /** | 20 /** |
17 * Tests of {@link DownloadUtils}. | 21 * Tests of {@link DownloadUtils}. |
18 */ | 22 */ |
19 @RunWith(ChromeJUnit4ClassRunner.class) | 23 @RunWith(ChromeJUnit4ClassRunner.class) |
20 public class DownloadUtilsTest { | 24 public class DownloadUtilsTest { |
| 25 private static final int MILLIS_PER_SECOND = 1000; |
| 26 |
21 /** | 27 /** |
22 * Test {@link DownloadUtils#getAbbrieviatedFileName()} method. | 28 * Test {@link DownloadUtils#getAbbrieviatedFileName()} method. |
23 */ | 29 */ |
24 @Test | 30 @Test |
25 @SmallTest | 31 @SmallTest |
26 @Feature({"Download"}) | 32 @Feature({"Download"}) |
27 public void testGetAbbrieviatedFileName() { | 33 public void testGetAbbrieviatedFileName() { |
28 Assert.assertEquals("123.pdf", DownloadUtils.getAbbreviatedFileName("123
.pdf", 10)); | 34 Assert.assertEquals("123.pdf", DownloadUtils.getAbbreviatedFileName("123
.pdf", 10)); |
29 Assert.assertEquals( | 35 Assert.assertEquals( |
30 "1" + DownloadUtils.ELLIPSIS, DownloadUtils.getAbbreviatedFileNa
me("123.pdf", 1)); | 36 "1" + DownloadUtils.ELLIPSIS, DownloadUtils.getAbbreviatedFileNa
me("123.pdf", 1)); |
31 Assert.assertEquals( | 37 Assert.assertEquals( |
32 "12" + DownloadUtils.ELLIPSIS, DownloadUtils.getAbbreviatedFileN
ame("1234567", 2)); | 38 "12" + DownloadUtils.ELLIPSIS, DownloadUtils.getAbbreviatedFileN
ame("1234567", 2)); |
33 Assert.assertEquals("123" + DownloadUtils.ELLIPSIS + ".pdf", | 39 Assert.assertEquals("123" + DownloadUtils.ELLIPSIS + ".pdf", |
34 DownloadUtils.getAbbreviatedFileName("1234567.pdf", 7)); | 40 DownloadUtils.getAbbreviatedFileName("1234567.pdf", 7)); |
35 } | 41 } |
| 42 |
| 43 @Test |
| 44 @SmallTest |
| 45 @Feature({"Download"}) |
| 46 public void testFormatRemainingTime() { |
| 47 final Context context = InstrumentationRegistry.getInstrumentation().get
TargetContext(); |
| 48 Assert.assertEquals("0 secs left", DownloadUtils.formatRemainingTime(con
text, 0)); |
| 49 Assert.assertEquals( |
| 50 "1 sec left", DownloadUtils.formatRemainingTime(context, MILLIS_
PER_SECOND)); |
| 51 Assert.assertEquals("1 min left", |
| 52 DownloadUtils.formatRemainingTime( |
| 53 context, DownloadUtils.SECONDS_PER_MINUTE * MILLIS_PER_S
ECOND)); |
| 54 Assert.assertEquals( |
| 55 "2 mins left", DownloadUtils.formatRemainingTime(context, 149 *
MILLIS_PER_SECOND)); |
| 56 Assert.assertEquals( |
| 57 "3 mins left", DownloadUtils.formatRemainingTime(context, 150 *
MILLIS_PER_SECOND)); |
| 58 Assert.assertEquals("1 hour left", |
| 59 DownloadUtils.formatRemainingTime( |
| 60 context, DownloadUtils.SECONDS_PER_HOUR * MILLIS_PER_SEC
OND)); |
| 61 Assert.assertEquals("2 hours left", |
| 62 DownloadUtils.formatRemainingTime( |
| 63 context, 149 * DownloadUtils.SECONDS_PER_MINUTE * MILLIS
_PER_SECOND)); |
| 64 Assert.assertEquals("3 hours left", |
| 65 DownloadUtils.formatRemainingTime( |
| 66 context, 150 * DownloadUtils.SECONDS_PER_MINUTE * MILLIS
_PER_SECOND)); |
| 67 Assert.assertEquals("1 day left", |
| 68 DownloadUtils.formatRemainingTime( |
| 69 context, DownloadUtils.SECONDS_PER_DAY * MILLIS_PER_SECO
ND)); |
| 70 Assert.assertEquals("2 days left", |
| 71 DownloadUtils.formatRemainingTime( |
| 72 context, 59 * DownloadUtils.SECONDS_PER_HOUR * MILLIS_PE
R_SECOND)); |
| 73 Assert.assertEquals("3 days left", |
| 74 DownloadUtils.formatRemainingTime( |
| 75 context, 60 * DownloadUtils.SECONDS_PER_HOUR * MILLIS_PE
R_SECOND)); |
| 76 } |
| 77 |
| 78 @Test |
| 79 @SmallTest |
| 80 @Feature({"Download"}) |
| 81 public void testFormatBytesReceived() { |
| 82 final Context context = InstrumentationRegistry.getInstrumentation().get
TargetContext(); |
| 83 Assert.assertEquals("Downloaded 0.0 KB", |
| 84 DownloadUtils.getStringForBytes( |
| 85 context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 0)); |
| 86 Assert.assertEquals("Downloaded 0.5 KB", |
| 87 DownloadUtils.getStringForBytes( |
| 88 context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 512)); |
| 89 Assert.assertEquals("Downloaded 1.0 KB", |
| 90 DownloadUtils.getStringForBytes( |
| 91 context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024)); |
| 92 Assert.assertEquals("Downloaded 1.0 MB", |
| 93 DownloadUtils.getStringForBytes( |
| 94 context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024 *
1024)); |
| 95 Assert.assertEquals("Downloaded 1.0 GB", |
| 96 DownloadUtils.getStringForBytes( |
| 97 context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024 *
1024 * 1024)); |
| 98 } |
| 99 |
| 100 @Test |
| 101 @SmallTest |
| 102 @Feature({"Download"}) |
| 103 public void testFormatRemainingFiles() { |
| 104 final Context context = InstrumentationRegistry.getInstrumentation().get
TargetContext(); |
| 105 Progress progress = new Progress(3, Long.valueOf(5), OfflineItemProgress
Unit.FILES); |
| 106 Assert.assertEquals(60, progress.getPercentage()); |
| 107 Assert.assertEquals("2 files left", DownloadUtils.formatRemainingFiles(c
ontext, progress)); |
| 108 progress = new Progress(4, Long.valueOf(5), OfflineItemProgressUnit.FILE
S); |
| 109 Assert.assertEquals(80, progress.getPercentage()); |
| 110 Assert.assertEquals("1 file left", DownloadUtils.formatRemainingFiles(co
ntext, progress)); |
| 111 progress = new Progress(5, Long.valueOf(5), OfflineItemProgressUnit.FILE
S); |
| 112 Assert.assertEquals(100, progress.getPercentage()); |
| 113 Assert.assertEquals("0 files left", DownloadUtils.formatRemainingFiles(c
ontext, progress)); |
| 114 } |
36 } | 115 } |
OLD | NEW |