• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.arch.lifecycle.Lifecycle.Event.ON_START;
21 import static com.google.common.truth.Truth.assertThat;
22 import static org.mockito.ArgumentMatchers.nullable;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.app.Activity;
31 import android.arch.lifecycle.LifecycleOwner;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.os.BatteryManager;
35 import android.support.v14.preference.PreferenceFragment;
36 import android.support.v7.preference.PreferenceScreen;
37 import android.support.v7.widget.RecyclerView;
38 import android.widget.TextView;
39 
40 import com.android.settings.R;
41 import com.android.settings.applications.LayoutPreference;
42 import com.android.settings.testutils.SettingsRobolectricTestRunner;
43 import com.android.settings.testutils.shadow.SettingsShadowResources;
44 import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl;
45 import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
46 import com.android.settings.widget.EntityHeaderController;
47 import com.android.settingslib.core.lifecycle.Lifecycle;
48 
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.Mock;
54 import org.mockito.MockitoAnnotations;
55 import org.robolectric.RuntimeEnvironment;
56 import org.robolectric.annotation.Config;
57 
58 @RunWith(SettingsRobolectricTestRunner.class)
59 @Config(shadows = {
60                 SettingsShadowResources.class,
61                 SettingsShadowResourcesImpl.class,
62                 SettingsShadowResources.SettingsShadowTheme.class,
63                 ShadowEntityHeaderController.class
64         })
65 public class BatteryHeaderPreferenceControllerTest {
66 
67     private static final int BATTERY_LEVEL = 60;
68     private static final String TIME_LEFT = "2h30min";
69     private static final String BATTERY_STATUS = "Charging";
70 
71     @Mock
72     private Activity mActivity;
73     @Mock
74     private PreferenceFragment mPreferenceFragment;
75     @Mock
76     private PreferenceScreen mPreferenceScreen;
77     @Mock
78     private BatteryInfo mBatteryInfo;
79     @Mock
80     private EntityHeaderController mEntityHeaderController;
81     private BatteryHeaderPreferenceController mController;
82     private Context mContext;
83     private BatteryMeterView mBatteryMeterView;
84     private TextView mBatteryPercentText;
85     private TextView mSummary;
86     private TextView mSummary2;
87     private LayoutPreference mBatteryLayoutPref;
88     private Intent mBatteryIntent;
89     private LifecycleOwner mLifecycleOwner;
90     private Lifecycle mLifecycle;
91 
92     @Before
setUp()93     public void setUp() {
94         MockitoAnnotations.initMocks(this);
95 
96         mLifecycleOwner = () -> mLifecycle;
97         mLifecycle = new Lifecycle(mLifecycleOwner);
98         mContext = spy(RuntimeEnvironment.application);
99         mBatteryMeterView = new BatteryMeterView(mContext);
100         mBatteryPercentText = new TextView(mContext);
101         mSummary = new TextView(mContext);
102         ShadowEntityHeaderController.setUseMock(mEntityHeaderController);
103         mSummary2 = new TextView(mContext);
104 
105         mBatteryIntent = new Intent();
106         mBatteryIntent.putExtra(BatteryManager.EXTRA_LEVEL, BATTERY_LEVEL);
107         mBatteryIntent.putExtra(BatteryManager.EXTRA_SCALE, 100);
108         mBatteryIntent.putExtra(BatteryManager.EXTRA_PLUGGED, 1);
109         doReturn(mBatteryIntent).when(mContext).registerReceiver(any(), any());
110 
111         mBatteryLayoutPref = new LayoutPreference(mContext, R.layout.battery_header);
112         doReturn(mBatteryLayoutPref).when(mPreferenceScreen)
113             .findPreference(BatteryHeaderPreferenceController.KEY_BATTERY_HEADER);
114 
115         mBatteryInfo.batteryLevel = BATTERY_LEVEL;
116 
117         mController = new BatteryHeaderPreferenceController(
118                 mContext, mActivity, mPreferenceFragment, mLifecycle);
119         mController.mBatteryMeterView = mBatteryMeterView;
120         mController.mBatteryPercentText = mBatteryPercentText;
121         mController.mSummary1 = mSummary;
122         mController.mSummary2 = mSummary2;
123     }
124 
125     @After
tearDown()126     public void tearDown() {
127         ShadowEntityHeaderController.reset();
128     }
129 
130     @Test
testDisplayPreference_displayBatteryLevel()131     public void testDisplayPreference_displayBatteryLevel() {
132         mController.displayPreference(mPreferenceScreen);
133 
134         assertThat(((BatteryMeterView) mBatteryLayoutPref.findViewById(
135                 R.id.battery_header_icon)).getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
136         assertThat(((TextView) mBatteryLayoutPref.findViewById(R.id.battery_percent)).getText())
137             .isEqualTo("60%");
138     }
139 
140     @Test
testUpdatePreference_hasRemainingTime_showRemainingLabel()141     public void testUpdatePreference_hasRemainingTime_showRemainingLabel() {
142         mBatteryInfo.remainingLabel = TIME_LEFT;
143 
144         mController.updateHeaderPreference(mBatteryInfo);
145 
146         assertThat(mSummary.getText()).isEqualTo(mBatteryInfo.remainingLabel);
147     }
148 
149     @Test
testUpdatePreference_updateBatteryInfo()150     public void testUpdatePreference_updateBatteryInfo() {
151         mBatteryInfo.remainingLabel = TIME_LEFT;
152         mBatteryInfo.batteryLevel = BATTERY_LEVEL;
153         mBatteryInfo.discharging = true;
154 
155         mController.updateHeaderPreference(mBatteryInfo);
156 
157         assertThat(mBatteryMeterView.mDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
158         assertThat(mBatteryMeterView.mDrawable.getCharging()).isEqualTo(false);
159     }
160 
161     @Test
testUpdatePreference_noRemainingTime_showStatusLabel()162     public void testUpdatePreference_noRemainingTime_showStatusLabel() {
163         mBatteryInfo.remainingLabel = null;
164         mBatteryInfo.statusLabel = BATTERY_STATUS;
165 
166         mController.updateHeaderPreference(mBatteryInfo);
167 
168         assertThat(mSummary.getText()).isEqualTo(BATTERY_STATUS);
169     }
170 
171     @Test
testOnStart_shouldStyleActionBar()172     public void testOnStart_shouldStyleActionBar() {
173         when(mEntityHeaderController.setRecyclerView(nullable(RecyclerView.class), eq(mLifecycle)))
174                 .thenReturn(mEntityHeaderController);
175 
176         mController.displayPreference(mPreferenceScreen);
177         mLifecycle.handleLifecycleEvent(ON_START);
178 
179         verify(mEntityHeaderController).styleActionBar(mActivity);
180     }
181 
182     @Test
testQuickUpdateHeaderPreference_showBatteryLevelAndChargingState()183     public void testQuickUpdateHeaderPreference_showBatteryLevelAndChargingState() {
184         mController.quickUpdateHeaderPreference();
185 
186         assertThat(mBatteryMeterView.getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
187         assertThat(mBatteryMeterView.getCharging()).isTrue();
188         assertThat(mBatteryPercentText.getText()).isEqualTo("60%");
189     }
190 }
191