blob: 1409da1a90310b654269ae99b9cabbfb3f7d6dd8 [file] [log] [blame]
Brad Stenning9a8b2c82018-08-03 14:14:26 -07001/*
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
17package com.android.server.notification;
18
19import static android.app.NotificationManager.IMPORTANCE_LOW;
20
21import static org.hamcrest.MatcherAssert.assertThat;
22import static org.hamcrest.Matchers.is;
23import static org.mockito.Mockito.spy;
24import static org.mockito.Mockito.when;
25
26import android.app.Notification;
27import android.app.NotificationChannel;
28import android.content.pm.PackageManager;
29import android.os.UserHandle;
30import android.service.notification.StatusBarNotification;
31import android.testing.TestableContext;
32
33import com.android.server.UiServiceTestCase;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.mockito.Mock;
38import org.mockito.MockitoAnnotations;
39
40public 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}