• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.LOLLIPOP;
4 import static android.os.Build.VERSION_CODES.M;
5 import static com.google.common.truth.Truth.assertThat;
6 import static org.robolectric.Shadows.shadowOf;
7 
8 import android.content.Context;
9 import android.os.BatteryManager;
10 import androidx.test.core.app.ApplicationProvider;
11 import androidx.test.ext.junit.runners.AndroidJUnit4;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.robolectric.annotation.Config;
16 
17 @RunWith(AndroidJUnit4.class)
18 @Config(minSdk = LOLLIPOP)
19 public class ShadowBatteryManagerTest {
20   private BatteryManager batteryManager;
21   private ShadowBatteryManager shadowBatteryManager;
22   private static final int TEST_ID = 123;
23 
24   @Before
before()25   public void before() {
26     batteryManager =
27         (BatteryManager)
28             ApplicationProvider.getApplicationContext().getSystemService(Context.BATTERY_SERVICE);
29     shadowBatteryManager = shadowOf(batteryManager);
30   }
31 
32   @Test
33   @Config(minSdk = M)
testIsCharging()34   public void testIsCharging() {
35     assertThat(batteryManager.isCharging()).isFalse();
36 
37     shadowBatteryManager.setIsCharging(true);
38 
39     assertThat(batteryManager.isCharging()).isTrue();
40 
41     shadowBatteryManager.setIsCharging(false);
42 
43     assertThat(batteryManager.isCharging()).isFalse();
44   }
45 
46   @Test
testGetIntProperty()47   public void testGetIntProperty() {
48     assertThat(batteryManager.getIntProperty(TEST_ID)).isEqualTo(Integer.MIN_VALUE);
49 
50     shadowBatteryManager.setIntProperty(TEST_ID, 5);
51     assertThat(batteryManager.getIntProperty(TEST_ID)).isEqualTo(5);
52 
53     shadowBatteryManager.setIntProperty(TEST_ID, 0);
54     assertThat(batteryManager.getIntProperty(TEST_ID)).isEqualTo(0);
55 
56     shadowBatteryManager.setIntProperty(TEST_ID, Integer.MAX_VALUE);
57     assertThat(batteryManager.getIntProperty(TEST_ID)).isEqualTo(Integer.MAX_VALUE);
58   }
59 
60   @Test
testGetLongProperty()61   public void testGetLongProperty() {
62     assertThat(batteryManager.getLongProperty(TEST_ID)).isEqualTo(Long.MIN_VALUE);
63 
64     shadowBatteryManager.setLongProperty(TEST_ID, 5L);
65     assertThat(batteryManager.getLongProperty(TEST_ID)).isEqualTo(5L);
66 
67     shadowBatteryManager.setLongProperty(TEST_ID, 0);
68     assertThat(batteryManager.getLongProperty(TEST_ID)).isEqualTo(0);
69 
70     shadowBatteryManager.setLongProperty(TEST_ID, Long.MAX_VALUE);
71     assertThat(batteryManager.getLongProperty(TEST_ID)).isEqualTo(Long.MAX_VALUE);
72   }
73 }
74