Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 com.google.common.truth.Truth.assertThat; |
| 19 | |
| 20 | import android.app.NotificationHistory; |
| 21 | import android.app.NotificationHistory.HistoricalNotification; |
| 22 | import android.graphics.drawable.Icon; |
| 23 | import android.test.suitebuilder.annotation.SmallTest; |
| 24 | |
| 25 | import androidx.test.InstrumentationRegistry; |
| 26 | import androidx.test.runner.AndroidJUnit4; |
| 27 | |
| 28 | import com.android.server.UiServiceTestCase; |
| 29 | |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | |
| 33 | @SmallTest |
| 34 | @RunWith(AndroidJUnit4.class) |
| 35 | public class NotificationHistoryFilterTest extends UiServiceTestCase { |
| 36 | |
| 37 | private HistoricalNotification getHistoricalNotification(int index) { |
| 38 | return getHistoricalNotification("package" + index, "channelId" + index, index); |
| 39 | } |
| 40 | private HistoricalNotification getHistoricalNotification(String pkg, int index) { |
| 41 | return getHistoricalNotification(pkg, "channelId" + index, index); |
| 42 | } |
| 43 | |
| 44 | private HistoricalNotification getHistoricalNotification(String packageName, String channelId, |
| 45 | int index) { |
| 46 | String expectedChannelName = "channelName" + index; |
| 47 | int expectedUid = 1123456 + index; |
| 48 | int expectedUserId = 11 + index; |
| 49 | long expectedPostTime = 987654321 + index; |
| 50 | String expectedTitle = "title" + index; |
| 51 | String expectedText = "text" + index; |
| 52 | Icon expectedIcon = Icon.createWithResource(InstrumentationRegistry.getContext(), |
| 53 | index); |
| 54 | |
| 55 | return new HistoricalNotification.Builder() |
| 56 | .setPackage(packageName) |
| 57 | .setChannelName(expectedChannelName) |
| 58 | .setChannelId(channelId) |
| 59 | .setUid(expectedUid) |
| 60 | .setUserId(expectedUserId) |
| 61 | .setPostedTimeMs(expectedPostTime) |
| 62 | .setTitle(expectedTitle) |
| 63 | .setText(expectedText) |
| 64 | .setIcon(expectedIcon) |
| 65 | .build(); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testBuilder() { |
| 70 | NotificationHistoryFilter filter = new NotificationHistoryFilter.Builder() |
| 71 | .setChannel("pkg", "channel") |
| 72 | .setMaxNotifications(3) |
| 73 | .build(); |
| 74 | |
| 75 | assertThat(filter.getPackage()).isEqualTo("pkg"); |
| 76 | assertThat(filter.getChannel()).isEqualTo("channel"); |
| 77 | assertThat(filter.getMaxNotifications()).isEqualTo(3); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testMatchesCountFilter() { |
| 82 | NotificationHistoryFilter filter = new NotificationHistoryFilter.Builder() |
| 83 | .setMaxNotifications(3) |
| 84 | .build(); |
| 85 | |
| 86 | NotificationHistory history = new NotificationHistory(); |
| 87 | assertThat(filter.matchesCountFilter(history)).isTrue(); |
| 88 | history.addNotificationToWrite(getHistoricalNotification(1)); |
| 89 | assertThat(filter.matchesCountFilter(history)).isTrue(); |
| 90 | history.addNotificationToWrite(getHistoricalNotification(2)); |
| 91 | assertThat(filter.matchesCountFilter(history)).isTrue(); |
| 92 | history.addNotificationToWrite(getHistoricalNotification(3)); |
| 93 | assertThat(filter.matchesCountFilter(history)).isFalse(); |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testMatchesCountFilter_noCountFilter() { |
| 98 | NotificationHistoryFilter filter = new NotificationHistoryFilter.Builder() |
| 99 | .build(); |
| 100 | |
| 101 | NotificationHistory history = new NotificationHistory(); |
| 102 | assertThat(filter.matchesCountFilter(history)).isTrue(); |
| 103 | history.addNotificationToWrite(getHistoricalNotification(1)); |
| 104 | assertThat(filter.matchesCountFilter(history)).isTrue(); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testMatchesPackageAndChannelFilter_pkgOnly() { |
| 109 | NotificationHistoryFilter filter = new NotificationHistoryFilter.Builder() |
| 110 | .setPackage("pkg") |
| 111 | .build(); |
| 112 | |
| 113 | HistoricalNotification hnMatches = getHistoricalNotification("pkg", 1); |
| 114 | assertThat(filter.matchesPackageAndChannelFilter(hnMatches)).isTrue(); |
| 115 | HistoricalNotification hnMatches2 = getHistoricalNotification("pkg", 2); |
| 116 | assertThat(filter.matchesPackageAndChannelFilter(hnMatches2)).isTrue(); |
| 117 | |
| 118 | HistoricalNotification hnNoMatch = getHistoricalNotification("pkg2", 2); |
| 119 | assertThat(filter.matchesPackageAndChannelFilter(hnNoMatch)).isFalse(); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testMatchesPackageAndChannelFilter_channelAlso() { |
| 124 | NotificationHistoryFilter filter = new NotificationHistoryFilter.Builder() |
| 125 | .setChannel("pkg", "channel") |
| 126 | .build(); |
| 127 | |
| 128 | HistoricalNotification hn1 = getHistoricalNotification("pkg", 1); |
| 129 | assertThat(filter.matchesPackageAndChannelFilter(hn1)).isFalse(); |
| 130 | |
| 131 | HistoricalNotification hn2 = getHistoricalNotification("pkg", "channel", 1); |
| 132 | assertThat(filter.matchesPackageAndChannelFilter(hn2)).isTrue(); |
| 133 | |
| 134 | HistoricalNotification hn3 = getHistoricalNotification("pkg2", "channel", 1); |
| 135 | assertThat(filter.matchesPackageAndChannelFilter(hn3)).isFalse(); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testIsFiltering() { |
| 140 | NotificationHistoryFilter filter = new NotificationHistoryFilter.Builder() |
| 141 | .build(); |
| 142 | assertThat(filter.isFiltering()).isFalse(); |
| 143 | |
| 144 | filter = new NotificationHistoryFilter.Builder() |
| 145 | .setPackage("pkg") |
| 146 | .build(); |
| 147 | assertThat(filter.isFiltering()).isTrue(); |
| 148 | |
| 149 | filter = new NotificationHistoryFilter.Builder() |
| 150 | .setChannel("pkg", "channel") |
| 151 | .build(); |
| 152 | assertThat(filter.isFiltering()).isTrue(); |
| 153 | |
| 154 | filter = new NotificationHistoryFilter.Builder() |
| 155 | .setMaxNotifications(5) |
| 156 | .build(); |
| 157 | assertThat(filter.isFiltering()).isTrue(); |
| 158 | } |
| 159 | } |