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 | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 28 | import android.os.Handler; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 29 | import android.os.IBinder; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 30 | import android.os.UserHandle; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 31 | import android.provider.Settings; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 32 | import android.util.ArrayMap; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 34 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 35 | import org.junit.rules.TestRule; |
| 36 | import org.junit.rules.TestWatcher; |
| 37 | import org.junit.runner.Description; |
| 38 | import org.junit.runners.model.Statement; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 39 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 40 | /** |
| 41 | * A ContextWrapper with utilities specifically designed to make Testing easier. |
| 42 | * |
| 43 | * <ul> |
| 44 | * <li>System services can be mocked out with {@link #addMockSystemService}</li> |
| 45 | * <li>Service binding can be mocked out with {@link #addMockService}</li> |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 46 | * <li>Resources can be mocked out using {@link #getOrCreateTestableResources()}</li> |
Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 47 | * <li>Settings support {@link TestableSettingsProvider}</li> |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 48 | * <li>Has support for {@link LeakCheck} for services and receivers</li> |
| 49 | * </ul> |
| 50 | * |
| 51 | * <p>TestableContext should be defined as a rule on your test so it can clean up after itself. |
| 52 | * Like the following:</p> |
| 53 | * <pre class="prettyprint"> |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 54 | * @Rule |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 55 | * private final TestableContext mContext = new TestableContext(InstrumentationRegister.getContext()); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 56 | * </pre> |
| 57 | */ |
| 58 | public class TestableContext extends ContextWrapper implements TestRule { |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 59 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 60 | private final TestableContentResolver mTestableContentResolver; |
Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 61 | private final TestableSettingsProvider mSettingsProvider; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 62 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 63 | private ArrayMap<String, Object> mMockSystemServices; |
| 64 | private ArrayMap<ComponentName, IBinder> mMockServices; |
| 65 | private ArrayMap<ServiceConnection, ComponentName> mActiveServices; |
| 66 | |
Jason Monk | 26bc899 | 2017-01-04 14:17:47 -0500 | [diff] [blame] | 67 | private PackageManager mMockPackageManager; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 68 | private LeakCheck.Tracker mReceiver; |
| 69 | private LeakCheck.Tracker mService; |
| 70 | private LeakCheck.Tracker mComponent; |
Jason Monk | 77f1b05 | 2017-05-02 14:22:21 -0400 | [diff] [blame] | 71 | private TestableResources mTestableResources; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 72 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 73 | public TestableContext(Context base) { |
| 74 | this(base, null); |
| 75 | } |
| 76 | |
| 77 | public TestableContext(Context base, LeakCheck check) { |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 78 | super(base); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 79 | mTestableContentResolver = new TestableContentResolver(base); |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 80 | ContentProviderClient settings = base.getContentResolver() |
| 81 | .acquireContentProviderClient(Settings.AUTHORITY); |
Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 82 | mSettingsProvider = TestableSettingsProvider.getFakeSettingsProvider(settings); |
| 83 | mTestableContentResolver.addProvider(Settings.AUTHORITY, mSettingsProvider); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 84 | mReceiver = check != null ? check.getTracker("receiver") : null; |
| 85 | mService = check != null ? check.getTracker("service") : null; |
| 86 | mComponent = check != null ? check.getTracker("component") : null; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Jason Monk | 26bc899 | 2017-01-04 14:17:47 -0500 | [diff] [blame] | 89 | public void setMockPackageManager(PackageManager mock) { |
| 90 | mMockPackageManager = mock; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public PackageManager getPackageManager() { |
| 95 | if (mMockPackageManager != null) { |
| 96 | return mMockPackageManager; |
| 97 | } |
| 98 | return super.getPackageManager(); |
| 99 | } |
| 100 | |
Jason Monk | 77f1b05 | 2017-05-02 14:22:21 -0400 | [diff] [blame] | 101 | /** |
| 102 | * Makes sure the resources being returned by this TestableContext are a version of |
| 103 | * TestableResources. |
| 104 | * @see #getResources() |
| 105 | */ |
| 106 | public void ensureTestableResources() { |
| 107 | if (mTestableResources == null) { |
| 108 | mTestableResources = new TestableResources(super.getResources()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get (and create if necessary) {@link TestableResources} for this TestableContext. |
| 114 | */ |
| 115 | public TestableResources getOrCreateTestableResources() { |
| 116 | ensureTestableResources(); |
| 117 | return mTestableResources; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns a Resources instance for the test. |
| 122 | * |
| 123 | * By default this returns the same resources object that would come from the |
| 124 | * {@link ContextWrapper}, but if {@link #ensureTestableResources()} or |
| 125 | * {@link #getOrCreateTestableResources()} has been called, it will return resources gotten from |
| 126 | * {@link TestableResources}. |
| 127 | */ |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 128 | @Override |
| 129 | public Resources getResources() { |
Jason Monk | 77f1b05 | 2017-05-02 14:22:21 -0400 | [diff] [blame] | 130 | return mTestableResources != null ? mTestableResources.getResources() |
| 131 | : super.getResources(); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 134 | /** |
| 135 | * @see #getSystemService(String) |
| 136 | */ |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 137 | public <T> void addMockSystemService(Class<T> service, T mock) { |
| 138 | addMockSystemService(getSystemServiceName(service), mock); |
| 139 | } |
| 140 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 141 | /** |
| 142 | * @see #getSystemService(String) |
| 143 | */ |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 144 | public void addMockSystemService(String name, Object service) { |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 145 | if (mMockSystemServices == null) mMockSystemServices = new ArrayMap<>(); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 146 | mMockSystemServices.put(name, service); |
| 147 | } |
| 148 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 149 | /** |
| 150 | * If a matching mock service has been added through {@link #addMockSystemService} then |
| 151 | * that will be returned, otherwise the real service will be acquired from the base |
| 152 | * context. |
| 153 | */ |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 154 | @Override |
| 155 | public Object getSystemService(String name) { |
| 156 | if (mMockSystemServices != null && mMockSystemServices.containsKey(name)) { |
| 157 | return mMockSystemServices.get(name); |
| 158 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 159 | if (name.equals(LAYOUT_INFLATER_SERVICE)) { |
| 160 | return getBaseContext().getSystemService(LayoutInflater.class).cloneInContext(this); |
| 161 | } |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 162 | return super.getSystemService(name); |
| 163 | } |
| 164 | |
Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 165 | TestableSettingsProvider getSettingsProvider() { |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 166 | return mSettingsProvider; |
| 167 | } |
| 168 | |
| 169 | @Override |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 170 | public TestableContentResolver getContentResolver() { |
| 171 | return mTestableContentResolver; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 174 | /** |
| 175 | * Will always return itself for a TestableContext to ensure the testable effects extend |
| 176 | * to the application context. |
| 177 | */ |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 178 | @Override |
| 179 | public Context getApplicationContext() { |
| 180 | // Return this so its always a TestableContext. |
| 181 | return this; |
| 182 | } |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 183 | |
| 184 | @Override |
| 185 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 186 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 187 | return super.registerReceiver(receiver, filter); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 192 | String broadcastPermission, Handler scheduler) { |
| 193 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 194 | return super.registerReceiver(receiver, filter, broadcastPermission, scheduler); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 199 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
| 200 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 201 | return super.registerReceiverAsUser(receiver, user, filter, broadcastPermission, |
| 202 | scheduler); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 207 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).clearAllocations(); |
| 208 | super.unregisterReceiver(receiver); |
| 209 | } |
| 210 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 211 | /** |
| 212 | * Adds a mock service to be connected to by a bindService call. |
| 213 | * <p> |
| 214 | * Normally a TestableContext will pass through all bind requests to the base context |
| 215 | * but when addMockService has been called for a ComponentName being bound, then |
| 216 | * TestableContext will immediately trigger a {@link ServiceConnection#onServiceConnected} |
| 217 | * with the specified service, and will call {@link ServiceConnection#onServiceDisconnected} |
| 218 | * when the service is unbound. |
| 219 | * </p> |
| 220 | */ |
| 221 | public void addMockService(ComponentName component, IBinder service) { |
| 222 | if (mMockServices == null) mMockServices = new ArrayMap<>(); |
| 223 | mMockServices.put(component, service); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @see #addMockService(ComponentName, IBinder) |
| 228 | */ |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 229 | @Override |
| 230 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 231 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 232 | if (checkMocks(service.getComponent(), conn)) return true; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 233 | return super.bindService(service, conn, flags); |
| 234 | } |
| 235 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 236 | /** |
| 237 | * @see #addMockService(ComponentName, IBinder) |
| 238 | */ |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 239 | @Override |
| 240 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 241 | Handler handler, UserHandle user) { |
| 242 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 243 | if (checkMocks(service.getComponent(), conn)) return true; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 244 | return super.bindServiceAsUser(service, conn, flags, handler, user); |
| 245 | } |
| 246 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 247 | /** |
| 248 | * @see #addMockService(ComponentName, IBinder) |
| 249 | */ |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 250 | @Override |
| 251 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 252 | UserHandle user) { |
| 253 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 254 | if (checkMocks(service.getComponent(), conn)) return true; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 255 | return super.bindServiceAsUser(service, conn, flags, user); |
| 256 | } |
| 257 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 258 | private boolean checkMocks(ComponentName component, ServiceConnection conn) { |
| 259 | if (mMockServices != null && component != null && mMockServices.containsKey(component)) { |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 260 | if (mActiveServices == null) mActiveServices = new ArrayMap<>(); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 261 | mActiveServices.put(conn, component); |
| 262 | conn.onServiceConnected(component, mMockServices.get(component)); |
| 263 | return true; |
| 264 | } |
| 265 | return false; |
| 266 | } |
| 267 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 268 | /** |
| 269 | * @see #addMockService(ComponentName, IBinder) |
| 270 | */ |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 271 | @Override |
| 272 | public void unbindService(ServiceConnection conn) { |
| 273 | if (mService != null) mService.getLeakInfo(conn).clearAllocations(); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 274 | if (mActiveServices != null && mActiveServices.containsKey(conn)) { |
| 275 | conn.onServiceDisconnected(mActiveServices.get(conn)); |
| 276 | mActiveServices.remove(conn); |
| 277 | return; |
| 278 | } |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 279 | super.unbindService(conn); |
| 280 | } |
| 281 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 282 | /** |
| 283 | * Check if the TestableContext has a mock binding for a specified component. Will return |
| 284 | * true between {@link ServiceConnection#onServiceConnected} and |
| 285 | * {@link ServiceConnection#onServiceDisconnected} callbacks for a mock service. |
| 286 | * |
| 287 | * @see #addMockService(ComponentName, IBinder) |
| 288 | */ |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 289 | public boolean isBound(ComponentName component) { |
| 290 | return mActiveServices != null && mActiveServices.containsValue(component); |
| 291 | } |
| 292 | |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 293 | @Override |
| 294 | public void registerComponentCallbacks(ComponentCallbacks callback) { |
| 295 | if (mComponent != null) mComponent.getLeakInfo(callback).addAllocation(new Throwable()); |
| 296 | super.registerComponentCallbacks(callback); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void unregisterComponentCallbacks(ComponentCallbacks callback) { |
| 301 | if (mComponent != null) mComponent.getLeakInfo(callback).clearAllocations(); |
| 302 | super.unregisterComponentCallbacks(callback); |
| 303 | } |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 304 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 305 | @Override |
| 306 | public Statement apply(Statement base, Description description) { |
| 307 | return new TestWatcher() { |
| 308 | @Override |
| 309 | protected void succeeded(Description description) { |
Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 310 | mSettingsProvider.clearValuesAndCheck(TestableContext.this); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 311 | } |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 312 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 313 | @Override |
| 314 | protected void failed(Throwable e, Description description) { |
Jason Monk | f06a317 | 2017-04-25 16:30:53 -0400 | [diff] [blame] | 315 | mSettingsProvider.clearValuesAndCheck(TestableContext.this); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 316 | } |
| 317 | }.apply(base, description); |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 318 | } |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 319 | } |