| 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.support.test.InstrumentationRegistry; |
| 8 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
| 9 import android.test.InstrumentationTestCase; | 10 |
| 11 import org.junit.Assert; |
| 12 import org.junit.Before; |
| 13 import org.junit.Test; |
| 14 import org.junit.runner.RunWith; |
| 10 | 15 |
| 11 import org.chromium.base.ThreadUtils; | 16 import org.chromium.base.ThreadUtils; |
| 12 import org.chromium.base.test.util.AdvancedMockContext; | 17 import org.chromium.base.test.util.AdvancedMockContext; |
| 13 import org.chromium.base.test.util.Feature; | 18 import org.chromium.base.test.util.Feature; |
| 14 import org.chromium.base.test.util.RetryOnFailure; | 19 import org.chromium.base.test.util.RetryOnFailure; |
| 20 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| 15 import org.chromium.content.browser.test.util.Criteria; | 21 import org.chromium.content.browser.test.util.Criteria; |
| 16 import org.chromium.content.browser.test.util.CriteriaHelper; | 22 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 17 | 23 |
| 18 import java.util.UUID; | 24 import java.util.UUID; |
| 19 | 25 |
| 20 /** | 26 /** |
| 21 * Tests of {@link SystemDownloadNotifier}. | 27 * Tests of {@link SystemDownloadNotifier}. |
| 22 */ | 28 */ |
| 23 public class SystemDownloadNotifierTest extends InstrumentationTestCase { | 29 @RunWith(ChromeJUnit4ClassRunner.class) |
| 30 public class SystemDownloadNotifierTest { |
| 24 private MockSystemDownloadNotifier mDownloadNotifier; | 31 private MockSystemDownloadNotifier mDownloadNotifier; |
| 25 private MockDownloadNotificationService mService; | 32 private MockDownloadNotificationService mService; |
| 26 | 33 |
| 27 static class MockSystemDownloadNotifier extends SystemDownloadNotifier { | 34 static class MockSystemDownloadNotifier extends SystemDownloadNotifier { |
| 28 boolean mStarted = false; | 35 boolean mStarted = false; |
| 29 | 36 |
| 30 MockSystemDownloadNotifier(Context context) { | 37 MockSystemDownloadNotifier(Context context) { |
| 31 super(context); | 38 super(context); |
| 32 } | 39 } |
| 33 | 40 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 53 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 60 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 54 @Override | 61 @Override |
| 55 public void run() { | 62 public void run() { |
| 56 MockSystemDownloadNotifier.super.updateDownloadNotification( | 63 MockSystemDownloadNotifier.super.updateDownloadNotification( |
| 57 notificationInfo, autoRelease); | 64 notificationInfo, autoRelease); |
| 58 } | 65 } |
| 59 }); | 66 }); |
| 60 } | 67 } |
| 61 } | 68 } |
| 62 | 69 |
| 63 @Override | 70 @Before |
| 64 public void setUp() throws Exception { | 71 public void setUp() throws Exception { |
| 65 super.setUp(); | 72 mDownloadNotifier = new MockSystemDownloadNotifier( |
| 66 mDownloadNotifier = new MockSystemDownloadNotifier(getInstrumentation().
getTargetContext()); | 73 InstrumentationRegistry.getInstrumentation().getTargetContext())
; |
| 67 } | 74 } |
| 68 | 75 |
| 69 /** | 76 /** |
| 70 * Helper method to simulate that the DownloadNotificationService is connect
ed. | 77 * Helper method to simulate that the DownloadNotificationService is connect
ed. |
| 71 */ | 78 */ |
| 72 private void onServiceConnected() { | 79 private void onServiceConnected() { |
| 73 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 80 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 74 @Override | 81 @Override |
| 75 public void run() { | 82 public void run() { |
| 76 mService = new MockDownloadNotificationService(); | 83 mService = new MockDownloadNotificationService(); |
| 77 mService.setContext(new AdvancedMockContext( | 84 mService.setContext( |
| 78 getInstrumentation().getTargetContext().getApplicationCo
ntext())); | 85 new AdvancedMockContext(InstrumentationRegistry.getInstr
umentation() |
| 86 .getTargetContext() |
| 87 .getApplicationContext()
)); |
| 79 mService.onCreate(); | 88 mService.onCreate(); |
| 80 } | 89 } |
| 81 }); | 90 }); |
| 82 mDownloadNotifier.setDownloadNotificationService(mService); | 91 mDownloadNotifier.setDownloadNotificationService(mService); |
| 83 mDownloadNotifier.handlePendingNotifications(); | 92 mDownloadNotifier.handlePendingNotifications(); |
| 84 } | 93 } |
| 85 | 94 |
| 86 /** | 95 /** |
| 87 * Tests that pending notifications will be handled after service is connect
ed. | 96 * Tests that pending notifications will be handled after service is connect
ed. |
| 88 */ | 97 */ |
| 98 @Test |
| 89 @SmallTest | 99 @SmallTest |
| 90 @Feature({"Download"}) | 100 @Feature({"Download"}) |
| 91 @RetryOnFailure | 101 @RetryOnFailure |
| 92 public void testNotificationNotHandledUntilServiceConnection() { | 102 public void testNotificationNotHandledUntilServiceConnection() { |
| 93 DownloadInfo info = new DownloadInfo.Builder() | 103 DownloadInfo info = new DownloadInfo.Builder() |
| 94 .setDownloadGuid(UUID.randomUUID().toString()).build(); | 104 .setDownloadGuid(UUID.randomUUID().toString()).build(); |
| 95 mDownloadNotifier.notifyDownloadProgress(info, 1L, true); | 105 mDownloadNotifier.notifyDownloadProgress(info, 1L, true); |
| 96 CriteriaHelper.pollUiThread(new Criteria() { | 106 CriteriaHelper.pollUiThread(new Criteria() { |
| 97 @Override | 107 @Override |
| 98 public boolean isSatisfied() { | 108 public boolean isSatisfied() { |
| 99 return mDownloadNotifier.mStarted; | 109 return mDownloadNotifier.mStarted; |
| 100 } | 110 } |
| 101 }); | 111 }); |
| 102 | 112 |
| 103 onServiceConnected(); | 113 onServiceConnected(); |
| 104 assertEquals(1, mService.getNotificationIds().size()); | 114 Assert.assertEquals(1, mService.getNotificationIds().size()); |
| 105 } | 115 } |
| 106 | 116 |
| 107 /** | 117 /** |
| 108 * Tests that service will be stopped once all notifications are inactive. | 118 * Tests that service will be stopped once all notifications are inactive. |
| 109 */ | 119 */ |
| 120 @Test |
| 110 @SmallTest | 121 @SmallTest |
| 111 @Feature({"Download"}) | 122 @Feature({"Download"}) |
| 112 public void testServiceStoppedWhenAllDownloadsFinish() { | 123 public void testServiceStoppedWhenAllDownloadsFinish() { |
| 113 onServiceConnected(); | 124 onServiceConnected(); |
| 114 DownloadInfo info = new DownloadInfo.Builder() | 125 DownloadInfo info = new DownloadInfo.Builder() |
| 115 .setDownloadGuid(UUID.randomUUID().toString()).build(); | 126 .setDownloadGuid(UUID.randomUUID().toString()).build(); |
| 116 mDownloadNotifier.notifyDownloadProgress(info, 1L, true); | 127 mDownloadNotifier.notifyDownloadProgress(info, 1L, true); |
| 117 CriteriaHelper.pollUiThread(new Criteria() { | 128 CriteriaHelper.pollUiThread(new Criteria() { |
| 118 @Override | 129 @Override |
| 119 public boolean isSatisfied() { | 130 public boolean isSatisfied() { |
| 120 return mDownloadNotifier.mStarted; | 131 return mDownloadNotifier.mStarted; |
| 121 } | 132 } |
| 122 }); | 133 }); |
| 123 DownloadInfo info2 = new DownloadInfo.Builder() | 134 DownloadInfo info2 = new DownloadInfo.Builder() |
| 124 .setDownloadGuid(UUID.randomUUID().toString()).build(); | 135 .setDownloadGuid(UUID.randomUUID().toString()).build(); |
| 125 mDownloadNotifier.notifyDownloadProgress(info2, 1L, true); | 136 mDownloadNotifier.notifyDownloadProgress(info2, 1L, true); |
| 126 | 137 |
| 127 mDownloadNotifier.notifyDownloadFailed(info); | 138 mDownloadNotifier.notifyDownloadFailed(info); |
| 128 mDownloadNotifier.notifyDownloadSuccessful(info2, 100L, true, false); | 139 mDownloadNotifier.notifyDownloadSuccessful(info2, 100L, true, false); |
| 129 CriteriaHelper.pollUiThread(new Criteria() { | 140 CriteriaHelper.pollUiThread(new Criteria() { |
| 130 @Override | 141 @Override |
| 131 public boolean isSatisfied() { | 142 public boolean isSatisfied() { |
| 132 return !mDownloadNotifier.mStarted; | 143 return !mDownloadNotifier.mStarted; |
| 133 } | 144 } |
| 134 }); | 145 }); |
| 135 } | 146 } |
| 136 } | 147 } |
| OLD | NEW |