### Question:
NtpTimeHelper { synchronized void retrieveAndInjectNtpTime() { if (mInjectNtpTimeState == STATE_RETRIEVING_AND_INJECTING) { return; } if (!isNetworkConnected()) { mInjectNtpTimeState = STATE_PENDING_NETWORK; return; } mInjectNtpTimeState = STATE_RETRIEVING_AND_INJECTING; mWakeLock.acquire(WAKELOCK_TIMEOUT_MILLIS); new Thread(this::blockingGetNtpTimeAndInject).start(); } @VisibleForTesting  NtpTimeHelper(Context context, Looper looper, InjectNtpTimeCallback callback,
            NtpTrustedTime ntpTime);   NtpTimeHelper(Context context, Looper looper, InjectNtpTimeCallback callback);   }### Answer:
@Test public void handleInjectNtpTime_cachedAgeLow_injectTime() throws InterruptedException { doReturn(NtpTimeHelper.NTP_INTERVAL - 1).when(mMockNtpTrustedTime).getCacheAge(); doReturn(MOCK_NTP_TIME).when(mMockNtpTrustedTime).getCachedNtpTime(); mNtpTimeHelper.retrieveAndInjectNtpTime(); waitForTasksToBePostedOnHandlerAndRunThem(); assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isTrue(); }
@Test public void handleInjectNtpTime_injectTimeFailed_injectTimeDelayed() throws InterruptedException { doReturn(NtpTimeHelper.NTP_INTERVAL + 1).when(mMockNtpTrustedTime).getCacheAge(); doReturn(false).when(mMockNtpTrustedTime).forceRefresh(); mNtpTimeHelper.retrieveAndInjectNtpTime(); waitForTasksToBePostedOnHandlerAndRunThem(); assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isFalse(); doReturn(true).when(mMockNtpTrustedTime).forceRefresh(); doReturn(1L).when(mMockNtpTrustedTime).getCacheAge(); doReturn(MOCK_NTP_TIME).when(mMockNtpTrustedTime).getCachedNtpTime(); ShadowSystemClock.sleep(NtpTimeHelper.RETRY_INTERVAL); waitForTasksToBePostedOnHandlerAndRunThem(); assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isTrue(); }
@Test public void handleInjectNtpTime_injectTimeFailed_injectTimeDelayed() throws InterruptedException { doReturn(NtpTimeHelper.NTP_INTERVAL + 1).when(mMockNtpTrustedTime).getCacheAge(); doReturn(false).when(mMockNtpTrustedTime).forceRefresh(); mNtpTimeHelper.retrieveAndInjectNtpTime(); waitForTasksToBePostedOnHandlerAndRunThem(); assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isFalse(); doReturn(true).when(mMockNtpTrustedTime).forceRefresh(); doReturn(1L).when(mMockNtpTrustedTime).getCacheAge(); doReturn(MOCK_NTP_TIME).when(mMockNtpTrustedTime).getCachedNtpTime(); SystemClock.sleep(NtpTimeHelper.RETRY_INTERVAL); waitForTasksToBePostedOnHandlerAndRunThem(); assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isTrue(); }