1 /* 2 * Copyright (C) 2017 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 package com.android.settingslib; 17 18 import static com.android.settingslib.Utils.STORAGE_MANAGER_ENABLED_PROPERTY; 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.when; 26 27 import android.app.ActivityManager; 28 import android.content.ContentResolver; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.content.res.Resources; 32 import android.hardware.usb.UsbManager; 33 import android.hardware.usb.UsbPort; 34 import android.hardware.usb.UsbPortStatus; 35 import android.location.LocationManager; 36 import android.media.AudioManager; 37 import android.os.BatteryManager; 38 import android.os.SystemProperties; 39 import android.os.UserHandle; 40 import android.provider.Settings; 41 import android.telephony.AccessNetworkConstants; 42 import android.telephony.NetworkRegistrationInfo; 43 import android.telephony.ServiceState; 44 import android.text.TextUtils; 45 46 import org.junit.After; 47 import org.junit.Before; 48 import org.junit.Test; 49 import org.junit.runner.RunWith; 50 import org.mockito.ArgumentMatcher; 51 import org.mockito.Mock; 52 import org.mockito.MockitoAnnotations; 53 import org.robolectric.RobolectricTestRunner; 54 import org.robolectric.RuntimeEnvironment; 55 import org.robolectric.annotation.Config; 56 import org.robolectric.annotation.Implementation; 57 import org.robolectric.annotation.Implements; 58 import org.robolectric.shadows.ShadowSettings; 59 60 import java.util.ArrayList; 61 import java.util.HashMap; 62 import java.util.List; 63 import java.util.Map; 64 65 @RunWith(RobolectricTestRunner.class) 66 @Config(shadows = {UtilsTest.ShadowSecure.class, UtilsTest.ShadowLocationManager.class}) 67 public class UtilsTest { 68 private static final double[] TEST_PERCENTAGES = {0, 0.4, 0.5, 0.6, 49, 49.3, 49.8, 50, 100}; 69 private static final String TAG = "UtilsTest"; 70 private static final String PERCENTAGE_0 = "0%"; 71 private static final String PERCENTAGE_1 = "1%"; 72 private static final String PERCENTAGE_49 = "49%"; 73 private static final String PERCENTAGE_50 = "50%"; 74 private static final String PERCENTAGE_100 = "100%"; 75 76 private AudioManager mAudioManager; 77 private Context mContext; 78 @Mock 79 private LocationManager mLocationManager; 80 @Mock 81 private ServiceState mServiceState; 82 @Mock 83 private NetworkRegistrationInfo mNetworkRegistrationInfo; 84 @Mock 85 private UsbPort mUsbPort; 86 @Mock 87 private UsbManager mUsbManager; 88 @Mock 89 private UsbPortStatus mUsbPortStatus; 90 91 @Before setUp()92 public void setUp() { 93 MockitoAnnotations.initMocks(this); 94 mContext = spy(RuntimeEnvironment.application); 95 when(mContext.getSystemService(Context.LOCATION_SERVICE)).thenReturn(mLocationManager); 96 when(mContext.getSystemService(UsbManager.class)).thenReturn(mUsbManager); 97 ShadowSecure.reset(); 98 mAudioManager = mContext.getSystemService(AudioManager.class); 99 } 100 101 @After reset()102 public void reset() { 103 Settings.Secure.putInt(mContext.getContentResolver(), 104 Utils.INCOMPATIBLE_CHARGER_WARNING_DISABLED, 0); 105 } 106 107 @Test testUpdateLocationEnabled()108 public void testUpdateLocationEnabled() { 109 int currentUserId = ActivityManager.getCurrentUser(); 110 Utils.updateLocationEnabled(mContext, true, currentUserId, 111 Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS); 112 113 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 114 Settings.Secure.LOCATION_CHANGER, Settings.Secure.LOCATION_CHANGER_UNKNOWN)) 115 .isEqualTo(Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS); 116 } 117 118 @Test testFormatPercentage_RoundTrue_RoundUpIfPossible()119 public void testFormatPercentage_RoundTrue_RoundUpIfPossible() { 120 final String[] expectedPercentages = {PERCENTAGE_0, PERCENTAGE_0, PERCENTAGE_1, 121 PERCENTAGE_1, PERCENTAGE_49, PERCENTAGE_49, PERCENTAGE_50, PERCENTAGE_50, 122 PERCENTAGE_100}; 123 124 for (int i = 0, size = TEST_PERCENTAGES.length; i < size; i++) { 125 final String percentage = Utils.formatPercentage(TEST_PERCENTAGES[i], true); 126 assertThat(percentage).isEqualTo(expectedPercentages[i]); 127 } 128 } 129 130 @Test testFormatPercentage_RoundFalse_NoRound()131 public void testFormatPercentage_RoundFalse_NoRound() { 132 final String[] expectedPercentages = {PERCENTAGE_0, PERCENTAGE_0, PERCENTAGE_0, 133 PERCENTAGE_0, PERCENTAGE_49, PERCENTAGE_49, PERCENTAGE_49, PERCENTAGE_50, 134 PERCENTAGE_100}; 135 136 for (int i = 0, size = TEST_PERCENTAGES.length; i < size; i++) { 137 final String percentage = Utils.formatPercentage(TEST_PERCENTAGES[i], false); 138 assertThat(percentage).isEqualTo(expectedPercentages[i]); 139 } 140 } 141 142 @Test testGetDefaultStorageManagerDaysToRetain_storageManagerDaysToRetainUsesResources()143 public void testGetDefaultStorageManagerDaysToRetain_storageManagerDaysToRetainUsesResources() { 144 Resources resources = mock(Resources.class); 145 when(resources.getInteger( 146 eq( 147 com.android 148 .internal 149 .R 150 .integer 151 .config_storageManagerDaystoRetainDefault))) 152 .thenReturn(60); 153 assertThat(Utils.getDefaultStorageManagerDaysToRetain(resources)).isEqualTo(60); 154 } 155 156 @Test testIsStorageManagerEnabled_UsesSystemProperties()157 public void testIsStorageManagerEnabled_UsesSystemProperties() { 158 SystemProperties.set(STORAGE_MANAGER_ENABLED_PROPERTY, "true"); 159 assertThat(Utils.isStorageManagerEnabled(mContext)).isTrue(); 160 } 161 actionMatches(String expected)162 private static ArgumentMatcher<Intent> actionMatches(String expected) { 163 return intent -> TextUtils.equals(expected, intent.getAction()); 164 } 165 166 @Implements(value = Settings.Secure.class) 167 public static class ShadowSecure extends ShadowSettings.ShadowSecure { 168 private static Map<String, Integer> map = new HashMap<>(); 169 170 @Implementation putIntForUser(ContentResolver cr, String name, int value, int userHandle)171 public static boolean putIntForUser(ContentResolver cr, String name, int value, 172 int userHandle) { 173 map.put(name, value); 174 return true; 175 } 176 177 @Implementation getIntForUser(ContentResolver cr, String name, int def, int userHandle)178 public static int getIntForUser(ContentResolver cr, String name, int def, int userHandle) { 179 if (map.containsKey(name)) { 180 return map.get(name); 181 } else { 182 return def; 183 } 184 } 185 reset()186 public static void reset() { 187 map.clear(); 188 } 189 } 190 191 @Implements(value = LocationManager.class) 192 public static class ShadowLocationManager { 193 194 @Implementation setLocationEnabledForUser(boolean enabled, UserHandle userHandle)195 public void setLocationEnabledForUser(boolean enabled, UserHandle userHandle) { 196 // Do nothing 197 } 198 } 199 200 @Test isAudioModeOngoingCall_modeInCommunication_returnTrue()201 public void isAudioModeOngoingCall_modeInCommunication_returnTrue() { 202 mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); 203 204 assertThat(Utils.isAudioModeOngoingCall(mContext)).isTrue(); 205 } 206 207 @Test isAudioModeOngoingCall_modeInCall_returnTrue()208 public void isAudioModeOngoingCall_modeInCall_returnTrue() { 209 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 210 211 assertThat(Utils.isAudioModeOngoingCall(mContext)).isTrue(); 212 } 213 214 @Test isAudioModeOngoingCall_modeRingtone_returnTrue()215 public void isAudioModeOngoingCall_modeRingtone_returnTrue() { 216 mAudioManager.setMode(AudioManager.MODE_RINGTONE); 217 218 assertThat(Utils.isAudioModeOngoingCall(mContext)).isTrue(); 219 } 220 221 @Test isAudioModeOngoingCall_modeNormal_returnFalse()222 public void isAudioModeOngoingCall_modeNormal_returnFalse() { 223 mAudioManager.setMode(AudioManager.MODE_NORMAL); 224 225 assertThat(Utils.isAudioModeOngoingCall(mContext)).isFalse(); 226 } 227 228 @Test isInService_servicestateNull_returnFalse()229 public void isInService_servicestateNull_returnFalse() { 230 assertThat(Utils.isInService(null)).isFalse(); 231 } 232 233 @Test isInService_voiceInService_returnTrue()234 public void isInService_voiceInService_returnTrue() { 235 when(mServiceState.getState()).thenReturn(ServiceState.STATE_IN_SERVICE); 236 237 assertThat(Utils.isInService(mServiceState)).isTrue(); 238 } 239 240 @Test isInService_voiceOutOfServiceDataInService_returnTrue()241 public void isInService_voiceOutOfServiceDataInService_returnTrue() { 242 when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE); 243 when(mServiceState.getDataRegistrationState()).thenReturn(ServiceState.STATE_IN_SERVICE); 244 when(mServiceState.getNetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS, 245 AccessNetworkConstants.TRANSPORT_TYPE_WLAN)).thenReturn(mNetworkRegistrationInfo); 246 when(mNetworkRegistrationInfo.getRegistrationState()).thenReturn( 247 NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 248 249 assertThat(Utils.isInService(mServiceState)).isTrue(); 250 } 251 252 @Test isInService_voiceOutOfServiceDataInServiceOnIwLan_returnFalse()253 public void isInService_voiceOutOfServiceDataInServiceOnIwLan_returnFalse() { 254 when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE); 255 when(mServiceState.getNetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS, 256 AccessNetworkConstants.TRANSPORT_TYPE_WLAN)).thenReturn(mNetworkRegistrationInfo); 257 when(mNetworkRegistrationInfo.getRegistrationState()).thenReturn( 258 NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 259 when(mServiceState.getDataRegistrationState()).thenReturn(ServiceState.STATE_IN_SERVICE); 260 261 assertThat(Utils.isInService(mServiceState)).isFalse(); 262 } 263 264 @Test isInService_voiceOutOfServiceDataOutOfService_returnFalse()265 public void isInService_voiceOutOfServiceDataOutOfService_returnFalse() { 266 when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE); 267 when(mServiceState.getDataRegistrationState()).thenReturn( 268 ServiceState.STATE_OUT_OF_SERVICE); 269 270 assertThat(Utils.isInService(mServiceState)).isFalse(); 271 } 272 273 @Test isInService_ServiceStatePowerOff_returnFalse()274 public void isInService_ServiceStatePowerOff_returnFalse() { 275 when(mServiceState.getState()).thenReturn(ServiceState.STATE_POWER_OFF); 276 277 assertThat(Utils.isInService(mServiceState)).isFalse(); 278 } 279 280 @Test getCombinedServiceState_servicestateNull_returnOutOfService()281 public void getCombinedServiceState_servicestateNull_returnOutOfService() { 282 assertThat(Utils.getCombinedServiceState(null)).isEqualTo( 283 ServiceState.STATE_OUT_OF_SERVICE); 284 } 285 286 @Test getCombinedServiceState_ServiceStatePowerOff_returnPowerOff()287 public void getCombinedServiceState_ServiceStatePowerOff_returnPowerOff() { 288 when(mServiceState.getState()).thenReturn(ServiceState.STATE_POWER_OFF); 289 290 assertThat(Utils.getCombinedServiceState(mServiceState)).isEqualTo( 291 ServiceState.STATE_POWER_OFF); 292 } 293 294 @Test getCombinedServiceState_voiceInService_returnInService()295 public void getCombinedServiceState_voiceInService_returnInService() { 296 when(mServiceState.getState()).thenReturn(ServiceState.STATE_IN_SERVICE); 297 298 assertThat(Utils.getCombinedServiceState(mServiceState)).isEqualTo( 299 ServiceState.STATE_IN_SERVICE); 300 } 301 302 @Test getCombinedServiceState_voiceOutOfServiceDataInService_returnInService()303 public void getCombinedServiceState_voiceOutOfServiceDataInService_returnInService() { 304 when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE); 305 when(mServiceState.getDataRegistrationState()).thenReturn(ServiceState.STATE_IN_SERVICE); 306 when(mServiceState.getNetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS, 307 AccessNetworkConstants.TRANSPORT_TYPE_WLAN)).thenReturn(mNetworkRegistrationInfo); 308 when(mNetworkRegistrationInfo.getRegistrationState()).thenReturn( 309 NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 310 311 assertThat(Utils.getCombinedServiceState(mServiceState)).isEqualTo( 312 ServiceState.STATE_IN_SERVICE); 313 } 314 315 @Test getCombinedServiceState_voiceOutOfServiceDataInServiceOnIwLan_returnOutOfService()316 public void getCombinedServiceState_voiceOutOfServiceDataInServiceOnIwLan_returnOutOfService() { 317 when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE); 318 when(mServiceState.getDataRegistrationState()).thenReturn(ServiceState.STATE_IN_SERVICE); 319 when(mServiceState.getNetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS, 320 AccessNetworkConstants.TRANSPORT_TYPE_WLAN)).thenReturn(mNetworkRegistrationInfo); 321 when(mNetworkRegistrationInfo.getRegistrationState()).thenReturn( 322 NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 323 324 assertThat(Utils.getCombinedServiceState(mServiceState)).isEqualTo( 325 ServiceState.STATE_OUT_OF_SERVICE); 326 } 327 328 @Test getCombinedServiceState_voiceOutOfServiceDataOutOfService_returnOutOfService()329 public void getCombinedServiceState_voiceOutOfServiceDataOutOfService_returnOutOfService() { 330 when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE); 331 when(mServiceState.getDataRegistrationState()).thenReturn( 332 ServiceState.STATE_OUT_OF_SERVICE); 333 334 assertThat(Utils.getCombinedServiceState(mServiceState)).isEqualTo( 335 ServiceState.STATE_OUT_OF_SERVICE); 336 } 337 338 @Test getBatteryStatus_statusIsFull_returnFullString()339 public void getBatteryStatus_statusIsFull_returnFullString() { 340 final Intent intent = new Intent() 341 .putExtra(BatteryManager.EXTRA_LEVEL, 100) 342 .putExtra(BatteryManager.EXTRA_SCALE, 100); 343 final Resources resources = mContext.getResources(); 344 345 assertThat(Utils.getBatteryStatus(mContext, intent, /* compactStatus= */ false)).isEqualTo( 346 resources.getString(R.string.battery_info_status_full)); 347 } 348 349 @Test getBatteryStatus_statusIsFullAndUseCompactStatus_returnFullyChargedString()350 public void getBatteryStatus_statusIsFullAndUseCompactStatus_returnFullyChargedString() { 351 final Intent intent = new Intent() 352 .putExtra(BatteryManager.EXTRA_LEVEL, 100) 353 .putExtra(BatteryManager.EXTRA_SCALE, 100); 354 final Resources resources = mContext.getResources(); 355 356 assertThat(Utils.getBatteryStatus(mContext, intent, /* compactStatus= */ true)).isEqualTo( 357 resources.getString(R.string.battery_info_status_full_charged)); 358 } 359 360 @Test getBatteryStatus_batteryLevelIs100_returnFullString()361 public void getBatteryStatus_batteryLevelIs100_returnFullString() { 362 final Intent intent = new Intent().putExtra(BatteryManager.EXTRA_STATUS, 363 BatteryManager.BATTERY_STATUS_FULL); 364 final Resources resources = mContext.getResources(); 365 366 assertThat(Utils.getBatteryStatus(mContext, intent, /* compactStatus= */ false)).isEqualTo( 367 resources.getString(R.string.battery_info_status_full)); 368 } 369 370 @Test getBatteryStatus_batteryLevelIs100AndUseCompactStatus_returnFullyString()371 public void getBatteryStatus_batteryLevelIs100AndUseCompactStatus_returnFullyString() { 372 final Intent intent = new Intent().putExtra(BatteryManager.EXTRA_STATUS, 373 BatteryManager.BATTERY_STATUS_FULL); 374 final Resources resources = mContext.getResources(); 375 376 assertThat(Utils.getBatteryStatus(mContext, intent, /* compactStatus= */ true)).isEqualTo( 377 resources.getString(R.string.battery_info_status_full_charged)); 378 } 379 380 @Test getBatteryStatus_batteryLevel99_returnChargingString()381 public void getBatteryStatus_batteryLevel99_returnChargingString() { 382 final Intent intent = new Intent(); 383 intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING); 384 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_USB); 385 final Resources resources = mContext.getResources(); 386 387 assertThat(Utils.getBatteryStatus(mContext, intent, /* compactStatus= */ false)).isEqualTo( 388 resources.getString(R.string.battery_info_status_charging)); 389 } 390 391 @Test getBatteryStatus_chargingDock_returnDockChargingString()392 public void getBatteryStatus_chargingDock_returnDockChargingString() { 393 final Intent intent = new Intent(); 394 intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING); 395 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_DOCK); 396 final Resources resources = mContext.getResources(); 397 398 assertThat(Utils.getBatteryStatus(mContext, intent, /* compactStatus= */ false)).isEqualTo( 399 resources.getString(R.string.battery_info_status_charging_dock)); 400 } 401 402 @Test getBatteryStatus_chargingWireless_returnWirelessChargingString()403 public void getBatteryStatus_chargingWireless_returnWirelessChargingString() { 404 final Intent intent = new Intent(); 405 intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING); 406 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_WIRELESS); 407 final Resources resources = mContext.getResources(); 408 409 assertThat(Utils.getBatteryStatus(mContext, intent, /* compactStatus= */ false)).isEqualTo( 410 resources.getString(R.string.battery_info_status_charging_wireless)); 411 } 412 413 @Test getBatteryStatus_chargingAndUseCompactStatus_returnCompactString()414 public void getBatteryStatus_chargingAndUseCompactStatus_returnCompactString() { 415 final Intent intent = new Intent(); 416 intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING); 417 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_USB); 418 final Resources resources = mContext.getResources(); 419 420 assertThat(Utils.getBatteryStatus(mContext, intent, /* compactStatus= */ true)).isEqualTo( 421 resources.getString(R.string.battery_info_status_charging)); 422 } 423 424 @Test getBatteryStatus_chargingWirelessAndUseCompactStatus_returnCompactString()425 public void getBatteryStatus_chargingWirelessAndUseCompactStatus_returnCompactString() { 426 final Intent intent = new Intent(); 427 intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING); 428 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_WIRELESS); 429 final Resources resources = mContext.getResources(); 430 431 assertThat(Utils.getBatteryStatus(mContext, intent, /* compactStatus= */ true)).isEqualTo( 432 resources.getString(R.string.battery_info_status_charging)); 433 } 434 435 @Test containsIncompatibleChargers_nullPorts_returnFalse()436 public void containsIncompatibleChargers_nullPorts_returnFalse() { 437 when(mUsbManager.getPorts()).thenReturn(null); 438 assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isFalse(); 439 } 440 441 @Test containsIncompatibleChargers_emptyPorts_returnFalse()442 public void containsIncompatibleChargers_emptyPorts_returnFalse() { 443 when(mUsbManager.getPorts()).thenReturn(new ArrayList<>()); 444 assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isFalse(); 445 } 446 447 @Test containsIncompatibleChargers_nullPortStatus_returnFalse()448 public void containsIncompatibleChargers_nullPortStatus_returnFalse() { 449 final List<UsbPort> usbPorts = new ArrayList<>(); 450 usbPorts.add(mUsbPort); 451 when(mUsbManager.getPorts()).thenReturn(usbPorts); 452 when(mUsbPort.getStatus()).thenReturn(null); 453 454 assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isFalse(); 455 } 456 457 @Test containsIncompatibleChargers_complianeWarningOther_returnTrue()458 public void containsIncompatibleChargers_complianeWarningOther_returnTrue() { 459 setupIncompatibleCharging(UsbPortStatus.COMPLIANCE_WARNING_OTHER); 460 assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isTrue(); 461 } 462 463 @Test containsIncompatibleChargers_complianeWarningDebug_returnTrue()464 public void containsIncompatibleChargers_complianeWarningDebug_returnTrue() { 465 setupIncompatibleCharging(UsbPortStatus.COMPLIANCE_WARNING_DEBUG_ACCESSORY); 466 assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isTrue(); 467 } 468 469 @Test containsIncompatibleChargers_unexpectedWarningType_returnFalse()470 public void containsIncompatibleChargers_unexpectedWarningType_returnFalse() { 471 setupIncompatibleCharging(UsbPortStatus.COMPLIANCE_WARNING_BC_1_2); 472 assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isFalse(); 473 } 474 475 @Test containsIncompatibleChargers_emptyComplianceWarnings_returnFalse()476 public void containsIncompatibleChargers_emptyComplianceWarnings_returnFalse() { 477 setupIncompatibleCharging(); 478 when(mUsbPortStatus.getComplianceWarnings()).thenReturn(new int[1]); 479 480 assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isFalse(); 481 } 482 483 @Test containsIncompatibleChargers_notSupportComplianceWarnings_returnFalse()484 public void containsIncompatibleChargers_notSupportComplianceWarnings_returnFalse() { 485 setupIncompatibleCharging(); 486 when(mUsbPort.supportsComplianceWarnings()).thenReturn(false); 487 488 assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isFalse(); 489 } 490 491 @Test containsIncompatibleChargers_usbNotConnected_returnFalse()492 public void containsIncompatibleChargers_usbNotConnected_returnFalse() { 493 setupIncompatibleCharging(); 494 when(mUsbPortStatus.isConnected()).thenReturn(false); 495 496 assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isFalse(); 497 } 498 499 @Test containsIncompatibleChargers_disableWarning_returnFalse()500 public void containsIncompatibleChargers_disableWarning_returnFalse() { 501 setupIncompatibleCharging(); 502 Settings.Secure.putInt(mContext.getContentResolver(), 503 Utils.INCOMPATIBLE_CHARGER_WARNING_DISABLED, 1); 504 505 assertThat(Utils.containsIncompatibleChargers(mContext, TAG)).isFalse(); 506 } 507 setupIncompatibleCharging()508 private void setupIncompatibleCharging() { 509 setupIncompatibleCharging(UsbPortStatus.COMPLIANCE_WARNING_OTHER); 510 } 511 setupIncompatibleCharging(int complianceWarningType)512 private void setupIncompatibleCharging(int complianceWarningType) { 513 final List<UsbPort> usbPorts = new ArrayList<>(); 514 usbPorts.add(mUsbPort); 515 when(mUsbManager.getPorts()).thenReturn(usbPorts); 516 when(mUsbPort.getStatus()).thenReturn(mUsbPortStatus); 517 when(mUsbPort.supportsComplianceWarnings()).thenReturn(true); 518 when(mUsbPortStatus.isConnected()).thenReturn(true); 519 when(mUsbPortStatus.getComplianceWarnings()) 520 .thenReturn(new int[]{complianceWarningType}); 521 } 522 } 523