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.Matchers.any;
22 import static org.mockito.Matchers.anyInt;
23 import static org.mockito.Matchers.anyString;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.verify;
29 
30 import android.app.Fragment;
31 import android.content.Context;
32 import android.os.Bundle;
33 import android.support.v14.preference.SwitchPreference;
34 import android.support.v7.preference.Preference;
35 
36 import com.android.settings.R;
37 import com.android.settings.SettingsActivity;
38 import com.android.settings.TestConfig;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.RuntimeEnvironment;
47 import org.robolectric.annotation.Config;
48 
49 @RunWith(RobolectricTestRunner.class)
50 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
51 public class BatteryOptimizationPreferenceControllerTest {
52     private static final String PKG_IN_WHITELIST = "com.pkg.in.whitelist";
53     private static final String PKG_NOT_IN_WHITELIST = "com.pkg.not.in.whitelist";
54     private static final String KEY_OPTIMIZATION = "battery_optimization";
55     private static final String KEY_OTHER = "other";
56     @Mock
57     private SettingsActivity mSettingsActivity;
58     @Mock
59     private Fragment mFragment;
60     @Mock
61     private TestPowerWhitelistBackend mBackend;
62 
63     private BatteryOptimizationPreferenceController mController;
64     private Preference mPreference;
65     private Context mContext;
66 
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70 
71         mContext = RuntimeEnvironment.application;
72         doReturn(false).when(mBackend).isWhitelisted(PKG_NOT_IN_WHITELIST);
73         doReturn(true).when(mBackend).isWhitelisted(PKG_IN_WHITELIST);
74 
75         mPreference = new SwitchPreference(mContext);
76         mController = spy(new BatteryOptimizationPreferenceController(mSettingsActivity, mFragment,
77                 PKG_NOT_IN_WHITELIST, mBackend));
78     }
79 
80     @Test
testHandlePreferenceTreeClick_OptimizationPreference_HandleClick()81     public void testHandlePreferenceTreeClick_OptimizationPreference_HandleClick() {
82         mPreference.setKey(KEY_OPTIMIZATION);
83 
84         final boolean handled = mController.handlePreferenceTreeClick(mPreference);
85 
86         assertThat(handled).isTrue();
87         verify(mSettingsActivity).startPreferencePanel(any(Fragment.class),
88                 anyString(), any(Bundle.class), anyInt(), any(CharSequence.class),
89                 any(Fragment.class), anyInt());
90     }
91 
92     @Test
testHandlePreferenceTreeClick_OtherPreference_NotHandleClick()93     public void testHandlePreferenceTreeClick_OtherPreference_NotHandleClick() {
94         mPreference.setKey(KEY_OTHER);
95 
96         final boolean handled = mController.handlePreferenceTreeClick(mPreference);
97 
98         assertThat(handled).isFalse();
99         verify(mSettingsActivity, never()).startPreferencePanel(any(Fragment.class),
100                 anyString(), any(Bundle.class), anyInt(), any(CharSequence.class),
101                 any(Fragment.class), anyInt());
102     }
103 
104     @Test
testUpdateState_appInWhitelist_showSummaryNotOptimized()105     public void testUpdateState_appInWhitelist_showSummaryNotOptimized() {
106         BatteryOptimizationPreferenceController controller =
107                 new BatteryOptimizationPreferenceController(mSettingsActivity, mFragment,
108                         PKG_IN_WHITELIST, mBackend);
109 
110         controller.updateState(mPreference);
111 
112         assertThat(mPreference.getSummary()).isEqualTo(mContext.getString(R.string.high_power_on));
113     }
114 
115     @Test
testUpdateState_appNotInWhitelist_showSummaryOptimized()116     public void testUpdateState_appNotInWhitelist_showSummaryOptimized() {
117         mController.updateState(mPreference);
118 
119         assertThat(mPreference.getSummary()).isEqualTo(mContext.getString(R.string.high_power_off));
120     }
121 
122     /**
123      * Create this test class so we could mock it
124      */
125     public static class TestPowerWhitelistBackend extends PowerWhitelistBackend {
126 
127         @Override
refreshList()128         void refreshList() {
129             // Do nothing so we could mock it without error
130         }
131     }
132 }
133