blob: abc6e3cf46bff0adbbf50bbfbbf07247ee947e3c [file] [log] [blame]
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001/*
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 */
16package com.android.server.notification;
17
18import org.junit.Before;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21
22import android.app.ActivityManager;
23import android.app.Notification;
24import android.app.Notification.Builder;
25import android.app.NotificationManager;
26import android.app.NotificationChannel;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040027import android.os.UserHandle;
28import android.service.notification.StatusBarNotification;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040029import android.support.test.runner.AndroidJUnit4;
30import android.test.suitebuilder.annotation.SmallTest;
31
32import org.mockito.Mock;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040033import org.mockito.MockitoAnnotations;
34
35import static org.mockito.Matchers.anyInt;
36import static org.mockito.Matchers.anyString;
37import static org.mockito.Mockito.when;
38
39import static org.junit.Assert.assertEquals;
40
Jason Monk74f5e362017-12-06 08:56:33 -050041import com.android.server.UiServiceTestCase;
42
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040043@SmallTest
44@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050045public class ImportanceExtractorTest extends UiServiceTestCase {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040046
47 @Mock RankingConfig mConfig;
48
49 private String mPkg = "com.android.server.notification";
50 private int mId = 1001;
51 private int mOtherId = 1002;
52 private String mTag = null;
53 private int mUid = 1000;
54 private int mPid = 2000;
55 private int mScore = 10;
56 private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
57
58 @Before
59 public void setUp() {
60 MockitoAnnotations.initMocks(this);
61 }
62
63 private NotificationRecord getNotificationRecord(NotificationChannel channel) {
64 final Builder builder = new Builder(getContext())
65 .setContentTitle("foo")
66 .setSmallIcon(android.R.drawable.sym_def_app_icon)
67 .setPriority(Notification.PRIORITY_HIGH)
68 .setDefaults(Notification.DEFAULT_SOUND);
69
70 Notification n = builder.build();
Julia Reynolds924eed12017-01-19 09:52:07 -050071 StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, mId, mTag, mUid,
Julia Reynolds423b9fc2016-11-09 09:51:08 -050072 mPid, n, mUser, null, System.currentTimeMillis());
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000073 NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040074 return r;
75 }
76
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040077 //
78 // Tests
79 //
80
81 @Test
Julia Reynoldsefcdff42018-08-09 09:42:56 -040082 public void testAppPreferenceChannelNone() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040083 ImportanceExtractor extractor = new ImportanceExtractor();
84 extractor.setConfig(mConfig);
85
86 when(mConfig.getImportance(anyString(), anyInt())).thenReturn(
87 NotificationManager.IMPORTANCE_MIN);
Julia Reynolds85769912016-10-25 09:08:57 -040088 NotificationChannel channel =
89 new NotificationChannel("a", "a", NotificationManager.IMPORTANCE_UNSPECIFIED);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040090
91 NotificationRecord r = getNotificationRecord(channel);
Julia Reynoldsa917a112017-03-21 11:09:14 -040092 int notificationImportance = r.getImportance();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040093
94 extractor.process(r);
95
Julia Reynoldsefcdff42018-08-09 09:42:56 -040096 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, r.getImportance());
Julia Reynoldsa917a112017-03-21 11:09:14 -040097 assertEquals(notificationImportance, r.getImportance());
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040098 }
99
100 @Test
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400101 public void testAppPreferenceChannelPreference() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400102 ImportanceExtractor extractor = new ImportanceExtractor();
103 extractor.setConfig(mConfig);
104
105 when(mConfig.getImportance(anyString(), anyInt())).thenReturn(
106 NotificationManager.IMPORTANCE_MIN);
Julia Reynolds85769912016-10-25 09:08:57 -0400107 NotificationChannel channel =
108 new NotificationChannel("a", "a", NotificationManager.IMPORTANCE_HIGH);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400109
110 NotificationRecord r = getNotificationRecord(channel);
111
112 extractor.process(r);
113
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400114 assertEquals(r.getImportance(), NotificationManager.IMPORTANCE_HIGH);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400115 }
116}