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

Commit 2a679254 authored by Bonian Chen's avatar Bonian Chen
Browse files

[Settings] enhancing data saver config control

Enhancing the control of R.bool.config_show_data_saver when false, which including:
1. Initial presentation is invisible
2. Leaving UI when triggered
3. Avoid from getting searched
4. Robolectric test case support

Bug: 243877672
Test: test cases and local testing
Merged-In: I909522c0244ebb012a27d6aff34120a4f90128c6
Change-Id: I909522c0244ebb012a27d6aff34120a4f90128c6
parent b93416b2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -99,6 +99,7 @@
        android:title="@string/data_saver_title"
        android:icon="@drawable/ic_settings_data_usage"
        android:order="10"
        settings:isPreferenceVisible="@bool/config_show_data_saver"
        android:fragment="com.android.settings.datausage.DataSaverSummary"/>

    <com.android.settings.vpn2.VpnInfoPreference
+1 −0
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@
        android:key="data_saver"
        android:title="@string/unrestricted_data_saver"
        android:fragment="com.android.settings.datausage.UnrestrictedDataAccess"
        settings:isPreferenceVisible="@bool/config_show_data_saver"
        settings:controller="com.android.settings.applications.specialaccess.DataSaverController" />

    <Preference
+12 −1
Original line number Diff line number Diff line
@@ -59,6 +59,11 @@ public class DataSaverSummary extends SettingsPreferenceFragment
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        if (!isDataSaverVisible(getContext())) {
            finishFragment();
            return;
        }

        addPreferencesFromResource(R.xml.data_saver);
        mUnrestrictedAccess = findPreference(KEY_UNRESTRICTED_ACCESS);
        mApplicationsState = ApplicationsState.getInstance(
@@ -192,12 +197,18 @@ public class DataSaverSummary extends SettingsPreferenceFragment

    }

    public static boolean isDataSaverVisible(Context context) {
        return context.getResources()
            .getBoolean(R.bool.config_show_data_saver);
    }

    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider(R.xml.data_saver) {

                @Override
                protected boolean isPageSearchEnabled(Context context) {
                    return DataUsageUtils.hasMobileData(context)
                    return isDataSaverVisible(context)
                            && DataUsageUtils.hasMobileData(context)
                            && DataUsageUtils.getDefaultSubscriptionId(context)
                            != SubscriptionManager.INVALID_SUBSCRIPTION_ID;
                }
+16 −0
Original line number Diff line number Diff line
@@ -18,9 +18,13 @@ package com.android.settings.applications.specialaccess;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.when;
import static org.mockito.Mockito.spy;

import android.content.Context;
import android.content.res.Resources;

import com.android.settings.R;

import org.junit.Before;
import org.junit.Test;
@@ -34,23 +38,35 @@ import org.robolectric.annotation.Config;
public class DataSaverControllerTest {

    private Context mContext;
    private Resources mResources;
    private DataSaverController mController;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = spy(RuntimeEnvironment.application.getApplicationContext());

        mResources = spy(mContext.getResources());
        when(mContext.getResources()).thenReturn(mResources);

        mController = new DataSaverController(mContext, "key");
    }

    @Test
    public void testDataSaver_byDefault_shouldBeShown() {
        when(mResources.getBoolean(R.bool.config_show_data_saver)).thenReturn(true);
        assertThat(mController.isAvailable()).isTrue();
    }

    @Test
    @Config(qualifiers = "mcc999")
    public void testDataSaver_ifDisabledByCarrier_shouldNotBeShown() {
        assertThat(mController.isAvailable()).isFalse();
    }

    @Test
    public void testDataSaver_ifDisabled_shouldNotBeShown() {
        when(mResources.getBoolean(R.bool.config_show_data_saver)).thenReturn(false);
        assertThat(mController.isAvailable()).isFalse();
    }
}