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