blob: f1fe3462432d617bd57c1eaa51fb9062165c015a [file] [log] [blame]
Chris Wren1031c972014-07-23 13:11:45 +00001/*
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 */
16package com.android.server.notification;
17
Julia Reynolds5d25ee72015-11-20 15:38:20 -050018import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED;
19import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_HIGH;
20import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_LOW;
21import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_MAX;
22import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_NONE;
23
Chris Wren5eab2b72015-06-16 13:56:22 -040024import org.mockito.Mock;
25import org.mockito.MockitoAnnotations;
26
Chris Wren1031c972014-07-23 13:11:45 +000027import android.app.Notification;
28import android.os.UserHandle;
29import android.service.notification.StatusBarNotification;
30import android.test.AndroidTestCase;
31import android.test.suitebuilder.annotation.SmallTest;
32
33import java.util.ArrayList;
34
35public class RankingHelperTest extends AndroidTestCase {
Chris Wren5eab2b72015-06-16 13:56:22 -040036 @Mock NotificationUsageStats mUsageStats;
Chris Wren51017d02015-12-15 15:34:46 -050037 @Mock RankingHandler handler;
Chris Wren1031c972014-07-23 13:11:45 +000038
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 Wren5eab2b72015-06-16 13:56:22 -040053 MockitoAnnotations.initMocks(this);
Chris Wren1031c972014-07-23 13:11:45 +000054 UserHandle user = UserHandle.ALL;
55
Julia Reynolds5d25ee72015-11-20 15:38:20 -050056 mHelper = new RankingHelper(getContext(), handler, mUsageStats,
Julia Reynoldsef37f282016-02-12 09:11:27 -050057 new String[] {ImportanceExtractor.class.getName()});
Chris Wren1031c972014-07-23 13:11:45 +000058
59 mNotiGroupGSortA = new Notification.Builder(getContext())
60 .setContentTitle("A")
61 .setGroup("G")
62 .setSortKey("A")
63 .setWhen(1205)
64 .build();
Chris Wrenbdf33762015-12-04 15:50:51 -050065 mRecordGroupGSortA = new NotificationRecord(getContext(), new StatusBarNotification(
66 "package", "package", 1, null, 0, 0, 0, mNotiGroupGSortA, user));
Chris Wren1031c972014-07-23 13:11:45 +000067
68 mNotiGroupGSortB = new Notification.Builder(getContext())
69 .setContentTitle("B")
70 .setGroup("G")
71 .setSortKey("B")
72 .setWhen(1200)
73 .build();
Chris Wrenbdf33762015-12-04 15:50:51 -050074 mRecordGroupGSortB = new NotificationRecord(getContext(), new StatusBarNotification(
75 "package", "package", 1, null, 0, 0, 0, mNotiGroupGSortB, user));
Chris Wren1031c972014-07-23 13:11:45 +000076
77 mNotiNoGroup = new Notification.Builder(getContext())
78 .setContentTitle("C")
79 .setWhen(1201)
80 .build();
Chris Wrenbdf33762015-12-04 15:50:51 -050081 mRecordNoGroup = new NotificationRecord(getContext(), new StatusBarNotification(
82 "package", "package", 1, null, 0, 0, 0, mNotiNoGroup, user));
Chris Wren1031c972014-07-23 13:11:45 +000083
84 mNotiNoGroup2 = new Notification.Builder(getContext())
85 .setContentTitle("D")
86 .setWhen(1202)
87 .build();
Chris Wrenbdf33762015-12-04 15:50:51 -050088 mRecordNoGroup2 = new NotificationRecord(getContext(), new StatusBarNotification(
89 "package", "package", 1, null, 0, 0, 0, mNotiNoGroup2, user));
Chris Wren1031c972014-07-23 13:11:45 +000090
91 mNotiNoGroupSortA = new Notification.Builder(getContext())
92 .setContentTitle("E")
93 .setWhen(1201)
94 .setSortKey("A")
95 .build();
Chris Wrenbdf33762015-12-04 15:50:51 -050096 mRecordNoGroupSortA = new NotificationRecord(getContext(), new StatusBarNotification(
97 "package", "package", 1, null, 0, 0, 0, mNotiNoGroupSortA, user));
Chris Wren1031c972014-07-23 13:11:45 +000098 }
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 Wren1031c972014-07-23 13:11:45 +0000149}