• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 
17 package android.scopedstorage.cts.device;
18 
19 import static android.scopedstorage.cts.device.DeviceTestUtils.createContentFromResource;
20 import static android.scopedstorage.cts.lib.TestUtils.getDcimDir;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import android.content.ContentResolver;
25 import android.net.Uri;
26 import android.provider.MediaStore;
27 
28 import org.junit.rules.ExternalResource;
29 
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.List;
33 
34 public class OwnedFilesRule extends ExternalResource {
35 
36     private static final String TAG = "OwnedFilesRule";
37     public static final int RESOURCE_ID = R.raw.scenery;
38     public static final int RESOURCE_ID_WITH_METADATA = R.raw.img_with_metadata;
39     private static final String NONCE = String.valueOf(System.nanoTime());
40 
41     private static final String IMAGE_1 = TAG + "_image1_" + NONCE + ".jpg";
42     private static final String IMAGE_2 = TAG + "_image2_" + NONCE + ".jpg";
43     private static final String VIDEO_1 = TAG + "_video1_" + NONCE + ".mp4";
44 
45     private static final File sImageFile1 = new File(getDcimDir(), IMAGE_1);
46     private static final File sImageFile2Metadata = new File(getDcimDir(), IMAGE_2);
47 
48     private static final File sVideoFile1 = new File(getDcimDir(), VIDEO_1);
49 
50     private final ContentResolver mContentResolver;
51     private Uri mImageUri1;
52     private Uri mImageUri2;
53     private Uri mVideoUri1;
54 
55     private static final List<File> ownedItemsList = List.of(sImageFile1, sImageFile2Metadata,
56             sVideoFile1);
57 
OwnedFilesRule(ContentResolver contentResolver)58     public OwnedFilesRule(ContentResolver contentResolver) {
59         this.mContentResolver = contentResolver;
60     }
61 
62     @Override
before()63     protected void before() throws IOException {
64         mImageUri1 = createFile(RESOURCE_ID, sImageFile1);
65         mImageUri2 = createFile(RESOURCE_ID_WITH_METADATA, sImageFile2Metadata);
66         mVideoUri1 = createFile(sVideoFile1);
67 
68     }
69 
createFile(int resourceId, File imageFile)70     public Uri createFile(int resourceId, File imageFile) throws IOException {
71         assertThat(imageFile.createNewFile()).isTrue();
72         Uri uri = MediaStore.scanFile(mContentResolver, imageFile);
73         createContentFromResource(resourceId, imageFile);
74         assertThat(uri).isNotNull();
75         return uri;
76     }
77 
createFile(File file)78     public Uri createFile(File file) throws IOException {
79         assertThat(file.createNewFile()).isTrue();
80         Uri uri = MediaStore.scanFile(mContentResolver, file);
81         assertThat(uri).isNotNull();
82         return uri;
83     }
84 
85     @Override
after()86     protected void after() {
87         sImageFile1.delete();
88         sImageFile2Metadata.delete();
89         sVideoFile1.delete();
90     }
91 
getImageFile1()92     public static File getImageFile1() {
93         return sImageFile1;
94     }
95 
getImageFile2Metadata()96     public static File getImageFile2Metadata() {
97         return sImageFile2Metadata;
98     }
99 
getVideoFile1()100     public static File getVideoFile1() {
101         return sVideoFile1;
102     }
103 
getAllFiles()104     public static List<File> getAllFiles() {
105         return ownedItemsList;
106     }
107 
getImageUri1()108     public Uri getImageUri1() {
109         return mImageUri1;
110     }
111 
getImageUri2_Metadata()112     public Uri getImageUri2_Metadata() {
113         return mImageUri2;
114     }
115 
getVideoUri1()116     public Uri getVideoUri1() {
117         return mVideoUri1;
118     }
119 }
120