blob: 5041779840b3459292759b41aecf261e579b3537 [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;
Julia Reynoldsfeb73412017-04-18 09:28:22 -040019
20import android.app.Notification;
21import android.app.NotificationChannel;
22import android.app.NotificationManager;
23import android.os.UserHandle;
24import android.service.notification.StatusBarNotification;
Julia Reynoldsfeb73412017-04-18 09:28:22 -040025import android.test.suitebuilder.annotation.SmallTest;
26
Brett Chabot84151d92019-02-27 15:37:59 -080027import androidx.test.runner.AndroidJUnit4;
28
Jason Monk74f5e362017-12-06 08:56:33 -050029import com.android.server.UiServiceTestCase;
30
Julia Reynoldsfeb73412017-04-18 09:28:22 -040031import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import java.util.ArrayList;
35import java.util.Collections;
36import java.util.List;
37
38@SmallTest
39@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050040public class GlobalSortKeyComparatorTest extends UiServiceTestCase {
Julia Reynoldsfeb73412017-04-18 09:28:22 -040041
42 private final String PKG = "PKG";
43 private final int UID = 1111111;
44 private static final String TEST_CHANNEL_ID = "test_channel_id";
45
46 @Test
47 public void testComparator() throws Exception {
Chris Wren89aa2262017-05-05 18:05:56 -040048 Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID)
Julia Reynoldsfeb73412017-04-18 09:28:22 -040049 .build();
Chris Wren89aa2262017-05-05 18:05:56 -040050 NotificationRecord left = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -040051 new StatusBarNotification(PKG,
52 PKG, 1, "media", UID, UID, n,
53 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000054 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -040055 left.setGlobalSortKey("first");
56
Chris Wren89aa2262017-05-05 18:05:56 -040057 NotificationRecord right = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -040058 new StatusBarNotification(PKG,
59 PKG, 1, "media", UID, UID, n,
60 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000061 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -040062 right.setGlobalSortKey("second");
63
Chris Wren89aa2262017-05-05 18:05:56 -040064 NotificationRecord last = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -040065 new StatusBarNotification(PKG,
66 PKG, 1, "media", UID, UID, n,
67 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000068 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -040069
70
71 final List<NotificationRecord> expected = new ArrayList<>();
72 expected.add(left);
73 expected.add(right);
74 expected.add(last);
75
76 List<NotificationRecord> actual = new ArrayList<>();
77 actual.addAll(expected);
78 Collections.shuffle(actual);
79
80 Collections.sort(actual, new GlobalSortKeyComparator());
81
82 assertEquals(expected, actual);
83 }
84
85 @Test
86 public void testNoCrash_leftNull() throws Exception {
Chris Wren89aa2262017-05-05 18:05:56 -040087 Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID)
Julia Reynoldsfeb73412017-04-18 09:28:22 -040088 .build();
Chris Wren89aa2262017-05-05 18:05:56 -040089 NotificationRecord left = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -040090 new StatusBarNotification(PKG,
91 PKG, 1, "media", UID, UID, n,
92 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000093 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -040094
Chris Wren89aa2262017-05-05 18:05:56 -040095 NotificationRecord right = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -040096 new StatusBarNotification(PKG,
97 PKG, 1, "media", UID, UID, n,
98 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000099 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400100 right.setGlobalSortKey("not null");
101
102 final List<NotificationRecord> expected = new ArrayList<>();
103 expected.add(right);
104 expected.add(left);
105
106 List<NotificationRecord> actual = new ArrayList<>();
107 actual.addAll(expected);
108 Collections.shuffle(actual);
109
110 Collections.sort(actual, new GlobalSortKeyComparator());
111
112 assertEquals(expected, actual);
113 }
114
115 @Test
116 public void testNoCrash_rightNull() throws Exception {
Chris Wren89aa2262017-05-05 18:05:56 -0400117 Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID)
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400118 .build();
Chris Wren89aa2262017-05-05 18:05:56 -0400119 NotificationRecord left = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400120 new StatusBarNotification(PKG,
121 PKG, 1, "media", UID, UID, n,
122 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000123 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400124 left.setGlobalSortKey("not null");
125
Chris Wren89aa2262017-05-05 18:05:56 -0400126 NotificationRecord right = new NotificationRecord(getContext(),
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400127 new StatusBarNotification(PKG,
128 PKG, 1, "media", UID, UID, n,
129 new UserHandle(UserHandle.myUserId()),
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000130 "", 1499), getDefaultChannel());
Julia Reynoldsfeb73412017-04-18 09:28:22 -0400131
132 final List<NotificationRecord> expected = new ArrayList<>();
133 expected.add(left);
134 expected.add(right);
135
136 List<NotificationRecord> actual = new ArrayList<>();
137 actual.addAll(expected);
138 Collections.shuffle(actual);
139
140 Collections.sort(actual, new GlobalSortKeyComparator());
141
142 assertEquals(expected, actual);
143 }
144
145 private NotificationChannel getDefaultChannel() {
146 return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name",
147 NotificationManager.IMPORTANCE_LOW);
148 }
149}