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

Commit 48085a8f authored by jeffreyhuang's avatar jeffreyhuang
Browse files

Add more test to LogdSizePreferenceController

 - Added additional tests

Bug: 34203528
Test: make RunSettingsLibRoboTests -j40
Change-Id: I92297a4cfd6175b9dd684a17b3060f89c2bedc31
parent 16e9b10a
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -33,21 +33,27 @@ public abstract class AbstractLogdSizePreferenceController extends
            + "AbstractLogdSizePreferenceController.LOGD_SIZE_UPDATED";
    public static final String EXTRA_CURRENT_LOGD_VALUE = "CURRENT_LOGD_VALUE";

    @VisibleForTesting
    static final String LOW_RAM_CONFIG_PROPERTY_KEY = "ro.config.low_ram";
    private static final String SELECT_LOGD_SIZE_KEY = "select_logd_size";
    @VisibleForTesting
    static final String SELECT_LOGD_SIZE_PROPERTY = "persist.logd.size";
    static final String SELECT_LOGD_TAG_PROPERTY = "persist.log.tag";
    // Tricky, isLoggable only checks for first character, assumes silence
    static final String SELECT_LOGD_TAG_SILENCE = "Settings";
    private static final String SELECT_LOGD_SNET_TAG_PROPERTY = "persist.log.tag.snet_event_log";
    @VisibleForTesting
    static final String SELECT_LOGD_SNET_TAG_PROPERTY = "persist.log.tag.snet_event_log";
    private static final String SELECT_LOGD_RUNTIME_SNET_TAG_PROPERTY = "log.tag.snet_event_log";
    private static final String SELECT_LOGD_DEFAULT_SIZE_PROPERTY = "ro.logd.size";
    @VisibleForTesting
    static final String SELECT_LOGD_DEFAULT_SIZE_VALUE = "262144";
    private static final String SELECT_LOGD_SVELTE_DEFAULT_SIZE_VALUE = "65536";
    // 32768 is merely a menu marker, 64K is our lowest log buffer size we replace it with.
    private static final String SELECT_LOGD_MINIMUM_SIZE_VALUE = "65536";
    @VisibleForTesting
    static final String SELECT_LOGD_MINIMUM_SIZE_VALUE = "65536";
    static final String SELECT_LOGD_OFF_SIZE_MARKER_VALUE = "32768";
    @VisibleForTesting
    static final String DEFAULT_SNET_TAG = "I";

    private ListPreference mLogdSize;

@@ -154,7 +160,7 @@ public abstract class AbstractLogdSizePreferenceController extends
            if ((snetValue == null) || (snetValue.length() == 0)) {
                snetValue = SystemProperties.get(SELECT_LOGD_RUNTIME_SNET_TAG_PROPERTY);
                if ((snetValue == null) || (snetValue.length() == 0)) {
                    SystemProperties.set(SELECT_LOGD_SNET_TAG_PROPERTY, "I");
                    SystemProperties.set(SELECT_LOGD_SNET_TAG_PROPERTY, DEFAULT_SNET_TAG);
                }
            }
            // Silence all log sources, security logs notwithstanding
+121 −4
Original line number Diff line number Diff line
@@ -16,9 +16,29 @@

package com.android.settingslib.development;

import static com.android.settingslib.development.AbstractLogdSizePreferenceController
        .DEFAULT_SNET_TAG;
import static com.android.settingslib.development.AbstractLogdSizePreferenceController
        .LOW_RAM_CONFIG_PROPERTY_KEY;
import static com.android.settingslib.development.AbstractLogdSizePreferenceController
        .SELECT_LOGD_MINIMUM_SIZE_VALUE;
import static com.android.settingslib.development.AbstractLogdSizePreferenceController
        .SELECT_LOGD_OFF_SIZE_MARKER_VALUE;
import static com.android.settingslib.development.AbstractLogdSizePreferenceController
        .SELECT_LOGD_SIZE_PROPERTY;
import static com.android.settingslib.development.AbstractLogdSizePreferenceController
        .SELECT_LOGD_SNET_TAG_PROPERTY;
import static com.android.settingslib.development.AbstractLogdSizePreferenceController
        .SELECT_LOGD_TAG_PROPERTY;
