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; |
| 33 | import org.mockito.Mockito; |
| 34 | import org.mockito.MockitoAnnotations; |
| 35 | |
| 36 | import static org.mockito.Matchers.anyInt; |
| 37 | import static org.mockito.Matchers.anyString; |
| 38 | import static org.mockito.Mockito.when; |
| 39 | |
| 40 | import static org.junit.Assert.assertEquals; |
| 41 | |
| 42 | @SmallTest |
| 43 | @RunWith(AndroidJUnit4.class) |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 44 | public class ImportanceExtractorTest extends NotificationTestCase { |
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 |
| 81 | public void testAppPreferenceChannelNone() throws Exception { |
| 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 | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 95 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, r.getUserImportance()); |
| 96 | assertEquals(notificationImportance, r.getImportance()); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 100 | public void testAppPreferenceChannelPreference() throws Exception { |
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 | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 113 | assertEquals(r.getUserImportance(), NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 114 | } |
| 115 | } |