EventForwardingBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (mCustomEventInterstitialListener == null) { return; } final long receivedIdentifier = intent.getLongExtra(BROADCAST_IDENTIFIER_KEY, -1); if (mBroadcastIdentifier != receivedIdentifier) { return; } final String action = intent.getAction(); if (ACTION_INTERSTITIAL_FAIL.equals(action)) { mCustomEventInterstitialListener.onInterstitialFailed(NETWORK_INVALID_STATE); } else if (ACTION_INTERSTITIAL_SHOW.equals(action)) { mCustomEventInterstitialListener.onInterstitialShown(); } else if (ACTION_INTERSTITIAL_DISMISS.equals(action)) { mCustomEventInterstitialListener.onInterstitialDismissed(); unregister(); } else if (ACTION_INTERSTITIAL_CLICK.equals(action)) { mCustomEventInterstitialListener.onInterstitialClicked(); } } EventForwardingBroadcastReceiver(CustomEventInterstitialListener customEventInterstitialListener, final long broadcastIdentifier); static IntentFilter getHtmlInterstitialIntentFilter(); @Override void onReceive(Context context, Intent intent); void register(Context context); void unregister(); static final String ACTION_INTERSTITIAL_FAIL; static final String ACTION_INTERSTITIAL_SHOW; static final String ACTION_INTERSTITIAL_DISMISS; static final String ACTION_INTERSTITIAL_CLICK; }
@Test public void onReceive_whenActionInterstitialFail_shouldNotifyListener() throws Exception { Intent intent = getIntentForActionAndIdentifier(ACTION_INTERSTITIAL_CLICK, broadcastIdentifier); subject.onReceive(context, intent); verify(customEventInterstitialListener).onInterstitialClicked(); }
@Test public void onReceive_whenActionInterstitialShow_shouldNotifyListener() throws Exception { Intent intent = getIntentForActionAndIdentifier(ACTION_INTERSTITIAL_SHOW, broadcastIdentifier); subject.onReceive(context, intent); verify(customEventInterstitialListener).onInterstitialShown(); }
@Test public void onReceive_whenActionInterstitialDismiss_shouldNotifyListener() throws Exception { Intent intent = getIntentForActionAndIdentifier(ACTION_INTERSTITIAL_DISMISS, broadcastIdentifier); subject.onReceive(context, intent); verify(customEventInterstitialListener).onInterstitialDismissed(); }
@Test public void onReceive_whenActionInterstitialClick_shouldNotifyListener() throws Exception { Intent intent = getIntentForActionAndIdentifier(ACTION_INTERSTITIAL_CLICK, broadcastIdentifier); subject.onReceive(context, intent); verify(customEventInterstitialListener).onInterstitialClicked(); }
@Test public void onReceive_withIncorrectBroadcastIdentifier_shouldDoNothing() throws Exception { long incorrectBroadcastIdentifier = broadcastIdentifier + 1; Intent fail = getIntentForActionAndIdentifier(ACTION_INTERSTITIAL_FAIL, incorrectBroadcastIdentifier); Intent show = getIntentForActionAndIdentifier(ACTION_INTERSTITIAL_SHOW, incorrectBroadcastIdentifier); Intent click = getIntentForActionAndIdentifier(ACTION_INTERSTITIAL_CLICK, incorrectBroadcastIdentifier); Intent dismiss = getIntentForActionAndIdentifier(ACTION_INTERSTITIAL_DISMISS, incorrectBroadcastIdentifier); subject.onReceive(context, fail); subject.onReceive(context, show); subject.onReceive(context, click); subject.onReceive(context, dismiss); verifyNoMoreInteractions(customEventInterstitialListener); }
@Test public void onReceiver_whenCustomEventInterstitialListenerIsNull_shouldNotBlowUp() throws Exception { Intent intent = new Intent(ACTION_INTERSTITIAL_SHOW); subject = new EventForwardingBroadcastReceiver(null, broadcastIdentifier); subject.onReceive(context, intent); }