1 /* 2 * Copyright (C) 2016 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 org.junit.Assert.assertEquals; 20 21 import android.app.NotificationChannel; 22 import android.content.Intent; 23 import android.os.Binder; 24 import android.os.Bundle; 25 import android.os.IBinder; 26 import android.service.notification.NotificationListenerService; 27 import android.service.notification.NotificationListenerService.Ranking; 28 import android.service.notification.NotificationRankingUpdate; 29 import android.service.notification.SnoozeCriterion; 30 import android.support.test.runner.AndroidJUnit4; 31 import android.test.suitebuilder.annotation.SmallTest; 32 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 import java.util.ArrayList; 37 import java.util.List; 38 39 @SmallTest 40 @RunWith(AndroidJUnit4.class) 41 public class NotificationListenerServiceTest extends NotificationTestCase { 42 43 private String[] mKeys = new String[] { "key", "key1", "key2", "key3"}; 44 45 @Test 46 public void testRanking() throws Exception { 47 TestListenerService service = new TestListenerService(); 48 service.applyUpdateLocked(generateUpdate()); 49 for (int i = 0; i < mKeys.length; i++) { 50 String key = mKeys[i]; 51 Ranking ranking = new Ranking(); 52 service.getCurrentRanking().getRanking(key, ranking); 53 assertEquals(getVisibilityOverride(i), ranking.getVisibilityOverride()); 54 assertEquals(getOverrideGroupKey(key), ranking.getOverrideGroupKey()); 55 assertEquals(!isIntercepted(i), ranking.matchesInterruptionFilter()); 56 assertEquals(getSuppressedVisualEffects(i), ranking.getSuppressedVisualEffects()); 57 assertEquals(getImportance(i), ranking.getImportance()); 58 assertEquals(getExplanation(key), ranking.getImportanceExplanation()); 59 assertEquals(getChannel(key, i), ranking.getChannel()); 60 assertEquals(getPeople(key, i), ranking.getAdditionalPeople()); 61 assertEquals(getSnoozeCriteria(key, i), ranking.getSnoozeCriteria()); 62 assertEquals(getShowBadge(i), ranking.canShowBadge()); 63 } 64 } 65 66 private NotificationRankingUpdate generateUpdate() { 67 List<String> interceptedKeys = new ArrayList<>(); 68 Bundle visibilityOverrides = new Bundle(); 69 Bundle overrideGroupKeys = new Bundle(); 70 Bundle suppressedVisualEffects = new Bundle(); 71 Bundle explanation = new Bundle(); 72 Bundle channels = new Bundle(); 73 Bundle overridePeople = new Bundle(); 74 Bundle snoozeCriteria = new Bundle(); 75 Bundle showBadge = new Bundle(); 76 int[] importance = new int[mKeys.length]; 77 78 for (int i = 0; i < mKeys.length; i++) { 79 String key = mKeys[i]; 80 visibilityOverrides.putInt(key, getVisibilityOverride(i)); 81 overrideGroupKeys.putString(key, getOverrideGroupKey(key)); 82 if (isIntercepted(i)) { 83 interceptedKeys.add(key); 84 } 85 suppressedVisualEffects.putInt(key, getSuppressedVisualEffects(i)); 86 importance[i] = getImportance(i); 87 explanation.putString(key, getExplanation(key)); 88 channels.putParcelable(key, getChannel(key, i)); 89 overridePeople.putStringArrayList(key, getPeople(key, i)); 90 snoozeCriteria.putParcelableArrayList(key, getSnoozeCriteria(key, i)); 91 showBadge.putBoolean(key, getShowBadge(i)); 92 } 93 NotificationRankingUpdate update = new NotificationRankingUpdate(mKeys, 94 interceptedKeys.toArray(new String[0]), visibilityOverrides, 95 suppressedVisualEffects, importance, explanation, overrideGroupKeys, 96 channels, overridePeople, snoozeCriteria, showBadge); 97 return update; 98 } 99 100 private int getVisibilityOverride(int index) { 101 return index * 9; 102 } 103 104 private String getOverrideGroupKey(String key) { 105 return key + key; 106 } 107 108 private boolean isIntercepted(int index) { 109 return index % 2 == 0; 110 } 111 112 private int getSuppressedVisualEffects(int index) { 113 return index * 2; 114 } 115 116 private int getImportance(int index) { 117 return index; 118 } 119 120 private String getExplanation(String key) { 121 return key + "explain"; 122 } 123 124 private NotificationChannel getChannel(String key, int index) { 125 return new NotificationChannel(key, key, getImportance(index)); 126 } 127 128 private boolean getShowBadge(int index) { 129 return index % 3 == 0; 130 } 131 132 private ArrayList<String> getPeople(String key, int index) { 133 ArrayList<String> people = new ArrayList<>(); 134 for (int i = 0; i < index; i++) { 135 people.add(i + key); 136 } 137 return people; 138 } 139 140 private ArrayList<SnoozeCriterion> getSnoozeCriteria(String key, int index) { 141 ArrayList<SnoozeCriterion> snooze = new ArrayList<>(); 142 for (int i = 0; i < index; i++) { 143 snooze.add(new SnoozeCriterion(key + i, getExplanation(key), key)); 144 } 145 return snooze; 146 } 147 148 public static class TestListenerService extends NotificationListenerService { 149 private final IBinder binder = new LocalBinder(); 150 151 public TestListenerService() { 152 153 } 154 155 @Override 156 public IBinder onBind(Intent intent) { 157 super.onBind(intent); 158 return binder; 159 } 160 161 public class LocalBinder extends Binder { 162 TestListenerService getService() { 163 return TestListenerService.this; 164 } 165 } 166 } 167 } 168