@UsesMocks({
    AnnouncementPeriodicTask.class,
    StringUtils.class,
  })
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    oldDefaultLocale = Locale.getDefault();
    Locale.setDefault(DEFAULT_LOCALE);

    // Eww, the effort required just to mock TextToSpeech is insane
    final AtomicBoolean listenerCalled = new AtomicBoolean();
    OnInitListener blockingListener =
        new OnInitListener() {
          @Override
          public void onInit(int status) {
            synchronized (this) {
              listenerCalled.set(true);
              notify();
            }
          }
        };

    ttsDelegate = new TextToSpeechDelegate(getContext(), blockingListener);

    // Wait for all async operations done in the constructor to finish.
    synchronized (blockingListener) {
      while (!listenerCalled.get()) {
        // Releases the synchronized lock until we're woken up.
        blockingListener.wait();
      }
    }

    // Phew, done, now we can start forwarding calls
    tts = AndroidMock.createMock(TextToSpeechInterface.class);

    initListenerCapture = new Capture<OnInitListener>();
    phoneListenerCapture = new Capture<PhoneStateListener>();

    // Create a partial forwarding mock
    mockTask = AndroidMock.createMock(AnnouncementPeriodicTask.class, getContext());
    task =
        new AnnouncementPeriodicTask(getContext()) {
          @Override
          protected TextToSpeech newTextToSpeech(Context ctx, OnInitListener onInitListener) {
            return mockTask.newTextToSpeech(ctx, onInitListener);
          }

          @Override
          protected String getAnnouncement(TripStatistics stats) {
            return mockTask.getAnnouncement(stats);
          }

          @Override
          protected void listenToPhoneState(PhoneStateListener listener, int events) {
            mockTask.listenToPhoneState(listener, events);
          }
        };
  }
 private void startTask(int state) {
   AndroidMock.resetToNice(tts);
   AndroidMock.replay(tts);
   doStart();
   OnInitListener ttsInitListener = initListenerCapture.getValue();
   ttsInitListener.onInit(state);
   AndroidMock.resetToDefault(tts);
 }
  public void testStart_notReady() {
    doStart();
    OnInitListener ttsInitListener = initListenerCapture.getValue();
    assertNotNull(ttsInitListener);

    AndroidMock.replay(tts);

    ttsInitListener.onInit(TextToSpeech.ERROR);

    AndroidMock.verify(mockTask, tts);
  }