Don't send data for non-main users

This is a regression caused by ag/26661827

Fix: 333872544
Test: manual
Test: atest PrivateSpaceSafetySourceTest
Change-Id: I0465899af3745fa39eab8806dbceb347d70b0aa3
This commit is contained in:
Manish Singh
2024-04-25 11:37:27 +00:00
parent 30a03c4ee8
commit 50b5bae531
2 changed files with 27 additions and 7 deletions

View File

@@ -56,14 +56,14 @@ public final class PrivateSpaceSafetySource {
Log.i(TAG, "Private Space not allowed for user " + context.getUser()); Log.i(TAG, "Private Space not allowed for user " + context.getUser());
return; return;
} }
} else { }
// Check the profile type - we don't want to show this for anything other than primary // Check the profile type - we don't want to show this for anything other than primary
// user. // user.
if (userManager != null && !userManager.isMainUser()) { if (userManager != null && !userManager.isMainUser()) {
Log.i(TAG, "setSafetySourceData not main user"); Log.i(TAG, "setSafetySourceData not main user");
return; return;
} }
}
if (!Flags.allowPrivateProfile() if (!Flags.allowPrivateProfile()
|| !android.multiuser.Flags.enablePrivateSpaceFeatures()) { || !android.multiuser.Flags.enablePrivateSpaceFeatures()) {

View File

@@ -26,11 +26,13 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.os.Flags; import android.os.Flags;
import android.os.UserManager;
import android.platform.test.flag.junit.SetFlagsRule; import android.platform.test.flag.junit.SetFlagsRule;
import android.safetycenter.SafetyEvent; import android.safetycenter.SafetyEvent;
import android.safetycenter.SafetySourceData; import android.safetycenter.SafetySourceData;
@@ -55,7 +57,9 @@ public class PrivateSpaceSafetySourceTest {
private static final SafetyEvent EVENT_TYPE_DEVICE_REBOOTED = private static final SafetyEvent EVENT_TYPE_DEVICE_REBOOTED =
new SafetyEvent.Builder(SAFETY_EVENT_TYPE_DEVICE_REBOOTED).build(); new SafetyEvent.Builder(SAFETY_EVENT_TYPE_DEVICE_REBOOTED).build();
private Context mContext = ApplicationProvider.getApplicationContext(); private Context mContext = ApplicationProvider.getApplicationContext();
private Context mMockContext;
@Mock private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper; @Mock private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper;
@Mock private UserManager mUserManager;
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
/** Required setup before a test. */ /** Required setup before a test. */
@@ -147,4 +151,20 @@ public class PrivateSpaceSafetySourceTest {
assertThat(safetySourceStatus.getPendingIntent().getIntent().getIdentifier()) assertThat(safetySourceStatus.getPendingIntent().getIntent().getIdentifier())
.isEqualTo(SAFETY_SOURCE_ID); .isEqualTo(SAFETY_SOURCE_ID);
} }
/** Tests that setSafetySourceData sets the source status enabled. */
@Test
public void setSafetySourceData_whenNonMainUser_doesNotSendData() {
mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
mMockContext = spy(mContext);
when(mSafetyCenterManagerWrapper.isEnabled(mMockContext)).thenReturn(true);
when(mMockContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
when(mUserManager.isMainUser()).thenReturn(false);
PrivateSpaceSafetySource.setSafetySourceData(mMockContext, EVENT_TYPE_DEVICE_REBOOTED);
verify(mSafetyCenterManagerWrapper, never()).setSafetySourceData(
any(), any(), any(), any());
}
} }