Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Julia Reynolds | 5d25ee7 | 2015-11-20 15:38:20 -0500 | [diff] [blame] | 18 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED; |
| 19 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_HIGH; |
| 20 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_LOW; |
| 21 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_MAX; |
| 22 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_NONE; |
| 23 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 24 | import org.mockito.Mock; |
| 25 | import org.mockito.MockitoAnnotations; |
| 26 | |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 27 | import android.app.Notification; |
| 28 | import android.os.UserHandle; |
| 29 | import android.service.notification.StatusBarNotification; |
| 30 | import android.test.AndroidTestCase; |
| 31 | import android.test.suitebuilder.annotation.SmallTest; |
| 32 | |
| 33 | import java.util.ArrayList; |
| 34 | |
| 35 | public class RankingHelperTest extends AndroidTestCase { |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 36 | @Mock NotificationUsageStats mUsageStats; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 37 | @Mock RankingHandler handler; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 38 | |
| 39 | private Notification mNotiGroupGSortA; |
| 40 | private Notification mNotiGroupGSortB; |
| 41 | private Notification mNotiNoGroup; |
| 42 | private Notification mNotiNoGroup2; |
| 43 | private Notification mNotiNoGroupSortA; |
| 44 | private NotificationRecord mRecordGroupGSortA; |
| 45 | private NotificationRecord mRecordGroupGSortB; |
| 46 | private NotificationRecord mRecordNoGroup; |
| 47 | private NotificationRecord mRecordNoGroup2; |
| 48 | private NotificationRecord mRecordNoGroupSortA; |
| 49 | private RankingHelper mHelper; |
| 50 | |
| 51 | @Override |
| 52 | public void setUp() { |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 53 | MockitoAnnotations.initMocks(this); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 54 | UserHandle user = UserHandle.ALL; |
| 55 | |
Julia Reynolds | 5d25ee7 | 2015-11-20 15:38:20 -0500 | [diff] [blame] | 56 | mHelper = new RankingHelper(getContext(), handler, mUsageStats, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 57 | new String[] {ImportanceExtractor.class.getName()}); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 58 | |
| 59 | mNotiGroupGSortA = new Notification.Builder(getContext()) |
| 60 | .setContentTitle("A") |
| 61 | .setGroup("G") |
| 62 | .setSortKey("A") |
| 63 | .setWhen(1205) |
| 64 | .build(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 65 | mRecordGroupGSortA = new NotificationRecord(getContext(), new StatusBarNotification( |
| 66 | "package", "package", 1, null, 0, 0, 0, mNotiGroupGSortA, user)); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 67 | |
| 68 | mNotiGroupGSortB = new Notification.Builder(getContext()) |
| 69 | .setContentTitle("B") |
| 70 | .setGroup("G") |
| 71 | .setSortKey("B") |
| 72 | .setWhen(1200) |
| 73 | .build(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 74 | mRecordGroupGSortB = new NotificationRecord(getContext(), new StatusBarNotification( |
| 75 | "package", "package", 1, null, 0, 0, 0, mNotiGroupGSortB, user)); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 76 | |
| 77 | mNotiNoGroup = new Notification.Builder(getContext()) |
| 78 | .setContentTitle("C") |
| 79 | .setWhen(1201) |
| 80 | .build(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 81 | mRecordNoGroup = new NotificationRecord(getContext(), new StatusBarNotification( |
| 82 | "package", "package", 1, null, 0, 0, 0, mNotiNoGroup, user)); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 83 | |
| 84 | mNotiNoGroup2 = new Notification.Builder(getContext()) |
| 85 | .setContentTitle("D") |
| 86 | .setWhen(1202) |
| 87 | .build(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 88 | mRecordNoGroup2 = new NotificationRecord(getContext(), new StatusBarNotification( |
| 89 | "package", "package", 1, null, 0, 0, 0, mNotiNoGroup2, user)); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 90 | |
| 91 | mNotiNoGroupSortA = new Notification.Builder(getContext()) |
| 92 | .setContentTitle("E") |
| 93 | .setWhen(1201) |
| 94 | .setSortKey("A") |
| 95 | .build(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 96 | mRecordNoGroupSortA = new NotificationRecord(getContext(), new StatusBarNotification( |
| 97 | "package", "package", 1, null, 0, 0, 0, mNotiNoGroupSortA, user)); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @SmallTest |
| 101 | public void testFindAfterRankingWithASplitGroup() throws Exception { |
| 102 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(3); |
| 103 | notificationList.add(mRecordGroupGSortA); |
| 104 | notificationList.add(mRecordGroupGSortB); |
| 105 | notificationList.add(mRecordNoGroup); |
| 106 | notificationList.add(mRecordNoGroupSortA); |
| 107 | mHelper.sort(notificationList); |
| 108 | assertTrue(mHelper.indexOf(notificationList, mRecordGroupGSortA) >= 0); |
| 109 | assertTrue(mHelper.indexOf(notificationList, mRecordGroupGSortB) >= 0); |
| 110 | assertTrue(mHelper.indexOf(notificationList, mRecordNoGroup) >= 0); |
| 111 | assertTrue(mHelper.indexOf(notificationList, mRecordNoGroupSortA) >= 0); |
| 112 | } |
| 113 | |
| 114 | @SmallTest |
| 115 | public void testSortShouldNotThrowWithPlainNotifications() throws Exception { |
| 116 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(2); |
| 117 | notificationList.add(mRecordNoGroup); |
| 118 | notificationList.add(mRecordNoGroup2); |
| 119 | mHelper.sort(notificationList); |
| 120 | } |
| 121 | |
| 122 | @SmallTest |
| 123 | public void testSortShouldNotThrowOneSorted() throws Exception { |
| 124 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(2); |
| 125 | notificationList.add(mRecordNoGroup); |
| 126 | notificationList.add(mRecordNoGroupSortA); |
| 127 | mHelper.sort(notificationList); |
| 128 | } |
| 129 | |
| 130 | @SmallTest |
| 131 | public void testSortShouldNotThrowOneNotification() throws Exception { |
| 132 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(1); |
| 133 | notificationList.add(mRecordNoGroup); |
| 134 | mHelper.sort(notificationList); |
| 135 | } |
| 136 | |
| 137 | @SmallTest |
| 138 | public void testSortShouldNotThrowOneSortKey() throws Exception { |
| 139 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(1); |
| 140 | notificationList.add(mRecordGroupGSortB); |
| 141 | mHelper.sort(notificationList); |
| 142 | } |
| 143 | |
| 144 | @SmallTest |
| 145 | public void testSortShouldNotThrowOnEmptyList() throws Exception { |
| 146 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(); |
| 147 | mHelper.sort(notificationList); |
| 148 | } |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 149 | } |