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(); |
| 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 | } |