Android Open Source - sleep-timer Pause Music Notifier 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/*www  .  ja va 2 s.  com*/
Distributed under the MIT License: http://opensource.org/licenses/MIT
 */

package com.oldsneerjaw.sleeptimer;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.res.Resources;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import com.oldsneerjaw.sleeptimer.test.AndroidMockingTestCase;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.mockito.Mockito;

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

    private static final int NOTIFICATION_ID = 1;

    private static final String NOTIFICATION_TITLE = "foo";
    private static final String NOTIFICATION_TEXT = "bar";

    private NotificationManager mockNotificationManager;
    private PauseMusicNotifier notifier;

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

        Context mockContext = Mockito.mock(Context.class);
        Mockito.when(mockContext.getPackageName()).thenReturn("com.oldsneerjaw.sleeptimer");
        Mockito.when(mockContext.getApplicationContext()).thenReturn(mockContext);

        mockNotificationManager = Mockito.mock(NotificationManager.class);

        Resources mockResources = Mockito.mock(Resources.class);
        Mockito.when(mockResources.getString(R.string.paused_music_notification_title)).thenReturn(NOTIFICATION_TITLE);
        Mockito.when(mockResources.getString(R.string.paused_music_notification_text)).thenReturn(NOTIFICATION_TEXT);

        notifier = new PauseMusicNotifier(mockContext, mockNotificationManager, mockResources);
    }

    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);

        PauseMusicNotifier instance1 = PauseMusicNotifier.get(context1);
        PauseMusicNotifier instance2 = PauseMusicNotifier.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");

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

        assertNotSame(instance1, instance2);
    }

    public void testPostNotification() {
        notifier.postNotification();

        Mockito.verify(mockNotificationManager).notify(
                Mockito.eq(NOTIFICATION_ID),
                Mockito.argThat(new BaseMatcher<Notification>() {
                    @Override
                    public boolean matches(Object o) {
                        if (!(o instanceof Notification)) {
                            return false;
                        }

                        Notification candidate = (Notification) o;

                        return TextUtils.equals(NOTIFICATION_TITLE, candidate.tickerText)
                                && (candidate.icon == R.drawable.ic_launcher)
                                && (candidate.priority == NotificationCompat.PRIORITY_DEFAULT)
                                && (candidate.contentIntent != null)
                                && ((candidate.flags & Notification.FLAG_AUTO_CANCEL) != 0);
                    }

                    @Override
                    public void describeTo(Description description) {
                        // Describe the notification that was expected in the event of test failure
                        description.appendText("a notification with the correct title, icon, default priority, a pending intent and set to auto cancel");
                    }
                })
        );
    }

    public void testCancelNotification() {
        notifier.cancelNotification();

        Mockito.verify(mockNotificationManager).cancel(NOTIFICATION_ID);
    }
}




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