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 */ 16package com.android.settings.fuelgauge; 17 18import static org.mockito.Mockito.verify; 19 20import android.content.Context; 21import android.content.Intent; 22import android.os.BatteryManager; 23import android.os.PowerManager; 24 25import com.android.settings.TestConfig; 26import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 28import org.junit.Before; 29import org.junit.Test; 30import org.junit.runner.RunWith; 31import org.mockito.Mock; 32import org.mockito.MockitoAnnotations; 33import org.robolectric.annotation.Config; 34 35@RunWith(SettingsRobolectricTestRunner.class) 36@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 37public class BatterySaverReceiverTest { 38 @Mock 39 private BatterySaverReceiver.BatterySaverListener mBatterySaverListener; 40 @Mock 41 private Context mContext; 42 private BatterySaverReceiver mBatterySaverReceiver; 43 44 @Before 45 public void setUp() { 46 MockitoAnnotations.initMocks(this); 47 48 mBatterySaverReceiver = new BatterySaverReceiver(mContext); 49 mBatterySaverReceiver.setBatterySaverListener(mBatterySaverListener); 50 } 51 52 @Test 53 public void testOnReceive_devicePluggedIn_pluggedInTrue() { 54 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 55 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_AC); 56 57 mBatterySaverReceiver.onReceive(mContext, intent); 58 59 verify(mBatterySaverListener).onBatteryChanged(true); 60 } 61 62 @Test 63 public void testOnReceive_deviceNotPluggedIn_pluggedInFalse() { 64 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 65 intent.putExtra(BatteryManager.EXTRA_PLUGGED, 0); 66 67 mBatterySaverReceiver.onReceive(mContext, intent); 68 69 verify(mBatterySaverListener).onBatteryChanged(false); 70 } 71 72 @Test 73 public void testOnReceive_powerSaveModeChanged_invokeCallback() { 74 Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING); 75 76 mBatterySaverReceiver.onReceive(mContext, intent); 77 78 verify(mBatterySaverListener).onPowerSaveModeChanged(); 79 } 80 81} 82