Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 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 | package com.android.server.notification; |
| 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | import org.junit.runner.RunWith; |
| 21 | |
| 22 | import android.app.ActivityManager; |
| 23 | import android.app.Notification; |
| 24 | import android.app.Notification.Builder; |
| 25 | import android.app.NotificationManager; |
| 26 | import android.app.NotificationChannel; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 27 | import android.os.UserHandle; |
| 28 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 29 | import android.support.test.runner.AndroidJUnit4; |
| 30 | import android.test.suitebuilder.annotation.SmallTest; |
| 31 | |
| 32 | import org.mockito.Mock; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 33 | import org.mockito.MockitoAnnotations; |
| 34 | |
| 35 | import static org.mockito.Matchers.anyInt; |
| 36 | import static org.mockito.Matchers.anyString; |
| 37 | import static org.mockito.Mockito.when; |
| 38 | |
| 39 | import static org.junit.Assert.assertEquals; |
| 40 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 41 | import com.android.server.UiServiceTestCase; |
| 42 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 43 | @SmallTest |
| 44 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 45 | public class ImportanceExtractorTest extends UiServiceTestCase { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 46 | |
| 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 Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 71 | StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, mId, mTag, mUid, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 72 | mPid, n, mUser, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 73 | NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 74 | return r; |
| 75 | } |
| 76 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 77 | // |
| 78 | // Tests |
| 79 | // |
| 80 | |
| 81 | @Test |
| 82 | public void testAppPreferenceChannelNone() throws Exception { |
| 83 | ImportanceExtractor extractor = new ImportanceExtractor(); |
| 84 | extractor.setConfig(mConfig); |
| 85 | |
| 86 | when(mConfig.getImportance(anyString(), anyInt())).thenReturn( |
| 87 | NotificationManager.IMPORTANCE_MIN); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 88 | NotificationChannel channel = |
| 89 | new NotificationChannel("a", "a", NotificationManager.IMPORTANCE_UNSPECIFIED); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 90 | |
| 91 | NotificationRecord r = getNotificationRecord(channel); |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 92 | int notificationImportance = r.getImportance(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 93 | |
| 94 | extractor.process(r); |
| 95 | |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 96 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, r.getUserImportance()); |
| 97 | assertEquals(notificationImportance, r.getImportance()); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 101 | public void testAppPreferenceChannelPreference() throws Exception { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 102 | ImportanceExtractor extractor = new ImportanceExtractor(); |
| 103 | extractor.setConfig(mConfig); |
| 104 | |
| 105 | when(mConfig.getImportance(anyString(), anyInt())).thenReturn( |
| 106 | NotificationManager.IMPORTANCE_MIN); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 107 | NotificationChannel channel = |
| 108 | new NotificationChannel("a", "a", NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 109 | |
| 110 | NotificationRecord r = getNotificationRecord(channel); |
| 111 | |
| 112 | extractor.process(r); |
| 113 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 114 | assertEquals(r.getUserImportance(), NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 115 | } |
| 116 | } |