blob: 97f21040014186a714cb234f395677daaceafcb9 [file] [log] [blame]
Julia Reynoldsfeb73412017-04-18 09:28:22 -04001/*
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 */
16package com.android.server.notification;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.Matchers.eq;
22
23import android.app.Notification;
24import android.app.NotificationChannel;
25import android.app.NotificationManager;
26import android.os.UserHandle;
27import android.service.notification.StatusBarNotification;
Julia Reynoldsfeb73412017-04-18 09:28:22 -040028import android.support.test.runner.AndroidJUnit4;
29import android.test.suitebuilder.annotation.SmallTest;
30
Jason Monk74f5e362017-12-06 08:56:33 -050031import com.android.server.UiServiceTestCase;
32
Julia Reynoldsfeb73412017-04-18 09:28:22 -040033import org.junit.Test;
34import org.junit.runner.RunWith;
35
36import java.util.ArrayList;
37import java.util.Collections;
38import java.util.List;
39
40@SmallTest
41@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050042public class GlobalSortKeyComparatorTest extends UiServiceTestCase {
Julia Reynoldsfeb73412017-04-18 09:28:22 -040043
44 private final String PKG = "PKG";
45 private final int UID = 1111111;
46 private static final String TEST_CHANNEL_ID = "test_channel_id";
47
48 @Test
49 public void testComparator() throws Exception {
Chris Wren89aa2262017-05-05 18:05:56 -040050 Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID)
Julia Reynoldsfeb73412017-04-18 09:28:22 -040051 .build();
Chris Wren89aa2262017-05-05 18:05:56 -040052 NotificationRecord left = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -040053 new StatusBarNotification(PKG,
54 PKG, 1, "media", UID, UID, n,
55 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000056 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -040057 left.setGlobalSortKey("first");
58
Chris Wren89aa2262017-05-05 18:05:56 -040059 NotificationRecord right = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -040060 new StatusBarNotification(PKG,
61 PKG, 1, "media", UID, UID, n,
62 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000063 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -040064 right.setGlobalSortKey("second");
65
Chris Wren89aa2262017-05-05 18:05:56 -040066 NotificationRecord last = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -040067 new StatusBarNotification(PKG,
68 PKG, 1, "media", UID, UID, n,
69 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000070 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -040071
72
73 final List<NotificationRecord> expected = new ArrayList<>();
74 expected.add(left);
75 expected.add(right);
76 expected.add(last);
77
78 List<NotificationRecord> actual = new ArrayList<>();
79 actual.addAll(expected);
80 Collections.shuffle(actual);
81
82 Collections.sort(actual, new GlobalSortKeyComparator());
83
84 assertEquals(expected, actual);
85 }
86
87 @Test
88 public void testNoCrash_leftNull() throws Exception {
Chris Wren89aa2262017-05-05 18:05:56 -040089 Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID)
Julia Reynoldsfeb73412017-04-18 09:28:22 -040090 .build();
Chris Wren89aa2262017-05-05 18:05:56 -040091 NotificationRecord left = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -040092 new StatusBarNotification(PKG,
93 PKG, 1, "media", UID, UID, n,
94 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000095 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -040096
Chris Wren89aa2262017-05-05 18:05:56 -040097 NotificationRecord right = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -040098 new StatusBarNotification(PKG,
99 PKG, 1, "media", UID, UID, n,
100 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000101 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400102 right.setGlobalSortKey("not null");
103
104 final List<NotificationRecord> expected = new ArrayList<>();
105 expected.add(right);
106 expected.add(left);
107
108 List<NotificationRecord> actual = new ArrayList<>();
109 actual.addAll(expected);
110 Collections.shuffle(actual);
111
112 Collections.sort(actual, new GlobalSortKeyComparator());
113
114 assertEquals(expected, actual);
115 }
116
117 @Test
118 public void testNoCrash_rightNull() throws Exception {
Chris Wren89aa2262017-05-05 18:05:56 -0400119 Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID)
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400120 .build();
Chris Wren89aa2262017-05-05 18:05:56 -0400121 NotificationRecord left = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400122 new StatusBarNotification(PKG,
123 PKG, 1, "media", UID, UID, n,
124 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000125 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400126 left.setGlobalSortKey("not null");
127
Chris Wren89aa2262017-05-05 18:05:56 -0400128 NotificationRecord right = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400129 new StatusBarNotification(PKG,
130 PKG, 1, "media", UID, UID, n,
131 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000132 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400133
134 final List<NotificationRecord> expected = new ArrayList<>();
135 expected.add(left);
136 expected.add(right);
137
138 List<NotificationRecord> actual = new ArrayList<>();
139 actual.addAll(expected);
140 Collections.shuffle(actual);
141
142 Collections.sort(actual, new GlobalSortKeyComparator());
143
144 assertEquals(expected, actual);
145 }
146
147 private NotificationChannel getDefaultChannel() {
148 return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name",
149 NotificationManager.IMPORTANCE_LOW);
150 }
151}