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

Commit 35b4ca46 authored by Chilun Huang's avatar Chilun Huang Committed by Android (Google) Code Review
Browse files

Merge "Add index for Hotspot settings page into search"

parents 61949cc7 805ec798
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -15,10 +15,14 @@
 */
package com.android.settingslib;

import static android.os.UserManager.DISALLOW_CONFIG_TETHERING;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.SystemProperties;
import androidx.annotation.VisibleForTesting;
import android.os.UserHandle;
import android.telephony.CarrierConfigManager;
import androidx.annotation.VisibleForTesting;

public class TetherUtil {

@@ -49,4 +53,13 @@ public class TetherUtil {
        }
        return (provisionApp.length == 2);
    }

    public static boolean isTetherAvailable(Context context) {
        final ConnectivityManager cm = context.getSystemService(ConnectivityManager.class);
        final boolean tetherConfigDisallowed = RestrictedLockUtils.checkIfRestrictionEnforced(
                context, DISALLOW_CONFIG_TETHERING, UserHandle.myUserId()) != null;
        final boolean hasBaseUserRestriction = RestrictedLockUtils.hasBaseUserRestriction(
                context, DISALLOW_CONFIG_TETHERING, UserHandle.myUserId());
        return (cm.isTetheringSupported() || tetherConfigDisallowed) && !hasBaseUserRestriction;
    }
}
+103 −2
Original line number Diff line number Diff line
@@ -17,31 +17,132 @@
package com.android.settingslib;

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

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

import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;
import android.net.ConnectivityManager;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;

import java.util.ArrayList;
import java.util.List;

@RunWith(SettingsLibRobolectricTestRunner.class)
public class TetherUtilTest {

    @Mock
    private Context mContext;

    @Mock
    private ConnectivityManager mConnectivityManager;
    @Mock
    private UserManager mUserManager;

    @Before
    public void setUp() {
        mContext = spy(RuntimeEnvironment.application);

        MockitoAnnotations.initMocks(this);
        doReturn(mConnectivityManager)
                .when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE);
        doReturn(mUserManager)
                .when(mContext).getSystemService(Context.USER_SERVICE);
    }

    @Test
    public void isEntitlementCheckRequired_noConfigManager_returnTrue() {
        when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)).thenReturn(null);
        doReturn(null).when(mContext).getSystemService(Context.CARRIER_CONFIG_SERVICE);

        assertThat(TetherUtil.isEntitlementCheckRequired(mContext)).isTrue();
    }

    @Test
    public void isTetherAvailable_supported_configDisallowed_hasUserRestriction_returnTrue() {
        setupIsTetherAvailable(true, true, true);

        assertThat(TetherUtil.isTetherAvailable(mContext)).isFalse();
    }

    @Test
    public void isTetherAvailable_notSupported_configDisallowed_hasUserRestriction_returnTrue() {
        setupIsTetherAvailable(false, true, true);

        assertThat(TetherUtil.isTetherAvailable(mContext)).isFalse();
    }

    @Test
    public void isTetherAvailable_supported_configAllowed_hasUserRestriction_returnTrue() {
        setupIsTetherAvailable(true, false, true);

        assertThat(TetherUtil.isTetherAvailable(mContext)).isFalse();
    }

    @Test
    public void isTetherAvailable_notSupported_configAllowed_hasUserRestriction_returnFalse() {
        setupIsTetherAvailable(false, false, true);

        assertThat(TetherUtil.isTetherAvailable(mContext)).isFalse();
    }

    @Test
    public void isTetherAvailable_supported_configDisallowed_noUserRestriction_returnTrue() {
        setupIsTetherAvailable(true, true, false);

        assertThat(TetherUtil.isTetherAvailable(mContext)).isTrue();
    }

    @Test
    public void isTetherAvailable_notSupported_configDisallowed_noUserRestriction_returnTrue() {
        setupIsTetherAvailable(false, true, false);

        assertThat(TetherUtil.isTetherAvailable(mContext)).isTrue();
    }

    @Test
    public void isTetherAvailable_supported_configAllowed_noUserRestriction_returnTrue() {
        setupIsTetherAvailable(true, false, false);

        assertThat(TetherUtil.isTetherAvailable(mContext)).isTrue();
    }

    @Test
    public void isTetherAvailable_notSupported_configAllowed_noUserRestriction_returnFalse() {
        setupIsTetherAvailable(false, false, false);

        assertThat(TetherUtil.isTetherAvailable(mContext)).isFalse();
    }

    private void setupIsTetherAvailable(boolean tetherSupported, boolean configAllowed,
            boolean hasBseUserRestriction) {
        when(mConnectivityManager.isTetheringSupported()).thenReturn(tetherSupported);

        // For RestrictedLockUtils.checkIfRestrictionEnforced
        final int userId = UserHandle.myUserId();
        List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
        if (configAllowed) {
            // Add two enforcing users so that RestrictedLockUtils.checkIfRestrictionEnforced
            // returns non-null
            enforcingUsers.add(new UserManager.EnforcingUser(userId,
                    UserManager.RESTRICTION_SOURCE_DEVICE_OWNER));
            enforcingUsers.add(new UserManager.EnforcingUser(userId,
                    UserManager.RESTRICTION_SOURCE_PROFILE_OWNER));
        }
        when(mUserManager.getUserRestrictionSources(
                UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId)))
                .thenReturn(enforcingUsers);

        // For RestrictedLockUtils.hasBaseUserRestriction
        when(mUserManager.hasBaseUserRestriction(
                UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId)))
                .thenReturn(hasBseUserRestriction);
    }
}