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 |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 56 | * public 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 | 1fc931a | 2017-12-14 13:22:58 -0500 | [diff] [blame] | 86 | mSettingsProvider.clearValuesAndCheck(TestableContext.this); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 87 | mReceiver = check != null ? check.getTracker("receiver") : null; |
| 88 | mService = check != null ? check.getTracker("service") : null; |
| 89 | mComponent = check != null ? check.getTracker("component") : null; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Jason Monk | 26bc899 | 2017-01-04 14:17:47 -0500 | [diff] [blame] | 92 | public void setMockPackageManager(PackageManager mock) { |
| 93 | mMockPackageManager = mock; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public PackageManager getPackageManager() { |
| 98 | if (mMockPackageManager != null) { |
| 99 | return mMockPackageManager; |
| 100 | } |
| 101 | return super.getPackageManager(); |
| 102 | } |
| 103 | |
Jason Monk | 77f1b05 | 2017-05-02 14:22:21 -0400 | [diff] [blame] | 104 | /** |
| 105 | * Makes sure the resources being returned by this TestableContext are a version of |
| 106 | * TestableResources. |
| 107 | * @see #getResources() |
| 108 | */ |
| 109 | public void ensureTestableResources() { |
| 110 | if (mTestableResources == null) { |
| 111 | mTestableResources = new TestableResources(super.getResources()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get (and create if necessary) {@link TestableResources} for this TestableContext. |
| 117 | */ |
| 118 | public TestableResources getOrCreateTestableResources() { |
| 119 | ensureTestableResources(); |
| 120 | return mTestableResources; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns a Resources instance for the test. |
| 125 | * |
| 126 | * By default this returns the same resources object that would come from the |
| 127 | * {@link ContextWrapper}, but if {@link #ensureTestableResources()} or |
| 128 | * {@link #getOrCreateTestableResources()} has been called, it will return resources gotten from |
| 129 | * {@link TestableResources}. |
| 130 | */ |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 131 | @Override |
| 132 | public Resources getResources() { |
Jason Monk | 77f1b05 | 2017-05-02 14:22:21 -0400 | [diff] [blame] | 133 | return mTestableResources != null ? mTestableResources.getResources() |
| 134 | : super.getResources(); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 137 | /** |
| 138 | * @see #getSystemService(String) |
| 139 | */ |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 140 | public <T> void addMockSystemService(Class<T> service, T mock) { |
| 141 | addMockSystemService(getSystemServiceName(service), mock); |
| 142 | } |
| 143 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 144 | /** |
| 145 | * @see #getSystemService(String) |
| 146 | */ |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 147 | public void addMockSystemService(String name, Object service) { |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 148 | if (mMockSystemServices == null) mMockSystemServices = new ArrayMap<>(); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 149 | mMockSystemServices.put(name, service); |
| 150 | } |
| 151 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 152 | /** |
| 153 | * If a matching mock service has been added through {@link #addMockSystemService} then |
| 154 | * that will be returned, otherwise the real service will be acquired from the base |
| 155 | * context. |
| 156 | */ |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 157 | @Override |
| 158 | public Object getSystemService(String name) { |
| 159 | if (mMockSystemServices != null && mMockSystemServices.containsKey(name)) { |
| 160 | return mMockSystemServices.get(name); |
| 161 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 162 | if (name.equals(LAYOUT_INFLATER_SERVICE)) { |
| 163 | return getBaseContext().getSystemService(LayoutInflater.class).cloneInContext(this); |
| 164 | } |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 165 | return super.getSystemService(name); |
| 166 | } |
| 167 | |
Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 168 | TestableSettingsProvider getSettingsProvider() { |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 169 | return mSettingsProvider; |
| 170 | } |
| 171 | |
| 172 | @Override |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 173 | public TestableContentResolver getContentResolver() { |
| 174 | return mTestableContentResolver; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 177 | /** |
| 178 | * Will always return itself for a TestableContext to ensure the testable effects extend |
| 179 | * to the application context. |
| 180 | */ |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 181 | @Override |
| 182 | public Context getApplicationContext() { |
| 183 | // Return this so its always a TestableContext. |
| 184 | return this; |
| 185 | } |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 186 | |
| 187 | @Override |
| 188 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 189 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 190 | return super.registerReceiver(receiver, filter); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 195 | String broadcastPermission, Handler scheduler) { |
| 196 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 197 | return super.registerReceiver(receiver, filter, broadcastPermission, scheduler); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 202 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
| 203 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 204 | return super.registerReceiverAsUser(receiver, user, filter, broadcastPermission, |
| 205 | scheduler); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 210 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).clearAllocations(); |
| 211 | super.unregisterReceiver(receiver); |
| 212 | } |
| 213 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 214 | /** |
| 215 | * Adds a mock service to be connected to by a bindService call. |
| 216 | * <p> |
| 217 | * Normally a TestableContext will pass through all bind requests to the base context |
| 218 | * but when addMockService has been called for a ComponentName being bound, then |
| 219 | * TestableContext will immediately trigger a {@link ServiceConnection#onServiceConnected} |
| 220 | * with the specified service, and will call {@link ServiceConnection#onServiceDisconnected} |
| 221 | * when the service is unbound. |
| 222 | * </p> |
| 223 | */ |
| 224 | public void addMockService(ComponentName component, IBinder service) { |
| 225 | if (mMockServices == null) mMockServices = new ArrayMap<>(); |
| 226 | mMockServices.put(component, service); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @see #addMockService(ComponentName, IBinder) |
| 231 | */ |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 232 | @Override |
| 233 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 234 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 235 | if (checkMocks(service.getComponent(), conn)) return true; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 236 | return super.bindService(service, conn, flags); |
| 237 | } |
| 238 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 239 | /** |
| 240 | * @see #addMockService(ComponentName, IBinder) |
| 241 | */ |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 242 | @Override |
| 243 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 244 | Handler handler, UserHandle user) { |
| 245 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 246 | if (checkMocks(service.getComponent(), conn)) return true; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 247 | return super.bindServiceAsUser(service, conn, flags, handler, user); |
| 248 | } |
| 249 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 250 | /** |
| 251 | * @see #addMockService(ComponentName, IBinder) |
| 252 | */ |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 253 | @Override |
| 254 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 255 | UserHandle user) { |
| 256 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 257 | if (checkMocks(service.getComponent(), conn)) return true; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 258 | return super.bindServiceAsUser(service, conn, flags, user); |
| 259 | } |
| 260 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 261 | private boolean checkMocks(ComponentName component, ServiceConnection conn) { |
| 262 | if (mMockServices != null && component != null && mMockServices.containsKey(component)) { |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 263 | if (mActiveServices == null) mActiveServices = new ArrayMap<>(); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 264 | mActiveServices.put(conn, component); |
| 265 | conn.onServiceConnected(component, mMockServices.get(component)); |
| 266 | return true; |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 271 | /** |
| 272 | * @see #addMockService(ComponentName, IBinder) |
| 273 | */ |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 274 | @Override |
| 275 | public void unbindService(ServiceConnection conn) { |
| 276 | if (mService != null) mService.getLeakInfo(conn).clearAllocations(); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 277 | if (mActiveServices != null && mActiveServices.containsKey(conn)) { |
| 278 | conn.onServiceDisconnected(mActiveServices.get(conn)); |
| 279 | mActiveServices.remove(conn); |
| 280 | return; |
| 281 | } |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 282 | super.unbindService(conn); |
| 283 | } |
| 284 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 285 | /** |
| 286 | * Check if the TestableContext has a mock binding for a specified component. Will return |
| 287 | * true between {@link ServiceConnection#onServiceConnected} and |
| 288 | * {@link ServiceConnection#onServiceDisconnected} callbacks for a mock service. |
| 289 | * |
| 290 | * @see #addMockService(ComponentName, IBinder) |
| 291 | */ |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 292 | public boolean isBound(ComponentName component) { |
| 293 | return mActiveServices != null && mActiveServices.containsValue(component); |
| 294 | } |
| 295 | |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 296 | @Override |
| 297 | public void registerComponentCallbacks(ComponentCallbacks callback) { |
| 298 | if (mComponent != null) mComponent.getLeakInfo(callback).addAllocation(new Throwable()); |
Adrian Roos | 11dfd27 | 2019-03-25 19:21:26 +0100 | [diff] [blame] | 299 | getBaseContext().registerComponentCallbacks(callback); |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void unregisterComponentCallbacks(ComponentCallbacks callback) { |
| 304 | if (mComponent != null) mComponent.getLeakInfo(callback).clearAllocations(); |
Adrian Roos | 11dfd27 | 2019-03-25 19:21:26 +0100 | [diff] [blame] | 305 | getBaseContext().unregisterComponentCallbacks(callback); |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 306 | } |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 307 | |
Jason Monk | d01cef9 | 2017-12-08 09:48:25 -0500 | [diff] [blame] | 308 | public TestablePermissions getTestablePermissions() { |
| 309 | if (mTestablePermissions == null) { |
| 310 | mTestablePermissions = new TestablePermissions(); |
| 311 | } |
| 312 | return mTestablePermissions; |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public int checkCallingOrSelfPermission(String permission) { |
| 317 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 318 | return mTestablePermissions.check(permission); |
| 319 | } |
| 320 | return super.checkCallingOrSelfPermission(permission); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public int checkCallingPermission(String permission) { |
| 325 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 326 | return mTestablePermissions.check(permission); |
| 327 | } |
| 328 | return super.checkCallingPermission(permission); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public int checkPermission(String permission, int pid, int uid) { |
| 333 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 334 | return mTestablePermissions.check(permission); |
| 335 | } |
| 336 | return super.checkPermission(permission, pid, uid); |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public int checkPermission(String permission, int pid, int uid, IBinder callerToken) { |
| 341 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 342 | return mTestablePermissions.check(permission); |
| 343 | } |
| 344 | return super.checkPermission(permission, pid, uid, callerToken); |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public int checkSelfPermission(String permission) { |
| 349 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 350 | return mTestablePermissions.check(permission); |
| 351 | } |
| 352 | return super.checkSelfPermission(permission); |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public void enforceCallingOrSelfPermission(String permission, String message) { |
| 357 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 358 | mTestablePermissions.enforce(permission); |
| 359 | } else { |
| 360 | super.enforceCallingOrSelfPermission(permission, message); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public void enforceCallingPermission(String permission, String message) { |
| 366 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 367 | mTestablePermissions.enforce(permission); |
| 368 | } else { |
| 369 | super.enforceCallingPermission(permission, message); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public void enforcePermission(String permission, int pid, int uid, String message) { |
| 375 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(permission)) { |
| 376 | mTestablePermissions.enforce(permission); |
| 377 | } else { |
| 378 | super.enforcePermission(permission, pid, uid, message); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { |
| 384 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 385 | return mTestablePermissions.check(uri, modeFlags); |
| 386 | } |
| 387 | return super.checkCallingOrSelfUriPermission(uri, modeFlags); |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public int checkCallingUriPermission(Uri uri, int modeFlags) { |
| 392 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 393 | return mTestablePermissions.check(uri, modeFlags); |
| 394 | } |
| 395 | return super.checkCallingUriPermission(uri, modeFlags); |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public void enforceCallingOrSelfUriPermission(Uri uri, int modeFlags, String message) { |
| 400 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 401 | mTestablePermissions.enforce(uri, modeFlags); |
| 402 | } else { |
| 403 | super.enforceCallingOrSelfUriPermission(uri, modeFlags, message); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
| 409 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 410 | return mTestablePermissions.check(uri, modeFlags); |
| 411 | } |
| 412 | return super.checkUriPermission(uri, pid, uid, modeFlags); |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) { |
| 417 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 418 | return mTestablePermissions.check(uri, modeFlags); |
| 419 | } |
| 420 | return super.checkUriPermission(uri, pid, uid, modeFlags, callerToken); |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public int checkUriPermission(Uri uri, String readPermission, String writePermission, int pid, |
| 425 | int uid, int modeFlags) { |
| 426 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 427 | return mTestablePermissions.check(uri, modeFlags); |
| 428 | } |
| 429 | return super.checkUriPermission(uri, readPermission, writePermission, pid, uid, modeFlags); |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public void enforceCallingUriPermission(Uri uri, int modeFlags, String message) { |
| 434 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 435 | mTestablePermissions.enforce(uri, modeFlags); |
| 436 | } else { |
| 437 | super.enforceCallingUriPermission(uri, modeFlags, message); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public void enforceUriPermission(Uri uri, int pid, int uid, int modeFlags, String message) { |
| 443 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 444 | mTestablePermissions.enforce(uri, modeFlags); |
| 445 | } else { |
| 446 | super.enforceUriPermission(uri, pid, uid, modeFlags, message); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public void enforceUriPermission(Uri uri, String readPermission, String writePermission, |
| 452 | int pid, int uid, int modeFlags, String message) { |
| 453 | if (mTestablePermissions != null && mTestablePermissions.wantsCall(uri)) { |
| 454 | mTestablePermissions.enforce(uri, modeFlags); |
| 455 | } else { |
| 456 | super.enforceUriPermission(uri, readPermission, writePermission, pid, uid, modeFlags, |
| 457 | message); |
| 458 | } |
| 459 | } |
| 460 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 461 | @Override |
| 462 | public Statement apply(Statement base, Description description) { |
| 463 | return new TestWatcher() { |
| 464 | @Override |
| 465 | protected void succeeded(Description description) { |
Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 466 | mSettingsProvider.clearValuesAndCheck(TestableContext.this); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 467 | } |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 468 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 469 | @Override |
| 470 | protected void failed(Throwable e, Description description) { |
Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 471 | mSettingsProvider.clearValuesAndCheck(TestableContext.this); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 472 | } |
| 473 | }.apply(base, description); |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 474 | } |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 475 | } |