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.IntentFilter; 23 import android.net.Uri; 24 import android.os.Environment; 25 26 import androidx.test.runner.AndroidJUnit4; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.io.File; 32 import java.util.concurrent.TimeUnit; 33 import java.util.concurrent.TimeoutException; 34 35 @RunWith(AndroidJUnit4.class) 36 public class DownloadManagerInstallerTest extends DownloadManagerTestBase { 37 private static final long POLLING_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(20); 38 private static final long POLLING_SLEEP_MILLIS = 100; 39 40 @Test testSetDestinationUri_otherAppObbDir()41 public void testSetDestinationUri_otherAppObbDir() throws Exception { 42 // getObbDir() may return {@code null} if shared storage is not currently available. 43 pollForExternalStorageState(); 44 final File obbDir = mContext.getObbDir(); 45 assertNotNull(obbDir); 46 47 String otherAppObbPath = obbDir.getPath().replace(mContext.getPackageName(), 48 "android.app.cts.some_random_package"); 49 File destPath = new File(otherAppObbPath); 50 destPath.mkdirs(); 51 52 File destFile = new File(destPath, "test.obb"); 53 deleteFromShell(destFile); 54 55 final DownloadCompleteReceiver receiver = new DownloadCompleteReceiver(); 56 try { 57 IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); 58 mContext.registerReceiver(receiver, intentFilter); 59 60 DownloadManager.Request requestPublic = new DownloadManager.Request(getGoodUrl()); 61 requestPublic.setDestinationUri(Uri.fromFile(destFile)); 62 long id = mDownloadManager.enqueue(requestPublic); 63 64 int allDownloads = getTotalNumberDownloads(); 65 assertEquals(1, allDownloads); 66 67 receiver.waitForDownloadComplete(SHORT_TIMEOUT, id); 68 assertSuccessfulDownload(id, destFile); 69 70 assertRemoveDownload(id, 0); 71 } finally { 72 mContext.unregisterReceiver(receiver); 73 } 74 } 75 76 /** 77 * Polls for external storage to be mounted. 78 */ pollForExternalStorageState()79 private static void pollForExternalStorageState() throws Exception { 80 for (int i = 0; i < POLLING_TIMEOUT_MILLIS / POLLING_SLEEP_MILLIS; i++) { 81 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 82 return; 83 } 84 Thread.sleep(POLLING_SLEEP_MILLIS); 85 } 86 throw new TimeoutException("Timed out while waiting for ExternalStorageState to be" 87 + " MEDIA_MOUNTED"); 88 } 89 } 90