1 /*
2  * Copyright (C) 2018 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.batterytip;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.os.BatteryStats;
22 
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.MockitoAnnotations;
27 
28 import java.time.Duration;
29 import org.robolectric.RobolectricTestRunner;
30 
31 @RunWith(RobolectricTestRunner.class)
32 public class HighUsageDataParserTest {
33 
34     private static final long PERIOD_ONE_MINUTE_MS = Duration.ofMinutes(1).toMillis();
35     private static final long PERIOD_ONE_HOUR_MS = Duration.ofHours(1).toMillis();
36     private static final long END_TIME_MS = 2 * PERIOD_ONE_MINUTE_MS;
37     private static final int THRESHOLD_LOW = 10;
38     private static final int THRESHOLD_HIGH = 20;
39     private HighUsageDataParser mDataParser;
40     private BatteryStats.HistoryItem mFirstItem;
41     private BatteryStats.HistoryItem mSecondItem;
42     private BatteryStats.HistoryItem mThirdItem;
43 
44     @Before
setUp()45     public void setUp() {
46         MockitoAnnotations.initMocks(this);
47 
48         mFirstItem = new BatteryStats.HistoryItem();
49         mFirstItem.batteryLevel = 100;
50         mFirstItem.currentTime = 0;
51         mSecondItem = new BatteryStats.HistoryItem();
52         mSecondItem.batteryLevel = 95;
53         mSecondItem.currentTime = PERIOD_ONE_MINUTE_MS;
54         mThirdItem = new BatteryStats.HistoryItem();
55         mThirdItem.batteryLevel = 80;
56         mThirdItem.currentTime = END_TIME_MS;
57     }
58 
59     @Test
testDataParser_thresholdLow_isHeavilyUsed()60     public void testDataParser_thresholdLow_isHeavilyUsed() {
61         mDataParser = new HighUsageDataParser(PERIOD_ONE_MINUTE_MS, THRESHOLD_LOW);
62         parseData();
63 
64         assertThat(mDataParser.isDeviceHeavilyUsed()).isTrue();
65     }
66 
67     @Test
testDataParser_thresholdHigh_notHeavilyUsed()68     public void testDataParser_thresholdHigh_notHeavilyUsed() {
69         mDataParser = new HighUsageDataParser(PERIOD_ONE_MINUTE_MS, THRESHOLD_HIGH);
70         parseData();
71 
72         assertThat(mDataParser.isDeviceHeavilyUsed()).isFalse();
73     }
74 
75     @Test
testDataParser_heavilyUsedInShortTime_stillReportHeavilyUsed()76     public void testDataParser_heavilyUsedInShortTime_stillReportHeavilyUsed() {
77         // Set threshold to 1 hour however device only used for 2 minutes
78         mDataParser = new HighUsageDataParser(PERIOD_ONE_HOUR_MS, THRESHOLD_LOW);
79         parseData();
80 
81         assertThat(mDataParser.isDeviceHeavilyUsed()).isTrue();
82     }
83 
parseData()84     private void parseData() {
85         // Report the battery usage in END_TIME_MS(2 minutes)
86         mDataParser.onParsingStarted(0, END_TIME_MS);
87         mDataParser.onDataPoint(0, mFirstItem);
88         mDataParser.onDataPoint(PERIOD_ONE_MINUTE_MS, mSecondItem);
89         mDataParser.onDataPoint(END_TIME_MS, mThirdItem);
90 
91         mDataParser.onParsingDone();
92     }
93 }
94