blob: 630a287c6f4a5984f0ae038a612e6601b1d0d3ad [file] [log] [blame]
Jason Monke9789282016-11-09 08:59:56 -05001/*
Jason Monk340b0e52017-03-08 14:57:56 -05002 * Copyright (C) 2017 The Android Open Source Project
Jason Monke9789282016-11-09 08:59:56 -05003 *
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 Monk340b0e52017-03-08 14:57:56 -050015package android.testing;
Jason Monke9789282016-11-09 08:59:56 -050016
Jason Monk9abca5e2016-11-11 16:18:14 -050017import android.content.BroadcastReceiver;
18import android.content.ComponentCallbacks;
Jason Monk3cfedd72016-12-09 09:31:37 -050019import android.content.ComponentName;
Jason Monke9789282016-11-09 08:59:56 -050020import android.content.ContentProviderClient;
Jason Monke9789282016-11-09 08:59:56 -050021import android.content.Context;
22import android.content.ContextWrapper;
Jason Monk9abca5e2016-11-11 16:18:14 -050023import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.ServiceConnection;
Jason Monk26bc8992017-01-04 14:17:47 -050026import android.content.pm.PackageManager;
Jason Monk3cfedd72016-12-09 09:31:37 -050027import android.content.res.Resources;
Jason Monk9abca5e2016-11-11 16:18:14 -050028import android.os.Handler;
Jason Monk3cfedd72016-12-09 09:31:37 -050029import android.os.IBinder;
Jason Monk9abca5e2016-11-11 16:18:14 -050030import android.os.UserHandle;
Jason Monke9789282016-11-09 08:59:56 -050031import android.provider.Settings;
Jason Monk3cfedd72016-12-09 09:31:37 -050032import android.util.ArrayMap;
Jason Monkaa573e92017-01-27 17:00:29 -050033import android.view.LayoutInflater;
Jason Monke9789282016-11-09 08:59:56 -050034
Jason Monk340b0e52017-03-08 14:57:56 -050035import org.junit.rules.TestRule;
36import org.junit.rules.TestWatcher;
37import org.junit.runner.Description;
38import org.junit.runners.model.Statement;
Jason Monk9abca5e2016-11-11 16:18:14 -050039
Jason Monk340b0e52017-03-08 14:57:56 -050040/**
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 Monkf06a3172017-04-25 16:30:53 -040046 * <li>Settings support {@link TestableSettingsProvider}</li>
Jason Monk340b0e52017-03-08 14:57:56 -050047 * <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 */
59public class TestableContext extends ContextWrapper implements TestRule {
Jason Monke9789282016-11-09 08:59:56 -050060
Jason Monk340b0e52017-03-08 14:57:56 -050061 private final TestableContentResolver mTestableContentResolver;
Jason Monkf06a3172017-04-25 16:30:53 -040062 private final TestableSettingsProvider mSettingsProvider;
Jason Monke9789282016-11-09 08:59:56 -050063
Jason Monk3cfedd72016-12-09 09:31:37 -050064 private ArrayMap<String, Object> mMockSystemServices;
65 private ArrayMap<ComponentName, IBinder> mMockServices;
66 private ArrayMap<ServiceConnection, ComponentName> mActiveServices;
67
Jason Monk26bc8992017-01-04 14:17:47 -050068 private PackageManager mMockPackageManager;
Jason Monk340b0e52017-03-08 14:57:56 -050069 private LeakCheck.Tracker mReceiver;
70 private LeakCheck.Tracker mService;
71 private LeakCheck.Tracker mComponent;
Jason Monk9abca5e2016-11-11 16:18:14 -050072
Jason Monk340b0e52017-03-08 14:57:56 -050073 public TestableContext(Context base) {
74 this(base, null);
75 }
76
77 public TestableContext(Context base, LeakCheck check) {
Jason Monke9789282016-11-09 08:59:56 -050078 super(base);
Jason Monk340b0e52017-03-08 14:57:56 -050079 mTestableContentResolver = new TestableContentResolver(base);
Jason Monke9789282016-11-09 08:59:56 -050080 ContentProviderClient settings = base.getContentResolver()
81 .acquireContentProviderClient(Settings.AUTHORITY);
Jason Monkf06a3172017-04-25 16:30:53 -040082 mSettingsProvider = TestableSettingsProvider.getFakeSettingsProvider(settings);
83 mTestableContentResolver.addProvider(Settings.AUTHORITY, mSettingsProvider);
Jason Monk340b0e52017-03-08 14:57:56 -050084 mReceiver = check != null ? check.getTracker("receiver") : null;
85 mService = check != null ? check.getTracker("service") : null;
86 mComponent = check != null ? check.getTracker("component") : null;
Jason Monke9789282016-11-09 08:59:56 -050087 }
88
Jason Monk26bc8992017-01-04 14:17:47 -050089 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 Monk3cfedd72016-12-09 09:31:37 -0500101 @Override
102 public Resources getResources() {
103 return super.getResources();
104 }
105
Adrian Roos91250682017-02-06 14:48:15 -0800106 public <T> void addMockSystemService(Class<T> service, T mock) {
107 addMockSystemService(getSystemServiceName(service), mock);
108 }
109
Jason Monk3cfedd72016-12-09 09:31:37 -0500110 public void addMockSystemService(String name, Object service) {
Jason Monk340b0e52017-03-08 14:57:56 -0500111 if (mMockSystemServices == null) mMockSystemServices = new ArrayMap<>();
Jason Monk3cfedd72016-12-09 09:31:37 -0500112 mMockSystemServices.put(name, service);
113 }
114
115 public void addMockService(ComponentName component, IBinder service) {
Jason Monk340b0e52017-03-08 14:57:56 -0500116 if (mMockServices == null) mMockServices = new ArrayMap<>();
Jason Monk3cfedd72016-12-09 09:31:37 -0500117 mMockServices.put(component, service);
118 }
119
Jason Monk3cfedd72016-12-09 09:31:37 -0500120 @Override
121 public Object getSystemService(String name) {
122 if (mMockSystemServices != null && mMockSystemServices.containsKey(name)) {
123 return mMockSystemServices.get(name);
124 }
Jason Monkaa573e92017-01-27 17:00:29 -0500125 if (name.equals(LAYOUT_INFLATER_SERVICE)) {
126 return getBaseContext().getSystemService(LayoutInflater.class).cloneInContext(this);
127 }
Jason Monk3cfedd72016-12-09 09:31:37 -0500128 return super.getSystemService(name);
129 }
130
Jason Monkf06a3172017-04-25 16:30:53 -0400131 TestableSettingsProvider getSettingsProvider() {
Jason Monke9789282016-11-09 08:59:56 -0500132 return mSettingsProvider;
133 }
134
135 @Override
Jason Monk340b0e52017-03-08 14:57:56 -0500136 public TestableContentResolver getContentResolver() {
137 return mTestableContentResolver;
Jason Monke9789282016-11-09 08:59:56 -0500138 }
139
140 @Override
141 public Context getApplicationContext() {
142 // Return this so its always a TestableContext.
143 return this;
144 }
Jason Monk9abca5e2016-11-11 16:18:14 -0500145
146 @Override
147 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
148 if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable());
149 return super.registerReceiver(receiver, filter);
150 }
151
152 @Override
153 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
154 String broadcastPermission, Handler scheduler) {
155 if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable());
156 return super.registerReceiver(receiver, filter, broadcastPermission, scheduler);
157 }
158
159 @Override
160 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
161 IntentFilter filter, String broadcastPermission, Handler scheduler) {
162 if (mReceiver != null) mReceiver.getLeakInfo(receiver).addAllocation(new Throwable());
163 return super.registerReceiverAsUser(receiver, user, filter, broadcastPermission,
164 scheduler);
165 }
166
167 @Override
168 public void unregisterReceiver(BroadcastReceiver receiver) {
169 if (mReceiver != null) mReceiver.getLeakInfo(receiver).clearAllocations();
170 super.unregisterReceiver(receiver);
171 }
172
173 @Override
174 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
175 if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable());
Jason Monk3cfedd72016-12-09 09:31:37 -0500176 if (checkMocks(service.getComponent(), conn)) return true;
Jason Monk9abca5e2016-11-11 16:18:14 -0500177 return super.bindService(service, conn, flags);
178 }
179
180 @Override
181 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
182 Handler handler, UserHandle user) {
183 if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable());
Jason Monk3cfedd72016-12-09 09:31:37 -0500184 if (checkMocks(service.getComponent(), conn)) return true;
Jason Monk9abca5e2016-11-11 16:18:14 -0500185 return super.bindServiceAsUser(service, conn, flags, handler, user);
186 }
187
188 @Override
189 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
190 UserHandle user) {
191 if (mService != null) mService.getLeakInfo(conn).addAllocation(new Throwable());
Jason Monk3cfedd72016-12-09 09:31:37 -0500192 if (checkMocks(service.getComponent(), conn)) return true;
Jason Monk9abca5e2016-11-11 16:18:14 -0500193 return super.bindServiceAsUser(service, conn, flags, user);
194 }
195
Jason Monk3cfedd72016-12-09 09:31:37 -0500196 private boolean checkMocks(ComponentName component, ServiceConnection conn) {
197 if (mMockServices != null && component != null && mMockServices.containsKey(component)) {
Jason Monk340b0e52017-03-08 14:57:56 -0500198 if (mActiveServices == null) mActiveServices = new ArrayMap<>();
Jason Monk3cfedd72016-12-09 09:31:37 -0500199 mActiveServices.put(conn, component);
200 conn.onServiceConnected(component, mMockServices.get(component));
201 return true;
202 }
203 return false;
204 }
205
Jason Monk9abca5e2016-11-11 16:18:14 -0500206 @Override
207 public void unbindService(ServiceConnection conn) {
208 if (mService != null) mService.getLeakInfo(conn).clearAllocations();
Jason Monk3cfedd72016-12-09 09:31:37 -0500209 if (mActiveServices != null && mActiveServices.containsKey(conn)) {
210 conn.onServiceDisconnected(mActiveServices.get(conn));
211 mActiveServices.remove(conn);
212 return;
213 }
Jason Monk9abca5e2016-11-11 16:18:14 -0500214 super.unbindService(conn);
215 }
216
Jason Monk3cfedd72016-12-09 09:31:37 -0500217 public boolean isBound(ComponentName component) {
218 return mActiveServices != null && mActiveServices.containsValue(component);
219 }
220
Jason Monk9abca5e2016-11-11 16:18:14 -0500221 @Override
222 public void registerComponentCallbacks(ComponentCallbacks callback) {
223 if (mComponent != null) mComponent.getLeakInfo(callback).addAllocation(new Throwable());
224 super.registerComponentCallbacks(callback);
225 }
226
227 @Override
228 public void unregisterComponentCallbacks(ComponentCallbacks callback) {
229 if (mComponent != null) mComponent.getLeakInfo(callback).clearAllocations();
230 super.unregisterComponentCallbacks(callback);
231 }
Jason Monk49fa0162017-01-11 09:21:56 -0500232
Jason Monk340b0e52017-03-08 14:57:56 -0500233 @Override
234 public Statement apply(Statement base, Description description) {
235 return new TestWatcher() {
236 @Override
237 protected void succeeded(Description description) {
Jason Monkf06a3172017-04-25 16:30:53 -0400238 mSettingsProvider.clearValuesAndCheck(TestableContext.this);
Jason Monk340b0e52017-03-08 14:57:56 -0500239 }
Jason Monk49fa0162017-01-11 09:21:56 -0500240
Jason Monk340b0e52017-03-08 14:57:56 -0500241 @Override
242 protected void failed(Throwable e, Description description) {
Jason Monkf06a3172017-04-25 16:30:53 -0400243 mSettingsProvider.clearValuesAndCheck(TestableContext.this);
Jason Monk340b0e52017-03-08 14:57:56 -0500244 }
245 }.apply(base, description);
Jason Monk49fa0162017-01-11 09:21:56 -0500246 }
Jason Monke9789282016-11-09 08:59:56 -0500247}