diff --git a/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceController.java b/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceController.java index 5bec7bd4aae..e7a18d0e6aa 100644 --- a/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceController.java +++ b/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceController.java @@ -30,6 +30,7 @@ import com.android.settings.overlay.FeatureFactory; import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStop; +import com.android.settingslib.Utils; import com.android.settingslib.utils.ThreadUtils; public class TopLevelBatteryPreferenceController extends BasePreferenceController implements @@ -129,6 +130,9 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle } private CharSequence generateLabel(BatteryInfo info) { + if (Utils.containsIncompatibleChargers(mContext, TAG)) { + return mContext.getString(R.string.battery_tip_incompatible_charging_title); + } if (!info.discharging && info.chargeLabel != null) { return info.chargeLabel; } else if (info.remainingLabel == null) { @@ -170,4 +174,4 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle String pkgName = lastPkgIndex > 0 ? classPath.substring(0, lastPkgIndex) : ""; return new ComponentName(pkgName, split[classNameIndex]); } -} \ No newline at end of file +} diff --git a/tests/robotests/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceControllerTest.java index 85e29429c9e..55fe8b8f0e9 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceControllerTest.java @@ -28,6 +28,9 @@ import static org.mockito.Mockito.when; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.hardware.usb.UsbManager; +import android.hardware.usb.UsbPort; +import android.hardware.usb.UsbPortStatus; import androidx.preference.Preference; import androidx.test.core.app.ApplicationProvider; @@ -38,21 +41,33 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; +import java.util.ArrayList; +import java.util.List; + @RunWith(RobolectricTestRunner.class) public class TopLevelBatteryPreferenceControllerTest { private Context mContext; private TopLevelBatteryPreferenceController mController; private BatterySettingsFeatureProvider mBatterySettingsFeatureProvider; + @Mock + private UsbPort mUsbPort; + @Mock + private UsbManager mUsbManager; + @Mock + private UsbPortStatus mUsbPortStatus; + @Before public void setUp() { MockitoAnnotations.initMocks(this); mContext = spy(ApplicationProvider.getApplicationContext()); mController = new TopLevelBatteryPreferenceController(mContext, "test_key"); + when(mContext.getSystemService(UsbManager.class)).thenReturn(mUsbManager); } @Test @@ -88,27 +103,61 @@ public class TopLevelBatteryPreferenceControllerTest { } @Test - public void getDashboardLabel_returnsCorrectLabel() { + public void getDashboardLabel_returnsBatterPercentString() { mController.mPreference = new Preference(mContext); BatteryInfo info = new BatteryInfo(); info.batteryPercentString = "3%"; + assertThat(mController.getDashboardLabel(mContext, info, true)) .isEqualTo(info.batteryPercentString); + } + @Test + public void getDashboardLabel_returnsRemainingLabel() { + mController.mPreference = new Preference(mContext); + BatteryInfo info = new BatteryInfo(); + info.batteryPercentString = "3%"; info.remainingLabel = "Phone will shut down soon"; + assertThat(mController.getDashboardLabel(mContext, info, true)) .isEqualTo("3% - Phone will shut down soon"); + } + @Test + public void getDashboardLabel_returnsChargeLabel() { + mController.mPreference = new Preference(mContext); + BatteryInfo info = new BatteryInfo(); info.discharging = false; info.chargeLabel = "5% - charging"; - assertThat(mController.getDashboardLabel(mContext, info, true)).isEqualTo("5% - charging"); + + assertThat(mController.getDashboardLabel(mContext, info, true)) + .isEqualTo(info.chargeLabel); + } + + @Test + public void getDashboardLabel_incompatibleCharger_returnsCorrectLabel() { + setupIncompatibleEvent(); + mController.mPreference = new Preference(mContext); + BatteryInfo info = new BatteryInfo(); + + assertThat(mController.getDashboardLabel(mContext, info, true)) + .isEqualTo(mContext.getString(R.string.battery_tip_incompatible_charging_title)); } @Test public void getSummary_batteryNotPresent_shouldShowWarningMessage() { mController.mIsBatteryPresent = false; - assertThat(mController.getSummary()) .isEqualTo(mContext.getString(R.string.battery_missing_message)); } + + private void setupIncompatibleEvent() { + final List usbPorts = new ArrayList<>(); + usbPorts.add(mUsbPort); + when(mUsbManager.getPorts()).thenReturn(usbPorts); + when(mUsbPort.getStatus()).thenReturn(mUsbPortStatus); + when(mUsbPort.supportsComplianceWarnings()).thenReturn(true); + when(mUsbPortStatus.isConnected()).thenReturn(true); + when(mUsbPortStatus.getComplianceWarnings()).thenReturn(new int[]{1}); + } }