OSDN Git Service

37c830661e8d11b89d165a6c774c09e7462bf5e4
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / fuelgauge / batterytip / BatteryManagerPreferenceControllerTest.java
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.android.settings.fuelgauge.batterytip;
17
18 import static com.google.common.truth.Truth.assertThat;
19
20 import static org.mockito.Mockito.spy;
21 import static org.mockito.Mockito.when;
22
23 import android.app.AppOpsManager;
24 import android.content.Context;
25 import android.provider.Settings;
26 import android.support.v7.preference.Preference;
27
28 import com.android.settings.testutils.FakeFeatureFactory;
29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
30
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;
37
38 @RunWith(SettingsRobolectricTestRunner.class)
39 public class BatteryManagerPreferenceControllerTest {
40     private static final int ON = 1;
41     private static final int OFF = 0;
42
43     @Mock
44     private AppOpsManager mAppOpsManager;
45
46
47     private Context mContext;
48     private Preference mPreference;
49     private FakeFeatureFactory mFeatureFactory;
50     private BatteryManagerPreferenceController mController;
51
52
53     @Before
54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56
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);
62     }
63
64     @Test
65     public void updateState_smartBatteryOnWithRestrictApps_showSummary() {
66         mController.updateSummary(mPreference, true /* smartBatteryOn */, 2);
67
68         assertThat(mPreference.getSummary()).isEqualTo("2 apps restricted");
69     }
70
71     @Test
72     public void updateState_smartBatteryOnWithoutRestriction_showSummary() {
73         when(mFeatureFactory.powerUsageFeatureProvider.isSmartBatterySupported()).thenReturn(true);
74         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.APP_STANDBY_ENABLED,
75                 ON);
76
77         mController.updateState(mPreference);
78
79         assertThat(mPreference.getSummary()).isEqualTo("On / Detecting when apps drain battery");
80     }
81
82     @Test
83     public void updateState_smartBatteryOff_showSummary() {
84         when(mFeatureFactory.powerUsageFeatureProvider.isSmartBatterySupported()).thenReturn(true);
85         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.APP_STANDBY_ENABLED,
86                 OFF);
87
88         mController.updateState(mPreference);
89
90         assertThat(mPreference.getSummary()).isEqualTo("Off");
91     }
92 }