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.Matchers.any; 21 import static org.mockito.Matchers.anyLong; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.content.Intent; 28 import android.os.BatteryStats; 29 30 import com.android.internal.os.BatteryStatsHelper; 31 import com.android.settings.testutils.BatteryTestUtils; 32 import com.android.settings.testutils.FakeFeatureFactory; 33 import com.android.settings.testutils.SettingsRobolectricTestRunner; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Answers; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RuntimeEnvironment; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 public class BatteryInfoLoaderTest { 45 46 private static final long TEST_TIME_REMAINING = 1000L; 47 48 @Mock (answer = Answers.RETURNS_DEEP_STUBS) 49 private BatteryStatsHelper mHelper; 50 @Mock (answer = Answers.RETURNS_DEEP_STUBS) 51 private BatteryStats mStats; 52 53 private Context mContext; 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 mContext = spy(RuntimeEnvironment.application); 59 FakeFeatureFactory.setupForTest().getPowerUsageFeatureProvider(mContext); 60 61 doReturn(mContext).when(mContext).getApplicationContext(); 62 when(mStats.computeBatteryTimeRemaining(anyLong())).thenReturn(TEST_TIME_REMAINING); 63 doReturn(mStats).when(mHelper).getStats(); 64 65 final Intent dischargingBatteryBroadcast = BatteryTestUtils.getDischargingIntent(); 66 doReturn(dischargingBatteryBroadcast).when(mContext).registerReceiver(any(), any()); 67 } 68 69 @Test 70 public void test_loadInBackground_dischargingOldEstimate_dischargingLabelNotNull() { 71 BatteryInfoLoader loader = new BatteryInfoLoader(mContext, mHelper); 72 loader.batteryUtils = new BatteryUtils(mContext); 73 74 BatteryInfo info = loader.loadInBackground(); 75 76 assertThat(info.remainingLabel).isNotNull(); 77 assertThat(info.remainingTimeUs).isEqualTo(TEST_TIME_REMAINING); 78 } 79 } 80