• 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 package com.android.settings.fuelgauge;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.os.BatteryStatsManager;
26 import android.os.BatteryUsageStats;
27 import android.os.BatteryUsageStatsQuery;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Captor;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class BatteryUsageStatsLoaderTest {
41     private Context mContext;
42     @Mock
43     private BatteryStatsManager mBatteryStatsManager;
44     @Mock
45     private BatteryUsageStats mBatteryUsageStats;
46     @Captor
47     private ArgumentCaptor<BatteryUsageStatsQuery> mUsageStatsQueryCaptor;
48 
49     @Before
setUp()50     public void setUp() {
51         MockitoAnnotations.initMocks(this);
52         mContext = spy(RuntimeEnvironment.application);
53         doReturn(mBatteryStatsManager).when(mContext).getSystemService(
54                 Context.BATTERY_STATS_SERVICE);
55     }
56 
57     @Test
testLoadInBackground_loadWithoutHistory()58     public void testLoadInBackground_loadWithoutHistory() {
59         BatteryUsageStatsLoader loader = new BatteryUsageStatsLoader(
60                 mContext, /* includeBatteryHistory */ false);
61 
62         when(mBatteryStatsManager.getBatteryUsageStats(mUsageStatsQueryCaptor.capture()))
63                 .thenReturn(mBatteryUsageStats);
64 
65         loader.loadInBackground();
66 
67         final int queryFlags = mUsageStatsQueryCaptor.getValue().getFlags();
68         assertThat(queryFlags
69                 & BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY).isEqualTo(0);
70     }
71 
72     @Test
testLoadInBackground_loadWithHistory()73     public void testLoadInBackground_loadWithHistory() {
74         BatteryUsageStatsLoader loader = new BatteryUsageStatsLoader(
75                 mContext, /* includeBatteryHistory */ true);
76 
77         when(mBatteryStatsManager.getBatteryUsageStats(mUsageStatsQueryCaptor.capture()))
78                 .thenReturn(mBatteryUsageStats);
79 
80         loader.loadInBackground();
81 
82         final int queryFlags = mUsageStatsQueryCaptor.getValue().getFlags();
83         assertThat(queryFlags
84                 & BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY).isNotEqualTo(0);
85     }
86 }
87