1 /*
2  * Copyright (C) 2017 The Android Open Source Project
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 
15 package com.android.systemui;
16 
17 import android.content.BroadcastReceiver;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.content.IntentFilter;
21 import android.os.Handler;
22 import android.os.UserHandle;
23 import android.testing.LeakCheck;
24 import android.testing.TestableContext;
25 import android.util.ArraySet;
26 import android.util.Log;
27 import android.view.Display;
28 
29 import java.util.Set;
30 
31 public class SysuiTestableContext extends TestableContext {
32 
33     private Set<BroadcastReceiver> mRegisteredReceivers = new ArraySet<>();
34 
SysuiTestableContext(Context base)35     public SysuiTestableContext(Context base) {
36         super(base);
37         setTheme(R.style.Theme_SystemUI);
38     }
39 
SysuiTestableContext(Context base, LeakCheck check)40     public SysuiTestableContext(Context base, LeakCheck check) {
41         super(base, check);
42         setTheme(R.style.Theme_SystemUI);
43     }
44 
45     @Override
createDisplayContext(Display display)46     public Context createDisplayContext(Display display) {
47         if (display == null) {
48             throw new IllegalArgumentException("display must not be null");
49         }
50 
51         SysuiTestableContext context =
52                 new SysuiTestableContext(getBaseContext().createDisplayContext(display));
53         return context;
54     }
55 
cleanUpReceivers(String testName)56     public void cleanUpReceivers(String testName) {
57         Set<BroadcastReceiver> copy = new ArraySet<>(mRegisteredReceivers);
58         for (BroadcastReceiver r : copy) {
59             try {
60                 unregisterReceiver(r);
61                 Log.w(testName, "Receiver not unregistered from Context: " + r);
62             } catch (IllegalArgumentException e) {
63                 // Nothing to do here. Somehow it got unregistered.
64             }
65         }
66     }
67 
68     @Override
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)69     public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
70         if (receiver != null) {
71             mRegisteredReceivers.add(receiver);
72         }
73         return super.registerReceiver(receiver, filter);
74     }
75 
76     @Override
registerReceiver(BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler)77     public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
78             String broadcastPermission, Handler scheduler) {
79         if (receiver != null) {
80             mRegisteredReceivers.add(receiver);
81         }
82         return super.registerReceiver(receiver, filter, broadcastPermission, scheduler);
83     }
84 
85     @Override
registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, IntentFilter filter, String broadcastPermission, Handler scheduler)86     public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
87             IntentFilter filter, String broadcastPermission, Handler scheduler) {
88         if (receiver != null) {
89             mRegisteredReceivers.add(receiver);
90         }
91         return super.registerReceiverAsUser(receiver, user, filter, broadcastPermission, scheduler);
92     }
93 
94     @Override
unregisterReceiver(BroadcastReceiver receiver)95     public void unregisterReceiver(BroadcastReceiver receiver) {
96         if (receiver != null) {
97             mRegisteredReceivers.remove(receiver);
98         }
99         super.unregisterReceiver(receiver);
100     }
101 }
102