Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.server.notification; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 19 | |
| 20 | import android.app.Notification; |
| 21 | import android.app.NotificationChannel; |
| 22 | import android.app.NotificationManager; |
| 23 | import android.os.UserHandle; |
| 24 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 25 | import android.test.suitebuilder.annotation.SmallTest; |
| 26 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 27 | import androidx.test.runner.AndroidJUnit4; |
| 28 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 29 | import com.android.server.UiServiceTestCase; |
| 30 | |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.Collections; |
| 36 | import java.util.List; |
| 37 | |
| 38 | @SmallTest |
| 39 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 40 | public class GlobalSortKeyComparatorTest extends UiServiceTestCase { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 41 | |
| 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 Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 48 | Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID) |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 49 | .build(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 50 | NotificationRecord left = new NotificationRecord(getContext(), |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 51 | new StatusBarNotification(PKG, |
| 52 | PKG, 1, "media", UID, UID, n, |
| 53 | new UserHandle(UserHandle.myUserId()), |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 54 | "", 1499), getDefaultChannel()); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 55 | left.setGlobalSortKey("first"); |
| 56 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 57 | NotificationRecord right = new NotificationRecord(getContext(), |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 58 | new StatusBarNotification(PKG, |
| 59 | PKG, 1, "media", UID, UID, n, |
| 60 | new UserHandle(UserHandle.myUserId()), |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 61 | "", 1499), getDefaultChannel()); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 62 | right.setGlobalSortKey("second"); |
| 63 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 64 | NotificationRecord last = new NotificationRecord(getContext(), |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 65 | new StatusBarNotification(PKG, |
| 66 | PKG, 1, "media", UID, UID, n, |
| 67 | new UserHandle(UserHandle.myUserId()), |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 68 | "", 1499), getDefaultChannel()); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 69 | |
| 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 Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 87 | Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID) |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 88 | .build(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 89 | NotificationRecord left = new NotificationRecord(getContext(), |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 90 | new StatusBarNotification(PKG, |
| 91 | PKG, 1, "media", UID, UID, n, |
| 92 | new UserHandle(UserHandle.myUserId()), |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 93 | "", 1499), getDefaultChannel()); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 94 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 95 | NotificationRecord right = new NotificationRecord(getContext(), |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 96 | new StatusBarNotification(PKG, |
| 97 | PKG, 1, "media", UID, UID, n, |
| 98 | new UserHandle(UserHandle.myUserId()), |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 99 | "", 1499), getDefaultChannel()); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 100 | 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 Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 117 | Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID) |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 118 | .build(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 119 | NotificationRecord left = new NotificationRecord(getContext(), |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 120 | new StatusBarNotification(PKG, |
| 121 | PKG, 1, "media", UID, UID, n, |
| 122 | new UserHandle(UserHandle.myUserId()), |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 123 | "", 1499), getDefaultChannel()); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 124 | left.setGlobalSortKey("not null"); |
| 125 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 126 | NotificationRecord right = new NotificationRecord(getContext(), |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 127 | new StatusBarNotification(PKG, |
| 128 | PKG, 1, "media", UID, UID, n, |
| 129 | new UserHandle(UserHandle.myUserId()), |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 130 | "", 1499), getDefaultChannel()); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 131 | |
| 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 | } |