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.spy;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.graphics.ColorFilter;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.robolectric.RobolectricTestRunner;
33 import org.robolectric.RuntimeEnvironment;
34 
35 @RunWith(RobolectricTestRunner.class)
36 public class BatteryMeterViewTest {
37 
38     private static final int BATTERY_LEVEL = 100;
39     private static final int BATTERY_CRITICAL_LEVEL = 15;
40     private static final int BATTERY_LOW_LEVEL = 3;
41 
42     @Mock
43     private ColorFilter mErrorColorFilter;
44     @Mock
45     private ColorFilter mAccentColorFilter;
46     @Mock
47     private ColorFilter mForegroundColorFilter;
48     private Context mContext;
49     private BatteryMeterView mBatteryMeterView;
50     private BatteryMeterView.BatteryMeterDrawable mDrawable;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55 
56         mContext = RuntimeEnvironment.application;
57         mBatteryMeterView = new BatteryMeterView(mContext);
58         mDrawable = spy(new BatteryMeterView.BatteryMeterDrawable(mContext, 0));
59 
60         mBatteryMeterView.mDrawable = mDrawable;
61         mBatteryMeterView.mAccentColorFilter = mAccentColorFilter;
62         mBatteryMeterView.mErrorColorFilter = mErrorColorFilter;
63         mBatteryMeterView.mForegroundColorFilter = mForegroundColorFilter;
64 
65         when(mDrawable.getCriticalLevel()).thenReturn(BATTERY_CRITICAL_LEVEL);
66     }
67 
68     @Test
testSetBatteryInfo_setCorrectly()69     public void testSetBatteryInfo_setCorrectly() {
70         mBatteryMeterView.setBatteryLevel(BATTERY_LEVEL);
71 
72         assertThat(mDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
73     }
74 
75     @Test
testSetBatteryInfo_levelLow_setErrorColor()76     public void testSetBatteryInfo_levelLow_setErrorColor() {
77         mBatteryMeterView.setBatteryLevel(BATTERY_LOW_LEVEL);
78 
79         verify(mDrawable).setColorFilter(mErrorColorFilter);
80     }
81 
82     @Test
testSetBatteryInfo_levelNormal_setNormalColor()83     public void testSetBatteryInfo_levelNormal_setNormalColor() {
84         mBatteryMeterView.setBatteryLevel(BATTERY_LEVEL);
85 
86         verify(mDrawable).setColorFilter(mAccentColorFilter);
87     }
88 
89     @Test
testSetBatteryInfo_powerSave_setCorrectly()90     public void testSetBatteryInfo_powerSave_setCorrectly() {
91         mBatteryMeterView.setPowerSave(true);
92 
93         assertThat(mBatteryMeterView.getPowerSave()).isEqualTo(true);
94         verify(mDrawable).setColorFilter(mForegroundColorFilter);
95     }
96 }
97