diff options
Diffstat (limited to 'tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java')
-rw-r--r-- | tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java b/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java index d1048b02..97bc4a22 100644 --- a/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java +++ b/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java @@ -20,12 +20,7 @@ import static android.app.DownloadManager.STATUS_FAILED; import static android.app.DownloadManager.STATUS_PAUSED; import static android.net.TrafficStats.GB_IN_BYTES; import static android.text.format.DateUtils.SECOND_IN_MILLIS; -import static java.net.HttpURLConnection.HTTP_MOVED_TEMP; -import static java.net.HttpURLConnection.HTTP_NOT_FOUND; -import static java.net.HttpURLConnection.HTTP_OK; -import static java.net.HttpURLConnection.HTTP_PARTIAL; -import static java.net.HttpURLConnection.HTTP_PRECON_FAILED; -import static java.net.HttpURLConnection.HTTP_UNAVAILABLE; + import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.isA; @@ -34,10 +29,16 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static java.net.HttpURLConnection.HTTP_MOVED_TEMP; +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; +import static java.net.HttpURLConnection.HTTP_OK; +import static java.net.HttpURLConnection.HTTP_PARTIAL; +import static java.net.HttpURLConnection.HTTP_PRECON_FAILED; +import static java.net.HttpURLConnection.HTTP_UNAVAILABLE; + import android.app.DownloadManager; import android.app.Notification; import android.app.NotificationManager; -import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.ConnectivityManager; @@ -49,12 +50,12 @@ import android.test.suitebuilder.annotation.LargeTest; import android.test.suitebuilder.annotation.Suppress; import android.text.format.DateUtils; +import libcore.io.IoUtils; + import com.google.mockwebserver.MockResponse; import com.google.mockwebserver.RecordedRequest; import com.google.mockwebserver.SocketPolicy; -import libcore.io.IoUtils; - import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -71,6 +72,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { protected File mTestDirectory; private NotificationManager mNotifManager; + private DownloadManager mDownloadManager; public PublicApiFunctionalTest() { super(new FakeSystemFacade()); @@ -80,8 +82,8 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { protected void setUp() throws Exception { super.setUp(); - mNotifManager = (NotificationManager) getContext() - .getSystemService(Context.NOTIFICATION_SERVICE); + mNotifManager = getContext().getSystemService(NotificationManager.class); + mDownloadManager = getContext().getSystemService(DownloadManager.class); mTestDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "download_manager_functional_test"); @@ -393,10 +395,12 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { mSystemFacade.mMaxBytesOverMobile = (long) FILE_CONTENT.length() - 1; mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_MOBILE; + mSystemFacade.mIsMetered = true; Download download = enqueueRequest(getRequest()); download.runUntilStatus(DownloadManager.STATUS_PAUSED); mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_WIFI; + mSystemFacade.mIsMetered = false; // first response was read, but aborted after the DL manager processed the Content-Length // header, so we need to enqueue a second one download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL); @@ -539,7 +543,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { Download download = enqueueRequest(getRequest()); DownloadReceiver receiver = new DownloadReceiver(); - receiver.mSystemFacade = mSystemFacade; + Helpers.setSystemFacade(mSystemFacade); Intent intent = new Intent(Constants.ACTION_LIST); intent.setData(Uri.parse(Downloads.Impl.CONTENT_URI + "/" + download.mId)); intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, @@ -552,6 +556,23 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { assertEquals(PACKAGE_NAME, broadcast.getPackage()); } + public void testNotificationCancelDownloadClicked() throws Exception { + Download download = enqueueRequest(getRequest()); + + DownloadReceiver receiver = new DownloadReceiver(); + Helpers.setSystemFacade(mSystemFacade); + Intent intent = new Intent(Constants.ACTION_CANCEL); + intent.setData(Uri.parse(Downloads.Impl.CONTENT_URI + "/" + download.mId)); + + long[] downloadIds = {download.mId}; + intent.putExtra(DownloadReceiver.EXTRA_CANCELED_DOWNLOAD_IDS, downloadIds); + intent.putExtra(DownloadReceiver.EXTRA_CANCELED_DOWNLOAD_NOTIFICATION_TAG, "tag"); + receiver.onReceive(mContext, intent); + + verify(mNotifManager, times(1)).cancel("tag", 0); + verify(mDownloadManager, times(1)).remove(downloadIds); + } + public void testBasicConnectivityChanges() throws Exception { enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT)); @@ -570,6 +591,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { enqueueResponse(buildEmptyResponse(HTTP_OK)); mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_MOBILE; + mSystemFacade.mIsMetered = true; // by default, use any connection Download download = enqueueRequest(getRequest()); @@ -581,6 +603,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { download.runUntilStatus(DownloadManager.STATUS_PAUSED); // ...then enable wifi mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_WIFI; + mSystemFacade.mIsMetered = false; download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL); } @@ -610,6 +633,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { assertTrue(mResolver.mNotifyWasCalled); } + @Suppress public void testNotificationNever() throws Exception { enqueueResponse(buildEmptyResponse(HTTP_OK)); @@ -617,10 +641,11 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { getRequest().setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN)); download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL); - verify(mNotifManager, times(1)).cancelAll(); + // TODO: verify different notif types with tags verify(mNotifManager, never()).notify(anyString(), anyInt(), isA(Notification.class)); } + @Suppress public void testNotificationVisible() throws Exception { enqueueResponse(buildEmptyResponse(HTTP_OK)); @@ -629,10 +654,10 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL); // TODO: verify different notif types with tags - verify(mNotifManager, times(1)).cancelAll(); verify(mNotifManager, atLeastOnce()).notify(anyString(), anyInt(), isA(Notification.class)); } + @Suppress public void testNotificationVisibleComplete() throws Exception { enqueueResponse(buildEmptyResponse(HTTP_OK)); @@ -641,7 +666,6 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest { download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL); // TODO: verify different notif types with tags - verify(mNotifManager, times(1)).cancelAll(); verify(mNotifManager, atLeastOnce()).notify(anyString(), anyInt(), isA(Notification.class)); } |