import static com.android.settingslib.development.AbstractLogdSizePreferenceController
        .SELECT_LOGD_TAG_SILENCE;

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

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.os.SystemProperties;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.PreferenceScreen;
@@ -27,6 +47,7 @@ import com.android.settingslib.R;
import com.android.settingslib.SettingsLibRobolectricTestRunner;
import com.android.settingslib.TestConfig;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -45,22 +66,43 @@ public class LogdSizePreferenceControllerTest {
    @Mock
    private PreferenceScreen mPreferenceScreen;

    /**
     * List Values
     *
     * 0: off
     * 1: 64k
     * 2: 256k
     * 3: 1M
     * 4: 4M
     * 5: 16M
     */
    private String[] mListValues;
    private String[] mListSummaries;
    private Context mContext;
    private AbstractLogdSizePreferenceController mController;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mController = new AbstractLogdSizePreferenceController(RuntimeEnvironment.application) {};

        mContext = RuntimeEnvironment.application;
        mController = new AbstractLogdSizePreferenceController(RuntimeEnvironment.application) {
        };
        mListValues = mContext.getResources().getStringArray(R.array.select_logd_size_values);
        mListSummaries = mContext.getResources().getStringArray(R.array.select_logd_size_summaries);
        doReturn(mListPreference).when(mPreferenceScreen)
                .findPreference(mController.getPreferenceKey());

        mController.displayPreference(mPreferenceScreen);
    }

    @After
    public void tearDown() {
        SystemPropertiesTestImpl.clear();
    }

    @Test
    public void testUpateLogdSizeValues_lowRamEntries() {
        SystemProperties.set("ro.config.low_ram", "true");
    public void testUpdateLogdSizeValues_lowRamEntries() {
        SystemProperties.set(LOW_RAM_CONFIG_PROPERTY_KEY, "true");
        mController.updateLogdSizeValues();
        verify(mListPreference).setEntries(R.array.select_logd_size_lowram_titles);
    }
@@ -77,4 +119,79 @@ public class LogdSizePreferenceControllerTest {
        verify(mListPreference).setValue(
                AbstractLogdSizePreferenceController.SELECT_LOGD_OFF_SIZE_MARKER_VALUE);
    }

    @Test
    public void onPreferenceChange_noTagsSizeValueOff_shouldSetTagAndSnetTagAndSet64KSize() {
        mController.onPreferenceChange(mListPreference, SELECT_LOGD_OFF_SIZE_MARKER_VALUE);

        final String tag = SystemProperties.get(SELECT_LOGD_TAG_PROPERTY);
        final String logSize = SystemProperties.get(SELECT_LOGD_SIZE_PROPERTY);
        final String snetTag = SystemProperties.get(SELECT_LOGD_SNET_TAG_PROPERTY);

        assertThat(tag).isEqualTo(SELECT_LOGD_TAG_SILENCE);
        assertThat(logSize).isEqualTo(SELECT_LOGD_MINIMUM_SIZE_VALUE);
        assertThat(snetTag).isEqualTo(DEFAULT_SNET_TAG);
    }

    @Test
    public void onPreferenceChange_noTagsSizeValue64K_shouldNotSetTagAndSet64KSize() {
        mController.onPreferenceChange(mListPreference, SELECT_LOGD_MINIMUM_SIZE_VALUE);

        final String tag = SystemProperties.get(SELECT_LOGD_TAG_PROPERTY);
        final String logSize = SystemProperties.get(SELECT_LOGD_SIZE_PROPERTY);
        final String snetTag = SystemProperties.get(SELECT_LOGD_SNET_TAG_PROPERTY);

        assertThat(tag).isEmpty();
        assertThat(logSize).isEqualTo(SELECT_LOGD_MINIMUM_SIZE_VALUE);
        assertThat(snetTag).isEmpty();
    }

    @Test
    public void onPreferenceChange_set1M_shouldUpdateSettingLogSizeTo1M() {
        mController.onPreferenceChange(mListPreference, mListValues[3]);

        final String logSize = SystemProperties.get(SELECT_LOGD_SIZE_PROPERTY);

        assertThat(logSize).isEqualTo(mListValues[3]);
    }

    @Test
    public void onPreferenceChange_noValue_shouldUpdateSettingToEmpty() {
        mController.onPreferenceChange(mListPreference, "" /* new value */);

        final String logSize = SystemProperties.get(SELECT_LOGD_SIZE_PROPERTY);

        assertThat(logSize).isEmpty();
    }

    @Test
    public void updateLogdSizeValues_noValueSet_shouldSetDefaultTo64K() {
        SystemProperties.set(SELECT_LOGD_SIZE_PROPERTY, "" /* new value */);

        mController.updateLogdSizeValues();

        verify(mListPreference).setValue(mListValues[2]);
        verify(mListPreference).setSummary(mListSummaries[2]);
    }

    @Test
    public void updateLogdSizeValues_noValueSetLowRamSet_shouldSetDefaultTo64K() {
        SystemProperties.set(LOW_RAM_CONFIG_PROPERTY_KEY, Boolean.toString(true));
        SystemProperties.set(SELECT_LOGD_SIZE_PROPERTY, "" /* new value */);

        mController.updateLogdSizeValues();

        verify(mListPreference).setValue(mListValues[1]);
        verify(mListPreference).setSummary(mListSummaries[1]);
    }

    @Test
    public void updateLogdSizeValues_64KSet_shouldSet64K() {
        SystemProperties.set(SELECT_LOGD_SIZE_PROPERTY, mListValues[1]);

        mController.updateLogdSizeValues();

        verify(mListPreference).setValue(mListValues[1]);
        verify(mListPreference).setSummary(mListSummaries[1]);
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -54,4 +54,8 @@ public class SystemPropertiesTestImpl extends ShadowSystemProperties {
    public static void set(String key, String val) {
        sProperties.put(key, val);
    }

    public static synchronized void clear() {
        sProperties.clear();
    }
}