OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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; | 7 import android.content.Context; |
8 import android.content.Intent; | 8 import android.content.Intent; |
9 import android.content.SharedPreferences; | 9 import android.content.SharedPreferences; |
10 import android.os.Handler; | 10 import android.os.Handler; |
11 import android.os.HandlerThread; | 11 import android.os.HandlerThread; |
12 import android.os.IBinder; | 12 import android.os.IBinder; |
13 import android.support.test.filters.SmallTest; | 13 import android.support.test.filters.SmallTest; |
14 import android.test.ServiceTestCase; | 14 import android.test.ServiceTestCase; |
15 | 15 |
16 import org.chromium.base.ContextUtils; | 16 import org.chromium.base.ContextUtils; |
17 import org.chromium.base.ThreadUtils; | 17 import org.chromium.base.ThreadUtils; |
18 import org.chromium.base.test.util.AdvancedMockContext; | 18 import org.chromium.base.test.util.AdvancedMockContext; |
19 import org.chromium.base.test.util.Feature; | 19 import org.chromium.base.test.util.Feature; |
20 import org.chromium.base.test.util.RetryOnFailure; | 20 import org.chromium.base.test.util.RetryOnFailure; |
21 import org.chromium.components.offline_items_collection.ContentId; | 21 import org.chromium.components.offline_items_collection.ContentId; |
22 import org.chromium.components.offline_items_collection.LegacyHelpers; | 22 import org.chromium.components.offline_items_collection.LegacyHelpers; |
| 23 import org.chromium.components.offline_items_collection.OfflineItem.Progress; |
| 24 import org.chromium.components.offline_items_collection.OfflineItemProgressUnit; |
23 | 25 |
24 import java.util.ArrayList; | 26 import java.util.ArrayList; |
25 import java.util.HashSet; | 27 import java.util.HashSet; |
26 import java.util.List; | 28 import java.util.List; |
27 import java.util.Set; | 29 import java.util.Set; |
28 import java.util.UUID; | 30 import java.util.UUID; |
29 | 31 |
30 /** | 32 /** |
31 * Tests of {@link DownloadNotificationService}. | 33 * Tests of {@link DownloadNotificationService}. |
32 */ | 34 */ |
33 public class DownloadNotificationServiceTest extends | 35 public class DownloadNotificationServiceTest extends |
34 ServiceTestCase<MockDownloadNotificationService> { | 36 ServiceTestCase<MockDownloadNotificationService> { |
35 private static final int MILLIS_PER_SECOND = 1000; | |
36 | 37 |
37 private static class MockDownloadManagerService extends DownloadManagerServi
ce { | 38 private static class MockDownloadManagerService extends DownloadManagerServi
ce { |
38 final List<DownloadItem> mDownloads = new ArrayList<DownloadItem>(); | 39 final List<DownloadItem> mDownloads = new ArrayList<DownloadItem>(); |
39 | 40 |
40 public MockDownloadManagerService(Context context) { | 41 public MockDownloadManagerService(Context context) { |
41 super(context, null, getTestHandler(), 1000); | 42 super(context, null, getTestHandler(), 1000); |
42 } | 43 } |
43 | 44 |
44 @Override | 45 @Override |
45 protected void init() {} | 46 protected void init() {} |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 public void run() { | 290 public void run() { |
290 getService().updateNotificationsForShutdown(); | 291 getService().updateNotificationsForShutdown(); |
291 } | 292 } |
292 }); | 293 }); |
293 assertEquals(2, getService().getNotificationIds().size()); | 294 assertEquals(2, getService().getNotificationIds().size()); |
294 assertTrue(getService().getNotificationIds().contains(3)); | 295 assertTrue(getService().getNotificationIds().contains(3)); |
295 assertTrue(getService().getNotificationIds().contains(4)); | 296 assertTrue(getService().getNotificationIds().contains(4)); |
296 | 297 |
297 DownloadNotificationService service = bindNotificationService(); | 298 DownloadNotificationService service = bindNotificationService(); |
298 ContentId id3 = LegacyHelpers.buildLegacyContentId(false, UUID.randomUUI
D().toString()); | 299 ContentId id3 = LegacyHelpers.buildLegacyContentId(false, UUID.randomUUI
D().toString()); |
299 service.notifyDownloadProgress(id3, "test", 1, 100L, 1L, 1L, true, true,
false, null); | 300 service.notifyDownloadProgress(id3, "test", |
| 301 new Progress(1, 100L, OfflineItemProgressUnit.PERCENTAGE), 100L,
1L, 1L, true, true, |
| 302 false, null); |
300 assertEquals(3, getService().getNotificationIds().size()); | 303 assertEquals(3, getService().getNotificationIds().size()); |
301 int lastNotificationId = getService().getLastAddedNotificationId(); | 304 int lastNotificationId = getService().getLastAddedNotificationId(); |
302 Set<String> entries = DownloadManagerService.getStoredDownloadInfo( | 305 Set<String> entries = DownloadManagerService.getStoredDownloadInfo( |
303 sharedPrefs, DownloadSharedPreferenceHelper.KEY_PENDING_DOWNLOAD
_NOTIFICATIONS); | 306 sharedPrefs, DownloadSharedPreferenceHelper.KEY_PENDING_DOWNLOAD
_NOTIFICATIONS); |
304 assertEquals(3, entries.size()); | 307 assertEquals(3, entries.size()); |
305 | 308 |
306 ContentId id1 = LegacyHelpers.buildLegacyContentId(false, guid1); | 309 ContentId id1 = LegacyHelpers.buildLegacyContentId(false, guid1); |
307 service.notifyDownloadSuccessful( | 310 service.notifyDownloadSuccessful( |
308 id1, "/path/to/success", "success", 100L, false, false, true, nu
ll); | 311 id1, "/path/to/success", "success", 100L, false, false, true, nu
ll); |
309 entries = DownloadManagerService.getStoredDownloadInfo( | 312 entries = DownloadManagerService.getStoredDownloadInfo( |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 @Override | 421 @Override |
419 public void run() { | 422 public void run() { |
420 getService().onTaskRemoved(new Intent()); | 423 getService().onTaskRemoved(new Intent()); |
421 } | 424 } |
422 }); | 425 }); |
423 | 426 |
424 assertTrue(getService().isPaused()); | 427 assertTrue(getService().isPaused()); |
425 assertFalse(sharedPrefs.contains( | 428 assertFalse(sharedPrefs.contains( |
426 DownloadSharedPreferenceHelper.KEY_PENDING_DOWNLOAD_NOTIFICATION
S)); | 429 DownloadSharedPreferenceHelper.KEY_PENDING_DOWNLOAD_NOTIFICATION
S)); |
427 } | 430 } |
428 | |
429 @SmallTest | |
430 @Feature({"Download"}) | |
431 public void testFormatRemainingTime() { | |
432 Context context = getSystemContext().getApplicationContext(); | |
433 assertEquals("0 secs left", DownloadNotificationService.formatRemainingT
ime(context, 0)); | |
434 assertEquals("1 sec left", DownloadNotificationService.formatRemainingTi
me( | |
435 context, MILLIS_PER_SECOND)); | |
436 assertEquals("1 min left", DownloadNotificationService.formatRemainingTi
me(context, | |
437 DownloadNotificationService.SECONDS_PER_MINUTE * MILLIS_PER_SECO
ND)); | |
438 assertEquals("2 mins left", DownloadNotificationService.formatRemainingT
ime(context, | |
439 149 * MILLIS_PER_SECOND)); | |
440 assertEquals("3 mins left", DownloadNotificationService.formatRemainingT
ime(context, | |
441 150 * MILLIS_PER_SECOND)); | |
442 assertEquals("1 hour left", DownloadNotificationService.formatRemainingT
ime(context, | |
443 DownloadNotificationService.SECONDS_PER_HOUR * MILLIS_PER_SECOND
)); | |
444 assertEquals("2 hours left", DownloadNotificationService.formatRemaining
Time(context, | |
445 149 * DownloadNotificationService.SECONDS_PER_MINUTE * MILLIS_PE
R_SECOND)); | |
446 assertEquals("3 hours left", DownloadNotificationService.formatRemaining
Time(context, | |
447 150 * DownloadNotificationService.SECONDS_PER_MINUTE * MILLIS_PE
R_SECOND)); | |
448 assertEquals("1 day left", DownloadNotificationService.formatRemainingTi
me(context, | |
449 DownloadNotificationService.SECONDS_PER_DAY * MILLIS_PER_SECOND)
); | |
450 assertEquals("2 days left", DownloadNotificationService.formatRemainingT
ime(context, | |
451 59 * DownloadNotificationService.SECONDS_PER_HOUR * MILLIS_PER_S
ECOND)); | |
452 assertEquals("3 days left", DownloadNotificationService.formatRemainingT
ime(context, | |
453 60 * DownloadNotificationService.SECONDS_PER_HOUR * MILLIS_PER_S
ECOND)); | |
454 } | |
455 | |
456 // Tests that the downloaded bytes on the notification is correct. | |
457 @SmallTest | |
458 @Feature({"Download"}) | |
459 public void testFormatBytesReceived() { | |
460 Context context = getSystemContext().getApplicationContext(); | |
461 assertEquals("Downloaded 0.0 KB", DownloadUtils.getStringForBytes( | |
462 context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 0)); | |
463 assertEquals("Downloaded 0.5 KB", DownloadUtils.getStringForBytes( | |
464 context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 512)); | |
465 assertEquals("Downloaded 1.0 KB", DownloadUtils.getStringForBytes( | |
466 context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024)); | |
467 assertEquals("Downloaded 1.0 MB", DownloadUtils.getStringForBytes( | |
468 context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024 * 1024)); | |
469 assertEquals("Downloaded 1.0 GB", DownloadUtils.getStringForBytes( | |
470 context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024 * 1024 * 1
024)); | |
471 } | |
472 } | 431 } |
OLD | NEW |