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