1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * 15 * 16 */ 17 18package com.android.settings.fuelgauge; 19 20import static com.google.common.truth.Truth.assertThat; 21import static org.mockito.ArgumentMatchers.nullable; 22import static org.mockito.Matchers.any; 23import static org.mockito.Matchers.eq; 24import static org.mockito.Mockito.doReturn; 25import static org.mockito.Mockito.spy; 26import static org.mockito.Mockito.verify; 27import static org.mockito.Mockito.when; 28 29import android.app.Activity; 30import android.content.Context; 31import android.content.Intent; 32import android.os.BatteryManager; 33import android.support.v14.preference.PreferenceFragment; 34import android.support.v7.preference.PreferenceScreen; 35import android.support.v7.widget.RecyclerView; 36import android.widget.TextView; 37 38import com.android.settings.R; 39import com.android.settings.TestConfig; 40import com.android.settings.applications.LayoutPreference; 41import com.android.settings.testutils.SettingsRobolectricTestRunner; 42import com.android.settings.testutils.shadow.SettingsShadowResources; 43import com.android.settings.testutils.shadow.ShadowEntityHeaderController; 44import com.android.settings.widget.EntityHeaderController; 45import com.android.settingslib.core.lifecycle.Lifecycle; 46 47import org.junit.After; 48import org.junit.Before; 49import org.junit.Test; 50import org.junit.runner.RunWith; 51import org.mockito.Mock; 52import org.mockito.MockitoAnnotations; 53import org.robolectric.RuntimeEnvironment; 54import org.robolectric.annotation.Config; 55 56@RunWith(SettingsRobolectricTestRunner.class) 57@Config(manifest = TestConfig.MANIFEST_PATH, 58 sdk = TestConfig.SDK_VERSION, 59 shadows = { 60 SettingsShadowResources.class, 61 SettingsShadowResources.SettingsShadowTheme.class, 62 ShadowEntityHeaderController.class 63 }) 64public class BatteryHeaderPreferenceControllerTest { 65 private static final int BATTERY_LEVEL = 60; 66 private static final String TIME_LEFT = "2h30min"; 67 private static final String BATTERY_STATUS = "Charging"; 68 69 @Mock 70 private Activity mActivity; 71 @Mock 72 private PreferenceFragment mPreferenceFragment; 73 @Mock 74 private PreferenceScreen mPreferenceScreen; 75 @Mock 76 private BatteryInfo mBatteryInfo; 77 @Mock 78 private EntityHeaderController mEntityHeaderController; 79 private BatteryHeaderPreferenceController mController; 80 private Context mContext; 81 private BatteryMeterView mBatteryMeterView; 82 private TextView mBatteryPercentText; 83 private TextView mSummary; 84 private TextView mSummary2; 85 private LayoutPreference mBatteryLayoutPref; 86 private Intent mBatteryIntent; 87 private Lifecycle mLifecycle; 88 89 @Before 90 public void setUp() { 91 MockitoAnnotations.initMocks(this); 92 93 mLifecycle = new Lifecycle(); 94 mContext = spy(RuntimeEnvironment.application); 95 mBatteryMeterView = new BatteryMeterView(mContext); 96 mBatteryPercentText = new TextView(mContext); 97 mSummary = new TextView(mContext); 98 ShadowEntityHeaderController.setUseMock(mEntityHeaderController); 99 mSummary2 = new TextView(mContext); 100 101 mBatteryIntent = new Intent(); 102 mBatteryIntent.putExtra(BatteryManager.EXTRA_LEVEL, BATTERY_LEVEL); 103 mBatteryIntent.putExtra(BatteryManager.EXTRA_SCALE, 100); 104 mBatteryIntent.putExtra(BatteryManager.EXTRA_PLUGGED, 1); 105 doReturn(mBatteryIntent).when(mContext).registerReceiver(any(), any()); 106 107 mBatteryLayoutPref = new LayoutPreference(mContext, R.layout.battery_header); 108 doReturn(mBatteryLayoutPref).when(mPreferenceScreen).findPreference( 109 BatteryHeaderPreferenceController.KEY_BATTERY_HEADER); 110 111 mBatteryInfo.batteryLevel = BATTERY_LEVEL; 112 113 mController = new BatteryHeaderPreferenceController( 114 mContext, mActivity, mPreferenceFragment, mLifecycle); 115 mController.mBatteryMeterView = mBatteryMeterView; 116 mController.mBatteryPercentText = mBatteryPercentText; 117 mController.mSummary1 = mSummary; 118 mController.mSummary2 = mSummary2; 119 } 120 121 @After 122 public void tearDown() { 123 ShadowEntityHeaderController.reset(); 124 } 125 126 @Test 127 public void testDisplayPreference_displayBatteryLevel() { 128 mController.displayPreference(mPreferenceScreen); 129 130 assertThat(((BatteryMeterView) mBatteryLayoutPref.findViewById( 131 R.id.battery_header_icon)).getBatteryLevel()).isEqualTo(BATTERY_LEVEL); 132 assertThat(((TextView) mBatteryLayoutPref.findViewById( 133 R.id.battery_percent)).getText()).isEqualTo("60%"); 134 } 135 136 @Test 137 public void testUpdatePreference_hasRemainingTime_showRemainingLabel() { 138 mBatteryInfo.remainingLabel = TIME_LEFT; 139 140 mController.updateHeaderPreference(mBatteryInfo); 141 142 assertThat(mSummary.getText()).isEqualTo(mBatteryInfo.remainingLabel); 143 } 144 145 @Test 146 public void testUpdatePreference_updateBatteryInfo() { 147 mBatteryInfo.remainingLabel = TIME_LEFT; 148 mBatteryInfo.batteryLevel = BATTERY_LEVEL; 149 mBatteryInfo.discharging = true; 150 151 mController.updateHeaderPreference(mBatteryInfo); 152 153 assertThat(mBatteryMeterView.mDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL); 154 assertThat(mBatteryMeterView.mDrawable.getCharging()).isEqualTo(false); 155 } 156 157 @Test 158 public void testUpdatePreference_noRemainingTime_showStatusLabel() { 159 mBatteryInfo.remainingLabel = null; 160 mBatteryInfo.statusLabel = BATTERY_STATUS; 161 162 mController.updateHeaderPreference(mBatteryInfo); 163 164 assertThat(mSummary.getText()).isEqualTo(BATTERY_STATUS); 165 } 166 167 @Test 168 public void testOnStart_shouldStyleActionBar() { 169 when(mEntityHeaderController.setRecyclerView(nullable(RecyclerView.class), eq(mLifecycle))) 170 .thenReturn(mEntityHeaderController); 171 172 mController.displayPreference(mPreferenceScreen); 173 mLifecycle.onStart(); 174 175 verify(mEntityHeaderController).styleActionBar(mActivity); 176 } 177 178 @Test 179 public void testQuickUpdateHeaderPreference_showBatteryLevelAndChargingState() { 180 mController.quickUpdateHeaderPreference(); 181 182 assertThat(mBatteryMeterView.getBatteryLevel()).isEqualTo(BATTERY_LEVEL); 183 assertThat(mBatteryMeterView.getCharging()).isTrue(); 184 assertThat(mBatteryPercentText.getText()).isEqualTo("60%"); 185 } 186} 187