| 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> |
| 46 | * <li>Settings support {@link TestableSettings}</li> |
| 47 | * <li>Has support for {@link LeakCheck} for services and receivers</li> |
| 48 | * </ul> |
| 49 | * |
| 50 | * <p>TestableContext should be defined as a rule on your test so it can clean up after itself. |
| 51 | * Like the following:</p> |
| 52 | * <pre class="prettyprint"> |
| 53 | * {@literal |
| 54 | * @Rule |
| 55 | * private final TestableContext mContext = new TestableContext(InstrumentationRegister.getContext()); |
| 56 | * } |
| 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; |
| 62 | private final TestableSettings 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 | 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 | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 82 | mSettingsProvider = TestableSettings.getFakeSettingsProvider(settings, |
| 83 | mTestableContentResolver); |
| 84 | mTestableContentResolver.addProvider(Settings.AUTHORITY, mSettingsProvider.getProvider()); |
| 85 | mReceiver = check != null ? check.getTracker("receiver") : null; |
| 86 | mService = check != null ? check.getTracker("service") : null; |
| 87 | mComponent = check != null ? check.getTracker("component") : null; |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| Jason Monk | 26bc899 | 2017-01-04 14:17:47 -0500 | [diff] [blame] | 90 | public void setMockPackageManager(PackageManager mock) { |
| 91 | mMockPackageManager = mock; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public PackageManager getPackageManager() { |
| 96 | if (mMockPackageManager != null) { |
| 97 | return mMockPackageManager; |
| 98 | } |
| 99 | return super.getPackageManager(); |
| 100 | } |
| 101 | |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 102 | @Override |
| 103 | public Resources getResources() { |
| 104 | return super.getResources(); |
| 105 | } |
| 106 | |
| Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 107 | public <T> void addMockSystemService(Class<T> service, T mock) { |
| 108 | addMockSystemService(getSystemServiceName(service), mock); |
| 109 | } |
| 110 | |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 111 | public void addMockSystemService(String name, Object service) { |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 112 | if (mMockSystemServices == null) mMockSystemServices = new ArrayMap<>(); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 113 | mMockSystemServices.put(name, service); |
| 114 | } |
| 115 | |
| 116 | public void addMockService(ComponentName component, IBinder service) { |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 117 | if (mMockServices == null) mMockServices = new ArrayMap<>(); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 118 | mMockServices.put(component, service); |
| 119 | } |
| 120 | |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 121 | @Override |
| 122 | public Object getSystemService(String name) { |
| 123 | if (mMockSystemServices != null && mMockSystemServices.containsKey(name)) { |
| 124 | return mMockSystemServices.get(name); |
| 125 | } |
| Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 126 | if (name.equals(LAYOUT_INFLATER_SERVICE)) { |
| 127 | return getBaseContext().getSystemService(LayoutInflater.class).cloneInContext(this); |
| 128 | } |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 129 | return super.getSystemService(name); |
| 130 | } |
| 131 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 132 | public TestableSettings getSettingsProvider() { |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 133 | return mSettingsProvider; |
| 134 | } |
| 135 | |
| 136 | @Override |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 137 | public TestableContentResolver getContentResolver() { |
| 138 | return mTestableContentResolver; |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Context getApplicationContext() { |
| 143 | // Return this so its always a TestableContext. |
| 144 | return this; |
| 145 | } |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 146 | |
| 147 | @Override |
| 148 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 149 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 150 | return super.registerReceiver(receiver, filter); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 155 | String broadcastPermission, Handler scheduler) { |
| 156 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 157 | return super.registerReceiver(receiver, filter, broadcastPermission, scheduler); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 162 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
| 163 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable()); |
| 164 | return super.registerReceiverAsUser(receiver, user, filter, broadcastPermission, |
| 165 | scheduler); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 170 | if (mReceiver != null) mReceiver.getLeakInfo(receiver).clearAllocations(); |
| 171 | super.unregisterReceiver(receiver); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 176 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 177 | if (checkMocks(service.getComponent(), conn)) return true; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 178 | return super.bindService(service, conn, flags); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 183 | Handler handler, UserHandle user) { |
| 184 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 185 | if (checkMocks(service.getComponent(), conn)) return true; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 186 | return super.bindServiceAsUser(service, conn, flags, handler, user); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 191 | UserHandle user) { |
| 192 | if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable()); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 193 | if (checkMocks(service.getComponent(), conn)) return true; |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 194 | return super.bindServiceAsUser(service, conn, flags, user); |
| 195 | } |
| 196 | |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 197 | private boolean checkMocks(ComponentName component, ServiceConnection conn) { |
| 198 | if (mMockServices != null && component != null && mMockServices.containsKey(component)) { |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 199 | if (mActiveServices == null) mActiveServices = new ArrayMap<>(); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 200 | mActiveServices.put(conn, component); |
| 201 | conn.onServiceConnected(component, mMockServices.get(component)); |
| 202 | return true; |
| 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 207 | @Override |
| 208 | public void unbindService(ServiceConnection conn) { |
| 209 | if (mService != null) mService.getLeakInfo(conn).clearAllocations(); |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 210 | if (mActiveServices != null && mActiveServices.containsKey(conn)) { |
| 211 | conn.onServiceDisconnected(mActiveServices.get(conn)); |
| 212 | mActiveServices.remove(conn); |
| 213 | return; |
| 214 | } |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 215 | super.unbindService(conn); |
| 216 | } |
| 217 | |
| Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 218 | public boolean isBound(ComponentName component) { |
| 219 | return mActiveServices != null && mActiveServices.containsValue(component); |
| 220 | } |
| 221 | |
| Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 222 | @Override |
| 223 | public void registerComponentCallbacks(ComponentCallbacks callback) { |
| 224 | if (mComponent != null) mComponent.getLeakInfo(callback).addAllocation(new Throwable()); |
| 225 | super.registerComponentCallbacks(callback); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void unregisterComponentCallbacks(ComponentCallbacks callback) { |
| 230 | if (mComponent != null) mComponent.getLeakInfo(callback).clearAllocations(); |
| 231 | super.unregisterComponentCallbacks(callback); |
| 232 | } |
| Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 233 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 234 | @Override |
| 235 | public Statement apply(Statement base, Description description) { |
| 236 | return new TestWatcher() { |
| 237 | @Override |
| 238 | protected void succeeded(Description description) { |
| 239 | mSettingsProvider.clearOverrides(); |
| 240 | } |
| Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 241 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 242 | @Override |
| 243 | protected void failed(Throwable e, Description description) { |
| 244 | mSettingsProvider.clearOverrides(); |
| 245 | } |
| 246 | }.apply(base, description); |
| Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 247 | } |
| Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 248 | } |