Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 09c1a4a8 authored by ykhung's avatar ykhung
Browse files

Support incompatible charger state in the Settings main page

https://screenshot.googleplex.com/9af4YCnCCjKHCFY

Bug: 271775549
Test: make test RunSettingsRoboTests
Change-Id: I6562e1b48a85ceceb20389ed87e55e0093040be2
parent d7bbff00
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -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) {
+52 −3
Original line number Diff line number Diff line
@@ -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});
    }
}