1 /* 2 * Copyright (C) 2018 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 17 package com.android.settings.fuelgauge; 18 19 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.ComponentName; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.hardware.usb.UsbManager; 32 import android.hardware.usb.UsbPort; 33 import android.hardware.usb.UsbPortStatus; 34 import android.os.BatteryManager; 35 36 import androidx.preference.Preference; 37 import androidx.test.core.app.ApplicationProvider; 38 39 import com.android.settings.R; 40 import com.android.settings.testutils.BatteryTestUtils; 41 42 import org.junit.Before; 43 import org.junit.Ignore; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 import org.robolectric.RobolectricTestRunner; 49 import org.robolectric.annotation.Config; 50 51 import java.util.ArrayList; 52 import java.util.List; 53 54 @RunWith(RobolectricTestRunner.class) 55 public class TopLevelBatteryPreferenceControllerTest { 56 private Context mContext; 57 private TopLevelBatteryPreferenceController mController; 58 private BatterySettingsFeatureProvider mBatterySettingsFeatureProvider; 59 60 @Mock 61 private UsbPort mUsbPort; 62 @Mock 63 private UsbManager mUsbManager; 64 @Mock 65 private UsbPortStatus mUsbPortStatus; 66 67 @Before setUp()68 public void setUp() { 69 MockitoAnnotations.initMocks(this); 70 mContext = spy(ApplicationProvider.getApplicationContext()); 71 mController = new TopLevelBatteryPreferenceController(mContext, "test_key"); 72 when(mContext.getSystemService(UsbManager.class)).thenReturn(mUsbManager); 73 } 74 75 @Test getAvailibilityStatus_availableByDefault()76 public void getAvailibilityStatus_availableByDefault() { 77 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 78 } 79 80 @Test convertClassPathToComponentName_nullInput_returnsNull()81 public void convertClassPathToComponentName_nullInput_returnsNull() { 82 assertThat(mController.convertClassPathToComponentName(null)).isNull(); 83 } 84 85 @Test 86 @Ignore convertClassPathToComponentName_emptyStringInput_returnsNull()87 public void convertClassPathToComponentName_emptyStringInput_returnsNull() { 88 assertThat(mController.convertClassPathToComponentName("")).isNull(); 89 } 90 91 @Test convertClassPathToComponentName_singleClassName_returnsCorrectComponentName()92 public void convertClassPathToComponentName_singleClassName_returnsCorrectComponentName() { 93 ComponentName output = mController.convertClassPathToComponentName("ClassName"); 94 95 assertThat(output.getPackageName()).isEqualTo(""); 96 assertThat(output.getClassName()).isEqualTo("ClassName"); 97 } 98 99 @Test convertClassPathToComponentName_validAddress_returnsCorrectComponentName()100 public void convertClassPathToComponentName_validAddress_returnsCorrectComponentName() { 101 ComponentName output = mController.convertClassPathToComponentName("my.fragment.ClassName"); 102 103 assertThat(output.getPackageName()).isEqualTo("my.fragment"); 104 assertThat(output.getClassName()).isEqualTo("ClassName"); 105 } 106 107 @Test getDashboardLabel_returnsBatterPercentString()108 public void getDashboardLabel_returnsBatterPercentString() { 109 mController.mPreference = new Preference(mContext); 110 BatteryInfo info = new BatteryInfo(); 111 info.batteryPercentString = "3%"; 112 113 assertThat(mController.getDashboardLabel(mContext, info, true)) 114 .isEqualTo(info.batteryPercentString); 115 } 116 117 @Test getDashboardLabel_returnsRemainingLabel()118 public void getDashboardLabel_returnsRemainingLabel() { 119 mController.mPreference = new Preference(mContext); 120 BatteryInfo info = new BatteryInfo(); 121 info.batteryPercentString = "3%"; 122 info.remainingLabel = "Phone will shut down soon"; 123 124 assertThat(mController.getDashboardLabel(mContext, info, true)) 125 .isEqualTo("3% - Phone will shut down soon"); 126 } 127 128 @Test getDashboardLabel_returnsChargeLabel()129 public void getDashboardLabel_returnsChargeLabel() { 130 mController.mPreference = new Preference(mContext); 131 BatteryInfo info = new BatteryInfo(); 132 info.discharging = false; 133 info.chargeLabel = "5% - charging"; 134 135 assertThat(mController.getDashboardLabel(mContext, info, true)) 136 .isEqualTo(info.chargeLabel); 137 } 138 139 @Test getDashboardLabel_incompatibleCharger_returnsCorrectLabel()140 public void getDashboardLabel_incompatibleCharger_returnsCorrectLabel() { 141 BatteryTestUtils.setupIncompatibleEvent(mUsbPort, mUsbManager, mUsbPortStatus); 142 mController.mPreference = new Preference(mContext); 143 BatteryInfo info = new BatteryInfo(); 144 145 assertThat(mController.getDashboardLabel(mContext, info, true)) 146 .isEqualTo(mContext.getString(R.string.battery_info_status_not_charging)); 147 } 148 149 @Test getDashboardLabel_notChargingState_returnsCorrectLabel()150 public void getDashboardLabel_notChargingState_returnsCorrectLabel() { 151 mController.mPreference = new Preference(mContext); 152 BatteryInfo info = new BatteryInfo(); 153 info.batteryStatus = BatteryManager.BATTERY_STATUS_NOT_CHARGING; 154 info.statusLabel = "expected returned label"; 155 156 assertThat(mController.getDashboardLabel(mContext, info, true)) 157 .isEqualTo(info.statusLabel); 158 } 159 160 @Test getSummary_batteryNotPresent_shouldShowWarningMessage()161 public void getSummary_batteryNotPresent_shouldShowWarningMessage() { 162 mController.mIsBatteryPresent = false; 163 assertThat(mController.getSummary()) 164 .isEqualTo(mContext.getString(R.string.battery_missing_message)); 165 } 166 } 167