Merge "Support incompatible charger state in the Settings main page" into udc-dev am: 65efc1f45e

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/22954539

Change-Id: Ic42b12957b18d1bb54a84806ace41f30ae23b437
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot
2023-05-02 02:36:13 +00:00
committed by Automerger Merge Worker
2 changed files with 57 additions and 4 deletions

View File

@@ -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) {

View File

@@ -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<UsbPort> 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});
}
}