1 /* 2 * Copyright (C) 2016 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.utils.leaks; 16 17 import android.testing.LeakCheck; 18 import android.util.ArrayMap; 19 20 import com.android.systemui.SysuiTestCase; 21 import com.android.systemui.shared.plugins.PluginManager; 22 import com.android.systemui.statusbar.phone.ManagedProfileController; 23 import com.android.systemui.statusbar.phone.StatusBarIconController; 24 import com.android.systemui.statusbar.policy.BatteryController; 25 import com.android.systemui.statusbar.policy.BluetoothController; 26 import com.android.systemui.statusbar.policy.CastController; 27 import com.android.systemui.statusbar.policy.ConfigurationController; 28 import com.android.systemui.statusbar.policy.FlashlightController; 29 import com.android.systemui.statusbar.policy.HotspotController; 30 import com.android.systemui.statusbar.policy.KeyguardStateController; 31 import com.android.systemui.statusbar.policy.LocationController; 32 import com.android.systemui.statusbar.policy.NetworkController; 33 import com.android.systemui.statusbar.policy.NextAlarmController; 34 import com.android.systemui.statusbar.policy.RotationLockController; 35 import com.android.systemui.statusbar.policy.SecurityController; 36 import com.android.systemui.statusbar.policy.UserInfoController; 37 import com.android.systemui.statusbar.policy.ZenModeController; 38 import com.android.systemui.tuner.TunerService; 39 40 import org.junit.Assert; 41 import org.junit.Rule; 42 43 import java.util.Map; 44 45 /** 46 * Base class for tests to check if receivers are left registered, services bound, or other 47 * listeners listening. 48 */ 49 public abstract class LeakCheckedTest extends SysuiTestCase { 50 private static final String TAG = "LeakCheckedTest"; 51 52 public static final Class<?>[] ALL_SUPPORTED_CLASSES = new Class[]{ 53 BluetoothController.class, 54 LocationController.class, 55 RotationLockController.class, 56 ZenModeController.class, 57 CastController.class, 58 HotspotController.class, 59 FlashlightController.class, 60 UserInfoController.class, 61 KeyguardStateController.class, 62 BatteryController.class, 63 SecurityController.class, 64 ManagedProfileController.class, 65 NextAlarmController.class, 66 NetworkController.class, 67 PluginManager.class, 68 TunerService.class, 69 StatusBarIconController.class, 70 ConfigurationController.class, 71 }; 72 73 @Rule 74 public SysuiLeakCheck mLeakCheck = new SysuiLeakCheck(); 75 76 @Override getLeakCheck()77 public LeakCheck getLeakCheck() { 78 return mLeakCheck; 79 } 80 injectLeakCheckedDependencies(Class<?>.... cls)81 public void injectLeakCheckedDependencies(Class<?>... cls) { 82 for (Class<?> c : cls) { 83 injectLeakCheckedDependency(c); 84 } 85 } 86 injectLeakCheckedDependency(Class<T> c)87 public <T> void injectLeakCheckedDependency(Class<T> c) { 88 mDependency.injectTestDependency(c, mLeakCheck.getLeakChecker(c)); 89 } 90 91 public static class SysuiLeakCheck extends LeakCheck { 92 93 private final Map<Class, Object> mLeakCheckers = new ArrayMap<>(); 94 SysuiLeakCheck()95 public SysuiLeakCheck() { 96 super(); 97 } 98 getLeakChecker(Class<T> cls)99 public <T> T getLeakChecker(Class<T> cls) { 100 Object obj = mLeakCheckers.get(cls); 101 if (obj == null) { 102 // Lazy create checkers so we only have the ones we need. 103 if (cls == BluetoothController.class) { 104 obj = new FakeBluetoothController(this); 105 } else if (cls == LocationController.class) { 106 obj = new FakeLocationController(this); 107 } else if (cls == RotationLockController.class) { 108 obj = new FakeRotationLockController(this); 109 } else if (cls == ZenModeController.class) { 110 obj = new FakeZenModeController(this); 111 } else if (cls == CastController.class) { 112 obj = new FakeCastController(this); 113 } else if (cls == HotspotController.class) { 114 obj = new FakeHotspotController(this); 115 } else if (cls == FlashlightController.class) { 116 obj = new FakeFlashlightController(this); 117 } else if (cls == UserInfoController.class) { 118 obj = new FakeUserInfoController(this); 119 } else if (cls == KeyguardStateController.class) { 120 obj = new FakeKeyguardStateController(this); 121 } else if (cls == BatteryController.class) { 122 obj = new FakeBatteryController(this); 123 } else if (cls == SecurityController.class) { 124 obj = new FakeSecurityController(this); 125 } else if (cls == ManagedProfileController.class) { 126 obj = new FakeManagedProfileController(this); 127 } else if (cls == NextAlarmController.class) { 128 obj = new FakeNextAlarmController(this); 129 } else if (cls == NetworkController.class) { 130 obj = new FakeNetworkController(this); 131 } else if (cls == PluginManager.class) { 132 obj = new FakePluginManager(this); 133 } else if (cls == TunerService.class) { 134 obj = new FakeTunerService(this); 135 } else if (cls == StatusBarIconController.class) { 136 obj = new FakeStatusBarIconController(this); 137 } else if (cls == ConfigurationController.class) { 138 obj = new FakeConfigurationController(this); 139 } else { 140 Assert.fail(cls.getName() + " is not supported by LeakCheckedTest yet"); 141 } 142 mLeakCheckers.put(cls, obj); 143 } 144 return (T) obj; 145 } 146 } 147 } 148