DownloadTracker { public void trackNewAppDownload(final TrackMe baseTrackme, @NonNull final Extra extra) { final boolean delay = mInternalTracking && INSTALL_SOURCE_GOOGLE_PLAY.equals(mPackMan.getInstallerPackageName(mPkgInfo.packageName)); if (delay) { Timber.tag(TAG).d("Google Play is install source, deferring tracking."); } final Thread trackTask = new Thread(() -> { if (delay) try {Thread.sleep(3000);} catch (Exception e) { Timber.tag(ContentValues.TAG).e(e);} trackNewAppDownloadInternal(baseTrackme, extra); }); if (!delay && !extra.isIntensiveWork()) trackTask.run(); else trackTask.start(); } DownloadTracker(Tracker tracker);  DownloadTracker(Tracker tracker, @NonNull PackageInfo packageInfo); void setVersion(@Nullable String version); String getVersion(); void trackOnce(TrackMe baseTrackme, @NonNull Extra extra); void trackNewAppDownload(final TrackMe baseTrackme, @NonNull final Extra extra);  }
@Test public void testTrackIdentifier() throws Exception { ApplicationInfo applicationInfo = new ApplicationInfo(); mPackageInfo.applicationInfo = applicationInfo; applicationInfo.sourceDir = UUID.randomUUID().toString(); final byte[] FAKE_APK_DATA = "this is an apk, awesome right?".getBytes(); final String FAKE_APK_DATA_MD5 = "771BD8971508985852AF8F96170C52FB"; try { FileOutputStream out = new FileOutputStream(applicationInfo.sourceDir); out.write(FAKE_APK_DATA); out.close(); } catch (java.io.IOException e) { e.printStackTrace(); } DownloadTracker downloadTracker = new DownloadTracker(mTracker); downloadTracker.trackNewAppDownload(new TrackMe(), new DownloadTracker.Extra.ApkChecksum(mContext)); TestHelper.sleep(100); verify(mTracker).track(mCaptor.capture()); checkNewAppDownload(mCaptor.getValue()); Matcher m = REGEX_DOWNLOADTRACK.matcher(mCaptor.getValue().get(QueryParams.DOWNLOAD)); assertTrue(m.matches()); assertEquals("package", m.group(1)); assertEquals(123, Integer.parseInt(m.group(2))); assertEquals(FAKE_APK_DATA_MD5, m.group(3)); assertEquals("http: downloadTracker.trackNewAppDownload(new TrackMe(), new DownloadTracker.Extra.None()); verify(mTracker, times(2)).track(mCaptor.capture()); checkNewAppDownload(mCaptor.getValue()); String downloadParams = mCaptor.getValue().get(QueryParams.DOWNLOAD); m = REGEX_DOWNLOADTRACK.matcher(downloadParams); assertTrue(downloadParams, m.matches()); assertEquals(3, m.groupCount()); assertEquals("package", m.group(1)); assertEquals(123, Integer.parseInt(m.group(2))); assertEquals(null, m.group(3)); assertEquals("http: new File(applicationInfo.sourceDir).delete(); }
@Test public void testTrackReferrer() throws Exception { DownloadTracker downloadTracker = new DownloadTracker(mTracker); downloadTracker.trackNewAppDownload(new TrackMe(), new DownloadTracker.Extra.None()); verify(mTracker).track(mCaptor.capture()); checkNewAppDownload(mCaptor.getValue()); String downloadParams = mCaptor.getValue().get(QueryParams.DOWNLOAD); Matcher m = REGEX_DOWNLOADTRACK.matcher(downloadParams); assertTrue(downloadParams, m.matches()); assertEquals(3, m.groupCount()); assertEquals("package", m.group(1)); assertEquals(123, Integer.parseInt(m.group(2))); assertEquals(null, m.group(3)); assertEquals("http: when(mPackageManager.getInstallerPackageName(anyString())).thenReturn(null); downloadTracker.trackNewAppDownload(new TrackMe(), new DownloadTracker.Extra.None()); verify(mTracker, times(2)).track(mCaptor.capture()); checkNewAppDownload(mCaptor.getValue()); m = REGEX_DOWNLOADTRACK.matcher(mCaptor.getValue().get(QueryParams.DOWNLOAD)); assertTrue(m.matches()); assertEquals(3, m.groupCount()); assertEquals("package", m.group(1)); assertEquals(123, Integer.parseInt(m.group(2))); assertEquals(null, m.group(3)); assertEquals(null, mCaptor.getValue().get(QueryParams.REFERRER)); }