• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
50     private BatteryStatsManager mBatteryStatsManager;
51     @Mock
52     private BatteryUsageStats mBatteryUsageStats;
53 
54     private Context mContext;
55 
56     @Before
setUp()57     public void setUp() {
58         MockitoAnnotations.initMocks(this);
59         mContext = spy(RuntimeEnvironment.application);
60         FakeFeatureFactory.setupForTest().getPowerUsageFeatureProvider(mContext);
61 
62         doReturn(mContext).when(mContext).getApplicationContext();
63         when(mContext.getSystemService(eq(Context.BATTERY_STATS_SERVICE)))
64                 .thenReturn(mBatteryStatsManager);
65         when(mBatteryUsageStats.getBatteryTimeRemainingMs()).thenReturn(TEST_TIME_REMAINING);
66         when(mBatteryStatsManager.getBatteryUsageStats(any(BatteryUsageStatsQuery.class)))
67                 .thenReturn(mBatteryUsageStats);
68 
69         final Intent dischargingBatteryBroadcast = BatteryTestUtils.getDischargingIntent();
70         doReturn(dischargingBatteryBroadcast).when(mContext).registerReceiver(any(), any());
71     }
72 
73     @Test
test_loadInBackground_dischargingOldEstimate_dischargingLabelNotNull()74     public void test_loadInBackground_dischargingOldEstimate_dischargingLabelNotNull() {
75         BatteryInfoLoader loader = new BatteryInfoLoader(mContext);
76         loader.mBatteryUtils = new BatteryUtils(mContext);
77 
78         BatteryInfo info = loader.loadInBackground();
79 
80         assertThat(info.remainingLabel).isNotNull();
81         assertThat(info.remainingTimeUs).isEqualTo(TEST_TIME_REMAINING * 1000);
82     }
83 }
84