Clear static icon and label cache if ui mode is changed

we should clear icon cache if theme is changed between light and dark
theme, otherwise we may show incorrect styled icon for applications

Bug: 185187729
Test: make SettingsRoboTests
Change-Id: Ib06abe5a5345b2ea363a04ecaec886a4765fd8ab
This commit is contained in:
ykhung
2021-05-12 14:26:30 +08:00
committed by YUKAI HUNG
parent 0d00ace0d2
commit 5a783d0815
2 changed files with 48 additions and 1 deletions

View File

@@ -29,6 +29,8 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.format.DateUtils;
@@ -76,6 +78,8 @@ public final class BatteryChartPreferenceControllerTest {
@Mock private PowerGaugePreference mPowerGaugePreference;
@Mock private ExpandDividerPreference mExpandDividerPreference;
@Mock private BatteryUtils mBatteryUtils;
@Mock private Configuration mConfiguration;
@Mock private Resources mResources;
private Context mContext;
private BatteryDiffEntry mBatteryDiffEntry;
@@ -104,6 +108,33 @@ public final class BatteryChartPreferenceControllerTest {
createBatteryHistoryMap());
}
@Test
public void testOnResume_uiModeIsChanged_clearBatteryDiffEntryCache() {
doReturn(mResources).when(mContext).getResources();
doReturn(mConfiguration).when(mResources).getConfiguration();
mConfiguration.uiMode = Configuration.UI_MODE_NIGHT_UNDEFINED;
// Ensures the testing environment is correct.
assertThat(BatteryDiffEntry.sResourceCache).hasSize(1);
mBatteryChartPreferenceController.onResume();
// Changes the uiMode in the configuration.
mConfiguration.uiMode = Configuration.UI_MODE_NIGHT_YES;
mBatteryChartPreferenceController.onResume();
assertThat(BatteryDiffEntry.sResourceCache).isEmpty();
}
@Test
public void testOnResume_uiModeIsNotChanged_notClearBatteryDiffEntryCache() {
doReturn(mResources).when(mContext).getResources();
doReturn(mConfiguration).when(mResources).getConfiguration();
mConfiguration.uiMode = Configuration.UI_MODE_NIGHT_UNDEFINED;
// Ensures the testing environment is correct.
assertThat(BatteryDiffEntry.sResourceCache).hasSize(1);
mBatteryChartPreferenceController.onResume();
assertThat(BatteryDiffEntry.sResourceCache).isNotEmpty();
}
@Test
public void testOnDestroy_activityIsChanging_clearBatteryEntryCache() {
doReturn(true).when(mSettingsActivity).isChangingConfigurations();