blob: c99777b160f07a9bfd0ff50f2ce214526f532275 [file] [log] [blame]
Eyal Posenera9cf9c72018-12-18 16:23:54 +02001/*
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 */
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090016
Eyal Posenera9cf9c72018-12-18 16:23:54 +020017package android.service.notification;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertNull;
21
22import static org.mockito.Mockito.mock;
23import static org.mockito.Mockito.when;
24
25import android.app.ActivityManager;
26import android.app.Notification;
27import android.content.Context;
28import android.content.pm.ApplicationInfo;
29import android.content.pm.PackageManager;
30import android.metrics.LogMaker;
31import android.os.UserHandle;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090032
33import androidx.test.InstrumentationRegistry;
34import androidx.test.filters.SmallTest;
35import androidx.test.runner.AndroidJUnit4;
Eyal Posenera9cf9c72018-12-18 16:23:54 +020036
37import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
38
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.mockito.Mock;
43import org.mockito.MockitoAnnotations;
44
45@RunWith(AndroidJUnit4.class)
46@SmallTest
47public class StatusBarNotificationTest {
48
49 private final Context mMockContext = mock(Context.class);
50 @Mock
51 private PackageManager mPm;
52
53 private static final String PKG = "com.example.o";
54 private static final int UID = 9583;
55 private static final int ID = 1;
56 private static final String TAG = "tag1";
57 private static final String CHANNEL_ID = "channel";
58 private static final String CHANNEL_ID_LONG =
59 "give_a_developer_a_string_argument_and_who_knows_what_they_will_pass_in_there";
60 private static final String GROUP_ID_1 = "group1";
61 private static final String GROUP_ID_2 = "group2";
62 private static final String GROUP_ID_LONG =
63 "0|com.foo.bar|g:content://com.foo.bar.ui/account%3A-0000000/account/";
64 private static final android.os.UserHandle USER =
65 UserHandle.of(ActivityManager.getCurrentUser());
66
67 @Before
68 public void setUp() {
69 MockitoAnnotations.initMocks(this);
70
71 when(mMockContext.getResources()).thenReturn(
72 InstrumentationRegistry.getContext().getResources());
73 when(mMockContext.getPackageManager()).thenReturn(mPm);
74 when(mMockContext.getApplicationInfo()).thenReturn(new ApplicationInfo());
75 }
76
77 @Test
78 public void testLogMaker() {
79 final LogMaker logMaker = getNotification(PKG, GROUP_ID_1, CHANNEL_ID).getLogMaker();
Eyal Posenera9cf9c72018-12-18 16:23:54 +020080 assertEquals(CHANNEL_ID,
81 (String) logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID));
82 assertEquals(PKG, logMaker.getPackageName());
83 assertEquals(ID, logMaker.getTaggedData(MetricsEvent.NOTIFICATION_ID));
84 assertEquals(TAG, logMaker.getTaggedData(MetricsEvent.NOTIFICATION_TAG));
85 assertEquals(GROUP_ID_1,
86 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
Will Brockman828427e2019-01-28 09:55:45 -050087 assertEquals(0,
88 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY));
89 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CATEGORY));
90 }
91
92 @Test
93 public void testLogMakerWithCategory() {
94 Notification.Builder builder = getNotificationBuilder(GROUP_ID_1, CHANNEL_ID)
95 .setCategory(Notification.CATEGORY_MESSAGE);
96 final LogMaker logMaker = getNotification(PKG, builder).getLogMaker();
97 assertEquals(Notification.CATEGORY_MESSAGE,
98 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CATEGORY));
Eyal Posenera9cf9c72018-12-18 16:23:54 +020099 }
100
101 @Test
102 public void testLogMakerNoChannel() {
103 final LogMaker logMaker = getNotification(PKG, GROUP_ID_1, null).getLogMaker();
104
105 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID));
106 }
107
108 @Test
109 public void testLogMakerLongChannel() {
110 final LogMaker logMaker = getNotification(PKG, null, CHANNEL_ID_LONG).getLogMaker();
111 final String loggedId = (String) logMaker
112 .getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID);
113 assertEquals(StatusBarNotification.MAX_LOG_TAG_LENGTH, loggedId.length());
114 assertEquals(CHANNEL_ID_LONG.substring(0, 10), loggedId.substring(0, 10));
115 }
116
117 @Test
118 public void testLogMakerNoGroup() {
119 final LogMaker logMaker = getNotification(PKG, null, CHANNEL_ID).getLogMaker();
120
121 assertNull(
122 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
123 }
124
125 @Test
126 public void testLogMakerLongGroup() {
127 final LogMaker logMaker = getNotification(PKG, GROUP_ID_LONG, CHANNEL_ID)
128 .getLogMaker();
129
130 final String loggedId = (String)
131 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID);
132 assertEquals(StatusBarNotification.MAX_LOG_TAG_LENGTH, loggedId.length());
133 assertEquals(GROUP_ID_LONG.substring(0, 10), loggedId.substring(0, 10));
134 }
135
136 @Test
137 public void testLogMakerOverrideGroup() {
138 StatusBarNotification sbn = getNotification(PKG, GROUP_ID_1, CHANNEL_ID);
139 assertEquals(GROUP_ID_1,
140 sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
141
142 sbn.setOverrideGroupKey(GROUP_ID_2);
143 assertEquals(GROUP_ID_2,
144 sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
145
146 sbn.setOverrideGroupKey(null);
147 assertEquals(GROUP_ID_1,
148 sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
149 }
150
151 private StatusBarNotification getNotification(String pkg, String group, String channelId) {
Will Brockman828427e2019-01-28 09:55:45 -0500152 return getNotification(pkg, getNotificationBuilder(group, channelId));
153 }
154
155 private Notification.Builder getNotificationBuilder(String group, String channelId) {
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200156 final Notification.Builder builder = new Notification.Builder(mMockContext, channelId)
157 .setContentTitle("foo")
158 .setSmallIcon(android.R.drawable.sym_def_app_icon);
159
160 if (group != null) {
161 builder.setGroup(group);
162 }
Will Brockman828427e2019-01-28 09:55:45 -0500163 return builder;
164 }
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200165
Will Brockman828427e2019-01-28 09:55:45 -0500166 private StatusBarNotification getNotification(String pkg, Notification.Builder builder) {
167
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200168 return new StatusBarNotification(
Will Brockman828427e2019-01-28 09:55:45 -0500169 pkg, pkg, ID, TAG, UID, UID, builder.build(), USER, null, UID);
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200170 }
171
172}