Android Open Source - sleep-timer Pause Music Service Test






From Project

Back to project page sleep-timer.

License

The source code is released under:

MIT License

If you think the Android project sleep-timer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
Copyright (c) 2013 Joel Andrews// w ww  .  j  av a 2  s.  c  o m
Distributed under the MIT License: http://opensource.org/licenses/MIT
 */

package com.oldsneerjaw.sleeptimer;

import android.media.AudioManager;

import com.oldsneerjaw.sleeptimer.test.AndroidMockingTestCase;

import org.mockito.Mockito;

/**
 * Test cases for {@link PauseMusicService}.
 */
public class PauseMusicServiceTest extends AndroidMockingTestCase {

    private AudioManager mockAudioManager;
    private AudioManager.OnAudioFocusChangeListener mockListener;
    private PauseMusicNotifier mockNotifier;
    private PauseMusicService service;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mockAudioManager = Mockito.mock(AudioManager.class);
        mockListener = Mockito.mock(AudioManager.OnAudioFocusChangeListener.class);
        mockNotifier = Mockito.mock(PauseMusicNotifier.class);

        service = new PauseMusicService();
        service.onCreate(mockAudioManager, mockListener, mockNotifier);
    }

    public void testPauseAndNotify_Success() {
        Mockito.when(mockAudioManager.requestAudioFocus(mockListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN))
                .thenReturn(AudioManager.AUDIOFOCUS_REQUEST_GRANTED);

        assertTrue(service.pauseAndNotify());

        Mockito.verify(mockNotifier).postNotification();
    }

    public void testPauseAndNotify_Failed() {
        Mockito.when(mockAudioManager.requestAudioFocus(mockListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN))
                .thenReturn(AudioManager.AUDIOFOCUS_REQUEST_FAILED);

        assertFalse(service.pauseAndNotify());

        Mockito.verifyNoMoreInteractions(mockNotifier);
    }

    public void testOnDestroy() {
        service.onDestroy();

        Mockito.verify(mockAudioManager).abandonAudioFocus(mockListener);
    }

    public void testListenerOnAudioFocusChange_FocusLost() {
        PauseMusicService mockService = Mockito.mock(PauseMusicService.class);
        PauseMusicService.AudioFocusListener listener = mockService.new AudioFocusListener(mockAudioManager);

        listener.onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS);

        Mockito.verify(mockAudioManager).abandonAudioFocus(listener);
    }
}




Java Source Code List

com.oldsneerjaw.sleeptimer.CountdownActivity.java
com.oldsneerjaw.sleeptimer.CountdownNotifierTest.java
com.oldsneerjaw.sleeptimer.CountdownNotifier.java
com.oldsneerjaw.sleeptimer.MainActivityTest.java
com.oldsneerjaw.sleeptimer.MainActivity.java
com.oldsneerjaw.sleeptimer.PauseMusicNotifierTest.java
com.oldsneerjaw.sleeptimer.PauseMusicNotifier.java
com.oldsneerjaw.sleeptimer.PauseMusicReceiverTest.java
com.oldsneerjaw.sleeptimer.PauseMusicReceiver.java
com.oldsneerjaw.sleeptimer.PauseMusicServiceTest.java
com.oldsneerjaw.sleeptimer.PauseMusicService.java
com.oldsneerjaw.sleeptimer.SetTimerActivity.java
com.oldsneerjaw.sleeptimer.TimerManagerTest.java
com.oldsneerjaw.sleeptimer.TimerManager.java