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

Commit 98f593c5 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Customize the availability of Testing Settings Menu" into main am: d25fdc50 am: b6683ea5

parents aa7fd384 b6683ea5
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -842,4 +842,7 @@

    <!-- Package name for Linux terminal app -->
    <string name="config_linux_terminal_app_package_name" translatable="false">com.android.virtualization.terminal</string>

    <!-- Disable the Testing Setting Menu for user builds, i.e only display the menu on userdebug/eng builds -->
    <bool name="config_hide_testing_settings_menu_for_user_builds">false</bool>
</resources>
+8 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settings;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.telephony.TelephonyManager;

import com.android.settings.Settings.TestingSettingsActivity;
@@ -32,11 +33,17 @@ public class TestingSettingsBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && intent.getAction() != null
                && intent.getAction().equals(TelephonyManager.ACTION_SECRET_CODE)) {
                && intent.getAction().equals(TelephonyManager.ACTION_SECRET_CODE)
                && !isDisabled(context)) {
            Intent i = new Intent(Intent.ACTION_MAIN);
            i.setClass(context, TestingSettingsActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }

    private boolean isDisabled(Context context) {
        return "user".equals(Build.TYPE) && context.getResources().getBoolean(
                R.bool.config_hide_testing_settings_menu_for_user_builds);
    }
}
+69 −0
Original line number Diff line number Diff line
@@ -18,9 +18,13 @@ package com.android.settings;

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

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

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.telephony.TelephonyManager;

import org.junit.Before;
@@ -29,6 +33,7 @@ import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowBuild;

@RunWith(RobolectricTestRunner.class)
public class TestingSettingsBroadcastReceiverTest {
@@ -78,4 +83,68 @@ public class TestingSettingsBroadcastReceiverTest {
        final String dest = next.getComponent().getClassName();
        assertThat(dest).isEqualTo(Settings.TestingSettingsActivity.class.getName());
    }

    @Test
    public void onReceive_disabledForUserBuild_BuildType_User_shouldNotStartActivity() {
        // TestingSettingsMenu should be disabled if current Build.TYPE is "user" and
        // 'config_hide_testing_settings_menu_for_user_builds' is true
        ShadowBuild.setType("user");

        mContext = spy(RuntimeEnvironment.application);
        setUpConfig(mContext, true /*disable for user build*/);

        final Intent intent = new Intent();
        intent.setAction(TelephonyManager.ACTION_SECRET_CODE);

        mReceiver.onReceive(mContext, intent);

        final Intent next = Shadows.shadowOf(mApplication).getNextStartedActivity();
        assertThat(next).isNull();
    }

    @Test
    public void onReceive_disabledForUserBuild_BuildType_Userdebug_shouldStartActivity() {
        // TestingSettingsMenu should not be disabled if current Build.TYPE is "userdebug" and
        // 'config_hide_testing_settings_menu_for_user_builds' is true
        ShadowBuild.setType("userdebug");

        mContext = spy(RuntimeEnvironment.application);
        setUpConfig(mContext, true /*disable for user build*/);

        final Intent intent = new Intent();
        intent.setAction(TelephonyManager.ACTION_SECRET_CODE);

        mReceiver.onReceive(mContext, intent);

        final Intent next = Shadows.shadowOf(mApplication).getNextStartedActivity();
        assertThat(next).isNotNull();
        final String dest = next.getComponent().getClassName();
        assertThat(dest).isEqualTo(Settings.TestingSettingsActivity.class.getName());
    }

    @Test
    public void onReceive_notDisabledForUserBuildType_shouldStartActivity() {
        // TestingSettingsMenu should not be disabled if
        // 'config_hide_testing_settings_menu_for_user_builds' is false, regardless of Build.TYPE
        mContext = spy(RuntimeEnvironment.application);
        setUpConfig(mContext, false /*disable for user build*/);

        final Intent intent = new Intent();
        intent.setAction(TelephonyManager.ACTION_SECRET_CODE);

        mReceiver.onReceive(mContext, intent);

        final Intent next = Shadows.shadowOf(mApplication).getNextStartedActivity();
        assertThat(next).isNotNull();
        final String dest = next.getComponent().getClassName();
        assertThat(dest).isEqualTo(Settings.TestingSettingsActivity.class.getName());
    }

    private static void setUpConfig(Context context, boolean disabledForUserBuild) {
        when(context.getApplicationContext()).thenReturn(context);
        Resources spiedResources = spy(context.getResources());
        when(context.getResources()).thenReturn(spiedResources);
        when(spiedResources.getBoolean(R.bool.config_hide_testing_settings_menu_for_user_builds))
                .thenReturn(disabledForUserBuild);
    }
}