InstallReceiver extends BroadcastReceiver { @Override public void onReceive(@NonNull Context context, @Nullable Intent intent) { Autopilot.automaticTakeOff(context); if (!UAirship.isTakingOff() && !UAirship.isFlying()) { Logger.error("InstallReceiver - unable to track install referrer, takeOff not called."); return; } if (intent == null) { return; } String referrer = intent.getStringExtra(EXTRA_INSTALL_REFERRER); if (UAStringUtil.isEmpty(referrer) || !ACTION_INSTALL_REFERRER.equals(intent.getAction())) { Logger.debug("InstallReceiver - missing referrer or invalid action."); return; } InstallAttributionEvent event = new InstallAttributionEvent(referrer); UAirship.shared().getAnalytics().addEvent(event); }  @Override void onReceive(@NonNull Context context, @Nullable Intent intent);  }
@Test public void testCreateInstallAttributionEvent() { Intent intent = new Intent("com.android.vending.INSTALL_REFERRER") .putExtra("referrer", "some value"); receiver.onReceive(context, intent); verify(mockAnalytics).addEvent(argThat(new ArgumentMatcher<Event>() { @Override public boolean matches(Event event) { if (!(event instanceof InstallAttributionEvent)) { return false; } return "some value".equals(event.getEventData().opt("google_play_referrer").getString()); } })); }
@Test public void testMissingReferrer() { Intent intent = new Intent("com.android.vending.INSTALL_REFERRER"); receiver.onReceive(context, intent); verifyZeroInteractions(mockAnalytics); }
@Test public void testInvalidAction() { Intent intent = new Intent("action") .putExtra("referrer", "some value"); receiver.onReceive(context, intent); verifyZeroInteractions(mockAnalytics); }