1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.app.cts; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertNotNull; 20 21 import android.app.DownloadManager; 22 import android.content.Context; 23 import android.content.IntentFilter; 24 import android.net.Uri; 25 import android.os.Environment; 26 27 import androidx.test.runner.AndroidJUnit4; 28 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.io.File; 33 import java.util.concurrent.TimeUnit; 34 import java.util.concurrent.TimeoutException; 35 36 @RunWith(AndroidJUnit4.class) 37 public class DownloadManagerInstallerTest extends DownloadManagerTestBase { 38 private static final long POLLING_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(20); 39 private static final long POLLING_SLEEP_MILLIS = 100; 40 41 @Test testSetDestinationUri_otherAppObbDir()42 public void testSetDestinationUri_otherAppObbDir() throws Exception { 43 // getObbDir() may return {@code null} if shared storage is not currently available. 44 pollForExternalStorageState(); 45 final File obbDir = mContext.getObbDir(); 46 assertNotNull(obbDir); 47 48 String otherAppObbPath = obbDir.getPath().replace(mContext.getPackageName(), 49 "android.app.cts.some_random_package"); 50 File destPath = new File(otherAppObbPath); 51 destPath.mkdirs(); 52 53 File destFile = new File(destPath, "test.obb"); 54 deleteFromShell(destFile); 55 56 final DownloadCompleteReceiver receiver = new DownloadCompleteReceiver(); 57 try { 58 IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); 59 mContext.registerReceiver(receiver, intentFilter, Context.RECEIVER_EXPORTED); 60 61 DownloadManager.Request requestPublic = new DownloadManager.Request(getGoodUrl()); 62 requestPublic.setDestinationUri(Uri.fromFile(destFile)); 63 long id = mDownloadManager.enqueue(requestPublic); 64 65 int allDownloads = getTotalNumberDownloads(); 66 assertEquals(1, allDownloads); 67 68 receiver.waitForDownloadComplete(SHORT_TIMEOUT, id); 69 assertSuccessfulDownload(id, destFile); 70 71 assertRemoveDownload(id, 0); 72 } finally { 73 mContext.unregisterReceiver(receiver); 74 } 75 } 76 77 /** 78 * Polls for external storage to be mounted. 79 */ pollForExternalStorageState()80 private static void pollForExternalStorageState() throws Exception { 81 for (int i = 0; i < POLLING_TIMEOUT_MILLIS / POLLING_SLEEP_MILLIS; i++) { 82 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 83 return; 84 } 85 Thread.sleep(POLLING_SLEEP_MILLIS); 86 } 87 throw new TimeoutException("Timed out while waiting for ExternalStorageState to be" 88 + " MEDIA_MOUNTED"); 89 } 90 } 91