Merge "Invoke close() method for BatteryUsageStats to close cursor window" into tm-qpr-dev
diff --git a/src/com/android/settings/fuelgauge/BatteryBroadcastReceiver.java b/src/com/android/settings/fuelgauge/BatteryBroadcastReceiver.java
index 436cde8..558e0bf 100644
--- a/src/com/android/settings/fuelgauge/BatteryBroadcastReceiver.java
+++ b/src/com/android/settings/fuelgauge/BatteryBroadcastReceiver.java
@@ -28,7 +28,6 @@
import androidx.annotation.VisibleForTesting;
import com.android.settings.Utils;
-import com.android.settings.homepage.contextualcards.slices.BatteryFixSlice;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -135,6 +134,5 @@
mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_SAVER);
}
}
- BatteryFixSlice.updateBatteryTipAvailabilityCache(mContext);
}
}
\ No newline at end of file
diff --git a/src/com/android/settings/homepage/contextualcards/SettingsContextualCardProvider.java b/src/com/android/settings/homepage/contextualcards/SettingsContextualCardProvider.java
index 067fcf9..81142c2 100644
--- a/src/com/android/settings/homepage/contextualcards/SettingsContextualCardProvider.java
+++ b/src/com/android/settings/homepage/contextualcards/SettingsContextualCardProvider.java
@@ -50,12 +50,6 @@
.setCardName(CustomSliceRegistry.LOW_STORAGE_SLICE_URI.toString())
.setCardCategory(ContextualCard.Category.IMPORTANT)
.build();
- final ContextualCard batteryFixCard =
- ContextualCard.newBuilder()
- .setSliceUri(CustomSliceRegistry.BATTERY_FIX_SLICE_URI.toString())
- .setCardName(CustomSliceRegistry.BATTERY_FIX_SLICE_URI.toString())
- .setCardCategory(ContextualCard.Category.IMPORTANT)
- .build();
final String contextualAdaptiveSleepSliceUri =
CustomSliceRegistry.CONTEXTUAL_ADAPTIVE_SLEEP_URI.toString();
final ContextualCard contextualAdaptiveSleepCard =
@@ -80,7 +74,6 @@
.addCard(wifiCard)
.addCard(connectedDeviceCard)
.addCard(lowStorageCard)
- .addCard(batteryFixCard)
.addCard(contextualAdaptiveSleepCard)
.addCard(contextualFaceSettingsCard)
.addCard(darkThemeCard)
diff --git a/src/com/android/settings/homepage/contextualcards/slices/BatteryFixSlice.java b/src/com/android/settings/homepage/contextualcards/slices/BatteryFixSlice.java
deleted file mode 100644
index 33dcd4e..0000000
--- a/src/com/android/settings/homepage/contextualcards/slices/BatteryFixSlice.java
+++ /dev/null
@@ -1,258 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.settings.homepage.contextualcards.slices;
-
-import static android.content.Context.MODE_PRIVATE;
-
-import static com.android.settings.slices.CustomSliceRegistry.BATTERY_FIX_SLICE_URI;
-
-import android.app.PendingIntent;
-import android.app.settings.SettingsEnums;
-import android.content.Context;
-import android.content.Intent;
-import android.content.SharedPreferences;
-import android.graphics.PorterDuff;
-import android.graphics.PorterDuffColorFilter;
-import android.graphics.drawable.Drawable;
-import android.net.Uri;
-import android.os.BatteryUsageStats;
-import android.util.ArrayMap;
-import android.view.View;
-
-import androidx.annotation.VisibleForTesting;
-import androidx.annotation.WorkerThread;
-import androidx.core.graphics.drawable.IconCompat;
-import androidx.slice.Slice;
-import androidx.slice.builders.ListBuilder;
-import androidx.slice.builders.ListBuilder.RowBuilder;
-import androidx.slice.builders.SliceAction;
-
-import com.android.settings.R;
-import com.android.settings.SubSettings;
-import com.android.settings.Utils;
-import com.android.settings.fuelgauge.batterytip.BatteryTipLoader;
-import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
-import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
-import com.android.settings.fuelgauge.batteryusage.BatteryUsageStatsLoader;
-import com.android.settings.fuelgauge.batteryusage.PowerUsageSummary;
-import com.android.settings.slices.CustomSliceable;
-import com.android.settings.slices.SliceBackgroundWorker;
-import com.android.settings.slices.SliceBuilderUtils;
-import com.android.settingslib.utils.ThreadUtils;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-
-public class BatteryFixSlice implements CustomSliceable {
-
- @VisibleForTesting
- static final String PREFS = "battery_fix_prefs";
- @VisibleForTesting
- static final String KEY_CURRENT_TIPS_TYPE = "current_tip_type";
- static final String KEY_CURRENT_TIPS_STATE = "current_tip_state";
-
- // A map tracking which BatteryTip and which state of that tip is not important.
- private static final Map<Integer, List<Integer>> UNIMPORTANT_BATTERY_TIPS;
-
- static {
- UNIMPORTANT_BATTERY_TIPS = new ArrayMap<>();
- UNIMPORTANT_BATTERY_TIPS.put(BatteryTip.TipType.SUMMARY,
- Arrays.asList(BatteryTip.StateType.NEW, BatteryTip.StateType.HANDLED));
- UNIMPORTANT_BATTERY_TIPS.put(BatteryTip.TipType.HIGH_DEVICE_USAGE,
- Arrays.asList(BatteryTip.StateType.NEW, BatteryTip.StateType.HANDLED));
- UNIMPORTANT_BATTERY_TIPS.put(BatteryTip.TipType.BATTERY_SAVER,
- Arrays.asList(BatteryTip.StateType.HANDLED));
- }
-
- private static final String TAG = "BatteryFixSlice";
-
- private final Context mContext;
-
- public BatteryFixSlice(Context context) {
- mContext = context;
- }
-
- @Override
- public Uri getUri() {
- return BATTERY_FIX_SLICE_URI;
- }
-
- @Override
- public Slice getSlice() {
- final ListBuilder sliceBuilder =
- new ListBuilder(mContext, BATTERY_FIX_SLICE_URI, ListBuilder.INFINITY)
- .setAccentColor(COLOR_NOT_TINTED);
-
- if (!isBatteryTipAvailableFromCache(mContext)) {
- return buildBatteryGoodSlice(sliceBuilder, true /* isError */);
- }
-
- final SliceBackgroundWorker worker = SliceBackgroundWorker.getInstance(getUri());
- final List<BatteryTip> batteryTips = worker != null ? worker.getResults() : null;
-
- if (batteryTips == null) {
- // Because we need wait slice background worker return data
- return buildBatteryGoodSlice(sliceBuilder, false /* isError */);
- }
-
- for (BatteryTip batteryTip : batteryTips) {
- if (batteryTip.getState() == BatteryTip.StateType.INVISIBLE) {
- continue;
- }
- final Drawable drawable = mContext.getDrawable(batteryTip.getIconId());
- final int iconTintColorId = batteryTip.getIconTintColorId();
- if (iconTintColorId != View.NO_ID) {
- drawable.setColorFilter(new PorterDuffColorFilter(
- mContext.getResources().getColor(iconTintColorId),
- PorterDuff.Mode.SRC_IN));
- }
-
- final IconCompat icon = Utils.createIconWithDrawable(drawable);
- final SliceAction primaryAction = SliceAction.createDeeplink(getPrimaryAction(),
- icon,
- ListBuilder.ICON_IMAGE,
- batteryTip.getTitle(mContext));
- sliceBuilder.addRow(new RowBuilder()
- .setTitleItem(icon, ListBuilder.ICON_IMAGE)
- .setTitle(batteryTip.getTitle(mContext))
- .setSubtitle(batteryTip.getSummary(mContext))
- .setPrimaryAction(primaryAction));
- break;
- }
- return sliceBuilder.build();
- }
-
- @Override
- public Intent getIntent() {
- final String screenTitle = mContext.getText(R.string.power_usage_summary_title)
- .toString();
- final Uri contentUri = new Uri.Builder()
- .appendPath(BatteryTipPreferenceController.PREF_NAME).build();
-
- return SliceBuilderUtils.buildSearchResultPageIntent(mContext,
- PowerUsageSummary.class.getName(), BatteryTipPreferenceController.PREF_NAME,
- screenTitle,
- SettingsEnums.SLICE,
- this)
- .setClassName(mContext.getPackageName(), SubSettings.class.getName())
- .setData(contentUri);
- }
-
- @Override
- public int getSliceHighlightMenuRes() {
- return R.string.menu_key_battery;
- }
-
- @Override
- public void onNotifyChange(Intent intent) {
- }
-
- @Override
- public Class getBackgroundWorkerClass() {
- return BatteryTipWorker.class;
- }
-
- private PendingIntent getPrimaryAction() {
- final Intent intent = getIntent();
- return PendingIntent.getActivity(mContext, 0 /* requestCode */, intent,
- PendingIntent.FLAG_IMMUTABLE);
- }
-
- private Slice buildBatteryGoodSlice(ListBuilder sliceBuilder, boolean isError) {
- final IconCompat icon = IconCompat.createWithResource(mContext,
- R.drawable.ic_battery_status_good_24dp);
- final String title = mContext.getString(R.string.power_usage_summary_title);
- final SliceAction primaryAction = SliceAction.createDeeplink(getPrimaryAction(), icon,
- ListBuilder.ICON_IMAGE, title);
- sliceBuilder.addRow(new RowBuilder()
- .setTitleItem(icon, ListBuilder.ICON_IMAGE)
- .setTitle(title)
- .setPrimaryAction(primaryAction))
- .setIsError(isError);
- return sliceBuilder.build();
- }
-
- // TODO(b/114807643): we should find a better way to get current battery tip type quickly
- // Now we save battery tip type to shared preference when battery level changes
- public static void updateBatteryTipAvailabilityCache(Context context) {
- ThreadUtils.postOnBackgroundThread(() -> refreshBatteryTips(context));
- }
-
-
- @VisibleForTesting
- static boolean isBatteryTipAvailableFromCache(Context context) {
- final SharedPreferences prefs = context.getSharedPreferences(PREFS, MODE_PRIVATE);
-
- final int type = prefs.getInt(KEY_CURRENT_TIPS_TYPE, BatteryTip.TipType.SUMMARY);
- final int state = prefs.getInt(KEY_CURRENT_TIPS_STATE, BatteryTip.StateType.INVISIBLE);
- if (state == BatteryTip.StateType.INVISIBLE) {
- // State is INVISIBLE, We should not show anything.
- return false;
- }
- final boolean unimportant = UNIMPORTANT_BATTERY_TIPS.containsKey(type)
- && UNIMPORTANT_BATTERY_TIPS.get(type).contains(state);
- return !unimportant;
- }
-
- @WorkerThread
- @VisibleForTesting
- static List<BatteryTip> refreshBatteryTips(Context context) {
- final BatteryUsageStatsLoader statsLoader = new BatteryUsageStatsLoader(context,
- /* includeBatteryHistory */ false);
- final BatteryUsageStats batteryUsageStats = statsLoader.loadInBackground();
- final BatteryTipLoader loader = new BatteryTipLoader(context, batteryUsageStats);
- final List<BatteryTip> batteryTips = loader.loadInBackground();
- for (BatteryTip batteryTip : batteryTips) {
- if (batteryTip.getState() != BatteryTip.StateType.INVISIBLE) {
- context.getSharedPreferences(PREFS, MODE_PRIVATE)
- .edit()
- .putInt(KEY_CURRENT_TIPS_TYPE, batteryTip.getType())
- .putInt(KEY_CURRENT_TIPS_STATE, batteryTip.getState())
- .apply();
- break;
- }
- }
- return batteryTips;
- }
-
- public static class BatteryTipWorker extends SliceBackgroundWorker<BatteryTip> {
-
- private final Context mContext;
-
- public BatteryTipWorker(Context context, Uri uri) {
- super(context, uri);
- mContext = context;
- }
-
- @Override
- protected void onSlicePinned() {
- ThreadUtils.postOnBackgroundThread(() -> {
- final List<BatteryTip> batteryTips = refreshBatteryTips(mContext);
- updateResults(batteryTips);
- });
- }
-
- @Override
- protected void onSliceUnpinned() {
- }
-
- @Override
- public void close() {
- }
- }
-}
diff --git a/src/com/android/settings/slices/CustomSliceRegistry.java b/src/com/android/settings/slices/CustomSliceRegistry.java
index 569a0ea..c49d622 100644
--- a/src/com/android/settings/slices/CustomSliceRegistry.java
+++ b/src/com/android/settings/slices/CustomSliceRegistry.java
@@ -29,8 +29,6 @@
import com.android.settings.display.AlwaysOnDisplaySlice;
import com.android.settings.display.ScreenTimeoutPreferenceController;
import com.android.settings.flashlight.FlashlightSlice;
-import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
-import com.android.settings.homepage.contextualcards.slices.BatteryFixSlice;
import com.android.settings.homepage.contextualcards.slices.BluetoothDevicesSlice;
import com.android.settings.homepage.contextualcards.slices.ContextualAdaptiveSleepSlice;
import com.android.settings.homepage.contextualcards.slices.DarkThemeSlice;
@@ -65,16 +63,6 @@
.build();
/**
- * Uri for Battery Fix Slice.
- */
- public static final Uri BATTERY_FIX_SLICE_URI = new Uri.Builder()
- .scheme(ContentResolver.SCHEME_CONTENT)
- .authority(SettingsSliceProvider.SLICE_AUTHORITY)
- .appendEncodedPath(SettingsSlicesContract.PATH_SETTING_INTENT)
- .appendPath(BatteryTipPreferenceController.PREF_NAME)
- .build();
-
- /**
* Backing Uri for the Bluetooth Slice.
*/
public static final Uri BLUETOOTH_URI = new Uri.Builder()
@@ -331,7 +319,6 @@
static {
sUriToSlice = new ArrayMap<>();
- sUriToSlice.put(BATTERY_FIX_SLICE_URI, BatteryFixSlice.class);
sUriToSlice.put(BLUETOOTH_DEVICES_SLICE_URI, BluetoothDevicesSlice.class);
sUriToSlice.put(CONTEXTUAL_ADAPTIVE_SLEEP_URI, ContextualAdaptiveSleepSlice.class);
sUriToSlice.put(CONTEXTUAL_WIFI_SLICE_URI, ContextualWifiSlice.class);
diff --git a/tests/robotests/src/com/android/settings/accounts/AvatarViewMixinTest.java b/tests/robotests/src/com/android/settings/accounts/AvatarViewMixinTest.java
index 04db527..61017f7 100644
--- a/tests/robotests/src/com/android/settings/accounts/AvatarViewMixinTest.java
+++ b/tests/robotests/src/com/android/settings/accounts/AvatarViewMixinTest.java
@@ -41,7 +41,6 @@
import android.widget.ImageView;
import com.android.settings.homepage.SettingsHomepageActivity;
-import com.android.settings.homepage.contextualcards.slices.BatteryFixSliceTest;
import com.android.settings.testutils.shadow.ShadowUserManager;
import org.junit.Before;
@@ -95,7 +94,6 @@
}
@Test
- @Config(shadows = BatteryFixSliceTest.ShadowBatteryTipLoader.class)
public void onStart_useMockAvatarViewMixin_shouldBeExecuted() {
final AvatarViewMixin mockAvatar = spy(new AvatarViewMixin(mActivity, mImageView));
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryBroadcastReceiverTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryBroadcastReceiverTest.java
index d446930..867d8f4 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryBroadcastReceiverTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryBroadcastReceiverTest.java
@@ -33,7 +33,6 @@
import android.os.PowerManager;
import com.android.settings.Utils;
-import com.android.settings.homepage.contextualcards.slices.BatteryFixSliceTest;
import org.junit.Before;
import org.junit.Test;
@@ -42,7 +41,6 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
-import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
public class BatteryBroadcastReceiverTest {
@@ -77,9 +75,6 @@
}
@Test
- @Config(shadows = {
- BatteryFixSliceTest.ShadowBatteryTipLoader.class
- })
public void testOnReceive_batteryLevelChanged_dataUpdated() {
mBatteryBroadcastReceiver.onReceive(mContext, mChargingIntent);
@@ -91,9 +86,6 @@
}
@Test
- @Config(shadows = {
- BatteryFixSliceTest.ShadowBatteryTipLoader.class
- })
public void testOnReceive_batteryHealthChanged_dataUpdated() {
mChargingIntent
.putExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_OVERHEAT);
@@ -105,9 +97,6 @@
}
@Test
- @Config(shadows = {
- BatteryFixSliceTest.ShadowBatteryTipLoader.class
- })
public void onReceive_batteryNotPresent_shouldShowHelpMessage() {
mChargingIntent.putExtra(BatteryManager.EXTRA_PRESENT, false);
@@ -117,9 +106,6 @@
}
@Test
- @Config(shadows = {
- BatteryFixSliceTest.ShadowBatteryTipLoader.class
- })
public void testOnReceive_powerSaveModeChanged_listenerInvoked() {
mBatteryBroadcastReceiver.onReceive(mContext,
new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
@@ -128,9 +114,6 @@
}
@Test
- @Config(shadows = {
- BatteryFixSliceTest.ShadowBatteryTipLoader.class
- })
public void testOnReceive_batteryDataNotChanged_listenerNotInvoked() {
final String batteryLevel = Utils.getBatteryPercentage(mChargingIntent);
final String batteryStatus =
@@ -148,9 +131,6 @@
}
@Test
- @Config(shadows = {
- BatteryFixSliceTest.ShadowBatteryTipLoader.class
- })
public void testRegister_updateBatteryStatus() {
doReturn(mChargingIntent).when(mContext).registerReceiver(any(), any());
diff --git a/tests/robotests/src/com/android/settings/homepage/SettingsHomepageActivityTest.java b/tests/robotests/src/com/android/settings/homepage/SettingsHomepageActivityTest.java
index 86d6e12..7387407 100644
--- a/tests/robotests/src/com/android/settings/homepage/SettingsHomepageActivityTest.java
+++ b/tests/robotests/src/com/android/settings/homepage/SettingsHomepageActivityTest.java
@@ -39,7 +39,6 @@
import com.android.settings.R;
import com.android.settings.dashboard.suggestions.SuggestionFeatureProviderImpl;
-import com.android.settings.homepage.contextualcards.slices.BatteryFixSliceTest;
import com.android.settings.testutils.shadow.ShadowActivityEmbeddingUtils;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settingslib.core.lifecycle.HideNonSystemOverlayMixin;
@@ -151,9 +150,6 @@
}
@Test
- @Config(shadows = {
- BatteryFixSliceTest.ShadowBatteryTipLoader.class
- })
public void onStart_isNotDebuggable_shouldHideSystemOverlay() {
ReflectionHelpers.setStaticField(Build.class, "IS_DEBUGGABLE", false);
@@ -170,9 +166,6 @@
}
@Test
- @Config(shadows = {
- BatteryFixSliceTest.ShadowBatteryTipLoader.class,
- })
public void onStop_isNotDebuggable_shouldRemoveHideSystemOverlay() {
ReflectionHelpers.setStaticField(Build.class, "IS_DEBUGGABLE", false);
diff --git a/tests/robotests/src/com/android/settings/homepage/contextualcards/ContextualCardManagerTest.java b/tests/robotests/src/com/android/settings/homepage/contextualcards/ContextualCardManagerTest.java
index 69333d7..f18d94f 100644
--- a/tests/robotests/src/com/android/settings/homepage/contextualcards/ContextualCardManagerTest.java
+++ b/tests/robotests/src/com/android/settings/homepage/contextualcards/ContextualCardManagerTest.java
@@ -135,7 +135,7 @@
final List<String> actualCards = mManager.mSavedCards.stream().collect(Collectors.toList());
final List<String> expectedCards = Arrays.asList("test_low_storage", "test_flashlight",
- "test_dark_theme", "test_gesture", "test_battery");
+ "test_dark_theme", "test_gesture", "test_face_enroll");
assertThat(actualCards).containsExactlyElementsIn(expectedCards);
}
@@ -695,9 +695,9 @@
.setViewType(VIEW_TYPE_FULL_WIDTH)
.build());
cards.add(new ContextualCard.Builder()
- .setName("test_battery")
+ .setName("test_face_enroll")
.setCardType(ContextualCard.CardType.SLICE)
- .setSliceUri(CustomSliceRegistry.BATTERY_FIX_SLICE_URI)
+ .setSliceUri(CustomSliceRegistry.FACE_ENROLL_SLICE_URI)
.setViewType(VIEW_TYPE_FULL_WIDTH)
.build());
return cards;
diff --git a/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/BatteryFixSliceTest.java b/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/BatteryFixSliceTest.java
deleted file mode 100644
index 78541db..0000000
--- a/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/BatteryFixSliceTest.java
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.settings.homepage.contextualcards.slices;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import android.content.Context;
-import android.net.Uri;
-import android.os.BatteryUsageStats;
-
-import androidx.slice.Slice;
-import androidx.slice.SliceMetadata;
-import androidx.slice.SliceProvider;
-import androidx.slice.widget.SliceLiveData;
-
-import com.android.settings.R;
-import com.android.settings.fuelgauge.batterytip.AppInfo;
-import com.android.settings.fuelgauge.batterytip.BatteryTipLoader;
-import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
-import com.android.settings.fuelgauge.batterytip.tips.EarlyWarningTip;
-import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
-import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
-import com.android.settings.fuelgauge.batteryusage.BatteryUsageStatsLoader;
-import com.android.settings.slices.SliceBackgroundWorker;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.MockitoAnnotations;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
-import org.robolectric.annotation.Config;
-import org.robolectric.annotation.Implementation;
-import org.robolectric.annotation.Implements;
-import org.robolectric.annotation.Resetter;
-
-import java.util.ArrayList;
-import java.util.List;
-
-@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {
- BatteryFixSliceTest.ShadowBatteryUsageStatsLoader.class,
- BatteryFixSliceTest.ShadowBatteryTipLoader.class
-})
-public class BatteryFixSliceTest {
-
- private Context mContext;
- private BatteryFixSlice mSlice;
-
- @Before
- public void setUp() {
- MockitoAnnotations.initMocks(this);
- mContext = RuntimeEnvironment.application;
- mSlice = new BatteryFixSlice(mContext);
-
- // Set-up specs for SliceMetadata.
- SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
- }
-
- @After
- public void tearDown() {
- ShadowBatteryTipLoader.reset();
- ShadowSliceBackgroundWorker.reset();
- ShadowEarlyWarningTip.reset();
- }
-
- @Test
- public void refreshBatteryTips_hasImportantTip_shouldReturnTrue() {
- final List<BatteryTip> tips = new ArrayList<>();
- tips.add(new LowBatteryTip(BatteryTip.StateType.INVISIBLE, false));
- tips.add(new EarlyWarningTip(BatteryTip.StateType.NEW, false));
- ShadowBatteryTipLoader.setBatteryTips(tips);
-
- BatteryFixSlice.refreshBatteryTips(mContext);
-
- assertThat(BatteryFixSlice.isBatteryTipAvailableFromCache(mContext)).isTrue();
- }
-
- @Test
- public void getSlice_unimportantSlice_shouldSkip() {
- final List<BatteryTip> tips = new ArrayList<>();
- final List<AppInfo> appList = new ArrayList<>();
- appList.add(new AppInfo.Builder()
- .setPackageName("com.android.settings")
- .setScreenOnTimeMs(10000L)
- .build());
- tips.add(new LowBatteryTip(BatteryTip.StateType.INVISIBLE, false));
- tips.add(new EarlyWarningTip(BatteryTip.StateType.HANDLED, false));
- tips.add(new HighUsageTip(1000L, appList));
- ShadowBatteryTipLoader.setBatteryTips(tips);
-
- BatteryFixSlice.refreshBatteryTips(mContext);
- final Slice slice = mSlice.getSlice();
-
- assertThat(SliceMetadata.from(mContext, slice).isErrorSlice()).isTrue();
- }
-
- @Test
- @Config(shadows = {
- BatteryFixSliceTest.ShadowEarlyWarningTip.class,
- BatteryFixSliceTest.ShadowSliceBackgroundWorker.class
- })
- public void getSlice_hasImportantTip_shouldTintIcon() {
- final List<BatteryTip> tips = new ArrayList<>();
- tips.add(new EarlyWarningTip(BatteryTip.StateType.NEW, false));
- // Create fake cache data
- ShadowBatteryTipLoader.setBatteryTips(tips);
- BatteryFixSlice.refreshBatteryTips(mContext);
- // Create fake background worker data
- BatteryFixSlice.BatteryTipWorker batteryTipWorker = mock(
- BatteryFixSlice.BatteryTipWorker.class);
- when(batteryTipWorker.getResults()).thenReturn(tips);
- ShadowSliceBackgroundWorker.setBatteryTipWorkerWorker(batteryTipWorker);
-
- final Slice slice = mSlice.getSlice();
-
- assertThat(ShadowEarlyWarningTip.isIconTintColorIdCalled()).isTrue();
- }
-
- @Implements(BatteryUsageStatsLoader.class)
- public static class ShadowBatteryUsageStatsLoader {
-
- @Implementation
- protected BatteryUsageStats loadInBackground() {
- return null;
- }
- }
-
- @Implements(BatteryTipLoader.class)
- public static class ShadowBatteryTipLoader {
-
- private static List<BatteryTip> sBatteryTips = new ArrayList<>();
-
- @Resetter
- public static void reset() {
- sBatteryTips = new ArrayList<>();
- }
-
- @Implementation
- protected List<BatteryTip> loadInBackground() {
- return sBatteryTips;
- }
-
- private static void setBatteryTips(List<BatteryTip> tips) {
- sBatteryTips = tips;
- }
- }
-
- @Implements(SliceBackgroundWorker.class)
- public static class ShadowSliceBackgroundWorker {
-
- private static BatteryFixSlice.BatteryTipWorker sBatteryTipWorkerWorker;
-
- @Resetter
- public static void reset() {
- sBatteryTipWorkerWorker = null;
- }
-
- @Implementation
- protected static <T extends SliceBackgroundWorker> T getInstance(Uri uri) {
- return (T) sBatteryTipWorkerWorker;
- }
-
- public static void setBatteryTipWorkerWorker(BatteryFixSlice.BatteryTipWorker worker) {
- sBatteryTipWorkerWorker = worker;
- }
- }
-
- @Implements(EarlyWarningTip.class)
- public static class ShadowEarlyWarningTip {
-
- private static boolean mIsGetIconTintColorIdCalled;
-
- @Resetter
- public static void reset() {
- mIsGetIconTintColorIdCalled = false;
- }
-
- @Implementation
- protected int getIconTintColorId() {
- mIsGetIconTintColorIdCalled = true;
- return R.color.battery_bad_color_light;
- }
-
- public static boolean isIconTintColorIdCalled() {
- return mIsGetIconTintColorIdCalled;
- }
- }
-}