blob: 5943b02b4e4a59704090fca55ce4e1eb4f057a9f [file] [log] [blame]
Evan Laird3ceaa9b2019-08-05 17:11:54 -04001/*
2 * Copyright (C) 2019 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 com.android.systemui;
18
19import static com.android.systemui.ForegroundServiceLifetimeExtender.MIN_FGS_TIME_MS;
20
21import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertTrue;
23import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.when;
25
26import android.app.Notification;
27import android.service.notification.NotificationListenerService.Ranking;
28import android.service.notification.StatusBarNotification;
29
30import androidx.test.filters.SmallTest;
31import androidx.test.runner.AndroidJUnit4;
32
33import com.android.systemui.statusbar.notification.collection.NotificationEntry;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38
39@RunWith(AndroidJUnit4.class)
40@SmallTest
41public class ForegroundServiceNotificationListenerTest extends SysuiTestCase {
42 private ForegroundServiceLifetimeExtender mExtender = new ForegroundServiceLifetimeExtender();
43 private StatusBarNotification mSbn;
44 private NotificationEntry mEntry;
45 private Notification mNotif;
46
47 @Before
48 public void setup() {
49 mNotif = new Notification.Builder(mContext, "")
50 .setSmallIcon(R.drawable.ic_person)
51 .setContentTitle("Title")
52 .setContentText("Text")
53 .build();
54
55 mSbn = mock(StatusBarNotification.class);
56 when(mSbn.getNotification()).thenReturn(mNotif);
57
58 mEntry = new NotificationEntry(mSbn, mock(Ranking.class));
59 }
60
61 /**
62 * ForegroundServiceLifetimeExtenderTest
63 */
64 @Test
65 public void testShouldExtendLifetime_should_foreground() {
66 // Extend the lifetime of a FGS notification iff it has not been visible
67 // for the minimum time
68 mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
69 when(mSbn.getPostTime()).thenReturn(System.currentTimeMillis());
70 assertTrue(mExtender.shouldExtendLifetime(mEntry));
71 }
72
73 @Test
74 public void testShouldExtendLifetime_shouldNot_foreground() {
75 mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
76 when(mSbn.getPostTime()).thenReturn(System.currentTimeMillis() - MIN_FGS_TIME_MS - 1);
77 assertFalse(mExtender.shouldExtendLifetime(mEntry));
78 }
79
80 @Test
81 public void testShouldExtendLifetime_shouldNot_notForeground() {
82 mNotif.flags = 0;
83 when(mSbn.getPostTime()).thenReturn(System.currentTimeMillis() - MIN_FGS_TIME_MS - 1);
84 assertFalse(mExtender.shouldExtendLifetime(mEntry));
85 }
86}