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