blob: 0f32a827377eaea2fd818d9546a5ecd2e9d7c0ec [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
Will Brockman0ed07ca2019-03-08 11:45:34 -050092 /** Verify that modifying the returned logMaker won't leave stale data behind for
93 * the next caller.*/
94 @Test
95 public void testLogMakerNoStaleData() {
96 StatusBarNotification sbn = getNotification(PKG, GROUP_ID_1, CHANNEL_ID);
97 final LogMaker logMaker = sbn.getLogMaker();
98 int extraTag = MetricsEvent.FIELD_NOTIFICATION_CHANNEL_GROUP_ID; // An arbitrary new tag
99 logMaker.addTaggedData(extraTag, 1);
100 assertNull(sbn.getLogMaker().getTaggedData(extraTag));
101 }
102
Will Brockman828427e2019-01-28 09:55:45 -0500103 @Test
104 public void testLogMakerWithCategory() {
105 Notification.Builder builder = getNotificationBuilder(GROUP_ID_1, CHANNEL_ID)
106 .setCategory(Notification.CATEGORY_MESSAGE);
107 final LogMaker logMaker = getNotification(PKG, builder).getLogMaker();
108 assertEquals(Notification.CATEGORY_MESSAGE,
109 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CATEGORY));
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200110 }
111
112 @Test
113 public void testLogMakerNoChannel() {
114 final LogMaker logMaker = getNotification(PKG, GROUP_ID_1, null).getLogMaker();
115
116 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID));
117 }
118
119 @Test
120 public void testLogMakerLongChannel() {
121 final LogMaker logMaker = getNotification(PKG, null, CHANNEL_ID_LONG).getLogMaker();
122 final String loggedId = (String) logMaker
123 .getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID);
124 assertEquals(StatusBarNotification.MAX_LOG_TAG_LENGTH, loggedId.length());
125 assertEquals(CHANNEL_ID_LONG.substring(0, 10), loggedId.substring(0, 10));
126 }
127
128 @Test
129 public void testLogMakerNoGroup() {
130 final LogMaker logMaker = getNotification(PKG, null, CHANNEL_ID).getLogMaker();
131
132 assertNull(
133 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
134 }
135
136 @Test
137 public void testLogMakerLongGroup() {
138 final LogMaker logMaker = getNotification(PKG, GROUP_ID_LONG, CHANNEL_ID)
139 .getLogMaker();
140
141 final String loggedId = (String)
142 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID);
143 assertEquals(StatusBarNotification.MAX_LOG_TAG_LENGTH, loggedId.length());
144 assertEquals(GROUP_ID_LONG.substring(0, 10), loggedId.substring(0, 10));
145 }
146
147 @Test
148 public void testLogMakerOverrideGroup() {
149 StatusBarNotification sbn = getNotification(PKG, GROUP_ID_1, CHANNEL_ID);
150 assertEquals(GROUP_ID_1,
151 sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
152
153 sbn.setOverrideGroupKey(GROUP_ID_2);
154 assertEquals(GROUP_ID_2,
155 sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
156
157 sbn.setOverrideGroupKey(null);
158 assertEquals(GROUP_ID_1,
159 sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
160 }
161
162 private StatusBarNotification getNotification(String pkg, String group, String channelId) {
Will Brockman828427e2019-01-28 09:55:45 -0500163 return getNotification(pkg, getNotificationBuilder(group, channelId));
164 }
165
166 private Notification.Builder getNotificationBuilder(String group, String channelId) {
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200167 final Notification.Builder builder = new Notification.Builder(mMockContext, channelId)
168 .setContentTitle("foo")
169 .setSmallIcon(android.R.drawable.sym_def_app_icon);
170
171 if (group != null) {
172 builder.setGroup(group);
173 }
Will Brockman828427e2019-01-28 09:55:45 -0500174 return builder;
175 }
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200176
Will Brockman828427e2019-01-28 09:55:45 -0500177 private StatusBarNotification getNotification(String pkg, Notification.Builder builder) {
178
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200179 return new StatusBarNotification(
Will Brockman828427e2019-01-28 09:55:45 -0500180 pkg, pkg, ID, TAG, UID, UID, builder.build(), USER, null, UID);
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200181 }
182
183}