blob: 10ccebc42dd014ff26d76090716b417ed9bf07d1 [file] [log] [blame]
Kenny Root10362ab2010-03-12 11:13:50 -08001/*
2 * Copyright (C) 2007 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.test;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.content.Context;
20import android.content.ContextWrapper;
21import android.content.ContentProvider;
Makoto Onukif6b979a2010-06-04 16:12:15 -070022import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.database.sqlite.SQLiteDatabase;
Paul Duffin20af1df2018-01-05 13:52:17 +000024import android.test.mock.MockContentProvider;
Andrew Stadler54a16f02009-07-23 20:00:08 -070025import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27import java.io.File;
28import java.io.FileInputStream;
29import java.io.FileNotFoundException;
30import java.io.FileOutputStream;
Paul Duffin2231eb42017-05-10 15:05:24 +010031import java.io.IOException;
32import java.nio.file.Files;
33import java.nio.file.Paths;
34import java.nio.file.attribute.PosixFilePermission;
35import java.nio.file.attribute.PosixFilePermissions;
36import java.util.EnumSet;
Paul Duffin8c5a24d2017-05-10 13:30:16 +010037import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import java.util.Set;
39
40/**
41 * This is a class which delegates to the given context, but performs database
42 * and file operations with a renamed database/file name (prefixes default
43 * names with a given prefix).
Stephan Linznerb51617f2016-01-27 18:09:50 -080044 *
45 * @deprecated New tests should be written using the
46 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 */
Stephan Linznerb51617f2016-01-27 18:09:50 -080048@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049public class RenamingDelegatingContext extends ContextWrapper {
50
51 private Context mFileContext;
52 private String mFilePrefix = null;
Andrew Stadler54a16f02009-07-23 20:00:08 -070053 private File mCacheDir;
54 private final Object mSync = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Paul Duffin8c5a24d2017-05-10 13:30:16 +010056 private Set<String> mDatabaseNames = new HashSet<>();
57 private Set<String> mFileNames = new HashSet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
59 public static <T extends ContentProvider> T providerWithRenamedContext(
60 Class<T> contentProvider, Context c, String filePrefix)
61 throws IllegalAccessException, InstantiationException {
62 return providerWithRenamedContext(contentProvider, c, filePrefix, false);
63 }
64
65 public static <T extends ContentProvider> T providerWithRenamedContext(
66 Class<T> contentProvider, Context c, String filePrefix,
67 boolean allowAccessToExistingFilesAndDbs)
68 throws IllegalAccessException, InstantiationException {
69 Class<T> mProviderClass = contentProvider;
70 T mProvider = mProviderClass.newInstance();
71 RenamingDelegatingContext mContext = new RenamingDelegatingContext(c, filePrefix);
72 if (allowAccessToExistingFilesAndDbs) {
73 mContext.makeExistingFilesAndDbsAccessible();
74 }
Paul Duffin20af1df2018-01-05 13:52:17 +000075 MockContentProvider.attachInfoForTesting(mProvider, mContext, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 return mProvider;
77 }
78
79 /**
80 * Makes accessible all files and databases whose names match the filePrefix that was passed to
81 * the constructor. Normally only files and databases that were created through this context are
82 * accessible.
83 */
84 public void makeExistingFilesAndDbsAccessible() {
85 String[] databaseList = mFileContext.databaseList();
86 for (String diskName : databaseList) {
87 if (shouldDiskNameBeVisible(diskName)) {
88 mDatabaseNames.add(publicNameFromDiskName(diskName));
89 }
90 }
91 String[] fileList = mFileContext.fileList();
92 for (String diskName : fileList) {
93 if (shouldDiskNameBeVisible(diskName)) {
94 mFileNames.add(publicNameFromDiskName(diskName));
95 }
96 }
97 }
98
99 /**
100 * Returns if the given diskName starts with the given prefix or not.
101 * @param diskName name of the database/file.
102 */
103 boolean shouldDiskNameBeVisible(String diskName) {
104 return diskName.startsWith(mFilePrefix);
105 }
106
107 /**
108 * Returns the public name (everything following the prefix) of the given diskName.
109 * @param diskName name of the database/file.
110 */
111 String publicNameFromDiskName(String diskName) {
112 if (!shouldDiskNameBeVisible(diskName)) {
113 throw new IllegalArgumentException("disk file should not be visible: " + diskName);
114 }
115 return diskName.substring(mFilePrefix.length(), diskName.length());
116 }
117
118 /**
Evan Charlton73fca8e2015-03-02 07:07:05 -0800119 * @param context : the context that will be delegated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 * @param filePrefix : a prefix with which database and file names will be
121 * prefixed.
122 */
123 public RenamingDelegatingContext(Context context, String filePrefix) {
124 super(context);
125 mFileContext = context;
126 mFilePrefix = filePrefix;
127 }
128
129 /**
Evan Charlton73fca8e2015-03-02 07:07:05 -0800130 * @param context : the context that will be delegated.
131 * @param fileContext : the context that file and db methods will be delegated to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * @param filePrefix : a prefix with which database and file names will be
133 * prefixed.
134 */
135 public RenamingDelegatingContext(Context context, Context fileContext, String filePrefix) {
136 super(context);
137 mFileContext = fileContext;
138 mFilePrefix = filePrefix;
139 }
140
141 public String getDatabasePrefix() {
142 return mFilePrefix;
143 }
144
145 private String renamedFileName(String name) {
146 return mFilePrefix + name;
147 }
148
149 @Override
150 public SQLiteDatabase openOrCreateDatabase(String name,
151 int mode, SQLiteDatabase.CursorFactory factory) {
152 final String internalName = renamedFileName(name);
153 if (!mDatabaseNames.contains(name)) {
154 mDatabaseNames.add(name);
155 mFileContext.deleteDatabase(internalName);
156 }
157 return mFileContext.openOrCreateDatabase(internalName, mode, factory);
158 }
159
160 @Override
Makoto Onukif6b979a2010-06-04 16:12:15 -0700161 public SQLiteDatabase openOrCreateDatabase(String name,
162 int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) {
163 final String internalName = renamedFileName(name);
164 if (!mDatabaseNames.contains(name)) {
165 mDatabaseNames.add(name);
166 mFileContext.deleteDatabase(internalName);
167 }
168 return mFileContext.openOrCreateDatabase(internalName, mode, factory, errorHandler);
169 }
170
171 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 public boolean deleteDatabase(String name) {
173 if (mDatabaseNames.contains(name)) {
174 mDatabaseNames.remove(name);
175 return mFileContext.deleteDatabase(renamedFileName(name));
176 } else {
177 return false;
178 }
179 }
Stephan Linznerb51617f2016-01-27 18:09:50 -0800180
Andrew Stadlerb09296d2009-06-24 22:32:40 -0700181 @Override
182 public File getDatabasePath(String name) {
183 return mFileContext.getDatabasePath(renamedFileName(name));
184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
186 @Override
187 public String[] databaseList() {
188 return mDatabaseNames.toArray(new String[]{});
189 }
190
191 @Override
192 public FileInputStream openFileInput(String name)
193 throws FileNotFoundException {
194 final String internalName = renamedFileName(name);
195 if (mFileNames.contains(name)) {
196 return mFileContext.openFileInput(internalName);
197 } else {
198 throw new FileNotFoundException(internalName);
199 }
200 }
201
202 @Override
203 public FileOutputStream openFileOutput(String name, int mode)
204 throws FileNotFoundException {
205 mFileNames.add(name);
206 return mFileContext.openFileOutput(renamedFileName(name), mode);
207 }
208
209 @Override
210 public File getFileStreamPath(String name) {
211 return mFileContext.getFileStreamPath(renamedFileName(name));
212 }
213
214 @Override
215 public boolean deleteFile(String name) {
216 if (mFileNames.contains(name)) {
217 mFileNames.remove(name);
218 return mFileContext.deleteFile(renamedFileName(name));
219 } else {
220 return false;
221 }
222 }
223
224 @Override
225 public String[] fileList() {
226 return mFileNames.toArray(new String[]{});
227 }
Stephan Linznerb51617f2016-01-27 18:09:50 -0800228
Andrew Stadler54a16f02009-07-23 20:00:08 -0700229 /**
230 * In order to support calls to getCacheDir(), we create a temp cache dir (inside the real
231 * one) and return it instead. This code is basically getCacheDir(), except it uses the real
232 * cache dir as the parent directory and creates a test cache dir inside that.
233 */
234 @Override
235 public File getCacheDir() {
236 synchronized (mSync) {
237 if (mCacheDir == null) {
238 mCacheDir = new File(mFileContext.getCacheDir(), renamedFileName("cache"));
239 }
240 if (!mCacheDir.exists()) {
241 if(!mCacheDir.mkdirs()) {
242 Log.w("RenamingDelegatingContext", "Unable to create cache directory");
243 return null;
244 }
Paul Duffin2231eb42017-05-10 15:05:24 +0100245 try {
246 // Give the directory all possible permissions.
247 Files.setPosixFilePermissions(mCacheDir.toPath(),
248 EnumSet.allOf(PosixFilePermission.class));
249 } catch (IOException e) {
250 Log.e("RenamingDelegatingContext",
251 "Could not set permissions of test cacheDir", e);
252 }
Andrew Stadler54a16f02009-07-23 20:00:08 -0700253 }
254 }
255 return mCacheDir;
256 }
Kenny Root10362ab2010-03-12 11:13:50 -0800257}