Restrict screen saver settings searches to enabled users.

If a user can't enable screen saver, they shouldn't be able to
accidentally find screen saver related settings in Settings.

Bug: 260276394
Test: atest ScreenSaverPreferenceControllerTest
Test: manually by settings up a second user on a device that supports
screen saver, switching to that user, and making sure that searches for
"screen saver" don't accidentally reveal settings that can't be
accessed.

Change-Id: If2f465b2acb548455f0868507cf47217ea1a626f
This commit is contained in:
Will Leshner
2023-02-16 11:06:05 -08:00
parent 3a445a3ea1
commit ca1379f0b3
4 changed files with 43 additions and 21 deletions

View File

@@ -18,13 +18,15 @@ package com.android.settings.display;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.Resources;
import android.os.UserHandle;
import android.os.UserManager;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
@@ -32,14 +34,13 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
@RunWith(AndroidJUnit4.class)
public class ScreenSaverPreferenceControllerTest {
@Spy
private final Context mContext = ApplicationProvider.getApplicationContext();
@Spy
private final Resources mResources = mContext.getResources();
@Mock
private Context mContext;
@Mock
private Resources mResources;
@Mock
private UserManager mUserManager;
@@ -51,10 +52,14 @@ public class ScreenSaverPreferenceControllerTest {
public void setup() {
MockitoAnnotations.initMocks(this);
mController = new ScreenSaverPreferenceController(mContext, mPrefKey);
when(mContext.getResources()).thenReturn(mResources);
when(mContext.getSystemServiceName(UserManager.class))
.thenReturn(Context.USER_SERVICE);
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
when(mUserManager.getMainUser()).thenReturn(UserHandle.of(0));
when(mContext.createContextAsUser(any(), anyInt())).thenReturn(mContext);
mController = new ScreenSaverPreferenceController(mContext, mPrefKey);
}
@Test
@@ -64,7 +69,7 @@ public class ScreenSaverPreferenceControllerTest {
when(mResources.getBoolean(
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
.thenReturn(false);
when(mUserManager.isMainUser()).thenReturn(true);
when(mUserManager.isUserForeground()).thenReturn(true);
assertTrue(mController.isAvailable());
}
@@ -75,7 +80,7 @@ public class ScreenSaverPreferenceControllerTest {
when(mResources.getBoolean(
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
.thenReturn(false);
when(mUserManager.isMainUser()).thenReturn(false);
when(mUserManager.isUserForeground()).thenReturn(false);
assertTrue(mController.isAvailable());
}
@@ -86,7 +91,7 @@ public class ScreenSaverPreferenceControllerTest {
when(mResources.getBoolean(
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
.thenReturn(false);
when(mUserManager.isMainUser()).thenReturn(true);
when(mUserManager.isUserForeground()).thenReturn(true);
assertFalse(mController.isAvailable());
}
@@ -97,7 +102,7 @@ public class ScreenSaverPreferenceControllerTest {
when(mResources.getBoolean(
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
.thenReturn(true);
when(mUserManager.isMainUser()).thenReturn(true);
when(mUserManager.isUserForeground()).thenReturn(true);
assertTrue(mController.isAvailable());
}
@@ -108,7 +113,7 @@ public class ScreenSaverPreferenceControllerTest {
when(mResources.getBoolean(
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
.thenReturn(true);
when(mUserManager.isMainUser()).thenReturn(false);
when(mUserManager.isUserForeground()).thenReturn(false);
assertFalse(mController.isAvailable());
}
}