Fixing some flakiness of some DL Manager tests.

Change-Id: Ie9ad4d20b8f980480ce730c7c95876d0954db8b2
diff --git a/core/tests/coretests/src/android/app/DownloadManagerBaseTest.java b/core/tests/coretests/src/android/app/DownloadManagerBaseTest.java
index ed42e64..37fc6c7 100644
--- a/core/tests/coretests/src/android/app/DownloadManagerBaseTest.java
+++ b/core/tests/coretests/src/android/app/DownloadManagerBaseTest.java
@@ -66,6 +66,7 @@
     protected MockWebServer mServer = null;
     protected String mFileType = "text/plain";
     protected Context mContext = null;
+    protected MultipleDownloadsCompletedReceiver mReceiver = null;
     protected static final int DEFAULT_FILE_SIZE = 130 * 1024;  // 130kb
     protected static final int FILE_BLOCK_READ_SIZE = 1024 * 1024;
 
@@ -131,12 +132,15 @@
          */
         @Override
         public void onReceive(Context context, Intent intent) {
+            Log.i(LOG_TAG, "Received Notification:");
             if (intent.getAction().equalsIgnoreCase(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
-                ++mNumDownloadsCompleted;
-                Log.i(LOG_TAG, "MultipleDownloadsCompletedReceiver got intent: " +
-                        intent.getAction() + " --> total count: " + mNumDownloadsCompleted);
-                Bundle extras = intent.getExtras();
-                downloadIds.add(new Long(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID)));
+                synchronized(this) {
+                    ++mNumDownloadsCompleted;
+                    Log.i(LOG_TAG, "MultipleDownloadsCompletedReceiver got intent: " +
+                            intent.getAction() + " --> total count: " + mNumDownloadsCompleted);
+                    Bundle extras = intent.getExtras();
+                    downloadIds.add(new Long(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID)));
+                }
             }
         }
 
@@ -212,6 +216,7 @@
         mContext = getInstrumentation().getContext();
         mDownloadManager = (DownloadManager)mContext.getSystemService(Context.DOWNLOAD_SERVICE);
         mServer = new MockWebServer();
+        mReceiver = registerNewMultipleDownloadsReceiver();
         // Note: callers overriding this should call mServer.play() with the desired port #
     }
 
@@ -712,8 +717,9 @@
             Cursor cursor = mDownloadManager.query(query);
 
             try {
-                // If we've finished the downloads then we're done
-                if (cursor.getCount() == 0) {
+                // @TODO: there may be a little cleaner way to check for success, perhaps
+                // via STATUS_SUCCESSFUL and/or STATUS_FAILED
+                if (cursor.getCount() == 0 && mReceiver.numDownloadsCompleted() > 0) {
                     break;
                 }
                 currentWaitTime = timeoutWait(currentWaitTime, poll, timeoutMillis,
diff --git a/core/tests/coretests/src/android/app/DownloadManagerIntegrationTest.java b/core/tests/coretests/src/android/app/DownloadManagerIntegrationTest.java
index 27eea4d..cb7c2d2 100644
--- a/core/tests/coretests/src/android/app/DownloadManagerIntegrationTest.java
+++ b/core/tests/coretests/src/android/app/DownloadManagerIntegrationTest.java
@@ -61,7 +61,6 @@
             Environment.getRootDirectory().getAbsolutePath();
     private final static String CACHE_DIR =
             Environment.getDownloadCacheDirectory().getAbsolutePath();
-    protected MultipleDownloadsCompletedReceiver mReceiver = null;
 
     /**
      * {@inheritDoc}
@@ -72,7 +71,6 @@
         setWiFiStateOn(true);
         mServer.play();
         removeAllCurrentDownloads();
-        mReceiver = registerNewMultipleDownloadsReceiver();
     }
 
     /**
@@ -270,7 +268,7 @@
             try {
                 verifyInt(cursor, DownloadManager.COLUMN_STATUS, DownloadManager.STATUS_FAILED);
                 verifyInt(cursor, DownloadManager.COLUMN_REASON,
-                        DownloadManager.ERROR_FILE_ERROR);
+                        DownloadManager.ERROR_FILE_ALREADY_EXISTS);
             } finally {
                 cursor.close();
             }
@@ -429,6 +427,7 @@
                 }
             }
 
+            Log.i(LOG_TAG, "Done creating filler file.");
             assertTrue(DOWNLOAD_FILE_SIZE > (fs.getAvailableBlocks() * blockSize));
             byte[] blobData = generateData(DOWNLOAD_FILE_SIZE, DataType.TEXT);
             long dlRequest = doBasicDownload(blobData);