1/*
2 * Copyright (C) 2017 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
17package android.os;
18
19import static android.os.Environment.HAS_ANDROID;
20import static android.os.Environment.HAS_DCIM;
21import static android.os.Environment.HAS_DOWNLOADS;
22import static android.os.Environment.HAS_OTHER;
23import static android.os.Environment.classifyExternalStorageDirectory;
24
25import static org.junit.Assert.assertEquals;
26
27import android.content.Context;
28import android.support.test.InstrumentationRegistry;
29import android.support.test.runner.AndroidJUnit4;
30
31import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
36import java.io.File;
37
38@RunWith(AndroidJUnit4.class)
39public class EnvironmentTest {
40    private File dir;
41
42    private Context getContext() {
43        return InstrumentationRegistry.getContext();
44    }
45
46    @Before
47    public void setUp() throws Exception {
48        dir = getContext().getDir("testing", Context.MODE_PRIVATE);
49        FileUtils.deleteContents(dir);
50    }
51
52    @After
53    public void tearDown() throws Exception {
54        FileUtils.deleteContents(dir);
55    }
56
57    @Test
58    public void testClassify_empty() {
59        assertEquals(0, classifyExternalStorageDirectory(dir));
60    }
61
62    @Test
63    public void testClassify_emptyDirs() {
64        Environment.buildPath(dir, "DCIM").mkdirs();
65        Environment.buildPath(dir, "DCIM", "January").mkdirs();
66        Environment.buildPath(dir, "Downloads").mkdirs();
67        Environment.buildPath(dir, "LOST.DIR").mkdirs();
68        assertEquals(0, classifyExternalStorageDirectory(dir));
69    }
70
71    @Test
72    public void testClassify_emptyFactory() throws Exception {
73        Environment.buildPath(dir, "autorun.inf").createNewFile();
74        Environment.buildPath(dir, "LaunchU3.exe").createNewFile();
75        Environment.buildPath(dir, "LaunchPad.zip").createNewFile();
76        assertEquals(0, classifyExternalStorageDirectory(dir));
77    }
78
79    @Test
80    public void testClassify_photos() throws Exception {
81        Environment.buildPath(dir, "DCIM").mkdirs();
82        Environment.buildPath(dir, "DCIM", "IMG_1024.JPG").createNewFile();
83        Environment.buildPath(dir, "Download").mkdirs();
84        Environment.buildPath(dir, "Download", "foobar.pdf").createNewFile();
85        assertEquals(HAS_DCIM | HAS_DOWNLOADS, classifyExternalStorageDirectory(dir));
86    }
87
88    @Test
89    public void testClassify_other() throws Exception {
90        Environment.buildPath(dir, "Android").mkdirs();
91        Environment.buildPath(dir, "Android", "com.example").mkdirs();
92        Environment.buildPath(dir, "Android", "com.example", "internal.dat").createNewFile();
93        Environment.buildPath(dir, "Linux").mkdirs();
94        Environment.buildPath(dir, "Linux", "install-amd64-minimal-20170907.iso").createNewFile();
95        assertEquals(HAS_ANDROID | HAS_OTHER, classifyExternalStorageDirectory(dir));
96    }
97
98    @Test
99    public void testClassify_otherRoot() throws Exception {
100        Environment.buildPath(dir, "Taxes.pdf").createNewFile();
101        assertEquals(HAS_OTHER, classifyExternalStorageDirectory(dir));
102    }
103}
104