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