Merge "Optimize the tip detector loading performance"

This commit is contained in:
TreeHugger Robot
2023-02-09 03:21:04 +00:00
committed by Android (Google) Code Review
13 changed files with 56 additions and 125 deletions

View File

@@ -114,7 +114,7 @@ public class BatteryTipDialogFragmentTest {
new ArrayList<>(restrictApps));
mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, mAppInfo);
mDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW);
mDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW, false /* isPluggedIn */);
}
@After

View File

@@ -63,7 +63,8 @@ public class BatteryTipUtilsTest {
mRestrictAppTip = spy(new RestrictAppTip(BatteryTip.StateType.NEW, new ArrayList<>()));
mLowBatteryTip = spy(
new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */));
mBatteryDefenderTip = spy(new BatteryDefenderTip(BatteryTip.StateType.NEW));
mBatteryDefenderTip = spy(new BatteryDefenderTip(BatteryTip.StateType.NEW,
false /* isPluggedIn */));
}
@Test

View File

@@ -21,7 +21,6 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.os.PowerManager;
import com.android.settings.fuelgauge.BatteryInfo;
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
@@ -34,8 +33,6 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPowerManager;
import org.robolectric.util.ReflectionHelpers;
import java.util.concurrent.TimeUnit;
@@ -47,7 +44,6 @@ public class LowBatteryDetectorTest {
private BatteryInfo mBatteryInfo;
private BatteryTipPolicy mPolicy;
private LowBatteryDetector mLowBatteryDetector;
private ShadowPowerManager mShadowPowerManager;
private Context mContext;
@Before
@@ -56,18 +52,19 @@ public class LowBatteryDetectorTest {
mPolicy = spy(new BatteryTipPolicy(RuntimeEnvironment.application));
mContext = RuntimeEnvironment.application;
mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class));
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", true);
ReflectionHelpers.setField(mPolicy, "lowBatteryHour", 3);
mBatteryInfo.discharging = true;
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo);
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo,
false /* isPowerSaveMode */);
}
@Test
public void testDetect_disabledByPolicy_tipInvisible() {
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", false);
mShadowPowerManager.setIsPowerSaveMode(true);
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo,
true /* isPowerSaveMode */);
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
}
@@ -92,7 +89,8 @@ public class LowBatteryDetectorTest {
@Test
public void testDetect_batterySaverOn_tipInvisible() {
mShadowPowerManager.setIsPowerSaveMode(true);
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo,
true /* isPowerSaveMode */);
assertThat(mLowBatteryDetector.detect().getState())
.isEqualTo(BatteryTip.StateType.INVISIBLE);

View File

@@ -36,8 +36,6 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPowerManager;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
@@ -50,7 +48,7 @@ public class SmartBatteryDetectorTest {
private ContentResolver mContentResolver;
private SmartBatteryDetector mSmartBatteryDetector;
private BatteryTipPolicy mPolicy;
private ShadowPowerManager mShadowPowerManager;
@Mock
private BatteryInfo mBatteryInfo;
@@ -61,9 +59,8 @@ public class SmartBatteryDetectorTest {
mContext = RuntimeEnvironment.application;
mContentResolver = mContext.getContentResolver();
mPolicy = spy(new BatteryTipPolicy(mContext));
mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class));
mSmartBatteryDetector =
new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo, mContentResolver);
mSmartBatteryDetector = new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo,
mContentResolver, false /* isPowerSaveMode */);
}
@Test
@@ -77,7 +74,6 @@ public class SmartBatteryDetectorTest {
public void testDetect_smartBatteryOff_tipVisible() {
Settings.Global.putInt(mContentResolver,
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0);
mShadowPowerManager.setIsPowerSaveMode(false);
mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL;
assertThat(mSmartBatteryDetector.detect().isVisible()).isTrue();
@@ -87,8 +83,9 @@ public class SmartBatteryDetectorTest {
public void testDetect_batterySaverOn_tipInvisible() {
Settings.Global.putInt(mContentResolver,
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0);
mShadowPowerManager.setIsPowerSaveMode(true);
mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL;
mSmartBatteryDetector = new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo,
mContentResolver, true /* isPowerSaveMode */);
assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();
}
@@ -97,8 +94,9 @@ public class SmartBatteryDetectorTest {
public void testDetect_unexpectedBatteryLevel_tipInvisible() {
Settings.Global.putInt(mContentResolver,
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0);
mShadowPowerManager.setIsPowerSaveMode(true);
mBatteryInfo.batteryLevel = UNEXPECTED_BATTERY_LEVEL;
mSmartBatteryDetector = new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo,
mContentResolver, true /* isPowerSaveMode */);
assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();
}
@@ -107,7 +105,6 @@ public class SmartBatteryDetectorTest {
public void testDetect_smartBatteryOn_tipInvisible() {
Settings.Global.putInt(mContentResolver,
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 1);
mShadowPowerManager.setIsPowerSaveMode(false);
mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL;
assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();

View File

@@ -26,9 +26,6 @@ import static org.mockito.Mockito.when;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.util.Log;
import androidx.preference.Preference;
@@ -66,7 +63,8 @@ public class BatteryDefenderTipTest {
mFeatureFactory = FakeFeatureFactory.setupForTest();
mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider;
mContext = RuntimeEnvironment.application;
mBatteryDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW);
mBatteryDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW,
false /* isPluggedIn */);
when(mPreference.getContext()).thenReturn(mContext);
when(mCardPreference.getContext()).thenReturn(mContext);
@@ -133,7 +131,8 @@ public class BatteryDefenderTipTest {
@Test
public void updatePreference_whenCharging_setPrimaryButtonVisibleToBeTrue() {
fakeDeviceIsCharging(true);
mBatteryDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW,
true /* isPluggedIn */);
mBatteryDefenderTip.updatePreference(mCardPreference);
@@ -142,8 +141,6 @@ public class BatteryDefenderTipTest {
@Test
public void updatePreference_whenNotCharging_setPrimaryButtonVisibleToBeFalse() {
fakeDeviceIsCharging(false);
mBatteryDefenderTip.updatePreference(mCardPreference);
verify(mCardPreference).setPrimaryButtonVisible(false);
@@ -158,23 +155,10 @@ public class BatteryDefenderTipTest {
verify(mCardPreference).setPrimaryButtonVisible(false);
}
private void fakeDeviceIsCharging(boolean charging) {
int charged = charging ? 1 : 0; // 1 means charging, 0:not charging
Intent batteryChangedIntent = new Intent(Intent.ACTION_BATTERY_CHANGED);
batteryChangedIntent.putExtra(BatteryManager.EXTRA_PLUGGED, charged);
Context mockContext = mock(Context.class);
when(mockContext.getString(anyInt())).thenReturn("fake_string");
when(mCardPreference.getContext()).thenReturn(mockContext);
when(mockContext.registerReceiver(eq(null), any(IntentFilter.class)))
.thenReturn(batteryChangedIntent);
}
private void fakeGetChargingStatusFailed() {
Context mockContext = mock(Context.class);
when(mockContext.getString(anyInt())).thenReturn("fake_string");
when(mCardPreference.getContext()).thenReturn(mockContext);
when(mockContext.registerReceiver(eq(null), any(IntentFilter.class))).thenReturn(null);
}
private String getLastErrorLog() {