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 com.android.cts.noappstorage;
18 
19 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.content.Context;
24 import android.os.Environment;
25 
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 import java.io.File;
31 
32 /**
33  * Tests that exercise behaviour of an app without access to apps' data storage.
34  */
35 // TODO(b/211761016): add tests for external storage.
36 @RunWith(JUnit4.class)
37 public class NoAppDataStorageTest {
38 
39     private final Context mCeContext = getInstrumentation().getContext();
40     private final Context mDeContext = mCeContext.createDeviceProtectedStorageContext();
41 
42     @Test
testNoInternalCeStorage()43     public void testNoInternalCeStorage() throws Exception {
44         assertDirDoesNotExist(mCeContext.getDataDir());
45         assertDirDoesNotExist(mCeContext.getFilesDir());
46         assertDirDoesNotExist(mCeContext.getCacheDir());
47         assertDirDoesNotExist(mCeContext.getCodeCacheDir());
48     }
49 
50     @Test
testNoInternalDeStorage()51     public void testNoInternalDeStorage() throws Exception {
52         assertDirDoesNotExist(mDeContext.getDataDir());
53         assertDirDoesNotExist(mDeContext.getFilesDir());
54         assertDirDoesNotExist(mDeContext.getCacheDir());
55         assertDirDoesNotExist(mDeContext.getCodeCacheDir());
56     }
57 
58     @Test
testNoExternalStorage()59     public void testNoExternalStorage() throws Exception {
60         final String[] types = new String[] {
61                 Environment.DIRECTORY_MUSIC,
62                 Environment.DIRECTORY_PODCASTS,
63                 Environment.DIRECTORY_RINGTONES,
64                 Environment.DIRECTORY_ALARMS,
65                 Environment.DIRECTORY_NOTIFICATIONS,
66                 Environment.DIRECTORY_PICTURES,
67                 Environment.DIRECTORY_MOVIES,
68                 Environment.DIRECTORY_DOWNLOADS,
69                 Environment.DIRECTORY_DCIM,
70                 Environment.DIRECTORY_DOCUMENTS
71         };
72         for (String type : types) {
73             File dir = mCeContext.getExternalFilesDir(type);
74             assertThat(dir).isNull();
75         }
76     }
77 
assertDirDoesNotExist(File dir)78     private void assertDirDoesNotExist(File dir) throws Exception {
79         assertThat(dir.exists()).isFalse();
80         assertThat(dir.mkdirs()).isFalse();
81     }
82 }
83