blob: 3db275acfd0686d8b21d980ed547bf787d2b971b [file] [log] [blame]
Julia Reynolds6a63d1b2018-08-14 16:59:33 -04001/**
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 */
16
17package android.ext.services.notification;
18
19import static android.app.NotificationManager.IMPORTANCE_HIGH;
20import static android.app.NotificationManager.IMPORTANCE_MIN;
21
22import static org.mockito.ArgumentMatchers.any;
23import static org.mockito.ArgumentMatchers.anyInt;
24import static org.mockito.ArgumentMatchers.anyLong;
25import static org.mockito.ArgumentMatchers.anyString;
26import static org.mockito.Mockito.mock;
27import static org.mockito.Mockito.never;
28import static org.mockito.Mockito.times;
29import static org.mockito.Mockito.verify;
30import static org.mockito.Mockito.when;
31
32import android.app.AlarmManager;
33import android.app.Notification;
34import android.app.NotificationChannel;
35import android.app.PendingIntent;
36import android.content.pm.ApplicationInfo;
37import android.content.pm.IPackageManager;
38import android.os.Build;
39import android.os.Process;
40import android.os.UserHandle;
41import android.service.notification.StatusBarNotification;
Julia Reynolds6a63d1b2018-08-14 16:59:33 -040042import android.testing.TestableContext;
43
Brett Chabot84151d92019-02-27 15:37:59 -080044import androidx.test.InstrumentationRegistry;
45import androidx.test.runner.AndroidJUnit4;
46
Julia Reynolds6a63d1b2018-08-14 16:59:33 -040047import org.junit.Before;
48import org.junit.Rule;
49import org.junit.Test;
50import org.junit.runner.RunWith;
51import org.mockito.Mock;
52import org.mockito.MockitoAnnotations;
53
54@RunWith(AndroidJUnit4.class)
55public class AgingHelperTest {
56 private String mPkg = "pkg";
57 private int mUid = 2018;
58
59 @Rule
60 public final TestableContext mContext =
61 new TestableContext(InstrumentationRegistry.getTargetContext(), null);
62
63 @Mock
64 private NotificationCategorizer mCategorizer;
65 @Mock
66 private AlarmManager mAlarmManager;
67 @Mock
68 private IPackageManager mPackageManager;
69 @Mock
70 private AgingHelper.Callback mCallback;
Nadia Benbernou1ee91a32019-01-28 11:26:46 -050071 @Mock
72 private SmsHelper mSmsHelper;
Julia Reynolds6a63d1b2018-08-14 16:59:33 -040073
74 private AgingHelper mAgingHelper;
75
76 private StatusBarNotification generateSbn(String channelId) {
77 Notification n = new Notification.Builder(mContext, channelId)
78 .setContentTitle("foo")
79 .build();
80
81 return new StatusBarNotification(mPkg, mPkg, 0, "tag", mUid, mUid, n,
82 UserHandle.SYSTEM, null, 0);
83 }
84
85 @Before
86 public void setUp() throws Exception {
87 MockitoAnnotations.initMocks(this);
88 mPkg = mContext.getPackageName();
89 mUid = Process.myUid();
90
91 ApplicationInfo info = mock(ApplicationInfo.class);
92 when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt()))
93 .thenReturn(info);
94 info.targetSdkVersion = Build.VERSION_CODES.P;
95
96 mContext.addMockSystemService(AlarmManager.class, mAlarmManager);
97
98 mAgingHelper = new AgingHelper(mContext, mCategorizer, mCallback);
99 }
100
101 @Test
102 public void testNoSnoozingOnPost() {
103 NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH);
104 StatusBarNotification sbn = generateSbn(channel.getId());
Nadia Benbernou1ee91a32019-01-28 11:26:46 -0500105 NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
Julia Reynolds6a63d1b2018-08-14 16:59:33 -0400106
107
108 mAgingHelper.onNotificationPosted(entry);
109 verify(mAlarmManager, never()).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
110 }
111
112 @Test
113 public void testPostResetsSnooze() {
114 NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH);
115 StatusBarNotification sbn = generateSbn(channel.getId());
Nadia Benbernou1ee91a32019-01-28 11:26:46 -0500116 NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
Julia Reynolds6a63d1b2018-08-14 16:59:33 -0400117
118
119 mAgingHelper.onNotificationPosted(entry);
120 verify(mAlarmManager, times(1)).cancel(any(PendingIntent.class));
121 }
122
123 @Test
124 public void testSnoozingOnSeen() {
125 NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH);
126 StatusBarNotification sbn = generateSbn(channel.getId());
Nadia Benbernou1ee91a32019-01-28 11:26:46 -0500127 NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
Julia Reynolds6a63d1b2018-08-14 16:59:33 -0400128 entry.setSeen();
129 when(mCategorizer.getCategory(entry)).thenReturn(NotificationCategorizer.CATEGORY_PEOPLE);
130
131 mAgingHelper.onNotificationSeen(entry);
132 verify(mAlarmManager, times(1)).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
133 }
134
135 @Test
136 public void testNoSnoozingOnSeenUserLocked() {
137 NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH);
138 channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
139 StatusBarNotification sbn = generateSbn(channel.getId());
Nadia Benbernou1ee91a32019-01-28 11:26:46 -0500140 NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
Julia Reynolds6a63d1b2018-08-14 16:59:33 -0400141 when(mCategorizer.getCategory(entry)).thenReturn(NotificationCategorizer.CATEGORY_PEOPLE);
142
143 mAgingHelper.onNotificationSeen(entry);
144 verify(mAlarmManager, never()).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
145 }
146
147 @Test
148 public void testNoSnoozingOnSeenAlreadyLow() {
149 NotificationEntry entry = mock(NotificationEntry.class);
150 when(entry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_HIGH));
151 when(entry.getImportance()).thenReturn(IMPORTANCE_MIN);
152
153 mAgingHelper.onNotificationSeen(entry);
154 verify(mAlarmManager, never()).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
155 }
156}