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

Commit 805d59dc authored by Robin Lee's avatar Robin Lee
Browse files

Fix 4 broken work profile tests

Commit 2953f31b skipped
RunSettingsRoboTests. The tests started failing because
WorkSoundPreferenceController calls UserManager.isUserUnlocked(int) now
which is not implemented in robolectric.

Fixed by adding a wrapper method to AudioHelper.

Fix: 34819603
Test: make RunSettingsRoboTests
Change-Id: Ia79cc4def9442706752f7e7b9a895ffa8150fd9d
parent 4dfe53e1
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -42,6 +42,10 @@ public class AudioHelper {
        return Utils.getManagedProfileId(um, UserHandle.myUserId());
    }

    public boolean isUserUnlocked(UserManager um, @UserIdInt int userId) {
        return um.isUserUnlocked(userId);
    }

    public Context createPackageContextAsUser(@UserIdInt int profileId) {
        return Utils.createPackageContextAsUser(mContext, profileId);
    }
+2 −2
Original line number Diff line number Diff line
@@ -160,8 +160,8 @@ public class WorkSoundPreferenceController extends PreferenceController implemen
    }

    private CharSequence updateRingtoneName(Context context, int type) {
        if (context == null || !UserManager.get(context).isUserUnlocked(context.getUserId())) {
            return context.getString(R.string.managed_profile_not_available_label);
        if (context == null || !mHelper.isUserUnlocked(mUserManager, context.getUserId())) {
            return mContext.getString(R.string.managed_profile_not_available_label);
        }
        Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
        return Ringtone.getTitle(context, ringtoneUri, false /* followSettingsUri */,
+7 −18
Original line number Diff line number Diff line
@@ -41,10 +41,10 @@ import org.robolectric.annotation.Config;

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

import static org.junit.Assume.assumeTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -77,14 +77,15 @@ public class WorkSoundPreferenceControllerTest {
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
        when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
        mController = new WorkSoundPreferenceController(mContext, mFragment, null, mAudioHelper);
    }

    @Test
    public void isAvailable_managedProfileAndNotSingleVolume_shouldReturnTrue() {
        when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
        when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
                .thenReturn(UserHandle.myUserId());
        when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(true);
        when(mAudioHelper.isSingleVolume()).thenReturn(false);

        assertThat(mController.isAvailable()).isTrue();
@@ -92,9 +93,9 @@ public class WorkSoundPreferenceControllerTest {

    @Test
    public void isAvailable_noManagedProfile_shouldReturnFalse() {
        when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
        when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
                .thenReturn(UserHandle.USER_NULL);
        when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(true);
        when(mAudioHelper.isSingleVolume()).thenReturn(false);

        assertThat(mController.isAvailable()).isFalse();
@@ -102,9 +103,9 @@ public class WorkSoundPreferenceControllerTest {

    @Test
    public void isAvailable_singleVolume_shouldReturnFalse() {
        when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
        when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
                .thenReturn(UserHandle.myUserId());
        when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(true);
        when(mAudioHelper.isSingleVolume()).thenReturn(true);

        assertThat(mController.isAvailable()).isFalse();
@@ -112,12 +113,9 @@ public class WorkSoundPreferenceControllerTest {

    @Test
    public void onResume_available_shouldAddPreferenceCategory() {
        // Test requires UserManager.isUserUnlocked, which is an N API.
        assumeTrue(RuntimeEnvironment.getApiLevel() >= VERSION_CODES.N);

        when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
        when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
                .thenReturn(UserHandle.myUserId());
        when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(true);
        when(mAudioHelper.isSingleVolume()).thenReturn(false);
        when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
        when(mAudioHelper.createPackageContextAsUser(anyInt())).thenReturn(mContext);
@@ -130,9 +128,6 @@ public class WorkSoundPreferenceControllerTest {

    @Test
    public void onManagedProfileAdded_shouldAddPreferenceCategory() {
        // Test requires UserManager.isUserUnlocked, which is an N API.
        assumeTrue(RuntimeEnvironment.getApiLevel() >= VERSION_CODES.N);

        // Given a device without any managed profiles:
        when(mAudioHelper.isSingleVolume()).thenReturn(false);
        when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
@@ -156,15 +151,13 @@ public class WorkSoundPreferenceControllerTest {

    @Test
    public void onManagedProfileRemoved_shouldRemovePreferenceCategory() {
        // Test requires UserManager.isUserUnlocked, which is an N API.
        assumeTrue(RuntimeEnvironment.getApiLevel() >= VERSION_CODES.N);

        // Given a device with a managed profile:
        when(mAudioHelper.isSingleVolume()).thenReturn(false);
        when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
        when(mAudioHelper.createPackageContextAsUser(anyInt())).thenReturn(mContext);
        when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
                .thenReturn(UserHandle.myUserId());
        when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(true);
        mockWorkCategory();

        // Which is in resumed state:
@@ -180,7 +173,6 @@ public class WorkSoundPreferenceControllerTest {

    @Test
    public void onResume_notAvailable_shouldNotAddPreferenceCategory() {
        when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
        when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
            .thenReturn(UserHandle.USER_NULL);
        when(mAudioHelper.isSingleVolume()).thenReturn(true);
@@ -193,9 +185,6 @@ public class WorkSoundPreferenceControllerTest {

    @Test
    public void onPreferenceChange_shouldUpdateSummary() {
        // Test requires UserManager.isUserUnlocked, which is an N API.
        assumeTrue(RuntimeEnvironment.getApiLevel() >= VERSION_CODES.N);

        final Preference preference = mock(Preference.class);
        when(preference.getKey()).thenReturn(KEY_WORK_PHONE_RINGTONE);