Brad Stenning | 9a8b2c8 | 2018-08-03 14:14:26 -0700 | [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; |
| 20 | |
| 21 | import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | import static org.hamcrest.Matchers.is; |
| 23 | import static org.mockito.Mockito.spy; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
| 26 | import android.app.Notification; |
| 27 | import android.app.NotificationChannel; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.os.UserHandle; |
| 30 | import android.service.notification.StatusBarNotification; |
| 31 | import android.testing.TestableContext; |
| 32 | |
| 33 | import com.android.server.UiServiceTestCase; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.mockito.Mock; |
| 38 | import org.mockito.MockitoAnnotations; |
| 39 | |
| 40 | public class CriticalNotificationExtractorTest extends UiServiceTestCase { |
| 41 | @Mock |
| 42 | private PackageManager mPackageManagerClient; |
| 43 | private TestableContext mContext = spy(getContext()); |
| 44 | private CriticalNotificationExtractor mExtractor; |
| 45 | |
| 46 | @Before |
| 47 | public void setUp() { |
| 48 | MockitoAnnotations.initMocks(this); |
| 49 | mContext.setMockPackageManager(mPackageManagerClient); |
| 50 | mExtractor = new CriticalNotificationExtractor(); |
| 51 | } |
| 52 | |
| 53 | /** confirm there is no affect on notifcations if the automotive feature flag is not set */ |
| 54 | @Test |
| 55 | public void testExtractCritically_nonsupporting() throws Exception { |
| 56 | when(mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 57 | .thenReturn(false); |
| 58 | mExtractor.initialize(mContext, null); |
| 59 | |
| 60 | assertCriticality(Notification.CATEGORY_CAR_EMERGENCY, |
| 61 | CriticalNotificationExtractor.NORMAL); |
| 62 | |
| 63 | assertCriticality(Notification.CATEGORY_CAR_WARNING, CriticalNotificationExtractor.NORMAL); |
| 64 | |
| 65 | assertCriticality(Notification.CATEGORY_CAR_INFORMATION, |
| 66 | CriticalNotificationExtractor.NORMAL); |
| 67 | |
| 68 | assertCriticality(Notification.CATEGORY_CALL, |
| 69 | CriticalNotificationExtractor.NORMAL); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void testExtractCritically() throws Exception { |
| 74 | when(mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 75 | .thenReturn(true); |
| 76 | mExtractor.initialize(mContext, null); |
| 77 | |
| 78 | assertCriticality(Notification.CATEGORY_CAR_EMERGENCY, |
| 79 | CriticalNotificationExtractor.CRITICAL); |
| 80 | |
| 81 | assertCriticality(Notification.CATEGORY_CAR_WARNING, |
| 82 | CriticalNotificationExtractor.CRITICAL_LOW); |
| 83 | |
| 84 | assertCriticality(Notification.CATEGORY_CAR_INFORMATION, |
| 85 | CriticalNotificationExtractor.NORMAL); |
| 86 | |
| 87 | assertCriticality(Notification.CATEGORY_CALL, |
| 88 | CriticalNotificationExtractor.NORMAL); |
| 89 | } |
| 90 | |
| 91 | private void assertCriticality(String cat, int criticality) { |
| 92 | NotificationRecord info = generateRecord(cat); |
| 93 | mExtractor.process(info); |
| 94 | assertThat(info.getCriticality(), is(criticality)); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | private NotificationRecord generateRecord(String category) { |
| 99 | NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_LOW); |
| 100 | final Notification.Builder builder = new Notification.Builder(getContext()) |
| 101 | .setContentTitle("foo") |
| 102 | .setCategory(category) |
| 103 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 104 | Notification n = builder.build(); |
| 105 | StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0, |
| 106 | 0, n, UserHandle.ALL, null, System.currentTimeMillis()); |
| 107 | return new NotificationRecord(getContext(), sbn, channel); |
| 108 | } |
| 109 | } |