1 /*
2  * Copyright (C) 2019 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.assertArrayEquals;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 
23 import android.content.Intent;
24 import android.database.Cursor;
25 import android.net.Uri;
26 import android.os.Environment;
27 import android.os.FileUtils;
28 import android.provider.MediaStore;
29 
30 import androidx.test.runner.AndroidJUnit4;
31 
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.io.InputStream;
38 import java.io.OutputStream;
39 
40 @RunWith(AndroidJUnit4.class)
41 public class DownloadManagerLegacyTest extends DownloadManagerTestBase {
42     @Test
testAddCompletedDownload()43     public void testAddCompletedDownload() throws Exception {
44         final String[] filePaths = new String[] {
45                 createFile(Environment.getExternalStoragePublicDirectory(
46                         Environment.DIRECTORY_DOWNLOADS), "file1.txt").getPath(),
47                 "/sdcard/Download/file2.txt",
48         };
49 
50         for (String path : filePaths) {
51             final String fileContents = "Test content:" + path + "_" + System.nanoTime();
52 
53             final File file = new File(path);
54             writeToFile(file, fileContents);
55 
56             final long id = mDownloadManager.addCompletedDownload(file.getName(), "Test desc", true,
57                     "text/plain", path, fileContents.getBytes().length, true);
58             final String actualContents = readFromFile(mDownloadManager.openDownloadedFile(id));
59             assertEquals(fileContents, actualContents);
60 
61             final Uri downloadUri = mDownloadManager.getUriForDownloadedFile(id);
62             mContext.grantUriPermission("com.android.shell", downloadUri,
63                     Intent.FLAG_GRANT_READ_URI_PERMISSION);
64             final String rawFilePath = getRawFilePath(downloadUri);
65             final String rawFileContents = readFromRawFile(rawFilePath);
66             assertEquals(fileContents, rawFileContents);
67             assertRemoveDownload(id, 0);
68         }
69     }
70 
71     @Test
testAddCompletedDownload_invalidPublicDir()72     public void testAddCompletedDownload_invalidPublicDir() throws Exception {
73         final File file = new File(
74                 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
75                 "colors.txt");
76         final String fileContents = "RED;GREEN;BLUE";
77         writeToFile(file, fileContents);
78         try {
79             mDownloadManager.addCompletedDownload(file.getName(), "Test desc", true,
80                     "text/plain", file.getPath(), fileContents.getBytes().length, true);
81             fail(file + " is not valid for addCompletedDownload()");
82         } catch (Exception e) {
83             // expected
84         }
85     }
86 
87     /**
88      * Add a file using DownloadManager.addCompleteDownload
89      * and verify that the file has been added to MediaStore as well.
90      */
91     @Test
testAddCompletedDownload_mediaStoreEntry()92     public void testAddCompletedDownload_mediaStoreEntry() throws Exception {
93         final String assetName = "testmp3.mp3";
94         final String[] downloadPaths = {
95                 new File(Environment.getExternalStoragePublicDirectory(
96                         Environment.DIRECTORY_DOWNLOADS), "file1.mp3").getPath(),
97                 "/sdcard/Download/file2.mp3",
98         };
99         for (String downloadLocation : downloadPaths) {
100             final File file = new File(downloadLocation);
101             try (InputStream in = mContext.getAssets().open(assetName);
102                  OutputStream out = new FileOutputStream(file)) {
103                 FileUtils.copy(in, out);
104             }
105 
106             final long downloadId = mDownloadManager.addCompletedDownload(
107                     file.getName(), "Test desc",
108                     true, "audio/mp3", downloadLocation, 0, true);
109             assertTrue(downloadId >= 0);
110             final Uri downloadUri = mDownloadManager.getUriForDownloadedFile(downloadId);
111             final Uri mediaStoreUri = getMediaStoreUri(downloadUri);
112 
113             assertArrayEquals(hash(mContext.getAssets().open(assetName)),
114                     hash(mContext.getContentResolver().openInputStream(mediaStoreUri)));
115 
116             assertEquals("1", getMediaStoreColumnValue(mediaStoreUri,
117                     MediaStore.Audio.AudioColumns.IS_MUSIC));
118             assertEquals(new File(downloadLocation).length(), Long.parseLong(
119                     getMediaStoreColumnValue(mediaStoreUri, MediaStore.Audio.AudioColumns.SIZE)));
120 
121             // Delete entry in DownloadProvider and verify it's deleted from MediaProvider as well.
122             assertRemoveDownload(downloadId, 0);
123             try (Cursor cursor = mContext.getContentResolver().query(
124                     mediaStoreUri, null, null, null)) {
125                 assertEquals(0, cursor.getCount());
126             }
127         }
128     }
129 }