1package com.xtremelabs.robolectric.shadows;
2
3import android.app.Activity;
4import android.content.Context;
5import com.xtremelabs.robolectric.WithTestDefaultsRunner;
6
7import org.hamcrest.CoreMatchers;
8import org.junit.After;
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
12
13import java.io.File;
14import java.io.FileInputStream;
15import java.io.FileOutputStream;
16import java.io.FileWriter;
17import java.io.IOException;
18
19import static org.hamcrest.CoreMatchers.*;
20import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertThat;
22import static org.junit.Assert.assertTrue;
23
24@RunWith(WithTestDefaultsRunner.class)
25public class ContextTest {
26    private Context context;
27
28    @Before
29    public void setUp() throws Exception {
30        context = new Activity();
31        deleteDir(context.getFilesDir());
32        deleteDir(context.getCacheDir());
33
34        File[] files = context.getFilesDir().listFiles();
35        assertNotNull(files);
36        assertThat(files.length, is(0));
37
38        File[] cachedFiles = context.getFilesDir().listFiles();
39        assertNotNull(cachedFiles);
40        assertThat(cachedFiles.length, is(0));
41    }
42
43    @After
44    public void after() {
45    	deleteDir(context.getFilesDir());
46    	deleteDir(context.getCacheDir());
47    	deleteDir(context.getExternalCacheDir());
48    	deleteDir(context.getExternalFilesDir(null));
49    }
50
51    public void deleteDir(File path) {
52		if (path.isDirectory()) {
53			File[] files = path.listFiles();
54            assertNotNull(files);
55			for (File f : files) {
56				deleteDir(f);
57			}
58		}
59		path.delete();
60	}
61
62    @Test
63    public void shouldGetApplicationDataDirectory() throws IOException {
64        File dataDir = new File(ShadowContext.FILES_DIR, "data");
65        assertThat(dataDir.mkdir(), is(true));
66
67        dataDir = context.getDir("data", Context.MODE_PRIVATE);
68        assertThat(dataDir, not(nullValue()));
69        assertThat(dataDir.exists(), is(true));
70    }
71
72
73    @Test
74    public void shouldCreateIfDoesNotExistAndGetApplicationDataDirectory() {
75        File dataDir = new File(ShadowContext.FILES_DIR, "data");
76        assertThat(dataDir.exists(), is(false));
77
78        dataDir = context.getDir("data", Context.MODE_PRIVATE);
79        assertThat(dataDir, not(nullValue()));
80        assertThat(dataDir.exists(), is(true));
81    }
82
83    @Test
84    public void shouldStubThemeStuff() throws Exception {
85        assertThat(context.obtainStyledAttributes(null), not(nullValue()));
86        assertThat(context.obtainStyledAttributes(0, null), not(nullValue()));
87        assertThat(context.obtainStyledAttributes(null, null), not(nullValue()));
88        assertThat(context.obtainStyledAttributes(null, null, 0, 0), not(nullValue()));
89    }
90
91    @Test
92    public void getCacheDir_shouldCreateDirectory() throws Exception {
93        assertTrue(context.getCacheDir().exists());
94    }
95
96    @Test
97    public void getExternalCacheDir_shouldCreateDirectory() throws Exception {
98        assertTrue(context.getExternalCacheDir().exists());
99    }
100
101    @Test
102    public void shouldWriteToCacheDir() throws Exception {
103        assertNotNull(context.getCacheDir());
104        File cacheTest = new File(context.getCacheDir(), "__test__");
105
106        assertThat(cacheTest.getPath(), CoreMatchers.containsString("android-cache"));
107
108        FileOutputStream fos = null;
109        try {
110            fos = new FileOutputStream(cacheTest);
111            fos.write("test".getBytes());
112        } finally {
113            if (fos != null)
114                fos.close();
115        }
116        assertTrue(cacheTest.exists());
117    }
118
119    @Test
120    public void shouldWriteToExternalCacheDir() throws Exception {
121        assertNotNull(context.getExternalCacheDir());
122        File cacheTest = new File(context.getExternalCacheDir(), "__test__");
123
124        assertThat(cacheTest.getPath(), containsString("android-external-cache"));
125
126        FileOutputStream fos = null;
127        try {
128            fos = new FileOutputStream(cacheTest);
129            fos.write("test".getBytes());
130        } finally {
131            if (fos != null)
132                fos.close();
133        }
134
135        assertTrue(cacheTest.exists());
136    }
137
138    @Test
139    public void getFilesDir_shouldCreateDirectory() throws Exception {
140        assertTrue(context.getFilesDir().exists());
141    }
142
143    @Test
144    public void getExternalFilesDir_shouldCreateDirectory() throws Exception {
145        assertTrue(context.getExternalFilesDir(null).exists());
146    }
147
148    @Test
149    public void getExternalFilesDir_shouldCreateNamedDirectory() throws Exception {
150    	File f = context.getExternalFilesDir("__test__");
151        assertTrue(f.exists());
152        assertTrue(f.getAbsolutePath().endsWith("__test__"));
153    }
154
155    @Test
156    public void openFileInput_shouldReturnAFileInputStream() throws Exception {
157        String fileContents = "blah";
158
159        File file = new File(context.getFilesDir(), "__test__");
160        FileWriter fileWriter = new FileWriter(file);
161        fileWriter.write(fileContents);
162        fileWriter.close();
163
164        FileInputStream fileInputStream = null;
165        try {
166            fileInputStream = context.openFileInput("__test__");
167
168            byte[] bytes = new byte[fileContents.length()];
169            fileInputStream.read(bytes);
170            assertThat(bytes, equalTo(fileContents.getBytes()));
171        } finally {
172            if (fileInputStream != null)
173                fileInputStream.close();
174        }
175    }
176
177    @Test(expected = IllegalArgumentException.class)
178    public void openFileInput_shouldNotAcceptPathsWithSeparatorCharacters() throws Exception {
179        FileInputStream fileInputStream = null;
180        try {
181            fileInputStream = context.openFileInput("data" + File.separator + "test");
182        } finally {
183            if (fileInputStream != null)
184                fileInputStream.close();
185        }
186    }
187
188    @Test
189    public void openFileOutput_shouldReturnAFileOutputStream() throws Exception {
190        File file = new File("__test__");
191        String fileContents = "blah";
192        FileOutputStream fileOutputStream = null;
193        try {
194            fileOutputStream = context.openFileOutput("__test__", -1);
195            fileOutputStream.write(fileContents.getBytes());
196        } finally {
197            if (fileOutputStream != null)
198                fileOutputStream.close();
199        }
200        FileInputStream fileInputStream = null;
201        try {
202            fileInputStream = new FileInputStream(new File(context.getFilesDir(), file.getName()));
203            byte[] readBuffer = new byte[fileContents.length()];
204            fileInputStream.read(readBuffer);
205            assertThat(new String(readBuffer), equalTo(fileContents));
206        } finally {
207            if (fileInputStream != null)
208                fileInputStream.close();
209        }
210    }
211
212    @Test(expected = IllegalArgumentException.class)
213    public void openFileOutput_shouldNotAcceptPathsWithSeparatorCharacters() throws Exception {
214        FileOutputStream fos = null;
215        try {
216            fos = context.openFileOutput(File.separator + "data" + File.separator + "test" + File.separator + "hi", 0);
217        } finally {
218            if (fos != null)
219                fos.close();
220        }
221    }
222
223    @Test
224    public void deleteFile_shouldReturnTrue() throws IOException {
225        File filesDir = context.getFilesDir();
226        File file = new File(filesDir, "test.txt");
227        boolean successfully = file.createNewFile();
228        assertThat(successfully, is(true));
229        successfully = context.deleteFile(file.getName());
230        assertThat(successfully, is(true));
231    }
232
233    @Test
234    public void deleteFile_shouldReturnFalse() throws IOException {
235        File filesDir = context.getFilesDir();
236        File file = new File(filesDir, "test.txt");
237        boolean successfully = context.deleteFile(file.getName());
238        assertThat(successfully, is(false));
239    }
240}
241