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 com.android.settings.testutils.SettingsRobolectricTestRunner; 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 33 @RunWith(SettingsRobolectricTestRunner.class) 34 public class BatterySaverReceiverTest { 35 36 @Mock 37 private BatterySaverReceiver.BatterySaverListener mBatterySaverListener; 38 @Mock 39 private Context mContext; 40 private BatterySaverReceiver mBatterySaverReceiver; 41 42 @Before setUp()43 public void setUp() { 44 MockitoAnnotations.initMocks(this); 45 46 mBatterySaverReceiver = new BatterySaverReceiver(mContext); 47 mBatterySaverReceiver.setBatterySaverListener(mBatterySaverListener); 48 } 49 50 @Test testOnReceive_devicePluggedIn_pluggedInTrue()51 public void testOnReceive_devicePluggedIn_pluggedInTrue() { 52 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 53 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_AC); 54 55 mBatterySaverReceiver.onReceive(mContext, intent); 56 57 verify(mBatterySaverListener).onBatteryChanged(true); 58 } 59 60 @Test testOnReceive_deviceNotPluggedIn_pluggedInFalse()61 public void testOnReceive_deviceNotPluggedIn_pluggedInFalse() { 62 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 63 intent.putExtra(BatteryManager.EXTRA_PLUGGED, 0); 64 65 mBatterySaverReceiver.onReceive(mContext, intent); 66 67 verify(mBatterySaverListener).onBatteryChanged(false); 68 } 69 70 @Test testOnReceive_powerSaveModeChanged_invokeCallback()71 public void testOnReceive_powerSaveModeChanged_invokeCallback() { 72 Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING); 73 74 mBatterySaverReceiver.onReceive(mContext, intent); 75 76 verify(mBatterySaverListener).onPowerSaveModeChanged(); 77 } 78 } 79