1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  *
16  */
17 
18 package com.android.settings.fuelgauge;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 
27 import android.content.Context;
28 import android.content.Intent;
29 import android.os.BatteryManager;
30 import android.support.v7.preference.Preference;
31 import android.support.v7.preference.PreferenceScreen;
32 import android.widget.TextView;
33 
34 import com.android.settings.R;
35 import com.android.settings.SettingsRobolectricTestRunner;
36 import com.android.settings.TestConfig;
37 import com.android.settings.applications.LayoutPreference;
38 import com.android.settings.testutils.shadow.SettingsShadowResources;
39 import com.android.settings.testutils.shadow.ShadowDynamicIndexableContentMonitor;
40 import com.android.settingslib.BatteryInfo;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 
50 @RunWith(SettingsRobolectricTestRunner.class)
51 @Config(manifest = TestConfig.MANIFEST_PATH,
52         sdk = TestConfig.SDK_VERSION,
53         shadows = {
54                 SettingsShadowResources.class,
55                 SettingsShadowResources.SettingsShadowTheme.class,
56                 ShadowDynamicIndexableContentMonitor.class
57         })
58 public class BatteryHeaderPreferenceControllerTest {
59     private static final int BATTERY_LEVEL = 60;
60     private static final String TIME_LEFT = "2h30min";
61     private static final String BATTERY_STATUS = "Charging";
62 
63     @Mock
64     private PreferenceScreen mPreferenceScreen;
65     @Mock
66     private BatteryInfo mBatteryInfo;
67     private BatteryHeaderPreferenceController mController;
68     private Context mContext;
69     private BatteryMeterView mBatteryMeterView;
70     private TextView mTimeText;
71     private TextView mSummary;
72     private LayoutPreference mBatteryLayoutPref;
73     private Intent mBatteryIntent;
74 
75     @Before
setUp()76     public void setUp() {
77         MockitoAnnotations.initMocks(this);
78 
79         mContext = spy(RuntimeEnvironment.application);
80         mBatteryMeterView = new BatteryMeterView(mContext);
81         mTimeText = new TextView(mContext);
82         mSummary = new TextView(mContext);
83 
84         mBatteryIntent = new Intent();
85         mBatteryIntent.putExtra(BatteryManager.EXTRA_LEVEL, BATTERY_LEVEL);
86         mBatteryIntent.putExtra(BatteryManager.EXTRA_SCALE, 100);
87         doReturn(mBatteryIntent).when(mContext).registerReceiver(any(), any());
88 
89         mBatteryLayoutPref = new LayoutPreference(mContext, R.layout.battery_header);
90         doReturn(mBatteryLayoutPref).when(mPreferenceScreen).findPreference(
91                 BatteryHeaderPreferenceController.KEY_BATTERY_HEADER);
92 
93         mBatteryInfo.batteryLevel = BATTERY_LEVEL;
94 
95         mController = new BatteryHeaderPreferenceController(mContext);
96         mController.mBatteryMeterView = mBatteryMeterView;
97         mController.mTimeText = mTimeText;
98         mController.mSummary = mSummary;
99     }
100 
101     @Test
testDisplayPreference_displayBatteryLevel()102     public void testDisplayPreference_displayBatteryLevel() {
103         mController.displayPreference(mPreferenceScreen);
104 
105         assertThat(((BatteryMeterView) mBatteryLayoutPref.findViewById(
106                 R.id.battery_header_icon)).getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
107         assertThat(((TextView) mBatteryLayoutPref.findViewById(
108                 R.id.battery_percent)).getText()).isEqualTo("60%");
109     }
110 
111     @Test
testUpdatePreference_hasRemainingTime_showRemainingLabel()112     public void testUpdatePreference_hasRemainingTime_showRemainingLabel() {
113         mBatteryInfo.remainingLabel = TIME_LEFT;
114 
115         mController.updateHeaderPreference(mBatteryInfo);
116 
117         assertThat(mSummary.getText()).isEqualTo(mBatteryInfo.remainingLabel);
118     }
119 
120     @Test
testUpdatePreference_updateBatteryInfo()121     public void testUpdatePreference_updateBatteryInfo() {
122         mBatteryInfo.remainingLabel = TIME_LEFT;
123         mBatteryInfo.batteryLevel = BATTERY_LEVEL;
124         mBatteryInfo.discharging = true;
125 
126         mController.updateHeaderPreference(mBatteryInfo);
127 
128         assertThat(mBatteryMeterView.mDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
129         assertThat(mBatteryMeterView.mDrawable.getCharging()).isEqualTo(false);
130     }
131 
132     @Test
testUpdatePreference_noRemainingTime_showStatusLabel()133     public void testUpdatePreference_noRemainingTime_showStatusLabel() {
134         mBatteryInfo.remainingLabel = null;
135         mBatteryInfo.statusLabel = BATTERY_STATUS;
136 
137         mController.updateHeaderPreference(mBatteryInfo);
138 
139         assertThat(mSummary.getText()).isEqualTo(BATTERY_STATUS);
140     }
141 }
142