PermissionRequest extends NannyRequest { public void startRequest(Context context, @Nullable String rationale) { setPayload(newBroadcastIntent(context, rationale)); super.startRequest(context); } protected  PermissionRequest(RequestParams params); void startRequest(Context context, @Nullable String rationale);  }
@Test public void startRequest_ShouldSendBroadcastWithExpectedParameters() throws Exception { when(ctx.getPackageManager()).thenReturn(pm); when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(appInfo); target.listener(listener); target.startRequest(ctx, "rationale"); verify(ctx).registerReceiver((BroadcastReceiver) notNull(), filterCaptor.capture()); verify(ctx).sendBroadcast(intentCaptor.capture()); Intent request = intentCaptor.getValue(); assertThat(request.getComponent().getPackageName(), is(Nanny.SERVER_APP_ID)); assertThat(request.getStringExtra(Nanny.CLIENT_ADDRESS), is(filterCaptor.getValue().getAction(0))); assertThat(request.hasExtra(Nanny.PROTOCOL_VERSION), is(true)); Bundle entity = request.getBundleExtra(Nanny.ENTITY_BODY); assertThat(entity.containsKey(Nanny.SENDER_IDENTITY), is(true)); assertThat(entity.containsKey(Nanny.REQUEST_PARAMS), is(true)); assertThat(entity.containsKey(Nanny.REQUEST_RATIONALE), is(true)); }
@Test public void startRequest_ShouldNotSendClientAddress_WhenNoListenerIsAttached() throws Exception { when(ctx.getPackageManager()).thenReturn(pm); when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(appInfo); target.startRequest(ctx, null); verify(ctx, never()).registerReceiver((BroadcastReceiver) any(), (IntentFilter) any()); verify(ctx).sendBroadcast(intentCaptor.capture()); assertThat(intentCaptor.getValue().hasExtra(Nanny.CLIENT_ADDRESS), is(false)); }