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

Commit fc90ffe3 authored by Bennie Ouyang's avatar Bennie Ouyang Committed by Android (Google) Code Review
Browse files

Merge "Added Notification Stress Tests" into nyc-dev

parents c147dbbc a6b8e9c3
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -11,6 +11,9 @@ LOCAL_SRC_FILES := \
LOCAL_JAVA_LIBRARIES := android.test.runner
LOCAL_PACKAGE_NAME := NotificationStressTests

LOCAL_STATIC_JAVA_LIBRARIES := \
    ub-uiautomator

include $(BUILD_PACKAGE)

include $(call all-makefiles-under,$(LOCAL_PATH))
+61 −19
Original line number Diff line number Diff line
@@ -16,15 +16,19 @@

package android.app;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.RemoteException;
import android.os.SystemClock;
import android.support.test.uiautomator.UiDevice;
import android.test.InstrumentationTestCase;
import android.test.RepetitiveTest;
import android.test.TimedTest;
import android.util.Log;

import java.lang.InterruptedException;
import java.lang.reflect.Method;
import java.util.Random;

/**
@@ -34,6 +38,11 @@ import java.util.Random;
public class NotificationStressTest extends InstrumentationTestCase {

    private static final int NUM_ITERATIONS = 200;
    private static final int NUM_ITERATIONS_2 = 30;
    private static final int LONG_TIMEOUT = 2000;
    // 50 notifications per app: defined as Variable MAX_PACKAGE_NOTIFICATIONS in
    // NotificationManagerService.java
    private static final int MAX_NOTIFCATIONS = 50;
    private static final int[] ICONS = new int[] {
            android.R.drawable.stat_notify_call_mute,
            android.R.drawable.stat_notify_chat,
@@ -51,34 +60,56 @@ public class NotificationStressTest extends InstrumentationTestCase {
    private final Random mRandom = new Random();
    private Context mContext;
    private NotificationManager mNotificationManager;
    private int notifyId = 0;
    private UiDevice mDevice = null;
    private int mNotifyId = 0;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mDevice = UiDevice.getInstance(getInstrumentation());
        mContext = getInstrumentation().getContext();
        mNotificationManager = (NotificationManager) mContext.getSystemService(
                Context.NOTIFICATION_SERVICE);
        mDevice.setOrientationNatural();
        mNotificationManager.cancelAll();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        mDevice.unfreezeRotation();
        mNotificationManager.cancelAll();
    }

    @RepetitiveTest(numIterations = NUM_ITERATIONS)
    public void testNotificationStress() {
        // Cancel one of every five notifications to vary load on notification manager
        if (notifyId % 5 == 4) {
            mNotificationManager.cancel(notifyId - 4);
        if (mNotifyId % 5 == 4) {
            mNotificationManager.cancel(mNotifyId - 4);
        }
        sendNotification(mNotifyId++, "testNotificationStressNotify");
    }

    @RepetitiveTest(numIterations = NUM_ITERATIONS_2)
    public void testNotificationsWithShadeStress() throws Exception {
        mDevice.openNotification();
        Thread.sleep(LONG_TIMEOUT);
        for (int j = 0; j < MAX_NOTIFCATIONS; j++) {
            sendNotification(mNotifyId++, "testNotificationStressNotify");
        }
        Thread.sleep(500);
        assertTrue(mNotificationManager.getActiveNotifications().length == MAX_NOTIFCATIONS);
        for (int j = 0; j < MAX_NOTIFCATIONS; j++) {
            mNotificationManager.cancel(--mNotifyId);
        }
        if (isLockScreen()) {
            fail("Notification stress test failed, back to lockscreen");
        }
        sendNotification(notifyId++, "testNotificationStressNotify");
    }

    private void sendNotification(int id, CharSequence text) {
        // Fill in arbitrary content
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        Intent intent = new Intent(Intent.ACTION_VIEW);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
        CharSequence title = text + " " + id;
        CharSequence subtitle = String.valueOf(System.currentTimeMillis());
@@ -90,8 +121,19 @@ public class NotificationStressTest extends InstrumentationTestCase {
                .setContentTitle(title)
                .setContentText(subtitle)
                .setContentIntent(pendingIntent)
                .setPriority(Notification.PRIORITY_HIGH)
                .build();
        mNotificationManager.notify(id, notification);
        SystemClock.sleep(10);
    }

    private boolean isLockScreen() {
        KeyguardManager myKM = (KeyguardManager) mContext
                .getSystemService(Context.KEYGUARD_SERVICE);
        if (myKM.inKeyguardRestrictedInputMode()) {
            return true;
        } else {
            return false;
        }
    }
}