Android Open Source - sleep-timer Timer Manager 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/*from   w  ww. j  a va  2  s.  c  o  m*/
Distributed under the MIT License: http://opensource.org/licenses/MIT
 */

package com.oldsneerjaw.sleeptimer;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.SharedPreferences;

import com.oldsneerjaw.sleeptimer.test.AndroidMockingTestCase;

import org.hamcrest.core.Is;
import org.mockito.Mockito;

import java.util.Date;

/**
 * Test cases for {@link TimerManager}.
 *
 * @author Joel Andrews
 */
public class TimerManagerTest extends AndroidMockingTestCase {

    private static final String SCHEDULED_TIME_KEY = "com.oldsneerjaw.sleeptimer.TimerManager.scheduledTime";

    private SharedPreferences mockPreferences;
    private SharedPreferences.Editor mockPreferencesEditor;
    private AlarmManager mockAlarmManager;
    private TimerManager timerManager;

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

        // Suppress the irrelevant warning generated by Android Studio on this statement
        @SuppressWarnings("ConstantConditions")
        String packageName = getContext().getPackageName();

        mockAlarmManager = Mockito.mock(AlarmManager.class);

        mockPreferencesEditor = Mockito.mock(SharedPreferences.Editor.class);
        Mockito.when(mockPreferencesEditor.putLong(Mockito.anyString(), Mockito.anyLong())).thenReturn(mockPreferencesEditor);
        Mockito.when(mockPreferencesEditor.remove(Mockito.anyString())).thenReturn(mockPreferencesEditor);

        mockPreferences = Mockito.mock(SharedPreferences.class);
        Mockito.when(mockPreferences.edit()).thenReturn(mockPreferencesEditor);

        Context mockContext = Mockito.mock(Context.class);
        Mockito.when(mockContext.getPackageName()).thenReturn(packageName);
        Mockito.when(mockContext.getApplicationContext()).thenReturn(mockContext);

        timerManager = new TimerManager(mockContext, mockPreferences, mockAlarmManager);
    }

    public void testGetInstance_EqualContextsReturnSameInstance() {
        String packageName = "foo";

        Context context1 = Mockito.mock(Context.class);
        Mockito.when(context1.getPackageName()).thenReturn(packageName);

        Context context2 = Mockito.mock(Context.class);
        Mockito.when(context2.getPackageName()).thenReturn(packageName);

        TimerManager instance1 = TimerManager.get(context1);
        TimerManager instance2 = TimerManager.get(context2);

        assertSame(instance1, instance2);
    }

    public void testGetInstance_DifferentContextsReturnDifferentInstances() {
        Context context1 = Mockito.mock(Context.class);
        Mockito.when(context1.getPackageName()).thenReturn("foo");

        Context context2 = Mockito.mock(Context.class);
        Mockito.when(context2.getPackageName()).thenReturn("bar");

        TimerManager instance1 = TimerManager.get(context1);
        TimerManager instance2 = TimerManager.get(context2);

        assertNotSame(instance1, instance2);
    }

    public void testSetTimer() {
        final int hours = 1;
        final int minutes = 10;
        timerManager.setTimer(hours, minutes);

        Mockito.verify(mockAlarmManager).set(
                Mockito.intThat(Is.is(AlarmManager.RTC)),
                Mockito.anyLong(),
                Mockito.any(PendingIntent.class));

        Mockito.verify(mockPreferencesEditor).putLong(Mockito.argThat(Is.is(SCHEDULED_TIME_KEY)), Mockito.anyLong());
        Mockito.verify(mockPreferencesEditor).commit();

        Mockito.verifyNoMoreInteractions(mockAlarmManager, mockPreferencesEditor);
    }

    public void testCancelTimer() {
        timerManager.cancelTimer();

        Mockito.verify(mockAlarmManager).cancel(Mockito.any(PendingIntent.class));

        Mockito.verify(mockPreferencesEditor).remove(SCHEDULED_TIME_KEY);
        Mockito.verify(mockPreferencesEditor).commit();

        Mockito.verifyNoMoreInteractions(mockAlarmManager, mockPreferencesEditor);
    }

    public void testGetScheduledTime_PreferenceSet() {
        final long expectedTimeMs = 47382472;

        Mockito.when(mockPreferences.contains(SCHEDULED_TIME_KEY)).thenReturn(true);
        Mockito.when(mockPreferences.getLong(Mockito.argThat(Is.is(SCHEDULED_TIME_KEY)), Mockito.anyLong()))
                .thenReturn(expectedTimeMs);

        Date result = timerManager.getScheduledTime();

        assertEquals(new Date(expectedTimeMs), result);
    }

    public void testGetScheduledTime_PreferenceNotSet() {
        Mockito.when(mockPreferences.contains(SCHEDULED_TIME_KEY)).thenReturn(false);

        assertNull(timerManager.getScheduledTime());
    }
}




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