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