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