1 /*
2  * Copyright (C) 2021 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 
17 package com.android.settings.fuelgauge;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import com.android.settingslib.widget.RadioButtonPreference;
24 
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.robolectric.RobolectricTestRunner;
31 import org.robolectric.RuntimeEnvironment;
32 
33 @RunWith(RobolectricTestRunner.class)
34 public class OptimizedPreferenceControllerTest {
35     private static final int UID = 12345;
36     private static final String PACKAGE_NAME = "com.android.app";
37 
38     private OptimizedPreferenceController mController;
39     private RadioButtonPreference mPreference;
40 
41     @Mock BatteryOptimizeUtils mockBatteryOptimizeUtils;
42 
43     @Before
setUp()44     public void setUp() {
45         MockitoAnnotations.initMocks(this);
46 
47         mController = new OptimizedPreferenceController(
48                 RuntimeEnvironment.application, UID, PACKAGE_NAME);
49         mPreference = new RadioButtonPreference(RuntimeEnvironment.application);
50         mController.mBatteryOptimizeUtils = mockBatteryOptimizeUtils;
51     }
52 
53     @Test
testUpdateState_invalidPackage_prefEnabled()54     public void testUpdateState_invalidPackage_prefEnabled() {
55         when(mockBatteryOptimizeUtils.isValidPackageName()).thenReturn(false);
56 
57         mController.updateState(mPreference);
58 
59         assertThat(mPreference.isEnabled()).isTrue();
60         assertThat(mPreference.isChecked()).isTrue();
61     }
62 
63     @Test
testUpdateState_isSystemOrDefaultApp_prefUnchecked()64     public void testUpdateState_isSystemOrDefaultApp_prefUnchecked() {
65         when(mockBatteryOptimizeUtils.isValidPackageName()).thenReturn(true);
66         when(mockBatteryOptimizeUtils.isSystemOrDefaultApp()).thenReturn(true);
67 
68         mController.updateState(mPreference);
69 
70         assertThat(mPreference.isChecked()).isFalse();
71         assertThat(mPreference.isEnabled()).isFalse();
72     }
73 
74     @Test
testUpdateState_isOptimizedStates_prefChecked()75     public void testUpdateState_isOptimizedStates_prefChecked() {
76         when(mockBatteryOptimizeUtils.isValidPackageName()).thenReturn(true);
77         when(mockBatteryOptimizeUtils.getAppOptimizationMode()).thenReturn(
78                 BatteryOptimizeUtils.MODE_OPTIMIZED);
79 
80         mController.updateState(mPreference);
81 
82         assertThat(mPreference.isChecked()).isTrue();
83     }
84 
85     @Test
testUpdateState_prefUnchecked()86     public void testUpdateState_prefUnchecked() {
87         when(mockBatteryOptimizeUtils.isValidPackageName()).thenReturn(true);
88 
89         mController.updateState(mPreference);
90 
91         assertThat(mPreference.isChecked()).isFalse();
92     }
93 
94     @Test
testHandlePreferenceTreeClick_samePrefKey_verifyAction()95     public void testHandlePreferenceTreeClick_samePrefKey_verifyAction() {
96         mPreference.setKey(mController.KEY_OPTIMIZED_PREF);
97         mController.handlePreferenceTreeClick(mPreference);
98 
99         assertThat(mController.handlePreferenceTreeClick(mPreference)).isTrue();
100     }
101 
102     @Test
testHandlePreferenceTreeClick_incorrectPrefKey_noAction()103     public void testHandlePreferenceTreeClick_incorrectPrefKey_noAction() {
104         assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse();
105     }
106 }
107