AdIdRequestTask extends AsyncTask<Void, Void, AdIdResult> { static AdIdResult getAdId(Context applicationContext) { AdvertisingIdClient.Info adInfo = null; String errorMessage = null; try { adInfo = AdvertisingIdClient.getAdvertisingIdInfo(applicationContext); } catch (IOException e) { errorMessage = e.getMessage(); } catch (GooglePlayServicesNotAvailableException e) { errorMessage = e.getMessage(); } catch (GooglePlayServicesRepairableException e) { e.printStackTrace(); errorMessage = e.getMessage(); } return new AdIdResult(adInfo, errorMessage); } AdIdRequestTask(Context applicationContext, AdIdCallback callback);   }
@Test public void shouldReturnAnAdId() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); AdIdRequestTask.AdIdCallback adIdCallback = result -> { assertNotNull(result); assertNotNull(result.getAdId()); latch.countDown(); }; Robolectric.getBackgroundThreadScheduler().unPause(); AdIdRequestTask adIdRequestTask = new AdIdRequestTask(ApplicationProvider.getApplicationContext(), adIdCallback); adIdRequestTask.execute(); latch.await(); }
@Test public void shouldReturnNonTrackingToken() { AdIdResult idResult = new AdIdResult(null, null); assertEquals("nontracking", idResult.getAdId()); }