blob: 90018e5f8e6d1b71c07120b2b5a037b6684993c4 [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;
42import android.support.test.InstrumentationRegistry;
43import android.support.test.runner.AndroidJUnit4;
44import android.testing.TestableContext;
45
46import org.junit.Before;
47import org.junit.Rule;
48import org.junit.Test;
49import org.junit.runner.RunWith;
50import org.mockito.Mock;
51import org.mockito.MockitoAnnotations;
52
53@RunWith(AndroidJUnit4.class)
54public class AgingHelperTest {
55 private String mPkg = "pkg";
56 private int mUid = 2018;
57
58 @Rule
59 public final TestableContext mContext =
60 new TestableContext(InstrumentationRegistry.getTargetContext(), null);
61
62 @Mock
63 private NotificationCategorizer mCategorizer;
64 @Mock
65 private AlarmManager mAlarmManager;
66 @Mock
67 private IPackageManager mPackageManager;
68 @Mock
69 private AgingHelper.Callback mCallback;
Nadia Benbernou1ee91a32019-01-28 11:26:46 -050070 @Mock
71 private SmsHelper mSmsHelper;
Julia Reynolds6a63d1b2018-08-14 16:59:33 -040072
73 private AgingHelper mAgingHelper;
74
75 private StatusBarNotification generateSbn(String channelId) {
76 Notification n = new Notification.Builder(mContext, channelId)
77 .setContentTitle("foo")
78 .build();
79
80 return new StatusBarNotification(mPkg, mPkg, 0, "tag", mUid, mUid, n,
81 UserHandle.SYSTEM, null, 0);
82 }
83
84 @Before
85 public void setUp() throws Exception {
86 MockitoAnnotations.initMocks(this);
87 mPkg = mContext.getPackageName();
88 mUid = Process.myUid();
89
90 ApplicationInfo info = mock(ApplicationInfo.class);
91 when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt()))
92 .thenReturn(info);
93 info.targetSdkVersion = Build.VERSION_CODES.P;
94
95 mContext.addMockSystemService(AlarmManager.class, mAlarmManager);
96
97 mAgingHelper = new AgingHelper(mContext, mCategorizer, mCallback);
98 }
99
100 @Test
101 public void testNoSnoozingOnPost() {
102 NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH);
103 StatusBarNotification sbn = generateSbn(channel.getId());
Nadia Benbernou1ee91a32019-01-28 11:26:46 -0500104 NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
Julia Reynolds6a63d1b2018-08-14 16:59:33 -0400105
106
107 mAgingHelper.onNotificationPosted(entry);
108 verify(mAlarmManager, never()).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
109 }
110
111 @Test
112 public void testPostResetsSnooze() {
113 NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH);
114 StatusBarNotification sbn = generateSbn(channel.getId());
Nadia Benbernou1ee91a32019-01-28 11:26:46 -0500115 NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
Julia Reynolds6a63d1b2018-08-14 16:59:33 -0400116
117
118 mAgingHelper.onNotificationPosted(entry);
119 verify(mAlarmManager, times(1)).cancel(any(PendingIntent.class));
120 }
121
122 @Test
123 public void testSnoozingOnSeen() {
124 NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH);
125 StatusBarNotification sbn = generateSbn(channel.getId());
Nadia Benbernou1ee91a32019-01-28 11:26:46 -0500126 NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
Julia Reynolds6a63d1b2018-08-14 16:59:33 -0400127 entry.setSeen();
128 when(mCategorizer.getCategory(entry)).thenReturn(NotificationCategorizer.CATEGORY_PEOPLE);
129
130 mAgingHelper.onNotificationSeen(entry);
131 verify(mAlarmManager, times(1)).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
132 }
133
134 @Test
135 public void testNoSnoozingOnSeenUserLocked() {
136 NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH);
137 channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
138 StatusBarNotification sbn = generateSbn(channel.getId());
Nadia Benbernou1ee91a32019-01-28 11:26:46 -0500139 NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
Julia Reynolds6a63d1b2018-08-14 16:59:33 -0400140 when(mCategorizer.getCategory(entry)).thenReturn(NotificationCategorizer.CATEGORY_PEOPLE);
141
142 mAgingHelper.onNotificationSeen(entry);
143 verify(mAlarmManager, never()).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
144 }
145
146 @Test
147 public void testNoSnoozingOnSeenAlreadyLow() {
148 NotificationEntry entry = mock(NotificationEntry.class);
149 when(entry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_HIGH));
150 when(entry.getImportance()).thenReturn(IMPORTANCE_MIN);
151
152 mAgingHelper.onNotificationSeen(entry);
153 verify(mAlarmManager, never()).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
154 }
155}