Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.server.notification;
     18 
     19 import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
     20 import static android.app.NotificationManager.IMPORTANCE_LOW;
     21 
     22 import static com.android.server.notification.NotificationIntrusivenessExtractor.HANG_TIME_MS;
     23 
     24 import static junit.framework.Assert.assertFalse;
     25 import static junit.framework.Assert.assertNotNull;
     26 import static junit.framework.Assert.assertNull;
     27 
     28 import android.app.Notification;
     29 import android.app.NotificationChannel;
     30 import android.app.PendingIntent;
     31 import android.content.Intent;
     32 import android.os.UserHandle;
     33 import android.service.notification.StatusBarNotification;
     34 
     35 import com.android.server.UiServiceTestCase;
     36 
     37 import org.junit.Test;
     38 
     39 public class NotificationIntrusivenessExtractorTest extends UiServiceTestCase {
     40 
     41     @Test
     42     public void testNonIntrusive() {
     43         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_LOW);
     44         final Notification.Builder builder = new Notification.Builder(getContext())
     45                 .setContentTitle("foo")
     46                 .setSmallIcon(android.R.drawable.sym_def_app_icon);
     47 
     48         Notification n = builder.build();
     49         StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0,
     50                 0, n, UserHandle.ALL, null, System.currentTimeMillis());
     51         NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
     52 
     53         assertNull(new NotificationIntrusivenessExtractor().process(r));
     54     }
     55 
     56     @Test
     57     public void testIntrusive_fillScreen() {
     58         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
     59         final Notification.Builder builder = new Notification.Builder(getContext())
     60                 .setContentTitle("foo")
     61                 .setFullScreenIntent(PendingIntent.getActivity(
     62                         getContext(), 0, new Intent(""), 0), true)
     63                 .setSmallIcon(android.R.drawable.sym_def_app_icon);
     64 
     65         Notification n = builder.build();
     66         StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0,
     67                 0, n, UserHandle.ALL, null,
     68                 System.currentTimeMillis());
     69         NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
     70 
     71         assertNotNull(new NotificationIntrusivenessExtractor().process(r));
     72     }
     73 
     74     @Test
     75     public void testOldNotificationsNotIntrusive() {
     76         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
     77         final Notification.Builder builder = new Notification.Builder(getContext())
     78                 .setContentTitle("foo")
     79                 .setFullScreenIntent(PendingIntent.getActivity(
     80                         getContext(), 0, new Intent(""), 0), true)
     81                 .setSmallIcon(android.R.drawable.sym_def_app_icon);
     82 
     83         Notification n = builder.build();
     84         StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0,
     85                 0, n, UserHandle.ALL, null,
     86                 System.currentTimeMillis() - HANG_TIME_MS);
     87 
     88         NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
     89         assertNull(new NotificationIntrusivenessExtractor().process(r));
     90         assertFalse(r.isRecentlyIntrusive());
     91     }
     92 }
     93