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 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 19 | import static org.mockito.Matchers.anyInt; |
| 20 | import static org.mockito.Matchers.anyString; |
| 21 | import static org.mockito.Mockito.when; |
| 22 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 23 | import android.app.ActivityManager; |
| 24 | import android.app.Notification; |
| 25 | import android.app.Notification.Builder; |
| 26 | import android.app.NotificationChannel; |
| 27 | import android.app.NotificationManager; |
| 28 | import android.os.UserHandle; |
| 29 | import android.service.notification.StatusBarNotification; |
| 30 | import android.test.suitebuilder.annotation.SmallTest; |
| 31 | |
| 32 | import androidx.test.runner.AndroidJUnit4; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 33 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 34 | import com.android.server.UiServiceTestCase; |
| 35 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | import org.mockito.Mock; |
| 40 | import org.mockito.MockitoAnnotations; |
| 41 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 42 | @SmallTest |
| 43 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 44 | public class ImportanceExtractorTest extends UiServiceTestCase { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 45 | |
| 46 | @Mock RankingConfig mConfig; |
| 47 | |
| 48 | private String mPkg = "com.android.server.notification"; |
| 49 | private int mId = 1001; |
| 50 | private int mOtherId = 1002; |
| 51 | private String mTag = null; |
| 52 | private int mUid = 1000; |
| 53 | private int mPid = 2000; |
| 54 | private int mScore = 10; |
| 55 | private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser()); |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | MockitoAnnotations.initMocks(this); |
| 60 | } |
| 61 | |
| 62 | private NotificationRecord getNotificationRecord(NotificationChannel channel) { |
| 63 | final Builder builder = new Builder(getContext()) |
| 64 | .setContentTitle("foo") |
| 65 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 66 | .setPriority(Notification.PRIORITY_HIGH) |
| 67 | .setDefaults(Notification.DEFAULT_SOUND); |
| 68 | |
| 69 | Notification n = builder.build(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 70 | StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, mId, mTag, mUid, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 71 | mPid, n, mUser, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 72 | NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 73 | return r; |
| 74 | } |
| 75 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 76 | // |
| 77 | // Tests |
| 78 | // |
| 79 | |
| 80 | @Test |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 81 | public void testAppPreferenceChannelNone() { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 82 | ImportanceExtractor extractor = new ImportanceExtractor(); |
| 83 | extractor.setConfig(mConfig); |
| 84 | |
| 85 | when(mConfig.getImportance(anyString(), anyInt())).thenReturn( |
| 86 | NotificationManager.IMPORTANCE_MIN); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 87 | NotificationChannel channel = |
| 88 | new NotificationChannel("a", "a", NotificationManager.IMPORTANCE_UNSPECIFIED); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 89 | |
| 90 | NotificationRecord r = getNotificationRecord(channel); |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 91 | int notificationImportance = r.getImportance(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 92 | |
| 93 | extractor.process(r); |
| 94 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 95 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, r.getImportance()); |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 96 | assertEquals(notificationImportance, r.getImportance()); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Test |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 100 | public void testAppPreferenceChannelPreference() { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 101 | ImportanceExtractor extractor = new ImportanceExtractor(); |
| 102 | extractor.setConfig(mConfig); |
| 103 | |
| 104 | when(mConfig.getImportance(anyString(), anyInt())).thenReturn( |
| 105 | NotificationManager.IMPORTANCE_MIN); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 106 | NotificationChannel channel = |
| 107 | new NotificationChannel("a", "a", NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 108 | |
| 109 | NotificationRecord r = getNotificationRecord(channel); |
| 110 | |
| 111 | extractor.process(r); |
| 112 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 113 | assertEquals(r.getImportance(), NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 114 | } |
| 115 | } |