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.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.content.Intent;
29 import android.os.BatteryStatsManager;
30 import android.os.BatteryUsageStats;
31 import android.os.BatteryUsageStatsQuery;
32 
33 import com.android.settings.testutils.BatteryTestUtils;
34 import com.android.settings.testutils.FakeFeatureFactory;
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 BatteryInfoLoaderTest {
46 
47     private static final long TEST_TIME_REMAINING = 1000L;
48 
49     @Mock private BatteryStatsManager mBatteryStatsManager;
50     @Mock private BatteryUsageStats mBatteryUsageStats;
51 
52     private Context mContext;
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         mContext = spy(RuntimeEnvironment.application);
58         FakeFeatureFactory.setupForTest().getPowerUsageFeatureProvider();
59 
60         doReturn(mContext).when(mContext).getApplicationContext();
61         when(mContext.getSystemService(eq(Context.BATTERY_STATS_SERVICE)))
62                 .thenReturn(mBatteryStatsManager);
63         when(mBatteryUsageStats.getBatteryTimeRemainingMs()).thenReturn(TEST_TIME_REMAINING);
64         when(mBatteryStatsManager.getBatteryUsageStats(any(BatteryUsageStatsQuery.class)))
65                 .thenReturn(mBatteryUsageStats);
66 
67         final Intent dischargingBatteryBroadcast = BatteryTestUtils.getDischargingIntent();
68         doReturn(dischargingBatteryBroadcast).when(mContext).registerReceiver(any(), any());
69     }
70 
71     @Test
test_loadInBackground_dischargingOldEstimate_dischargingLabelNotNull()72     public void test_loadInBackground_dischargingOldEstimate_dischargingLabelNotNull() {
73         BatteryInfoLoader loader = new BatteryInfoLoader(mContext);
74         loader.mBatteryUtils = new BatteryUtils(mContext);
75 
76         BatteryInfo info = loader.loadInBackground();
77 
78         assertThat(info.remainingLabel).isNotNull();
79         assertThat(info.remainingTimeUs).isEqualTo(TEST_TIME_REMAINING * 1000);
80     }
81 }
82