Kenny Root | 10362ab | 2010-03-12 11:13:50 -0800 | [diff] [blame] | 1 | /* |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | package android.test; |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.ContextWrapper; |
| 21 | import android.content.ContentProvider; |
Makoto Onuki | f6b979a | 2010-06-04 16:12:15 -0700 | [diff] [blame] | 22 | import android.database.DatabaseErrorHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.database.sqlite.SQLiteDatabase; |
Paul Duffin | 20af1df | 2018-01-05 13:52:17 +0000 | [diff] [blame] | 24 | import android.test.mock.MockContentProvider; |
Andrew Stadler | 54a16f0 | 2009-07-23 20:00:08 -0700 | [diff] [blame] | 25 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | import java.io.File; |
| 28 | import java.io.FileInputStream; |
| 29 | import java.io.FileNotFoundException; |
| 30 | import java.io.FileOutputStream; |
Paul Duffin | 2231eb4 | 2017-05-10 15:05:24 +0100 | [diff] [blame] | 31 | import java.io.IOException; |
| 32 | import java.nio.file.Files; |
| 33 | import java.nio.file.Paths; |
| 34 | import java.nio.file.attribute.PosixFilePermission; |
| 35 | import java.nio.file.attribute.PosixFilePermissions; |
| 36 | import java.util.EnumSet; |
Paul Duffin | 8c5a24d | 2017-05-10 13:30:16 +0100 | [diff] [blame] | 37 | import java.util.HashSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import 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 Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 44 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | */ |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 48 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | public class RenamingDelegatingContext extends ContextWrapper { |
| 50 | |
| 51 | private Context mFileContext; |
| 52 | private String mFilePrefix = null; |
Andrew Stadler | 54a16f0 | 2009-07-23 20:00:08 -0700 | [diff] [blame] | 53 | private File mCacheDir; |
| 54 | private final Object mSync = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
Paul Duffin | 8c5a24d | 2017-05-10 13:30:16 +0100 | [diff] [blame] | 56 | private Set<String> mDatabaseNames = new HashSet<>(); |
| 57 | private Set<String> mFileNames = new HashSet<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
| 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 Duffin | 20af1df | 2018-01-05 13:52:17 +0000 | [diff] [blame] | 75 | MockContentProvider.attachInfoForTesting(mProvider, mContext, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | 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 Charlton | 73fca8e | 2015-03-02 07:07:05 -0800 | [diff] [blame] | 119 | * @param context : the context that will be delegated. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | * @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 Charlton | 73fca8e | 2015-03-02 07:07:05 -0800 | [diff] [blame] | 130 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | * @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 Onuki | f6b979a | 2010-06-04 16:12:15 -0700 | [diff] [blame] | 161 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | 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 Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 180 | |
Andrew Stadler | b09296d | 2009-06-24 22:32:40 -0700 | [diff] [blame] | 181 | @Override |
| 182 | public File getDatabasePath(String name) { |
| 183 | return mFileContext.getDatabasePath(renamedFileName(name)); |
| 184 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | |
| 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 Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 228 | |
Andrew Stadler | 54a16f0 | 2009-07-23 20:00:08 -0700 | [diff] [blame] | 229 | /** |
| 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 Duffin | 2231eb4 | 2017-05-10 15:05:24 +0100 | [diff] [blame] | 245 | 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 Stadler | 54a16f0 | 2009-07-23 20:00:08 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | return mCacheDir; |
| 256 | } |
Kenny Root | 10362ab | 2010-03-12 11:13:50 -0800 | [diff] [blame] | 257 | } |