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