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 private ColorFilter mErrorColorFilter; 43 @Mock private ColorFilter mAccentColorFilter; 44 @Mock private ColorFilter mForegroundColorFilter; 45 private Context mContext; 46 private BatteryMeterView mBatteryMeterView; 47 private BatteryMeterView.BatteryMeterDrawable mDrawable; 48 49 @Before setUp()50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 53 mContext = RuntimeEnvironment.application; 54 mBatteryMeterView = new BatteryMeterView(mContext); 55 mDrawable = spy(new BatteryMeterView.BatteryMeterDrawable(mContext, 0)); 56 57 mBatteryMeterView.mDrawable = mDrawable; 58 mBatteryMeterView.mAccentColorFilter = mAccentColorFilter; 59 mBatteryMeterView.mErrorColorFilter = mErrorColorFilter; 60 mBatteryMeterView.mForegroundColorFilter = mForegroundColorFilter; 61 62 when(mDrawable.getCriticalLevel()).thenReturn(BATTERY_CRITICAL_LEVEL); 63 } 64 65 @Test testSetBatteryInfo_setCorrectly()66 public void testSetBatteryInfo_setCorrectly() { 67 mBatteryMeterView.setBatteryLevel(BATTERY_LEVEL); 68 69 assertThat(mDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL); 70 } 71 72 @Test testSetBatteryInfo_levelLow_setErrorColor()73 public void testSetBatteryInfo_levelLow_setErrorColor() { 74 mBatteryMeterView.setBatteryLevel(BATTERY_LOW_LEVEL); 75 76 verify(mDrawable).setColorFilter(mErrorColorFilter); 77 } 78 79 @Test testSetBatteryInfo_levelNormal_setNormalColor()80 public void testSetBatteryInfo_levelNormal_setNormalColor() { 81 mBatteryMeterView.setBatteryLevel(BATTERY_LEVEL); 82 83 verify(mDrawable).setColorFilter(mAccentColorFilter); 84 } 85 86 @Test testSetBatteryInfo_powerSave_setCorrectly()87 public void testSetBatteryInfo_powerSave_setCorrectly() { 88 mBatteryMeterView.setPowerSave(true); 89 90 assertThat(mBatteryMeterView.getPowerSave()).isEqualTo(true); 91 verify(mDrawable).setColorFilter(mForegroundColorFilter); 92 } 93 } 94