| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 1 | /* |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 15 | package android.testing; |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 16 | |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 17 | import android.content.BroadcastReceiver; |
| 18 | import android.content.ComponentCallbacks; |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 19 | import android.content.ComponentName; |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 20 | import android.content.ContentProviderClient; |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.ContextWrapper; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | import android.content.ServiceConnection; |
| Jason Monk | 26bc899 | 2017-01-04 14:17:47 -0500 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 27 | import android.content.res.Resources; |
| Jason Monk | d01cef9 | 2017-12-08 09:48:25 -0500 | [diff] [blame] | 28 | import android.net.Uri; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 29 | import android.os.Handler; |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 30 | import android.os.IBinder; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 31 | import android.os.UserHandle; |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 32 | import android.provider.Settings; |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 33 | import android.util.ArrayMap; |
| Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 34 | import android.view.LayoutInflater; |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 35 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 36 | import org.junit.rules.TestRule; |
| 37 | import org.junit.rules.TestWatcher; |
| 38 | import org.junit.runner.Description; |
| 39 | import org.junit.runners.model.Statement; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 40 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 41 | /** |
| 42 | * A ContextWrapper with utilities specifically designed to make Testing easier. |
| 43 | * |
| 44 | * <ul> |
| 45 | * <li>System services can be mocked out with {@link #addMockSystemService}</li> |
| 46 | * <li>Service binding can be mocked out with {@link #addMockService}</li> |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 47 | * <li>Resources can be mocked out using {@link #getOrCreateTestableResources()}</li> |
| Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 48 | * <li>Settings support {@link TestableSettingsProvider}</li> |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 49 | * <li>Has support for {@link LeakCheck} for services and receivers</li> |
| 50 | * </ul> |
| 51 | * |
| 52 | * <p>TestableContext should be defined as a rule on your test so it can clean up after itself. |
| 53 | * Like the following:</p> |
| 54 | * <pre class="prettyprint"> |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 55 | * @Rule |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 56 | * private final TestableContext mContext = new TestableContext(InstrumentationRegister.getContext()); |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 57 | * </pre> |
| 58 | */ |
| 59 | public class TestableContext extends ContextWrapper implements TestRule { |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 60 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 61 | private final TestableContentResolver mTestableContentResolver; |
| Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 62 | private final TestableSettingsProvider mSettingsProvider; |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 63 | |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 64 | private ArrayMap<String, Object> mMockSystemServices; |
| 65 | private ArrayMap<ComponentName, IBinder> mMockServices; |
| 66 | private ArrayMap<ServiceConnection, ComponentName> mActiveServices; |
| 67 | |
| Jason Monk | 26bc899 | 2017-01-04 14:17:47 -0500 | [diff] [blame] | 68 | private PackageManager mMockPackageManager; |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 69 | private LeakCheck.Tracker mReceiver; |
| 70 | private LeakCheck.Tracker mService; |
| 71 | private LeakCheck.Tracker mComponent; |
| Jason Monk | 77f1b05 | 2017-05-02 14:22:21 -0400 | [diff] [blame] | 72 | private TestableResources mTestableResources; |
| Jason Monk | d01cef9 | 2017-12-08 09:48:25 -0500 | [diff] [blame] | 73 | private TestablePermissions mTestablePermissions; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 74 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 75 | public TestableContext(Context base) { |
| 76 | this(base, null); |
| 77 | } |
| 78 | |
| 79 | public TestableContext(Context base, LeakCheck check) { |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 80 | super(base); |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 81 | mTestableContentResolver = new TestableContentResolver(base); |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 82 | ContentProviderClient settings = base.getContentResolver() |
| 83 | .acquireContentProviderClient(Settings.AUTHORITY); |
| Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 84 | mSettingsProvider = TestableSettingsProvider.getFakeSettingsProvider(settings); |
| 85 | mTestableContentResolver.addProvider(Settings.AUTHORITY, mSettingsProvider); |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 86 | mReceiver = check != null ? check.getTracker("receiver") : null; |
| 87 | mService = check != null ? check.getTracker("service") : null; |
| 88 | mComponent = check != null ? check.getTracker("component") : null; |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| Jason Monk | 26bc899 | 2017-01-04 14:17:47 -0500 | [diff] [blame] | 91 | public void setMockPackageManager(PackageManager mock) { |
| 92 | mMockPackageManager = mock; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public PackageManager getPackageManager() { |
| 97 | if (mMockPackageManager != null) { |
| 98 | return mMockPackageManager; |
| 99 | } |
| 100 | return super.getPackageManager(); |
| 101 | } |
| 102 | |
| Jason Monk | 77f1b05 | 2017-05-02 14:22:21 -0400 | [diff] [blame] | 103 | /** |
| 104 | * Makes sure the resources being returned by this TestableContext are a version of |
| 105 | * TestableResources. |
| 106 | * @see #getResources() |
| 107 | */ |
| 108 | public void ensureTestableResources() { |
| 109 | if (mTestableResources == null) { |
| 110 | mTestableResources = new TestableResources(super.getResources()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get (and create if necessary) {@link TestableResources} for this TestableContext. |
| 116 | */ |
| 117 | public TestableResources getOrCreateTestableResources() { |
| 118 | ensureTestableResources(); |
| 119 | return mTestableResources; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns a Resources instance for the test. |
| 124 | * |
| 125 | * By default this returns the same resources object that would come from the |
| 126 | * {@link ContextWrapper}, but if {@link #ensureTestableResources()} or |
| 127 | * {@link #getOrCreateTestableResources()} has been called, it will return resources gotten from |
| 128 | * {@link TestableResources}. |
| 129 | */ |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 130 | @Override |
| 131 | public Resources getResources() { |
| Jason Monk | 77f1b05 | 2017-05-02 14:22:21 -0400 | [diff] [blame] | 132 | return mTestableResources != null ? mTestableResources.getResources() |
| 133 | : super.getResources(); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 136 | /** |
| 137 | * @see #getSystemService(String) |
| 138 | */ |
| Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 139 | public <T> void addMockSystemService(Class<T> service, T mock) { |
| 140 | addMockSystemService(getSystemServiceName(service), mock); |
| 141 | } |
| 142 | |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 143 | /** |
| 144 | * @see #getSystemService(String) |
| 145 | */ |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 146 | public void addMockSystemService(String name, Object service) { |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 147 | if (mMockSystemServices == null) mMockSystemServices = new ArrayMap<>(); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 148 | mMockSystemServices.put(name, service); |
| 149 | } |
| 150 | |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 151 | /** |
| 152 | * If a matching mock service has been added through {@link #addMockSystemService} then |
| 153 | * that will be returned, otherwise the real service will be acquired from the base |
| 154 | * context. |
| 155 | */ |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 156 | @Override |
| 157 | public Object getSystemService(String name) { |
| 158 | if (mMockSystemServices != null && mMockSystemServices.containsKey(name)) { |
| 159 | return mMockSystemServices.get(name); |
| 160 | } |
| Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 161 | if (name.equals(LAYOUT_INFLATER_SERVICE)) { |
| 162 | return getBaseContext().getSystemService(LayoutInflater.class).cloneInContext(this); |
| 163 | } |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 164 | return super.getSystemService(name); |
| 165 | } |
| 166 | |
| Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 167 | TestableSettingsProvider getSettingsProvider() { |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 168 | return mSettingsProvider; |
| 169 | } |
| 170 | |
| 171 | @Override |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 172 | public TestableContentResolver getContentResolver() { |
| 173 | return mTestableContentResolver; |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 174 | } |
| 175 | |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 176 | /** |
| 177 | * Will always return itself for a TestableContext to ensure the testable effects extend |
| 178 | * to the application context. |
| 179 | */ |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 180 | @Override |
| 181 | public Context getApplicationContext() { |
| 182 | // Return this so its always a TestableContext. |
| 183 | return this; |
| 184 | } |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 185 | |
| 186 | @Override |
| 187 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 188 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 189 | return super.registerReceiver(receiver, filter); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 194 | String broadcastPermission, Handler scheduler) { |
| 195 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 196 | return super.registerReceiver(receiver, filter, broadcastPermission, scheduler); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 201 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
| 202 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 203 | return super.registerReceiverAsUser(receiver, user, filter, broadcastPermission, |
| 204 | scheduler); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 209 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).clearAllocations(); |
| 210 | super.unregisterReceiver(receiver); |
| 211 | } |
| 212 | |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 213 | /** |
| 214 | * Adds a mock service to be connected to by a bindService call. |
| 215 | * <p> |
| 216 | * Normally a TestableContext will pass through all bind requests to the base context |
| 217 | * but when addMockService has been called for a ComponentName being bound, then |
| 218 | * TestableContext will immediately trigger a {@link ServiceConnection#onServiceConnected} |
| 219 | * with the specified service, and will call {@link ServiceConnection#onServiceDisconnected} |
| 220 | * when the service is unbound. |
| 221 | * </p> |
| 222 | */ |
| 223 | public void addMockService(ComponentName component, IBinder service) { |
| 224 | if (mMockServices == null) mMockServices = new ArrayMap<>(); |
| 225 | mMockServices.put(component, service); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @see #addMockService(ComponentName, IBinder) |
| 230 | */ |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 231 | @Override |
| 232 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 233 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 234 | if (checkMocks(service.getComponent(), conn)) return true; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 235 | return super.bindService(service, conn, flags); |
| 236 | } |
| 237 | |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 238 | /** |
| 239 | * @see #addMockService(ComponentName, IBinder) |
| 240 | */ |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 241 | @Override |
| 242 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 243 | Handler handler, UserHandle user) { |
| 244 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 245 | if (checkMocks(service.getComponent(), conn)) return true; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 246 | return super.bindServiceAsUser(service, conn, flags, handler, user); |
| 247 | } |
| 248 | |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 249 | /** |
| 250 | * @see #addMockService(ComponentName, IBinder) |
| 251 | */ |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 252 | @Override |
| 253 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 254 | UserHandle user) { |
| 255 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 256 | if (checkMocks(service.getComponent(), conn)) return true; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 257 | return super.bindServiceAsUser(service, conn, flags, user); |
| 258 | } |
| 259 | |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 260 | private boolean checkMocks(ComponentName component, ServiceConnection conn) { |
| 261 | if (mMockServices != null && component != null && mMockServices.containsKey(component)) { |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 262 | if (mActiveServices == null) mActiveServices = new ArrayMap<>(); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 263 | mActiveServices.put(conn, component); |
| 264 | conn.onServiceConnected(component, mMockServices.get(component)); |
| 265 | return true; |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 270 | /** |
| 271 | * @see #addMockService(ComponentName, IBinder) |
| 272 | */ |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 273 | @Override |
| 274 | public void unbindService(ServiceConnection conn) { |
| 275 | if (mService != null) mService.getLeakInfo(conn).clearAllocations(); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 276 | if (mActiveServices != null && mActiveServices.containsKey(conn)) { |
| 277 | conn.onServiceDisconnected(mActiveServices.get(conn)); |
| 278 | mActiveServices.remove(conn); |
| 279 | return; |
| 280 | } |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 281 | super.unbindService(conn); |
| 282 | } |
| 283 | |
| Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 284 | /** |
| 285 | * Check if the TestableContext has a mock binding for a specified component. Will return |
| 286 | * true between {@link ServiceConnection#onServiceConnected} and |
| 287 | * {@link ServiceConnection#onServiceDisconnected} callbacks for a mock service. |
| 288 | * |
| 289 | * @see #addMockService(ComponentName, IBinder) |
| 290 | */ |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 291 | public boolean isBound(ComponentName component) { |
| 292 | return mActiveServices != null && mActiveServices.containsValue(component); |
| 293 | } |
| 294 | |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 295 | @Override |
| 296 | public void registerComponentCallbacks(ComponentCallbacks callback) { |
| 297 | if (mComponent != null) mComponent.getLeakInfo(callback).addAllocation(new Throwable()); |
| 298 | super.registerComponentCallbacks(callback); |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public void unregisterComponentCallbacks(ComponentCallbacks callback) { |
| 303 | if (mComponent != null) mComponent.getLeakInfo(callback).clearAllocations(); |
| 304 | super.unregisterComponentCallbacks(callback); |
| 305 | } |
| Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 306 | |
| Jason Monk | d01cef9 | 2017-12-08 09:48:25 -0500 | [diff] [blame] | 307 | public TestablePermissions getTestablePermissions() { |
| 308 | if (mTestablePermissions == null) { |
| 309 | mTestablePermissions = new TestablePermissions(); |
| 310 | } |
| 311 | return mTestablePermissions; |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public int checkCallingOrSelfPermission(String permission) { |
| 316 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 317 | return mTestablePermissions.check(permission); |
| 318 | } |
| 319 | return super.checkCallingOrSelfPermission(permission); |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public int checkCallingPermission(String permission) { |
| 324 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 325 | return mTestablePermissions.check(permission); |
| 326 | } |
| 327 | return super.checkCallingPermission(permission); |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public int checkPermission(String permission, int pid, int uid) { |
| 332 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 333 | return mTestablePermissions.check(permission); |
| 334 | } |
| 335 | return super.checkPermission(permission, pid, uid); |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public int checkPermission(String permission, int pid, int uid, IBinder callerToken) { |
| 340 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 341 | return mTestablePermissions.check(permission); |
| 342 | } |
| 343 | return super.checkPermission(permission, pid, uid, callerToken); |
| 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public int checkSelfPermission(String permission) { |
| 348 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 349 | return mTestablePermissions.check(permission); |
| 350 | } |
| 351 | return super.checkSelfPermission(permission); |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void enforceCallingOrSelfPermission(String permission, String message) { |
| 356 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 357 | mTestablePermissions.enforce(permission); |
| 358 | } else { |
| 359 | super.enforceCallingOrSelfPermission(permission, message); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public void enforceCallingPermission(String permission, String message) { |
| 365 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 366 | mTestablePermissions.enforce(permission); |
| 367 | } else { |
| 368 | super.enforceCallingPermission(permission, message); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public void enforcePermission(String permission, int pid, int uid, String message) { |
| 374 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 375 | mTestablePermissions.enforce(permission); |
| 376 | } else { |
| 377 | super.enforcePermission(permission, pid, uid, message); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { |
| 383 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 384 | return mTestablePermissions.check(uri, modeFlags); |
| 385 | } |
| 386 | return super.checkCallingOrSelfUriPermission(uri, modeFlags); |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public int checkCallingUriPermission(Uri uri, int modeFlags) { |
| 391 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 392 | return mTestablePermissions.check(uri, modeFlags); |
| 393 | } |
| 394 | return super.checkCallingUriPermission(uri, modeFlags); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public void enforceCallingOrSelfUriPermission(Uri uri, int modeFlags, String message) { |
| 399 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 400 | mTestablePermissions.enforce(uri, modeFlags); |
| 401 | } else { |
| 402 | super.enforceCallingOrSelfUriPermission(uri, modeFlags, message); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
| 408 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 409 | return mTestablePermissions.check(uri, modeFlags); |
| 410 | } |
| 411 | return super.checkUriPermission(uri, pid, uid, modeFlags); |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) { |
| 416 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 417 | return mTestablePermissions.check(uri, modeFlags); |
| 418 | } |
| 419 | return super.checkUriPermission(uri, pid, uid, modeFlags, callerToken); |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public int checkUriPermission(Uri uri, String readPermission, String writePermission, int pid, |
| 424 | int uid, int modeFlags) { |
| 425 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 426 | return mTestablePermissions.check(uri, modeFlags); |
| 427 | } |
| 428 | return super.checkUriPermission(uri, readPermission, writePermission, pid, uid, modeFlags); |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public void enforceCallingUriPermission(Uri uri, int modeFlags, String message) { |
| 433 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 434 | mTestablePermissions.enforce(uri, modeFlags); |
| 435 | } else { |
| 436 | super.enforceCallingUriPermission(uri, modeFlags, message); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public void enforceUriPermission(Uri uri, int pid, int uid, int modeFlags, String message) { |
| 442 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 443 | mTestablePermissions.enforce(uri, modeFlags); |
| 444 | } else { |
| 445 | super.enforceUriPermission(uri, pid, uid, modeFlags, message); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public void enforceUriPermission(Uri uri, String readPermission, String writePermission, |
| 451 | int pid, int uid, int modeFlags, String message) { |
| 452 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 453 | mTestablePermissions.enforce(uri, modeFlags); |
| 454 | } else { |
| 455 | super.enforceUriPermission(uri, readPermission, writePermission, pid, uid, modeFlags, |
| 456 | message); |
| 457 | } |
| 458 | } |
| 459 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 460 | @Override |
| 461 | public Statement apply(Statement base, Description description) { |
| 462 | return new TestWatcher() { |
| 463 | @Override |
| 464 | protected void succeeded(Description description) { |
| Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 465 | mSettingsProvider.clearValuesAndCheck(TestableContext.this); |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 466 | } |
| Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 467 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 468 | @Override |
| 469 | protected void failed(Throwable e, Description description) { |
| Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 470 | mSettingsProvider.clearValuesAndCheck(TestableContext.this); |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 471 | } |
| 472 | }.apply(base, description); |
| Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 473 | } |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 474 | } |