blob: 32501ad88789b40a40200cb66fcfd19f9342f07b [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
Chris Wren5eab2b72015-06-16 13:56:22 -040018import org.mockito.Mock;
19import org.mockito.MockitoAnnotations;
20
Chris Wren1031c972014-07-23 13:11:45 +000021import android.app.Notification;
22import android.os.UserHandle;
23import android.service.notification.StatusBarNotification;
24import android.test.AndroidTestCase;
25import android.test.suitebuilder.annotation.SmallTest;
26
27import java.util.ArrayList;
28
29public class RankingHelperTest extends AndroidTestCase {
Chris Wren5eab2b72015-06-16 13:56:22 -040030 @Mock NotificationUsageStats mUsageStats;
Chris Wren51017d02015-12-15 15:34:46 -050031 @Mock RankingHandler handler;
Chris Wren1031c972014-07-23 13:11:45 +000032
33 private Notification mNotiGroupGSortA;
34 private Notification mNotiGroupGSortB;
35 private Notification mNotiNoGroup;
36 private Notification mNotiNoGroup2;
37 private Notification mNotiNoGroupSortA;
38 private NotificationRecord mRecordGroupGSortA;
39 private NotificationRecord mRecordGroupGSortB;
40 private NotificationRecord mRecordNoGroup;
41 private NotificationRecord mRecordNoGroup2;
42 private NotificationRecord mRecordNoGroupSortA;
43 private RankingHelper mHelper;
44
45 @Override
46 public void setUp() {
Chris Wren5eab2b72015-06-16 13:56:22 -040047 MockitoAnnotations.initMocks(this);
Chris Wren1031c972014-07-23 13:11:45 +000048 UserHandle user = UserHandle.ALL;
49
Julia Reynolds5d25ee72015-11-20 15:38:20 -050050 mHelper = new RankingHelper(getContext(), handler, mUsageStats,
Julia Reynoldsef37f282016-02-12 09:11:27 -050051 new String[] {ImportanceExtractor.class.getName()});
Chris Wren1031c972014-07-23 13:11:45 +000052
53 mNotiGroupGSortA = new Notification.Builder(getContext())
54 .setContentTitle("A")
55 .setGroup("G")
56 .setSortKey("A")
57 .setWhen(1205)
58 .build();
Chris Wrenbdf33762015-12-04 15:50:51 -050059 mRecordGroupGSortA = new NotificationRecord(getContext(), new StatusBarNotification(
60 "package", "package", 1, null, 0, 0, 0, mNotiGroupGSortA, user));
Chris Wren1031c972014-07-23 13:11:45 +000061
62 mNotiGroupGSortB = new Notification.Builder(getContext())
63 .setContentTitle("B")
64 .setGroup("G")
65 .setSortKey("B")
66 .setWhen(1200)
67 .build();
Chris Wrenbdf33762015-12-04 15:50:51 -050068 mRecordGroupGSortB = new NotificationRecord(getContext(), new StatusBarNotification(
69 "package", "package", 1, null, 0, 0, 0, mNotiGroupGSortB, user));
Chris Wren1031c972014-07-23 13:11:45 +000070
71 mNotiNoGroup = new Notification.Builder(getContext())
72 .setContentTitle("C")
73 .setWhen(1201)
74 .build();
Chris Wrenbdf33762015-12-04 15:50:51 -050075 mRecordNoGroup = new NotificationRecord(getContext(), new StatusBarNotification(
76 "package", "package", 1, null, 0, 0, 0, mNotiNoGroup, user));
Chris Wren1031c972014-07-23 13:11:45 +000077
78 mNotiNoGroup2 = new Notification.Builder(getContext())
79 .setContentTitle("D")
80 .setWhen(1202)
81 .build();
Chris Wrenbdf33762015-12-04 15:50:51 -050082 mRecordNoGroup2 = new NotificationRecord(getContext(), new StatusBarNotification(
83 "package", "package", 1, null, 0, 0, 0, mNotiNoGroup2, user));
Chris Wren1031c972014-07-23 13:11:45 +000084
85 mNotiNoGroupSortA = new Notification.Builder(getContext())
86 .setContentTitle("E")
87 .setWhen(1201)
88 .setSortKey("A")
89 .build();
Chris Wrenbdf33762015-12-04 15:50:51 -050090 mRecordNoGroupSortA = new NotificationRecord(getContext(), new StatusBarNotification(
91 "package", "package", 1, null, 0, 0, 0, mNotiNoGroupSortA, user));
Chris Wren1031c972014-07-23 13:11:45 +000092 }
93
94 @SmallTest
95 public void testFindAfterRankingWithASplitGroup() throws Exception {
96 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(3);
97 notificationList.add(mRecordGroupGSortA);
98 notificationList.add(mRecordGroupGSortB);
99 notificationList.add(mRecordNoGroup);
100 notificationList.add(mRecordNoGroupSortA);
101 mHelper.sort(notificationList);
102 assertTrue(mHelper.indexOf(notificationList, mRecordGroupGSortA) >= 0);
103 assertTrue(mHelper.indexOf(notificationList, mRecordGroupGSortB) >= 0);
104 assertTrue(mHelper.indexOf(notificationList, mRecordNoGroup) >= 0);
105 assertTrue(mHelper.indexOf(notificationList, mRecordNoGroupSortA) >= 0);
106 }
107
108 @SmallTest
109 public void testSortShouldNotThrowWithPlainNotifications() throws Exception {
110 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(2);
111 notificationList.add(mRecordNoGroup);
112 notificationList.add(mRecordNoGroup2);
113 mHelper.sort(notificationList);
114 }
115
116 @SmallTest
117 public void testSortShouldNotThrowOneSorted() throws Exception {
118 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(2);
119 notificationList.add(mRecordNoGroup);
120 notificationList.add(mRecordNoGroupSortA);
121 mHelper.sort(notificationList);
122 }
123
124 @SmallTest
125 public void testSortShouldNotThrowOneNotification() throws Exception {
126 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(1);
127 notificationList.add(mRecordNoGroup);
128 mHelper.sort(notificationList);
129 }
130
131 @SmallTest
132 public void testSortShouldNotThrowOneSortKey() throws Exception {
133 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(1);
134 notificationList.add(mRecordGroupGSortB);
135 mHelper.sort(notificationList);
136 }
137
138 @SmallTest
139 public void testSortShouldNotThrowOnEmptyList() throws Exception {
140 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>();
141 mHelper.sort(notificationList);
142 }
Chris Wren1031c972014-07-23 13:11:45 +0000143}