• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.TestConfig;
26 import com.android.settings.testutils.SettingsRobolectricTestRunner;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.robolectric.annotation.Config;
34 
35 @RunWith(SettingsRobolectricTestRunner.class)
36 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
37 public class BatterySaverReceiverTest {
38     @Mock
39     private BatterySaverReceiver.BatterySaverListener mBatterySaverListener;
40     @Mock
41     private Context mContext;
42     private BatterySaverReceiver mBatterySaverReceiver;
43 
44     @Before
setUp()45     public void setUp() {
46         MockitoAnnotations.initMocks(this);
47 
48         mBatterySaverReceiver = new BatterySaverReceiver(mContext);
49         mBatterySaverReceiver.setBatterySaverListener(mBatterySaverListener);
50     }
51 
52     @Test
testOnReceive_devicePluggedIn_pluggedInTrue()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
testOnReceive_deviceNotPluggedIn_pluggedInFalse()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
testOnReceive_powerSaveModeChanged_invokeCallback()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