public void testRun() throws Exception {
    // Expect service data calls
    TripStatistics stats = new TripStatistics();

    // Expect announcement building call
    expect(mockTask.getAnnouncement(same(stats))).andStubReturn(ANNOUNCEMENT);

    // Put task in "ready" state
    startTask(TextToSpeech.SUCCESS);

    expect(tts.isLanguageAvailable(DEFAULT_LOCALE)).andStubReturn(TextToSpeech.LANG_AVAILABLE);
    expect(tts.setLanguage(DEFAULT_LOCALE)).andReturn(TextToSpeech.LANG_AVAILABLE);
    expect(tts.setSpeechRate(AnnouncementPeriodicTask.TTS_SPEECH_RATE))
        .andReturn(TextToSpeech.SUCCESS);
    expect(tts.setOnUtteranceCompletedListener((OnUtteranceCompletedListener) EasyMock.anyObject()))
        .andReturn(0);

    // Expect actual announcement call
    expect(
            tts.speak(
                eq(ANNOUNCEMENT),
                eq(TextToSpeech.QUEUE_FLUSH),
                eq(AnnouncementPeriodicTask.SPEECH_PARAMS)))
        .andReturn(0);

    // Run the announcement
    AndroidMock.replay(tts);
    task.announce(stats);
    AndroidMock.verify(mockTask, tts);
  }
  public void testRun_ringWhileSpeaking() throws Exception {
    startTask(TextToSpeech.SUCCESS);

    expect(tts.isSpeaking()).andStubReturn(true);
    expect(tts.stop()).andReturn(TextToSpeech.SUCCESS);

    AndroidMock.replay(tts);

    // Update the state to ringing - this should stop the current announcement.
    PhoneStateListener phoneListener = phoneListenerCapture.getValue();
    phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null);

    // Run the announcement - this should do nothing.
    task.run(null);

    AndroidMock.verify(mockTask, tts);
  }
  public void testRun_whileRinging() throws Exception {
    startTask(TextToSpeech.SUCCESS);

    expect(tts.isSpeaking()).andStubReturn(false);

    // Run the announcement
    AndroidMock.replay(tts);
    PhoneStateListener phoneListener = phoneListenerCapture.getValue();
    phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null);
    task.run(null);
    AndroidMock.verify(mockTask, tts);
  }
  public void testShutdown() {
    // First, start
    doStart();
    AndroidMock.verify(mockTask);
    AndroidMock.reset(mockTask);

    // Then, shut down
    PhoneStateListener phoneListener = phoneListenerCapture.getValue();
    mockTask.listenToPhoneState(same(phoneListener), eq(PhoneStateListener.LISTEN_NONE));
    tts.shutdown();
    AndroidMock.replay(mockTask, tts);
    task.shutdown();
    AndroidMock.verify(mockTask, tts);
  }