Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0ffe755d authored by Geoffrey Pitsch's avatar Geoffrey Pitsch
Browse files

Convert notification tests to use JUnit4

Change-Id: I3d59b67819981a110bc77932fd2d9b6625a19ada
parent c148ba90
Loading
Loading
Loading
Loading
+39 −28
Original line number Diff line number Diff line
@@ -15,10 +15,14 @@
 */
package com.android.server.notification;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.Notification.Builder;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.net.Uri;
@@ -28,7 +32,8 @@ import android.os.UserHandle;
import android.os.Vibrator;
import android.service.notification.NotificationListenerService.Ranking;
import android.service.notification.StatusBarNotification;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;

import org.mockito.Mock;
@@ -45,7 +50,9 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class BuzzBeepBlinkTest extends AndroidTestCase {
@SmallTest
@RunWith(AndroidJUnit4.class)
public class BuzzBeepBlinkTest {

    @Mock AudioManager mAudioManager;
    @Mock Vibrator mVibrator;
@@ -62,7 +69,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
    private int mScore = 10;
    private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());

    @Override
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

@@ -209,7 +216,11 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verify(mVibrator, never()).cancel();
    }

    @SmallTest
    private Context getContext() {
        return InstrumentationRegistry.getTargetContext();
    }

    @Test
    public void testBeep() throws Exception {
        NotificationRecord r = getBeepyNotification();

@@ -223,7 +234,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
    // Tests
    //

    @SmallTest
    @Test
    public void testBeepInsistently() throws Exception {
        NotificationRecord r = getInsistentBeepyNotification();

@@ -232,7 +243,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyBeep();
    }

    @SmallTest
    @Test
    public void testNoInterruptionForMin() throws Exception {
        NotificationRecord r = getBeepyNotification();
        r.setImportance(Ranking.IMPORTANCE_MIN, "foo");
@@ -243,7 +254,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverVibrate();
    }

    @SmallTest
    @Test
    public void testNoInterruptionForIntercepted() throws Exception {
        NotificationRecord r = getBeepyNotification();
        r.setIntercepted(true);
@@ -254,7 +265,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverVibrate();
    }

    @SmallTest
    @Test
    public void testBeepTwice() throws Exception {
        NotificationRecord r = getBeepyNotification();

@@ -268,7 +279,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyBeepLooped();
    }

    @SmallTest
    @Test
    public void testHonorAlertOnlyOnceForBeep() throws Exception {
        NotificationRecord r = getBeepyNotification();
        NotificationRecord s = getBeepyOnceNotification();
@@ -283,7 +294,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverBeep();
    }

    @SmallTest
    @Test
    public void testNoisyUpdateDoesNotCancelAudio() throws Exception {
        NotificationRecord r = getBeepyNotification();

@@ -294,7 +305,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverStopAudio();
    }

    @SmallTest
    @Test
    public void testNoisyOnceUpdateDoesNotCancelAudio() throws Exception {
        NotificationRecord r = getBeepyNotification();
        NotificationRecord s = getBeepyOnceNotification();
@@ -306,7 +317,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverStopAudio();
    }

    @SmallTest
    @Test
    public void testQuietUpdateDoesNotCancelAudioFromOther() throws Exception {
        NotificationRecord r = getBeepyNotification();
        NotificationRecord s = getQuietNotification();
@@ -323,7 +334,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverStopAudio();
    }

    @SmallTest
    @Test
    public void testQuietInterloperDoesNotCancelAudio() throws Exception {
        NotificationRecord r = getBeepyNotification();
        NotificationRecord other = getQuietOtherNotification();
@@ -337,7 +348,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverStopAudio();
    }

    @SmallTest
    @Test
    public void testQuietUpdateCancelsAudio() throws Exception {
        NotificationRecord r = getBeepyNotification();
        NotificationRecord s = getQuietNotification();
@@ -352,7 +363,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyStopAudio();
    }

    @SmallTest
    @Test
    public void testQuietOnceUpdateCancelsAudio() throws Exception {
        NotificationRecord r = getBeepyNotification();
        NotificationRecord s = getQuietOnceNotification();
@@ -367,7 +378,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyStopAudio();
    }

    @SmallTest
    @Test
    public void testDemoteSoundToVibrate() throws Exception {
        NotificationRecord r = getBeepyNotification();

@@ -381,7 +392,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyVibrate();
    }

    @SmallTest
    @Test
    public void testDemotInsistenteSoundToVibrate() throws Exception {
        NotificationRecord r = getInsistentBeepyNotification();

@@ -394,7 +405,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyVibrateLooped();
    }

    @SmallTest
    @Test
    public void testVibrate() throws Exception {
        NotificationRecord r = getBuzzyNotification();

@@ -404,7 +415,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyVibrate();
    }

    @SmallTest
    @Test
    public void testInsistenteVibrate() throws Exception {
        NotificationRecord r = getInsistentBuzzyNotification();

@@ -412,7 +423,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyVibrateLooped();
    }

    @SmallTest
    @Test
    public void testVibratTwice() throws Exception {
        NotificationRecord r = getBuzzyNotification();

@@ -426,7 +437,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyVibrate();
    }

    @SmallTest
    @Test
    public void testHonorAlertOnlyOnceForBuzz() throws Exception {
        NotificationRecord r = getBuzzyNotification();
        NotificationRecord s = getBuzzyOnceNotification();
@@ -441,7 +452,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverVibrate();
    }

    @SmallTest
    @Test
    public void testNoisyUpdateDoesNotCancelVibrate() throws Exception {
        NotificationRecord r = getBuzzyNotification();

@@ -452,7 +463,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverStopVibrate();
    }

    @SmallTest
    @Test
    public void testNoisyOnceUpdateDoesNotCancelVibrate() throws Exception {
        NotificationRecord r = getBuzzyNotification();
        NotificationRecord s = getBuzzyOnceNotification();
@@ -464,7 +475,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverStopVibrate();
    }

    @SmallTest
    @Test
    public void testQuietUpdateDoesNotCancelVibrateFromOther() throws Exception {
        NotificationRecord r = getBuzzyNotification();
        NotificationRecord s = getQuietNotification();
@@ -481,7 +492,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverStopVibrate();
    }

    @SmallTest
    @Test
    public void testQuietInterloperDoesNotCancelVibrate() throws Exception {
        NotificationRecord r = getBuzzyNotification();
        NotificationRecord other = getQuietOtherNotification();
@@ -495,7 +506,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyNeverStopVibrate();
    }

    @SmallTest
    @Test
    public void testQuietUpdateCancelsVibrate() throws Exception {
        NotificationRecord r = getBuzzyNotification();
        NotificationRecord s = getQuietNotification();
@@ -509,7 +520,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyStopVibrate();
    }

    @SmallTest
    @Test
    public void testQuietOnceUpdateCancelsvibrate() throws Exception {
        NotificationRecord r = getBuzzyNotification();
        NotificationRecord s = getQuietOnceNotification();
@@ -524,7 +535,7 @@ public class BuzzBeepBlinkTest extends AndroidTestCase {
        verifyStopVibrate();
    }

    @SmallTest
    @Test
    public void testQuietUpdateCancelsDemotedVibrate() throws Exception {
        NotificationRecord r = getBeepyNotification();
        NotificationRecord s = getQuietNotification();
+22 −9
Original line number Diff line number Diff line
@@ -15,18 +15,27 @@
 */
package com.android.server.notification;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import android.app.Notification;
import android.content.Context;
import android.os.UserHandle;
import android.service.notification.StatusBarNotification;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.ArrayList;

public class RankingHelperTest extends AndroidTestCase {
import static org.junit.Assert.assertTrue;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class RankingHelperTest {
    @Mock NotificationUsageStats mUsageStats;
    @Mock RankingHandler handler;

@@ -42,7 +51,11 @@ public class RankingHelperTest extends AndroidTestCase {
    private NotificationRecord mRecordNoGroupSortA;
    private RankingHelper mHelper;

    @Override
    private Context getContext() {
        return InstrumentationRegistry.getTargetContext();
    }

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        UserHandle user = UserHandle.ALL;
@@ -91,7 +104,7 @@ public class RankingHelperTest extends AndroidTestCase {
                "package", "package", 1, null, 0, 0, 0, mNotiNoGroupSortA, user));
    }

    @SmallTest
    @Test
    public void testFindAfterRankingWithASplitGroup() throws Exception {
        ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(3);
        notificationList.add(mRecordGroupGSortA);
@@ -105,7 +118,7 @@ public class RankingHelperTest extends AndroidTestCase {
        assertTrue(mHelper.indexOf(notificationList, mRecordNoGroupSortA) >= 0);
    }

    @SmallTest
    @Test
    public void testSortShouldNotThrowWithPlainNotifications() throws Exception {
        ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(2);
        notificationList.add(mRecordNoGroup);
@@ -113,7 +126,7 @@ public class RankingHelperTest extends AndroidTestCase {
        mHelper.sort(notificationList);
    }

    @SmallTest
    @Test
    public void testSortShouldNotThrowOneSorted() throws Exception {
        ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(2);
        notificationList.add(mRecordNoGroup);
@@ -121,21 +134,21 @@ public class RankingHelperTest extends AndroidTestCase {
        mHelper.sort(notificationList);
    }

    @SmallTest
    @Test
    public void testSortShouldNotThrowOneNotification() throws Exception {
        ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(1);
        notificationList.add(mRecordNoGroup);
        mHelper.sort(notificationList);
    }

    @SmallTest
    @Test
    public void testSortShouldNotThrowOneSortKey() throws Exception {
        ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(1);
        notificationList.add(mRecordGroupGSortB);
        mHelper.sort(notificationList);
    }

    @SmallTest
    @Test
    public void testSortShouldNotThrowOnEmptyList() throws Exception {
        ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>();
        mHelper.sort(notificationList);
+23 −16
Original line number Diff line number Diff line
@@ -15,27 +15,34 @@
 */
package com.android.server.notification;


import android.test.AndroidTestCase;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

public class RateEstimatorTest extends AndroidTestCase {
@SmallTest
@RunWith(AndroidJUnit4.class)
public class RateEstimatorTest {
    private long mTestStartTime;
    private RateEstimator mEstimator;

    @Override
    @Before
    public void setUp() {
        mTestStartTime = 1225731600000L;
        mEstimator = new RateEstimator();
    }

    @SmallTest
    @Test
    public void testRunningTimeBackwardDoesntExplodeUpdate() throws Exception {
        assertUpdateTime(mTestStartTime);
        assertUpdateTime(mTestStartTime - 1000L);
    }

    @SmallTest
    @Test
    public void testRunningTimeBackwardDoesntExplodeGet() throws Exception {
        assertUpdateTime(mTestStartTime);
        final float rate = mEstimator.getRate(mTestStartTime - 1000L);
@@ -43,13 +50,13 @@ public class RateEstimatorTest extends AndroidTestCase {
        assertFalse(Float.isNaN(rate));
    }

    @SmallTest
    @Test
    public void testInstantaneousEventsDontExplodeUpdate() throws Exception {
        assertUpdateTime(mTestStartTime);
        assertUpdateTime(mTestStartTime);
    }

    @SmallTest
    @Test
    public void testInstantaneousEventsDontExplodeGet() throws Exception {
        assertUpdateTime(mTestStartTime);
        assertUpdateTime(mTestStartTime);
@@ -58,7 +65,7 @@ public class RateEstimatorTest extends AndroidTestCase {
        assertFalse(Float.isNaN(rate));
    }

    @SmallTest
    @Test
    public void testInstantaneousBurstIsEstimatedUnderTwoPercent() throws Exception {
        assertUpdateTime(mTestStartTime);
        long eventStart = mTestStartTime + 1000; // start event a long time after initialization
@@ -67,7 +74,7 @@ public class RateEstimatorTest extends AndroidTestCase {
        assertLessThan("Rate", rate, 20f);
    }

    @SmallTest
    @Test
    public void testCompactBurstIsEstimatedUnderTwoPercent() throws Exception {
        assertUpdateTime(mTestStartTime);
        long eventStart = mTestStartTime + 1000; // start event a long time after initialization
@@ -76,7 +83,7 @@ public class RateEstimatorTest extends AndroidTestCase {
        assertLessThan("Rate", rate, 20f);
    }

    @SmallTest
    @Test
    public void testSustained1000HzBurstIsEstimatedOverNinetyPercent() throws Exception {
        assertUpdateTime(mTestStartTime);
        long eventStart = mTestStartTime + 1000; // start event a long time after initialization
@@ -85,7 +92,7 @@ public class RateEstimatorTest extends AndroidTestCase {
        assertGreaterThan("Rate", rate, 900f);
    }

    @SmallTest
    @Test
    public void testSustained100HzBurstIsEstimatedOverNinetyPercent() throws Exception {
        assertUpdateTime(mTestStartTime);
        long eventStart = mTestStartTime + 1000; // start event a long time after initialization
@@ -95,7 +102,7 @@ public class RateEstimatorTest extends AndroidTestCase {
        assertGreaterThan("Rate", rate, 90f);
    }

    @SmallTest
    @Test
    public void testRecoverQuicklyAfterSustainedBurst() throws Exception {
        assertUpdateTime(mTestStartTime);
        long eventStart = mTestStartTime + 1000; // start event a long time after initialization
@@ -104,7 +111,7 @@ public class RateEstimatorTest extends AndroidTestCase {
        assertLessThan("Rate", rate, 2f);
    }

    @SmallTest
    @Test
    public void testEstimateShouldNotOvershoot() throws Exception {
        assertUpdateTime(mTestStartTime);
        long eventStart = mTestStartTime + 1000; // start event a long time after initialization
@@ -113,13 +120,13 @@ public class RateEstimatorTest extends AndroidTestCase {
        assertLessThan("Rate", rate, 1000f);
    }

    @SmallTest
    @Test
    public void testGetRateWithoutUpdate() throws Exception {
        final float rate = mEstimator.getRate(mTestStartTime);
        assertLessThan("Rate", rate, 0.1f);
    }

    @SmallTest
    @Test
    public void testGetRateWithOneUpdate() throws Exception {
        assertUpdateTime(mTestStartTime);
        final float rate = mEstimator.getRate(mTestStartTime+1);
+21 −13
Original line number Diff line number Diff line
@@ -17,23 +17,31 @@ package com.android.server.notification;

import android.app.Notification;
import android.os.Bundle;
import android.test.AndroidTestCase;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.SpannableString;

import java.util.ArrayList;
import java.util.Arrays;

public class ValidateNotificationPeopleTest extends AndroidTestCase {
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertEquals;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class ValidateNotificationPeopleTest {

    @Test
    public void testNoExtra() throws Exception {
        Bundle bundle = new Bundle();
        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
        assertNull("lack of extra should return null", result);
    }

    @SmallTest
    @Test
    public void testSingleString() throws Exception {
        String[] expected = { "foobar" };
        Bundle bundle = new Bundle();
@@ -42,7 +50,7 @@ public class ValidateNotificationPeopleTest extends AndroidTestCase {
        assertStringArrayEquals("string should be in result[0]", expected, result);
    }

    @SmallTest
    @Test
    public void testSingleCharArray() throws Exception {
        String[] expected = { "foobar" };
        Bundle bundle = new Bundle();
@@ -51,7 +59,7 @@ public class ValidateNotificationPeopleTest extends AndroidTestCase {
        assertStringArrayEquals("char[] should be in result[0]", expected, result);
    }

    @SmallTest
    @Test
    public void testSingleCharSequence() throws Exception {
        String[] expected = { "foobar" };
        Bundle bundle = new Bundle();
@@ -60,7 +68,7 @@ public class ValidateNotificationPeopleTest extends AndroidTestCase {
        assertStringArrayEquals("charSequence should be in result[0]", expected, result);
    }

    @SmallTest
    @Test
    public void testStringArraySingle() throws Exception {
        Bundle bundle = new Bundle();
        String[] expected = { "foobar" };
@@ -69,7 +77,7 @@ public class ValidateNotificationPeopleTest extends AndroidTestCase {
        assertStringArrayEquals("wrapped string should be in result[0]", expected, result);
    }

    @SmallTest
    @Test
    public void testStringArrayMultiple() throws Exception {
        Bundle bundle = new Bundle();
        String[] expected = { "foo", "bar", "baz" };
@@ -78,7 +86,7 @@ public class ValidateNotificationPeopleTest extends AndroidTestCase {
        assertStringArrayEquals("testStringArrayMultiple", expected, result);
    }

    @SmallTest
    @Test
    public void testStringArrayNulls() throws Exception {
        Bundle bundle = new Bundle();
        String[] expected = { "foo", null, "baz" };
@@ -87,7 +95,7 @@ public class ValidateNotificationPeopleTest extends AndroidTestCase {
        assertStringArrayEquals("testStringArrayNulls", expected, result);
    }

    @SmallTest
    @Test
    public void testCharSequenceArrayMultiple() throws Exception {
        Bundle bundle = new Bundle();
        String[] expected = { "foo", "bar", "baz" };
@@ -100,7 +108,7 @@ public class ValidateNotificationPeopleTest extends AndroidTestCase {
        assertStringArrayEquals("testCharSequenceArrayMultiple", expected, result);
    }

    @SmallTest
    @Test
    public void testMixedCharSequenceArrayList() throws Exception {
        Bundle bundle = new Bundle();
        String[] expected = { "foo", "bar", "baz" };
@@ -117,7 +125,7 @@ public class ValidateNotificationPeopleTest extends AndroidTestCase {
        assertStringArrayEquals("testMixedCharSequenceArrayList", expected, result);
    }

    @SmallTest
    @Test
    public void testStringArrayList() throws Exception {
        Bundle bundle = new Bundle();
        String[] expected = { "foo", null, "baz" };
@@ -130,7 +138,7 @@ public class ValidateNotificationPeopleTest extends AndroidTestCase {
        assertStringArrayEquals("testStringArrayList", expected, result);
    }

    @SmallTest
    @Test
    public void testCharSequenceArrayList() throws Exception {
        Bundle bundle = new Bundle();
        String[] expected = { "foo", "bar", "baz" };