Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.server.notification; |
| 18 | |
| 19 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 20 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; |
| 21 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 22 | |
| 23 | import static junit.framework.Assert.assertEquals; |
| 24 | import static junit.framework.Assert.assertFalse; |
| 25 | import static junit.framework.Assert.assertTrue; |
| 26 | |
| 27 | import static org.mockito.ArgumentMatchers.any; |
| 28 | import static org.mockito.Mockito.when; |
| 29 | |
| 30 | import android.app.Notification; |
| 31 | import android.app.NotificationChannel; |
| 32 | import android.app.NotificationManager; |
| 33 | import android.os.UserHandle; |
| 34 | import android.service.notification.StatusBarNotification; |
| 35 | |
| 36 | import com.android.server.UiServiceTestCase; |
| 37 | |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.mockito.Mock; |
| 41 | import org.mockito.MockitoAnnotations; |
| 42 | |
| 43 | public class ZenModeExtractorTest extends UiServiceTestCase { |
| 44 | |
| 45 | @Mock |
| 46 | ZenModeHelper mZenModeHelper; |
| 47 | |
| 48 | @Before |
| 49 | public void setUp() { |
| 50 | MockitoAnnotations.initMocks(this); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testExtractIntercepted() { |
| 55 | ZenModeExtractor extractor = new ZenModeExtractor(); |
| 56 | extractor.setZenHelper(mZenModeHelper); |
| 57 | NotificationRecord r = generateRecord(); |
| 58 | |
| 59 | assertFalse(r.isIntercepted()); |
| 60 | |
| 61 | when(mZenModeHelper.shouldIntercept(any())).thenReturn(true); |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 62 | when(mZenModeHelper.getConsolidatedNotificationPolicy()).thenReturn( |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 63 | new NotificationManager.Policy(0,0,0)); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 64 | |
| 65 | extractor.process(r); |
| 66 | |
| 67 | assertTrue(r.isIntercepted()); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testExtractVisualDisturbancesNotIntercepted() { |
| 72 | ZenModeExtractor extractor = new ZenModeExtractor(); |
| 73 | extractor.setZenHelper(mZenModeHelper); |
| 74 | NotificationRecord r = generateRecord(); |
| 75 | |
| 76 | when(mZenModeHelper.shouldIntercept(any())).thenReturn(false); |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 77 | when(mZenModeHelper.getConsolidatedNotificationPolicy()).thenReturn( |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 78 | new NotificationManager.Policy(0,0,0)); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 79 | |
| 80 | extractor.process(r); |
| 81 | |
| 82 | assertEquals(0, r.getSuppressedVisualEffects()); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testExtractVisualDisturbancesIntercepted() { |
| 87 | ZenModeExtractor extractor = new ZenModeExtractor(); |
| 88 | extractor.setZenHelper(mZenModeHelper); |
| 89 | NotificationRecord r = generateRecord(); |
| 90 | |
| 91 | when(mZenModeHelper.shouldIntercept(any())).thenReturn(true); |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 92 | when(mZenModeHelper.getConsolidatedNotificationPolicy()).thenReturn( |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 93 | new NotificationManager.Policy(0,0,0, SUPPRESSED_EFFECT_PEEK |
| 94 | | SUPPRESSED_EFFECT_NOTIFICATION_LIST)); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 95 | |
| 96 | extractor.process(r); |
| 97 | |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 98 | assertEquals(NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK |
| 99 | | NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST, |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 100 | r.getSuppressedVisualEffects()); |
| 101 | } |
| 102 | |
| 103 | private NotificationRecord generateRecord() { |
| 104 | NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_LOW); |
| 105 | final Notification.Builder builder = new Notification.Builder(getContext()) |
| 106 | .setContentTitle("foo") |
| 107 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 108 | Notification n = builder.build(); |
| 109 | StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0, |
| 110 | 0, n, UserHandle.ALL, null, System.currentTimeMillis()); |
| 111 | return new NotificationRecord(getContext(), sbn, channel); |
| 112 | } |
| 113 | } |