1 /* 2 * Copyright (C) 2017 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 17 package com.android.documentsui.services; 18 19 import static junit.framework.Assert.assertEquals; 20 import static junit.framework.Assert.assertNotNull; 21 import static junit.framework.Assert.assertTrue; 22 23 import android.app.Notification; 24 import android.app.NotificationManager; 25 import android.util.SparseArray; 26 27 import org.mockito.Mockito; 28 import org.mockito.invocation.InvocationOnMock; 29 import org.mockito.stubbing.Answer; 30 31 import java.util.HashMap; 32 33 class TestNotificationManager { 34 35 private final TestForegroundManager mForegroundManager; 36 private final SparseArray<HashMap<String, Notification>> mNotifications = new SparseArray<>(); 37 private final Answer<Void> mAnswer = this::invoke; 38 39 TestNotificationManager(TestForegroundManager foregroundManager) { 40 assert(foregroundManager != null); 41 mForegroundManager = foregroundManager; 42 } 43 44 private void notify(String tag, int id, Notification notification) { 45 if (notification == mForegroundManager.getForegroundNotification() 46 && id != mForegroundManager.getForegroundId()) { 47 throw new IllegalStateException("Mismatching ID and notification."); 48 } 49 50 if (mNotifications.get(id) == null) { 51 mNotifications.put(id, new HashMap<>()); 52 } 53 54 mNotifications.get(id).put(tag, notification); 55 } 56 57 private void cancel(String tag, int id) { 58 final HashMap<String, Notification> idMap = mNotifications.get(id); 59 if (idMap != null && idMap.containsKey(tag)) { 60 final Notification notification = idMap.get(tag); 61 // Only cancel non-foreground notification 62 if (mForegroundManager.getForegroundNotification() != notification) { 63 idMap.remove(tag); 64 } 65 } 66 } 67 68 private Void invoke(InvocationOnMock invocation) { 69 Object[] args = invocation.getArguments(); 70 switch (invocation.getMethod().getName()) { 71 case "notify": 72 if (args.length == 2) { 73 notify(null, (Integer) args[0], (Notification) args[1]); 74 } 75 if (args.length == 3) { 76 notify((String) args[0], (Integer) args[1], (Notification) args[2]); 77 } 78 break; 79 case "cancel": 80 if (args.length == 1) { 81 cancel(null, (Integer) args[0]); 82 } 83 if (args.length == 2) { 84 cancel((String) args[0], (Integer) args[1]); 85 } 86 break; 87 } 88 return null; 89 } 90 91 NotificationManager createNotificationManager() { 92 return Mockito.mock(NotificationManager.class, mAnswer); 93 } 94 95 void assertNumberOfNotifications(int expect) { 96 int count = 0; 97 for (int i = 0; i < mNotifications.size(); ++i) { 98 count += mNotifications.valueAt(i).size(); 99 } 100 101 assertEquals(expect, count); 102 } 103 } 104