Home | History | Annotate | Download | only in functional
      1 
      2 package com.android.notification.functional;
      3 
      4 import android.app.AppGlobals;
      5 import android.app.Notification;
      6 import android.app.NotificationChannel;
      7 import android.app.NotificationManager;
      8 import android.content.ContentResolver;
      9 import android.content.Context;
     10 import android.content.Intent;
     11 import android.media.AudioManager;
     12 import android.net.Uri;
     13 import android.os.Handler;
     14 import android.os.Looper;
     15 import android.os.RemoteException;
     16 import android.provider.Settings;
     17 import android.provider.Settings.Global;
     18 import android.provider.Settings.SettingNotFoundException;
     19 import android.service.notification.StatusBarNotification;
     20 import android.service.notification.ZenModeConfig;
     21 import android.support.test.uiautomator.By;
     22 import android.support.test.uiautomator.UiDevice;
     23 import android.support.test.uiautomator.UiObject;
     24 import android.support.test.uiautomator.UiObject2;
     25 import android.support.test.uiautomator.Until;
     26 import android.test.InstrumentationTestCase;
     27 import android.test.suitebuilder.annotation.LargeTest;
     28 import android.test.suitebuilder.annotation.MediumTest;
     29 import android.util.Log;
     30 
     31 import com.android.server.notification.NotificationManagerService;
     32 import com.android.server.notification.ConditionProviders;
     33 import com.android.server.notification.ManagedServices.UserProfiles;
     34 
     35 import java.util.concurrent.Callable;
     36 import java.util.concurrent.FutureTask;
     37 
     38 import com.android.server.notification.ZenModeHelper;
     39 import com.android.server.notification.NotificationRecord;
     40 import com.android.server.notification.ZenModeFiltering;
     41 
     42 public class NotificationDNDTests extends InstrumentationTestCase {
     43     private static final String LOG_TAG = NotificationDNDTests.class.getSimpleName();
     44     private static final int SHORT_TIMEOUT = 1000;
     45     private static final int LONG_TIMEOUT = 2000;
     46     private static final int NOTIFICATION_ID = 1;
     47     private static final String NOTIFICATION_CONTENT_TEXT = "INLINE REPLY TEST";
     48     private NotificationManager mNotificationManager;
     49     private UiDevice mDevice = null;
     50     private Context mContext;
     51     private NotificationHelper mHelper;
     52     private ContentResolver mResolver;
     53     private ZenModeHelper mZenHelper;
     54     private boolean isGranted = false;
     55 
     56     @Override
     57     public void setUp() throws Exception {
     58         super.setUp();
     59         mDevice = UiDevice.getInstance(getInstrumentation());
     60         mContext = getInstrumentation().getTargetContext();
     61         mNotificationManager = (NotificationManager) mContext
     62                 .getSystemService(Context.NOTIFICATION_SERVICE);
     63         mHelper = new NotificationHelper(mDevice, getInstrumentation(), mNotificationManager);
     64         mResolver = mContext.getContentResolver();
     65         ConditionProviders cps = new ConditionProviders(mContext,
     66                 new UserProfiles(), AppGlobals.getPackageManager());
     67         Callable<ZenModeHelper> callable = new Callable<ZenModeHelper>() {
     68             @Override
     69             public ZenModeHelper call() throws Exception {
     70                 return new ZenModeHelper(mContext, Looper.getMainLooper(), cps);
     71             }
     72         };
     73         FutureTask<ZenModeHelper> task = new FutureTask<>(callable);
     74         getInstrumentation().runOnMainSync(task);
     75         mZenHelper = task.get();
     76         mDevice.setOrientationNatural();
     77         mHelper.unlockScreen();
     78         mDevice.pressHome();
     79         mNotificationManager.cancelAll();
     80         isGranted = mNotificationManager.isNotificationPolicyAccessGranted();
     81     }
     82 
     83     @Override
     84     public void tearDown() throws Exception {
     85         mNotificationManager.cancelAll();
     86         mDevice.pressHome();
     87         mDevice.unfreezeRotation();
     88         super.tearDown();
     89     }
     90 
     91     @LargeTest
     92     public void testDND() throws Exception {
     93         if (!isGranted) {
     94             grantPolicyAccess(true);
     95         }
     96         int setting = mNotificationManager.getCurrentInterruptionFilter();
     97         try {
     98             mNotificationManager
     99                     .setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY);
    100             mHelper.sendNotificationsWithInlineReply(NOTIFICATION_ID, true);
    101             Thread.sleep(LONG_TIMEOUT);
    102             NotificationRecord nr = new NotificationRecord(mContext,
    103                     mHelper.getStatusBarNotification(NOTIFICATION_ID), mHelper.getDefaultChannel());
    104             ZenModeConfig mConfig = mZenHelper.getConfig();
    105             ZenModeFiltering zF = new ZenModeFiltering(mContext);
    106             assertTrue(zF.shouldIntercept(mNotificationManager.getZenMode(), mConfig, nr));
    107         } finally {
    108             mNotificationManager.setInterruptionFilter(setting);
    109             if (!isGranted) {
    110                 grantPolicyAccess(false);
    111             }
    112         }
    113     }
    114 
    115     @LargeTest
    116     public void testPriority() throws Exception {
    117         int setting = mNotificationManager.getCurrentInterruptionFilter();
    118         if (!isGranted) {
    119             grantPolicyAccess(true);
    120         }
    121         mNotificationManager
    122                 .setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY);
    123         try {
    124             mHelper.showAppNotificationSettings(mContext);
    125             mDevice.wait(Until.findObject(By.textContains("Uncategorized")), LONG_TIMEOUT)
    126                     .click();
    127             mDevice.wait(Until.findObject(By.textContains("Override Do Not Disturb")), LONG_TIMEOUT)
    128                     .click();
    129             Thread.sleep(LONG_TIMEOUT);
    130             mHelper.sendNotificationsWithInlineReply(NOTIFICATION_ID, true);
    131             Thread.sleep(LONG_TIMEOUT);
    132             NotificationRecord nr = new NotificationRecord(mContext,
    133                     mHelper.getStatusBarNotification(NOTIFICATION_ID), mHelper.getDefaultChannel());
    134             ZenModeConfig mConfig = mZenHelper.getConfig();
    135             ZenModeFiltering zF = new ZenModeFiltering(mContext);
    136             assertFalse(zF.shouldIntercept(mZenHelper.getZenMode(), mConfig, nr));
    137         } finally {
    138             mHelper.showAppNotificationSettings(mContext);
    139             mDevice.wait(Until.findObject(By.textContains("Uncategorized")), LONG_TIMEOUT)
    140                     .click();
    141             mDevice.wait(Until.findObject(By.textContains("Advanced")), LONG_TIMEOUT)
    142                     .click();
    143             mDevice.wait(Until.findObject(By.textContains("Override Do Not Disturb")), LONG_TIMEOUT)
    144                     .click();
    145             mNotificationManager.setInterruptionFilter(setting);
    146             if (!isGranted) {
    147                 grantPolicyAccess(false);
    148             }
    149         }
    150     }
    151 
    152     @LargeTest
    153     public void testBlockNotification() throws Exception {
    154         try {
    155             mHelper.showAppNotificationSettings(mContext);
    156             mDevice.wait(Until.findObject(
    157                     By.textContains("Show notifications")), LONG_TIMEOUT).click();
    158             Thread.sleep(LONG_TIMEOUT);
    159             mHelper.sendNotificationsWithInlineReply(NOTIFICATION_ID, true);
    160             Thread.sleep(LONG_TIMEOUT);
    161             if (mHelper.checkNotificationExistence(NOTIFICATION_ID, true)) {
    162                 fail(String.format("Notification %s has not been blocked", NOTIFICATION_ID));
    163             }
    164         } finally {
    165             mHelper.showAppNotificationSettings(mContext);
    166             mDevice.wait(Until.findObject(
    167                     By.textContains("Show notifications")), LONG_TIMEOUT).click();
    168         }
    169     }
    170 
    171     private void grantPolicyAccess(boolean grant) throws Exception {
    172         NotificationHelper.launchSettingsPage(mContext,
    173                 android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
    174         Thread.sleep(LONG_TIMEOUT);
    175         if (grant) {
    176             mDevice.wait(Until.findObject(
    177                     By.text("com.android.notification.functional")), LONG_TIMEOUT).click();
    178             Thread.sleep(SHORT_TIMEOUT);
    179             mDevice.wait(Until.findObject(By.text("Allow")), LONG_TIMEOUT).click();
    180         } else {
    181             mDevice.wait(Until.findObject(
    182                     By.text("com.android.notification.functional")), LONG_TIMEOUT).click();
    183             Thread.sleep(SHORT_TIMEOUT);
    184             mDevice.wait(Until.findObject(By.text("Turn off")), LONG_TIMEOUT).click();
    185         }
    186         Thread.sleep(LONG_TIMEOUT);
    187         mDevice.pressHome();
    188     }
    189 }
    190