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);
  }
 private void doStart() {
   mockTask.listenToPhoneState(
       capture(phoneListenerCapture), eq(PhoneStateListener.LISTEN_CALL_STATE));
   expect(mockTask.newTextToSpeech(same(getContext()), capture(initListenerCapture)))
       .andStubReturn(ttsDelegate);
   AndroidMock.replay(mockTask);
   task.start();
 }
  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);
  }
  public void testRun_noService() throws Exception {
    startTask(TextToSpeech.SUCCESS);

    // Run the announcement
    AndroidMock.replay(tts);
    task.run(null);
    AndroidMock.verify(mockTask, tts);
  }
  public void testRun_notReady() throws Exception {
    // Put task in "not ready" state
    startTask(TextToSpeech.ERROR);

    // Run the announcement
    AndroidMock.replay(tts);
    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 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);
  }
 /**
  * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with plural numbers without the
  * hour unit.
  */
 public void testGetAnnounceTime_plural_no_hour() {
   long time = (2 * 60 * 1000) + (2 * 1000); // 2 minutes 2 seconds
   assertEquals("2 minutes 2 seconds", task.getAnnounceTime(time));
 }
 /**
  * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with singular numbers without the
  * hour unit.
  */
 public void testGetAnnounceTime_singular_no_hour() {
   long time = (1 * 60 * 1000) + (1 * 1000); // 1 minute 1 second
   assertEquals("1 minute 1 second", task.getAnnounceTime(time));
 }
 /** Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with time one. */
 public void testGetAnnounceTime_time_one() {
   long time = 1 * 1000; // 1 second
   assertEquals("0 minutes 1 second", task.getAnnounceTime(time));
 }
 /** Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with time zero. */
 public void testGetAnnounceTime_time_zero() {
   long time = 0; // 0 seconds
   assertEquals("0 minutes 0 seconds", task.getAnnounceTime(time));
 }