InstallReferrerReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Timber.tag(TAG).d(intent.toString()); if (intent.getAction() == null || !RESPONSIBILITIES.contains(intent.getAction())) { Timber.tag(TAG).w("Got called outside our responsibilities: %s", intent.getAction()); return; } if (intent.getBooleanExtra("forwarded", false)) { Timber.tag(TAG).d("Dropping forwarded intent"); return; } if (intent.getAction().equals(REFERRER_SOURCE_GPLAY)) { String referrer = intent.getStringExtra(ARG_KEY_GPLAY_REFERRER); if (referrer != null) { final PendingResult result = goAsync(); new Thread(() -> { Matomo.getInstance(context.getApplicationContext()).getPreferences().edit().putString(PREF_KEY_INSTALL_REFERRER_EXTRAS, referrer).apply(); Timber.tag(TAG).d("Stored Google Play referrer extras: %s", referrer); result.finish(); }).start(); } } intent.setComponent(null); intent.setPackage(context.getPackageName()); intent.putExtra("forwarded", true); context.sendBroadcast(intent); }  @Override void onReceive(Context context, Intent intent);  }
@Test public void testReceiveGooglePlay() throws Exception { InstallReferrerReceiver receiver = new InstallReferrerReceiver(); Intent testIntent = new Intent("com.android.vending.INSTALL_REFERRER"); testIntent.setPackage(Robolectric.application.getPackageName()); String testReferrerData1 = "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name"; testIntent.putExtra(InstallReferrerReceiver.ARG_KEY_GPLAY_REFERRER, testReferrerData1); receiver.onReceive(Robolectric.application.getApplicationContext(), testIntent); Thread.sleep(250); String referrerDataFromPreferences = getMatomo().getPreferences().getString(InstallReferrerReceiver.PREF_KEY_INSTALL_REFERRER_EXTRAS, null); assertEquals(testReferrerData1, referrerDataFromPreferences); assertTrue(testIntent.getBooleanExtra("forwarded", false)); String testReferrerData2 = "pk_campaign=Email-Nov2011&pk_kwd=OrderNow"; testIntent.putExtra(InstallReferrerReceiver.ARG_KEY_GPLAY_REFERRER, testReferrerData2); receiver.onReceive(Robolectric.application.getApplicationContext(), testIntent); Thread.sleep(250); referrerDataFromPreferences = getMatomo().getPreferences().getString(InstallReferrerReceiver.PREF_KEY_INSTALL_REFERRER_EXTRAS, null); assertEquals(testReferrerData1, referrerDataFromPreferences); testIntent.putExtra("forwarded", false); receiver.onReceive(Robolectric.application.getApplicationContext(), testIntent); Thread.sleep(250); referrerDataFromPreferences = getMatomo().getPreferences().getString(InstallReferrerReceiver.PREF_KEY_INSTALL_REFERRER_EXTRAS, null); assertEquals(testReferrerData2, referrerDataFromPreferences); }
@Test public void testGracefulFailure() throws Exception { InstallReferrerReceiver receiver = new InstallReferrerReceiver(); Intent badIntent = new Intent("bad.action"); badIntent.setPackage(Robolectric.application.getPackageName()); String testReferrerData1 = "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name"; badIntent.putExtra(InstallReferrerReceiver.ARG_KEY_GPLAY_REFERRER, testReferrerData1); receiver.onReceive(Robolectric.application.getApplicationContext(), badIntent); Thread.sleep(250); String referrerDataFromPreferences = getMatomo().getPreferences().getString(InstallReferrerReceiver.PREF_KEY_INSTALL_REFERRER_EXTRAS, null); assertNull(referrerDataFromPreferences); Intent nullIntent = new Intent(); nullIntent.setPackage(Robolectric.application.getPackageName()); testReferrerData1 = "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name"; nullIntent.putExtra(InstallReferrerReceiver.ARG_KEY_GPLAY_REFERRER, testReferrerData1); receiver.onReceive(Robolectric.application.getApplicationContext(), nullIntent); Thread.sleep(250); referrerDataFromPreferences = getMatomo().getPreferences().getString(InstallReferrerReceiver.PREF_KEY_INSTALL_REFERRER_EXTRAS, null); assertNull(referrerDataFromPreferences); }