Merge "Add refresh broadcast id checks" into tm-dev

This commit is contained in:
TreeHugger Robot
2022-05-13 14:18:44 +00:00
committed by Android (Google) Code Review
2 changed files with 64 additions and 55 deletions

View File

@@ -49,9 +49,9 @@ public class SafetySourceBroadcastReceiver extends BroadcastReceiver {
if (ACTION_REFRESH_SAFETY_SOURCES.equals(intent.getAction())) { if (ACTION_REFRESH_SAFETY_SOURCES.equals(intent.getAction())) {
String[] sourceIdsExtra = String[] sourceIdsExtra =
intent.getStringArrayExtra(EXTRA_REFRESH_SAFETY_SOURCE_IDS); intent.getStringArrayExtra(EXTRA_REFRESH_SAFETY_SOURCE_IDS);
if (sourceIdsExtra != null && sourceIdsExtra.length > 0) {
final String refreshBroadcastId = intent.getStringExtra( final String refreshBroadcastId = intent.getStringExtra(
SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID); SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID);
if (sourceIdsExtra != null && sourceIdsExtra.length > 0 && refreshBroadcastId != null) {
final SafetyEvent safetyEvent = new SafetyEvent.Builder( final SafetyEvent safetyEvent = new SafetyEvent.Builder(
SAFETY_EVENT_TYPE_REFRESH_REQUESTED) SAFETY_EVENT_TYPE_REFRESH_REQUESTED)
.setRefreshBroadcastId(refreshBroadcastId).build(); .setRefreshBroadcastId(refreshBroadcastId).build();

View File

@@ -54,13 +54,13 @@ import java.util.List;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class SafetySourceBroadcastReceiverTest { public class SafetySourceBroadcastReceiverTest {
private static final String REFRESH_BROADCAST_ID = "REFRESH_BROADCAST_ID";
private Context mApplicationContext; private Context mApplicationContext;
@Mock @Mock private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper;
private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper;
@Mock @Mock private LockPatternUtils mLockPatternUtils;
private LockPatternUtils mLockPatternUtils;
@Before @Before
public void setUp() { public void setUp() {
@@ -78,19 +78,70 @@ public class SafetySourceBroadcastReceiverTest {
} }
@Test @Test
public void onReceive_onRefresh_whenSafetyCenterIsEnabled_withNoIntentAction_doesNotSetData() { public void onReceive_onRefresh_whenSafetyCenterIsDisabled_doesNotSetData() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(false);
Intent intent = new Intent().putExtra(EXTRA_REFRESH_SAFETY_SOURCE_IDS, new String[]{}); Intent intent =
new Intent()
.setAction(ACTION_REFRESH_SAFETY_SOURCES)
.putExtra(
EXTRA_REFRESH_SAFETY_SOURCE_IDS,
new String[] {LockScreenSafetySource.SAFETY_SOURCE_ID})
.putExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID, REFRESH_BROADCAST_ID);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent); new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
verify(mSafetyCenterManagerWrapper, never()).setSafetySourceData( verify(mSafetyCenterManagerWrapper, never())
any(), any(), any(), any()); .setSafetySourceData(any(), any(), any(), any());
} }
@Test @Test
public void onReceive_onRefresh_whenSafetyCenterIsDisabled_doesNotSetData() { public void onReceive_onRefresh_whenSafetyCenterIsEnabled_withNoIntentAction_doesNotSetData() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(false); when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
Intent intent =
new Intent()
.putExtra(
EXTRA_REFRESH_SAFETY_SOURCE_IDS,
new String[] {LockScreenSafetySource.SAFETY_SOURCE_ID})
.putExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID, REFRESH_BROADCAST_ID);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
verify(mSafetyCenterManagerWrapper, never())
.setSafetySourceData(any(), any(), any(), any());
}
@Test
public void onReceive_onRefresh_whenSafetyCenterIsEnabled_withNullSourceIds_doesNotSetData() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
Intent intent =
new Intent()
.setAction(ACTION_REFRESH_SAFETY_SOURCES)
.putExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID, REFRESH_BROADCAST_ID);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
verify(mSafetyCenterManagerWrapper, never())
.setSafetySourceData(any(), any(), any(), any());
}
@Test
public void onReceive_onRefresh_whenSafetyCenterIsEnabled_withNoSourceIds_doesNotSetData() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
Intent intent =
new Intent()
.setAction(ACTION_REFRESH_SAFETY_SOURCES)
.putExtra(EXTRA_REFRESH_SAFETY_SOURCE_IDS, new String[] {})
.putExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID, REFRESH_BROADCAST_ID);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
verify(mSafetyCenterManagerWrapper, never())
.setSafetySourceData(any(), any(), any(), any());
}
@Test
public void onReceive_onRefresh_whenSafetyCenterIsEnabled_withNoBroadcastId_doesNotSetData() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
Intent intent = Intent intent =
new Intent() new Intent()
.setAction(ACTION_REFRESH_SAFETY_SOURCES) .setAction(ACTION_REFRESH_SAFETY_SOURCES)
@@ -100,57 +151,12 @@ public class SafetySourceBroadcastReceiverTest {
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent); new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
verify(mSafetyCenterManagerWrapper, never()).setSafetySourceData( verify(mSafetyCenterManagerWrapper, never())
any(), any(), any(), any()); .setSafetySourceData(any(), any(), any(), any());
}
@Test
public void onReceive_onRefresh_whenSafetyCenterIsEnabled_withNullSourceIds_doesNotSetData() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
Intent intent = new Intent().setAction(ACTION_REFRESH_SAFETY_SOURCES);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
verify(mSafetyCenterManagerWrapper, never()).setSafetySourceData(
any(), any(), any(), any());
}
@Test
public void onReceive_onRefresh_whenSafetyCenterIsEnabled_withNoSourceIds_doesNotSetData() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
Intent intent =
new Intent()
.setAction(ACTION_REFRESH_SAFETY_SOURCES)
.putExtra(EXTRA_REFRESH_SAFETY_SOURCE_IDS, new String[]{});
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
verify(mSafetyCenterManagerWrapper, never()).setSafetySourceData(
any(), any(), any(), any());
} }
@Test @Test
public void onReceive_onRefresh_setsRefreshEvent() { public void onReceive_onRefresh_setsRefreshEvent() {
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
Intent intent =
new Intent()
.setAction(ACTION_REFRESH_SAFETY_SOURCES)
.putExtra(
EXTRA_REFRESH_SAFETY_SOURCE_IDS,
new String[]{ LockScreenSafetySource.SAFETY_SOURCE_ID });
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
ArgumentCaptor<SafetyEvent> captor = ArgumentCaptor.forClass(SafetyEvent.class);
verify(mSafetyCenterManagerWrapper, times(1))
.setSafetySourceData(any(), any(), any(), captor.capture());
assertThat(captor.getValue()).isEqualTo(
new SafetyEvent.Builder(SAFETY_EVENT_TYPE_REFRESH_REQUESTED).build());
}
@Test
public void onReceive_onRefreshWithBroadcastId_setsRefreshEventWithBroadcastId() {
final String refreshBroadcastId = "REFRESH_BROADCAST_ID";
when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true);
Intent intent = Intent intent =
new Intent() new Intent()
@@ -158,17 +164,18 @@ public class SafetySourceBroadcastReceiverTest {
.putExtra( .putExtra(
EXTRA_REFRESH_SAFETY_SOURCE_IDS, EXTRA_REFRESH_SAFETY_SOURCE_IDS,
new String[] {LockScreenSafetySource.SAFETY_SOURCE_ID}) new String[] {LockScreenSafetySource.SAFETY_SOURCE_ID})
.putExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID, refreshBroadcastId); .putExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID, REFRESH_BROADCAST_ID);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent); new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
ArgumentCaptor<SafetyEvent> captor = ArgumentCaptor.forClass(SafetyEvent.class); ArgumentCaptor<SafetyEvent> captor = ArgumentCaptor.forClass(SafetyEvent.class);
verify(mSafetyCenterManagerWrapper, times(1)) verify(mSafetyCenterManagerWrapper, times(1))
.setSafetySourceData(any(), any(), any(), captor.capture()); .setSafetySourceData(any(), any(), any(), captor.capture());
assertThat(captor.getValue().getRefreshBroadcastId()).isEqualTo(refreshBroadcastId); assertThat(captor.getValue())
assertThat(captor.getValue()).isEqualTo( .isEqualTo(
new SafetyEvent.Builder(SAFETY_EVENT_TYPE_REFRESH_REQUESTED) new SafetyEvent.Builder(SAFETY_EVENT_TYPE_REFRESH_REQUESTED)
.setRefreshBroadcastId(refreshBroadcastId).build()); .setRefreshBroadcastId(REFRESH_BROADCAST_ID)
.build());
} }
@Test @Test
@@ -179,7 +186,8 @@ public class SafetySourceBroadcastReceiverTest {
.setAction(ACTION_REFRESH_SAFETY_SOURCES) .setAction(ACTION_REFRESH_SAFETY_SOURCES)
.putExtra( .putExtra(
EXTRA_REFRESH_SAFETY_SOURCE_IDS, EXTRA_REFRESH_SAFETY_SOURCE_IDS,
new String[]{ LockScreenSafetySource.SAFETY_SOURCE_ID }); new String[] {LockScreenSafetySource.SAFETY_SOURCE_ID})
.putExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID, REFRESH_BROADCAST_ID);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent); new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
@@ -197,7 +205,8 @@ public class SafetySourceBroadcastReceiverTest {
.setAction(ACTION_REFRESH_SAFETY_SOURCES) .setAction(ACTION_REFRESH_SAFETY_SOURCES)
.putExtra( .putExtra(
EXTRA_REFRESH_SAFETY_SOURCE_IDS, EXTRA_REFRESH_SAFETY_SOURCE_IDS,
new String[]{ BiometricsSafetySource.SAFETY_SOURCE_ID }); new String[] {BiometricsSafetySource.SAFETY_SOURCE_ID})
.putExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID, REFRESH_BROADCAST_ID);
new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent); new SafetySourceBroadcastReceiver().onReceive(mApplicationContext, intent);
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);