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