Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [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 | */ |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 16 | |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 17 | package android.service.notification; |
| 18 | |
| 19 | import static junit.framework.Assert.assertEquals; |
| 20 | import static junit.framework.Assert.assertNull; |
| 21 | |
| 22 | import static org.mockito.Mockito.mock; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
| 25 | import android.app.ActivityManager; |
| 26 | import android.app.Notification; |
| 27 | import android.content.Context; |
| 28 | import android.content.pm.ApplicationInfo; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.metrics.LogMaker; |
| 31 | import android.os.UserHandle; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 32 | |
| 33 | import androidx.test.InstrumentationRegistry; |
| 34 | import androidx.test.filters.SmallTest; |
| 35 | import androidx.test.runner.AndroidJUnit4; |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 36 | |
| 37 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.mockito.Mock; |
| 43 | import org.mockito.MockitoAnnotations; |
| 44 | |
| 45 | @RunWith(AndroidJUnit4.class) |
| 46 | @SmallTest |
| 47 | public 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 Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 80 | 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 Brockman | 828427e | 2019-01-28 09:55:45 -0500 | [diff] [blame] | 87 | assertEquals(0, |
| 88 | logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY)); |
| 89 | assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CATEGORY)); |
| 90 | } |
| 91 | |
Will Brockman | 0ed07ca | 2019-03-08 11:45:34 -0500 | [diff] [blame] | 92 | /** 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 Brockman | 828427e | 2019-01-28 09:55:45 -0500 | [diff] [blame] | 103 | @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 Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 110 | } |
| 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 Brockman | 828427e | 2019-01-28 09:55:45 -0500 | [diff] [blame] | 163 | return getNotification(pkg, getNotificationBuilder(group, channelId)); |
| 164 | } |
| 165 | |
| 166 | private Notification.Builder getNotificationBuilder(String group, String channelId) { |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 167 | 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 Brockman | 828427e | 2019-01-28 09:55:45 -0500 | [diff] [blame] | 174 | return builder; |
| 175 | } |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 176 | |
Will Brockman | 828427e | 2019-01-28 09:55:45 -0500 | [diff] [blame] | 177 | private StatusBarNotification getNotification(String pkg, Notification.Builder builder) { |
| 178 | |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 179 | return new StatusBarNotification( |
Will Brockman | 828427e | 2019-01-28 09:55:45 -0500 | [diff] [blame] | 180 | pkg, pkg, ID, TAG, UID, UID, builder.build(), USER, null, UID); |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | } |