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.lib;
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 
32 import static org.junit.Assume.assumeTrue;
33 
34 import java.util.Arrays;
35 import java.util.List;
36 
37 public class ScopedStorageBaseDeviceTest {
38     private static final String VOLUME_PUBLIC = "volume_public";
39 
createPublicVolume()40     private static void createPublicVolume() throws Exception {
41         TestUtils.preparePublicVolume();
42         assertWithMessage("Expected newly created public volume name to be not null")
43                     .that(TestUtils.getCurrentPublicVolumeName())
44                     .isNotNull();
45     }
setupStorage()46     private static void setupStorage() throws Exception {
47         if (!getContext().getPackageManager().isInstantApp()) {
48             pollForExternalStorageState();
49             getExternalFilesDir().mkdirs();
50         }
51     }
52 
setupExternalStorage(String volumeName)53     public void setupExternalStorage(String volumeName) throws Exception {
54         assertThat(volumeName).isNotNull();
55 
56         if (volumeName.equals(MediaStore.VOLUME_EXTERNAL)) {
57             setupStorage();
58 
59             resetDefaultExternalStorageVolume();
60             TestUtils.assertDefaultVolumeIsPrimary();
61         } else {
62             assumeTrue(TestUtils.isAdoptableStorageSupported());
63             createPublicVolume();
64             setupStorage();
65 
66             final String publicVolumeName = TestUtils.getCurrentPublicVolumeName();
67             assertWithMessage("Expected public volume name to be not null")
68                     .that(publicVolumeName)
69                     .isNotNull();
70             setExternalStorageVolume(publicVolumeName);
71             TestUtils.assertDefaultVolumeIsPublic();
72         }
73         setupDefaultDirectories();
74     }
75 
getTestParameters()76     public static List<String> getTestParameters() {
77         return Arrays.asList(
78                 MediaStore.VOLUME_EXTERNAL,
79                 VOLUME_PUBLIC
80         );
81     }
82 }
83