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