Loading src/com/android/settings/fuelgauge/BatteryUtils.java +4 −2 Original line number Diff line number Diff line Loading @@ -449,12 +449,14 @@ public class BatteryUtils { final BatteryStats stats = statsHelper.getStats(); BatteryInfo batteryInfo; final Estimate estimate; Estimate estimate = null; // Get enhanced prediction if available if (mPowerUsageFeatureProvider != null && mPowerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(mContext)) { estimate = mPowerUsageFeatureProvider.getEnhancedBatteryPrediction(mContext); } else { } if (estimate == null) { estimate = new Estimate( PowerUtil.convertUsToMs(stats.computeBatteryTimeRemaining(elapsedRealtimeUs)), false /* isBasedOnUsage */, Loading tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java +22 −4 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; Loading @@ -38,7 +39,10 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.app.AppOpsManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; Loading Loading @@ -75,6 +79,8 @@ import java.util.List; @RunWith(RobolectricTestRunner.class) public class BatteryUtilsTest { private static final String TAG = "BatteryUtilsTest"; // unit that used to converted ms to us private static final long UNIT = 1000; private static final long TIME_STATE_TOP = 1500 * UNIT; Loading Loading @@ -160,6 +166,7 @@ public class BatteryUtilsTest { private FakeFeatureFactory mFeatureFactory; private PowerUsageFeatureProvider mProvider; private List<BatterySipper> mUsageList; private Context mContext; @Before public void setUp() throws PackageManager.NameNotFoundException { Loading Loading @@ -214,10 +221,10 @@ public class BatteryUtilsTest { mIdleBatterySipper.drainType = BatterySipper.DrainType.IDLE; mIdleBatterySipper.totalPowerMah = BATTERY_IDLE_USAGE; final Context shadowContext = spy(RuntimeEnvironment.application); doReturn(mPackageManager).when(shadowContext).getPackageManager(); doReturn(mAppOpsManager).when(shadowContext).getSystemService(Context.APP_OPS_SERVICE); mBatteryUtils = spy(new BatteryUtils(shadowContext)); mContext = spy(RuntimeEnvironment.application); doReturn(mPackageManager).when(mContext).getPackageManager(); doReturn(mAppOpsManager).when(mContext).getSystemService(Context.APP_OPS_SERVICE); mBatteryUtils = spy(new BatteryUtils(mContext)); mBatteryUtils.mPowerUsageFeatureProvider = mProvider; doReturn(0L).when(mBatteryUtils) .getForegroundServiceTotalTimeUs(any(BatteryStats.Uid.class), anyLong()); Loading Loading @@ -710,4 +717,15 @@ public class BatteryUtilsTest { verify(mAppOpsManager, never()).setMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, UID, PACKAGE_NAME, AppOpsManager.MODE_ALLOWED); } @Test public void getBatteryInfo_providerNull_shouldNotCrash() { when(mProvider.isEnhancedBatteryPredictionEnabled(mContext)).thenReturn(true); when(mProvider.getEnhancedBatteryPrediction(mContext)).thenReturn(null); when(mContext.registerReceiver(nullable(BroadcastReceiver.class), any(IntentFilter.class))).thenReturn(new Intent()); //Should not crash assertThat(mBatteryUtils.getBatteryInfo(mBatteryStatsHelper, TAG)).isNotNull(); } } Loading
src/com/android/settings/fuelgauge/BatteryUtils.java +4 −2 Original line number Diff line number Diff line Loading @@ -449,12 +449,14 @@ public class BatteryUtils { final BatteryStats stats = statsHelper.getStats(); BatteryInfo batteryInfo; final Estimate estimate; Estimate estimate = null; // Get enhanced prediction if available if (mPowerUsageFeatureProvider != null && mPowerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(mContext)) { estimate = mPowerUsageFeatureProvider.getEnhancedBatteryPrediction(mContext); } else { } if (estimate == null) { estimate = new Estimate( PowerUtil.convertUsToMs(stats.computeBatteryTimeRemaining(elapsedRealtimeUs)), false /* isBasedOnUsage */, Loading
tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java +22 −4 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; Loading @@ -38,7 +39,10 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.app.AppOpsManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; Loading Loading @@ -75,6 +79,8 @@ import java.util.List; @RunWith(RobolectricTestRunner.class) public class BatteryUtilsTest { private static final String TAG = "BatteryUtilsTest"; // unit that used to converted ms to us private static final long UNIT = 1000; private static final long TIME_STATE_TOP = 1500 * UNIT; Loading Loading @@ -160,6 +166,7 @@ public class BatteryUtilsTest { private FakeFeatureFactory mFeatureFactory; private PowerUsageFeatureProvider mProvider; private List<BatterySipper> mUsageList; private Context mContext; @Before public void setUp() throws PackageManager.NameNotFoundException { Loading Loading @@ -214,10 +221,10 @@ public class BatteryUtilsTest { mIdleBatterySipper.drainType = BatterySipper.DrainType.IDLE; mIdleBatterySipper.totalPowerMah = BATTERY_IDLE_USAGE; final Context shadowContext = spy(RuntimeEnvironment.application); doReturn(mPackageManager).when(shadowContext).getPackageManager(); doReturn(mAppOpsManager).when(shadowContext).getSystemService(Context.APP_OPS_SERVICE); mBatteryUtils = spy(new BatteryUtils(shadowContext)); mContext = spy(RuntimeEnvironment.application); doReturn(mPackageManager).when(mContext).getPackageManager(); doReturn(mAppOpsManager).when(mContext).getSystemService(Context.APP_OPS_SERVICE); mBatteryUtils = spy(new BatteryUtils(mContext)); mBatteryUtils.mPowerUsageFeatureProvider = mProvider; doReturn(0L).when(mBatteryUtils) .getForegroundServiceTotalTimeUs(any(BatteryStats.Uid.class), anyLong()); Loading Loading @@ -710,4 +717,15 @@ public class BatteryUtilsTest { verify(mAppOpsManager, never()).setMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, UID, PACKAGE_NAME, AppOpsManager.MODE_ALLOWED); } @Test public void getBatteryInfo_providerNull_shouldNotCrash() { when(mProvider.isEnhancedBatteryPredictionEnabled(mContext)).thenReturn(true); when(mProvider.getEnhancedBatteryPrediction(mContext)).thenReturn(null); when(mContext.registerReceiver(nullable(BroadcastReceiver.class), any(IntentFilter.class))).thenReturn(new Intent()); //Should not crash assertThat(mBatteryUtils.getBatteryInfo(mBatteryStatsHelper, TAG)).isNotNull(); } }