blob: 4aa10007bf533c3afa8391f77e15dad30b1a1446 [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();
80
81 assertEquals(CHANNEL_ID,
82 (String) logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID));
83 assertEquals(PKG, logMaker.getPackageName());
84 assertEquals(ID, logMaker.getTaggedData(MetricsEvent.NOTIFICATION_ID));
85 assertEquals(TAG, logMaker.getTaggedData(MetricsEvent.NOTIFICATION_TAG));
86 assertEquals(GROUP_ID_1,
87 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
88 }
89
90 @Test
91 public void testLogMakerNoChannel() {
92 final LogMaker logMaker = getNotification(PKG, GROUP_ID_1, null).getLogMaker();
93
94 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID));
95 }
96
97 @Test
98 public void testLogMakerLongChannel() {
99 final LogMaker logMaker = getNotification(PKG, null, CHANNEL_ID_LONG).getLogMaker();
100 final String loggedId = (String) logMaker
101 .getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID);
102 assertEquals(StatusBarNotification.MAX_LOG_TAG_LENGTH, loggedId.length());
103 assertEquals(CHANNEL_ID_LONG.substring(0, 10), loggedId.substring(0, 10));
104 }
105
106 @Test
107 public void testLogMakerNoGroup() {
108 final LogMaker logMaker = getNotification(PKG, null, CHANNEL_ID).getLogMaker();
109
110 assertNull(
111 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
112 }
113
114 @Test
115 public void testLogMakerLongGroup() {
116 final LogMaker logMaker = getNotification(PKG, GROUP_ID_LONG, CHANNEL_ID)
117 .getLogMaker();
118
119 final String loggedId = (String)
120 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID);
121 assertEquals(StatusBarNotification.MAX_LOG_TAG_LENGTH, loggedId.length());
122 assertEquals(GROUP_ID_LONG.substring(0, 10), loggedId.substring(0, 10));
123 }
124
125 @Test
126 public void testLogMakerOverrideGroup() {
127 StatusBarNotification sbn = getNotification(PKG, GROUP_ID_1, CHANNEL_ID);
128 assertEquals(GROUP_ID_1,
129 sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
130
131 sbn.setOverrideGroupKey(GROUP_ID_2);
132 assertEquals(GROUP_ID_2,
133 sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
134
135 sbn.setOverrideGroupKey(null);
136 assertEquals(GROUP_ID_1,
137 sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
138 }
139
140 private StatusBarNotification getNotification(String pkg, String group, String channelId) {
141 final Notification.Builder builder = new Notification.Builder(mMockContext, channelId)
142 .setContentTitle("foo")
143 .setSmallIcon(android.R.drawable.sym_def_app_icon);
144
145 if (group != null) {
146 builder.setGroup(group);
147 }
148
149 Notification n = builder.build();
150 return new StatusBarNotification(
151 pkg, pkg, ID, TAG, UID, UID, n, USER, null, UID);
152 }
153
154}