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 static org.mockito.ArgumentMatchers.any; |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.anyInt; |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 22 | import static org.mockito.ArgumentMatchers.anyLong; |
| 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.never; |
| 25 | import static org.mockito.Mockito.times; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.when; |
| 28 | |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 29 | import android.app.AlarmManager; |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 30 | import android.app.NotificationHistory; |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 31 | import android.app.NotificationHistory.HistoricalNotification; |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 32 | import android.content.Context; |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 33 | import android.graphics.drawable.Icon; |
| 34 | import android.os.Handler; |
| 35 | import android.util.AtomicFile; |
| 36 | |
| 37 | import androidx.test.InstrumentationRegistry; |
| 38 | import androidx.test.runner.AndroidJUnit4; |
| 39 | |
| 40 | import com.android.server.UiServiceTestCase; |
| 41 | |
| 42 | import org.junit.Before; |
| 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | import org.mockito.Mock; |
| 46 | import org.mockito.MockitoAnnotations; |
| 47 | |
| 48 | import java.io.File; |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 49 | import java.util.ArrayList; |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 50 | import java.util.Calendar; |
| 51 | import java.util.GregorianCalendar; |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 52 | import java.util.HashMap; |
| 53 | import java.util.List; |
| 54 | import java.util.Map; |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 55 | |
| 56 | @RunWith(AndroidJUnit4.class) |
| 57 | public class NotificationHistoryDatabaseTest extends UiServiceTestCase { |
| 58 | |
| 59 | File mRootDir; |
| 60 | @Mock |
| 61 | Handler mFileWriteHandler; |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 62 | @Mock |
| 63 | Context mContext; |
| 64 | @Mock |
| 65 | AlarmManager mAlarmManager; |
| 66 | TestFileAttrProvider mFileAttrProvider; |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 67 | |
| 68 | NotificationHistoryDatabase mDataBase; |
| 69 | |
| 70 | private HistoricalNotification getHistoricalNotification(int index) { |
| 71 | return getHistoricalNotification("package" + index, index); |
| 72 | } |
| 73 | |
| 74 | private HistoricalNotification getHistoricalNotification(String packageName, int index) { |
| 75 | String expectedChannelName = "channelName" + index; |
| 76 | String expectedChannelId = "channelId" + index; |
| 77 | int expectedUid = 1123456 + index; |
| 78 | int expectedUserId = 11 + index; |
| 79 | long expectedPostTime = 987654321 + index; |
| 80 | String expectedTitle = "title" + index; |
| 81 | String expectedText = "text" + index; |
| 82 | Icon expectedIcon = Icon.createWithResource(InstrumentationRegistry.getContext(), |
| 83 | index); |
| 84 | |
| 85 | return new HistoricalNotification.Builder() |
| 86 | .setPackage(packageName) |
| 87 | .setChannelName(expectedChannelName) |
| 88 | .setChannelId(expectedChannelId) |
| 89 | .setUid(expectedUid) |
| 90 | .setUserId(expectedUserId) |
| 91 | .setPostedTimeMs(expectedPostTime) |
| 92 | .setTitle(expectedTitle) |
| 93 | .setText(expectedText) |
| 94 | .setIcon(expectedIcon) |
| 95 | .build(); |
| 96 | } |
| 97 | |
| 98 | @Before |
| 99 | public void setUp() { |
| 100 | MockitoAnnotations.initMocks(this); |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 101 | when(mContext.getSystemService(AlarmManager.class)).thenReturn(mAlarmManager); |
| 102 | when(mContext.getUser()).thenReturn(getContext().getUser()); |
| 103 | when(mContext.getPackageName()).thenReturn(getContext().getPackageName()); |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 104 | |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 105 | mFileAttrProvider = new TestFileAttrProvider(); |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 106 | mRootDir = new File(mContext.getFilesDir(), "NotificationHistoryDatabaseTest"); |
| 107 | |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 108 | mDataBase = new NotificationHistoryDatabase( |
| 109 | mContext, mFileWriteHandler, mRootDir, mFileAttrProvider); |
| 110 | mDataBase.init(); |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Test |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 114 | public void testDeletionReceiver() { |
| 115 | verify(mContext, times(1)).registerReceiver(any(), any()); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testPrune() throws Exception { |
| 120 | GregorianCalendar cal = new GregorianCalendar(); |
| 121 | cal.setTimeInMillis(10); |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 122 | int retainDays = 1; |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 123 | |
| 124 | List<AtomicFile> expectedFiles = new ArrayList<>(); |
| 125 | |
| 126 | // add 5 files with a creation date of "today" |
| 127 | for (long i = cal.getTimeInMillis(); i >= 5; i--) { |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 128 | File file = mock(File.class); |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 129 | mFileAttrProvider.creationDates.put(file, i); |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 130 | AtomicFile af = new AtomicFile(file); |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 131 | expectedFiles.add(af); |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 132 | mDataBase.mHistoryFiles.addLast(af); |
| 133 | } |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 134 | |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 135 | cal.add(Calendar.DATE, -1 * retainDays); |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 136 | // Add 5 more files more than retainDays old |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 137 | for (int i = 5; i >= 0; i--) { |
| 138 | File file = mock(File.class); |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 139 | mFileAttrProvider.creationDates.put(file, cal.getTimeInMillis() - i); |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 140 | AtomicFile af = new AtomicFile(file); |
| 141 | mDataBase.mHistoryFiles.addLast(af); |
| 142 | } |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 143 | |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 144 | // back to today; trim everything a day + old |
| 145 | cal.add(Calendar.DATE, 1 * retainDays); |
| 146 | mDataBase.prune(retainDays, cal.getTimeInMillis()); |
| 147 | |
| 148 | assertThat(mDataBase.mHistoryFiles).containsExactlyElementsIn(expectedFiles); |
| 149 | |
| 150 | verify(mAlarmManager, times(6)).setExactAndAllowWhileIdle(anyInt(), anyLong(), any()); |
| 151 | |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void testOnPackageRemove_posts() { |
| 156 | mDataBase.onPackageRemoved("test"); |
| 157 | verify(mFileWriteHandler, times(1)).post(any()); |
| 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void testForceWriteToDisk() { |
| 162 | mDataBase.forceWriteToDisk(); |
| 163 | verify(mFileWriteHandler, times(1)).post(any()); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void testOnlyOneWriteRunnableInQueue() { |
| 168 | when(mFileWriteHandler.hasCallbacks(any())).thenReturn(true); |
| 169 | mDataBase.forceWriteToDisk(); |
| 170 | verify(mFileWriteHandler, never()).post(any()); |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | public void testAddNotification() { |
| 175 | HistoricalNotification n = getHistoricalNotification(1); |
| 176 | HistoricalNotification n2 = getHistoricalNotification(2); |
| 177 | |
| 178 | mDataBase.addNotification(n); |
| 179 | assertThat(mDataBase.mBuffer.getNotificationsToWrite()).contains(n); |
| 180 | verify(mFileWriteHandler, times(1)).postDelayed(any(), anyLong()); |
| 181 | |
| 182 | // second add should not trigger another write |
| 183 | mDataBase.addNotification(n2); |
| 184 | assertThat(mDataBase.mBuffer.getNotificationsToWrite()).contains(n2); |
| 185 | verify(mFileWriteHandler, times(1)).postDelayed(any(), anyLong()); |
| 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void testReadNotificationHistory_readsAllFiles() throws Exception { |
| 190 | for (long i = 10; i >= 5; i--) { |
| 191 | AtomicFile af = mock(AtomicFile.class); |
| 192 | mDataBase.mHistoryFiles.addLast(af); |
| 193 | } |
| 194 | |
| 195 | mDataBase.readNotificationHistory(); |
| 196 | |
| 197 | for (AtomicFile file : mDataBase.mHistoryFiles) { |
| 198 | verify(file, times(1)).openRead(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | @Test |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 203 | public void testReadNotificationHistory_readsBuffer() throws Exception { |
| 204 | HistoricalNotification hn = getHistoricalNotification(1); |
| 205 | mDataBase.addNotification(hn); |
| 206 | |
| 207 | NotificationHistory nh = mDataBase.readNotificationHistory(); |
| 208 | |
| 209 | assertThat(nh.getNotificationsToWrite()).contains(hn); |
| 210 | } |
| 211 | |
| 212 | @Test |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 213 | public void testReadNotificationHistory_withNumFilterDoesNotReadExtraFiles() throws Exception { |
| 214 | AtomicFile af = mock(AtomicFile.class); |
| 215 | when(af.getBaseFile()).thenReturn(new File(mRootDir, "af")); |
| 216 | mDataBase.mHistoryFiles.addLast(af); |
| 217 | |
| 218 | AtomicFile af2 = mock(AtomicFile.class); |
| 219 | when(af2.getBaseFile()).thenReturn(new File(mRootDir, "af2")); |
| 220 | mDataBase.mHistoryFiles.addLast(af2); |
| 221 | |
| 222 | mDataBase.readNotificationHistory(null, null, 0); |
| 223 | |
| 224 | verify(af, times(1)).openRead(); |
| 225 | verify(af2, never()).openRead(); |
| 226 | } |
| 227 | |
Julia Reynolds | 96951fc | 2019-11-13 16:04:33 -0500 | [diff] [blame] | 228 | private class TestFileAttrProvider implements NotificationHistoryDatabase.FileAttrProvider { |
| 229 | public Map<File, Long> creationDates = new HashMap<>(); |
| 230 | |
| 231 | @Override |
| 232 | public long getCreationTime(File file) { |
| 233 | return creationDates.get(file); |
| 234 | } |
| 235 | } |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 236 | } |