From 93155e1da7e89d4925e244f5afa94afb8ada7381 Mon Sep 17 00:00:00 2001 From: Steve Howard Date: Fri, 23 Jul 2010 20:32:21 -0700 Subject: Stub out and test system notifications. This change abstracts NotificationManager interactions behind SystemFacade and takes advantage of that to test notifications, to a limited degree. It also fixes a silly typo in AbstractDownloadManagerFunctionalTest, and it introduces an extra sleep between tests to avoid some flakiness. I'll look for a better solution to that problem after this change goes in. Change-Id: I3a0307f828955cd45b0e3581ad499da28cc0556e --- .../AbstractDownloadManagerFunctionalTest.java | 10 +++++--- .../providers/downloads/FakeSystemFacade.java | 29 ++++++++++++++++++++++ .../downloads/PublicApiFunctionalTest.java | 18 ++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) (limited to 'tests/src/com/android/providers') diff --git a/tests/src/com/android/providers/downloads/AbstractDownloadManagerFunctionalTest.java b/tests/src/com/android/providers/downloads/AbstractDownloadManagerFunctionalTest.java index cb4ad8c9..a401a5b8 100644 --- a/tests/src/com/android/providers/downloads/AbstractDownloadManagerFunctionalTest.java +++ b/tests/src/com/android/providers/downloads/AbstractDownloadManagerFunctionalTest.java @@ -165,12 +165,12 @@ public abstract class AbstractDownloadManagerFunctionalTest extends @Override protected void tearDown() throws Exception { - waitForUpdateThread(); + waitForThreads(); cleanUpDownloads(); super.tearDown(); } - private void waitForUpdateThread() throws InterruptedException { + private void waitForThreads() throws InterruptedException { DownloadService service = getService(); if (service == null) { return; @@ -181,6 +181,10 @@ public abstract class AbstractDownloadManagerFunctionalTest extends && System.currentTimeMillis() < startTimeMillis + 1000) { Thread.sleep(50); } + + // We can't explicitly wait for DownloadThreads, so just throw this last sleep in. Ugly, + // but necessary to avoid unbearable flakiness until I can find a better solution. + Thread.sleep(50); } private boolean isDatabaseEmpty() { @@ -289,7 +293,7 @@ public abstract class AbstractDownloadManagerFunctionalTest extends status = reader.getStatus(); } - long delta = startTimeMillis - startTimeMillis; + long delta = System.currentTimeMillis() - startTimeMillis; Log.d(LOG_TAG, "Status " + status + " reached after " + delta + "ms"); } diff --git a/tests/src/com/android/providers/downloads/FakeSystemFacade.java b/tests/src/com/android/providers/downloads/FakeSystemFacade.java index 0f8a9801..d35b558b 100644 --- a/tests/src/com/android/providers/downloads/FakeSystemFacade.java +++ b/tests/src/com/android/providers/downloads/FakeSystemFacade.java @@ -1,11 +1,15 @@ package com.android.providers.downloads; +import android.app.Notification; import android.content.Intent; import android.content.pm.PackageManager.NameNotFoundException; import android.net.ConnectivityManager; +import android.test.AssertionFailedError; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class FakeSystemFacade implements SystemFacade { long mTimeMillis = 0; @@ -13,6 +17,8 @@ public class FakeSystemFacade implements SystemFacade { boolean mIsRoaming = false; Integer mMaxBytesOverMobile = null; List mBroadcastsSent = new ArrayList(); + Map mActiveNotifications = new HashMap(); + List mCanceledNotifications = new ArrayList(); void incrementTimeMillis(long delta) { mTimeMillis += delta; @@ -43,4 +49,27 @@ public class FakeSystemFacade implements SystemFacade { public boolean userOwnsPackage(int uid, String pckg) throws NameNotFoundException { return true; } + + @Override + public void postNotification(int id, Notification notification) { + if (notification == null) { + throw new AssertionFailedError("Posting null notification"); + } + mActiveNotifications.put(id, notification); + } + + @Override + public void cancelNotification(int id) { + Notification notification = mActiveNotifications.remove(id); + if (notification != null) { + mCanceledNotifications.add(notification); + } + } + + @Override + public void cancelAllNotifications() { + for (int id : mActiveNotifications.keySet()) { + cancelNotification(id); + } + } } diff --git a/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java b/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java index 3d32ae3c..00419a3d 100644 --- a/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java +++ b/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java @@ -485,6 +485,24 @@ public class PublicApiFunctionalTest extends AbstractDownloadManagerFunctionalTe assertTrue(mResolver.mNotifyWasCalled); } + public void testNotifications() throws Exception { + enqueueEmptyResponse(HTTP_OK); + Download download = enqueueRequest(getRequest()); // no visibility requested + download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL); + assertEquals(0, mSystemFacade.mActiveNotifications.size()); + assertEquals(0, mSystemFacade.mCanceledNotifications.size()); + + enqueueEmptyResponse(HTTP_OK); + download = enqueueRequest( + getRequest() + .setShowNotification(DownloadManager.Request.NOTIFICATION_WHEN_RUNNING)); + download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL); + assertEquals(1, mSystemFacade.mActiveNotifications.size()); + // The notification doesn't actually get canceled until the UpdateThread runs again, which + // gets triggered by the DownloadThread updating the status in the provider. This is + // tough to test right now, so I'll leave it until the overall structure is changed. + } + private void runSimpleFailureTest(int expectedErrorCode) throws Exception { Download download = enqueueRequest(getRequest()); download.runUntilStatus(DownloadManager.STATUS_FAILED); -- cgit v1.2.3