2 * Copyright (C) 2018 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.android.settings.fuelgauge.batterytip;
18 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.spy;
21 import static org.mockito.Mockito.when;
23 import android.app.AppOpsManager;
24 import android.content.Context;
25 import android.provider.Settings;
26 import android.support.v7.preference.Preference;
28 import com.android.settings.testutils.FakeFeatureFactory;
29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RuntimeEnvironment;
38 @RunWith(SettingsRobolectricTestRunner.class)
39 public class BatteryManagerPreferenceControllerTest {
40 private static final int ON = 1;
41 private static final int OFF = 0;
44 private AppOpsManager mAppOpsManager;
47 private Context mContext;
48 private Preference mPreference;
49 private FakeFeatureFactory mFeatureFactory;
50 private BatteryManagerPreferenceController mController;
55 MockitoAnnotations.initMocks(this);
57 mContext = spy(RuntimeEnvironment.application);
58 when(mContext.getSystemService(AppOpsManager.class)).thenReturn(mAppOpsManager);
59 mFeatureFactory = FakeFeatureFactory.setupForTest();
60 mPreference = new Preference(mContext);
61 mController = new BatteryManagerPreferenceController(mContext);
65 public void updateState_smartBatteryOnWithRestrictApps_showSummary() {
66 mController.updateSummary(mPreference, true /* smartBatteryOn */, 2);
68 assertThat(mPreference.getSummary()).isEqualTo("2 apps restricted");
72 public void updateState_smartBatteryOnWithoutRestriction_showSummary() {
73 when(mFeatureFactory.powerUsageFeatureProvider.isSmartBatterySupported()).thenReturn(true);
74 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.APP_STANDBY_ENABLED,
77 mController.updateState(mPreference);
79 assertThat(mPreference.getSummary()).isEqualTo("On / Detecting when apps drain battery");
83 public void updateState_smartBatteryOff_showSummary() {
84 when(mFeatureFactory.powerUsageFeatureProvider.isSmartBatterySupported()).thenReturn(true);
85 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.APP_STANDBY_ENABLED,
88 mController.updateState(mPreference);
90 assertThat(mPreference.getSummary()).isEqualTo("Off");