Add summary for battery manager preference

Show different summary based on whether it is on and there is restricted
apps.

Also move method to get restricted app list to BatteryTipUtils since it
is used in 2 places.

Bug: 73018395
Test: RunSettingsRoboTests
Change-Id: Ib2306e0fd1f520fde6d1403ce9c527c241d36005
This commit is contained in:
Lei Yu
2018-03-16 13:25:53 -07:00
parent de0a3a5334
commit 6c16840ff5
6 changed files with 216 additions and 23 deletions

View File

@@ -0,0 +1,92 @@
/*
* 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.fuelgauge.batterytip;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.app.AppOpsManager;
import android.content.Context;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class BatteryManagerPreferenceControllerTest {
private static final int ON = 1;
private static final int OFF = 0;
@Mock
private AppOpsManager mAppOpsManager;
private Context mContext;
private Preference mPreference;
private FakeFeatureFactory mFeatureFactory;
private BatteryManagerPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(AppOpsManager.class)).thenReturn(mAppOpsManager);
mFeatureFactory = FakeFeatureFactory.setupForTest();
mPreference = new Preference(mContext);
mController = new BatteryManagerPreferenceController(mContext);
}
@Test
public void updateState_smartBatteryOnWithRestrictApps_showSummary() {
mController.updateSummary(mPreference, true /* smartBatteryOn */, 2);
assertThat(mPreference.getSummary()).isEqualTo("2 apps restricted");
}
@Test
public void updateState_smartBatteryOnWithoutRestriction_showSummary() {
when(mFeatureFactory.powerUsageFeatureProvider.isSmartBatterySupported()).thenReturn(true);
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.APP_STANDBY_ENABLED,
ON);
mController.updateState(mPreference);
assertThat(mPreference.getSummary()).isEqualTo("On / Restricting apps automatically");
}
@Test
public void updateState_smartBatteryOff_showSummary() {
when(mFeatureFactory.powerUsageFeatureProvider.isSmartBatterySupported()).thenReturn(true);
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.APP_STANDBY_ENABLED,
OFF);
mController.updateState(mPreference);
assertThat(mPreference.getSummary()).isEqualTo("Off");
}
}