1 /*
2  * Copyright (C) 2021 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.lib.TestUtils.getExternalFilesDir;
20 import static android.scopedstorage.cts.lib.TestUtils.pollForExternalStorageState;
21 import static android.scopedstorage.cts.lib.TestUtils.resetDefaultExternalStorageVolume;
22 import static android.scopedstorage.cts.lib.TestUtils.setExternalStorageVolume;
23 import static android.scopedstorage.cts.lib.TestUtils.setupDefaultDirectories;
24 
25 import static androidx.test.InstrumentationRegistry.getContext;
26 
27 import static com.google.common.truth.Truth.assertThat;
28 import static com.google.common.truth.Truth.assertWithMessage;
29 
30 import android.provider.MediaStore;
31 import android.scopedstorage.cts.lib.TestUtils;
32 
33 import org.junit.BeforeClass;
34 
35 import java.util.Arrays;
36 import java.util.List;
37 
38 class ScopedStorageBaseDeviceTest {
39     private static final String VOLUME_PUBLIC = "volume_public";
40 
41     @BeforeClass
setup()42     public static void setup() throws Exception {
43         createPublicVolume();
44         setupStorage();
45     }
46 
createPublicVolume()47     private static void createPublicVolume() throws Exception {
48         if (TestUtils.getCurrentPublicVolumeName() == null) {
49             TestUtils.createNewPublicVolume();
50             assertWithMessage("Expected newly created public volume name to be not null")
51                     .that(TestUtils.getCurrentPublicVolumeName())
52                     .isNotNull();
53         }
54     }
setupStorage()55     private static void setupStorage() throws Exception {
56         if (!getContext().getPackageManager().isInstantApp()) {
57             pollForExternalStorageState();
58             getExternalFilesDir().mkdirs();
59         }
60     }
61 
setupExternalStorage(String volumeName)62     void setupExternalStorage(String volumeName) {
63         assertThat(volumeName).isNotNull();
64         if (volumeName.equals(MediaStore.VOLUME_EXTERNAL)) {
65             resetDefaultExternalStorageVolume();
66             TestUtils.assertDefaultVolumeIsPrimary();
67         } else {
68             final String publicVolumeName = TestUtils.getCurrentPublicVolumeName();
69             assertWithMessage("Expected public volume name to be not null")
70                     .that(publicVolumeName)
71                     .isNotNull();
72             setExternalStorageVolume(publicVolumeName);
73             TestUtils.assertDefaultVolumeIsPublic();
74         }
75         setupDefaultDirectories();
76     }
77 
getTestParameters()78     static List<String> getTestParameters() {
79         return Arrays.asList(
80                 MediaStore.VOLUME_EXTERNAL,
81                 VOLUME_PUBLIC
82         );
83     }
84 }
85