• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.ArgumentMatchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 
27 import android.content.Context;
28 import android.content.Intent;
29 
30 import androidx.preference.Preference;
31 import androidx.preference.SwitchPreference;
32 
33 import com.android.settings.R;
34 import com.android.settings.SettingsActivity;
35 import com.android.settings.dashboard.DashboardFragment;
36 import com.android.settingslib.fuelgauge.PowerAllowlistBackend;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RobolectricTestRunner;
44 import org.robolectric.RuntimeEnvironment;
45 
46 @RunWith(RobolectricTestRunner.class)
47 public class BatteryOptimizationPreferenceControllerTest {
48 
49     private static final String PKG_IN_ALLOWLIST = "com.pkg.in.allowlist";
50     private static final String PKG_NOT_IN_ALLOWLIST = "com.pkg.not.in.allowlist";
51     private static final String KEY_OPTIMIZATION = "battery_optimization";
52     private static final String KEY_OTHER = "other";
53     @Mock
54     private SettingsActivity mSettingsActivity;
55     @Mock
56     private DashboardFragment mFragment;
57     @Mock
58     private TestPowerAllowlistBackend mBackend;
59 
60     private BatteryOptimizationPreferenceController mController;
61     private Preference mPreference;
62     private Context mContext;
63 
64     @Before
setUp()65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67 
68         mContext = RuntimeEnvironment.application;
69         doReturn(false).when(mBackend).isAllowlisted(PKG_NOT_IN_ALLOWLIST);
70         doReturn(true).when(mBackend).isAllowlisted(PKG_IN_ALLOWLIST);
71 
72         mPreference = new SwitchPreference(mContext);
73         mController = spy(new BatteryOptimizationPreferenceController(mSettingsActivity, mFragment,
74                 PKG_NOT_IN_ALLOWLIST, mBackend));
75     }
76 
77     @Test
testHandlePreferenceTreeClick_OptimizationPreference_HandleClick()78     public void testHandlePreferenceTreeClick_OptimizationPreference_HandleClick() {
79         mPreference.setKey(KEY_OPTIMIZATION);
80 
81         final boolean handled = mController.handlePreferenceTreeClick(mPreference);
82 
83         assertThat(handled).isTrue();
84         verify(mSettingsActivity).startActivity(any(Intent.class));
85     }
86 
87     @Test
testHandlePreferenceTreeClick_OtherPreference_NotHandleClick()88     public void testHandlePreferenceTreeClick_OtherPreference_NotHandleClick() {
89         mPreference.setKey(KEY_OTHER);
90 
91         final boolean handled = mController.handlePreferenceTreeClick(mPreference);
92 
93         assertThat(handled).isFalse();
94         verify(mSettingsActivity, never()).startActivity(any(Intent.class));
95     }
96 
97     @Test
testUpdateState_appInAllowlist_showSummaryNotOptimized()98     public void testUpdateState_appInAllowlist_showSummaryNotOptimized() {
99         BatteryOptimizationPreferenceController controller =
100                 new BatteryOptimizationPreferenceController(mSettingsActivity, mFragment,
101                         PKG_IN_ALLOWLIST, mBackend);
102 
103         controller.updateState(mPreference);
104 
105         assertThat(mPreference.getSummary()).isEqualTo(mContext.getString(R.string.high_power_on));
106     }
107 
108     @Test
testUpdateState_appNotInAllowlist_showSummaryOptimized()109     public void testUpdateState_appNotInAllowlist_showSummaryOptimized() {
110         mController.updateState(mPreference);
111 
112         assertThat(mPreference.getSummary()).isEqualTo(mContext.getString(R.string.high_power_off));
113     }
114 
115     @Test
testUpdateState_refreshList()116     public void testUpdateState_refreshList() {
117         mController.updateState(mPreference);
118 
119         verify(mBackend).refreshList();
120     }
121 
122     /**
123      * Create this test class so we could mock it
124      */
125     public static class TestPowerAllowlistBackend extends PowerAllowlistBackend {
126 
TestPowerAllowlistBackend(Context context)127         public TestPowerAllowlistBackend(Context context) {
128             super(context);
129         }
130 
131         @Override
refreshList()132         public void refreshList() {
133             // Do nothing so we could mock it without error
134         }
135     }
136 }
137