blob: 212c93dc576dbf9cc374aacb5aa37a8529a05fdf [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;
Ned Burns636c3792019-09-11 16:59:07 -040020import static com.android.systemui.statusbar.NotificationEntryHelper.modifySbn;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040021
22import static org.junit.Assert.assertFalse;
23import static org.junit.Assert.assertTrue;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040024
25import android.app.Notification;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040026
27import androidx.test.filters.SmallTest;
28import androidx.test.runner.AndroidJUnit4;
29
Ned Burns636c3792019-09-11 16:59:07 -040030import com.android.systemui.statusbar.NotificationEntryBuilder;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040031import com.android.systemui.statusbar.notification.collection.NotificationEntry;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36
37@RunWith(AndroidJUnit4.class)
38@SmallTest
39public class ForegroundServiceNotificationListenerTest extends SysuiTestCase {
40 private ForegroundServiceLifetimeExtender mExtender = new ForegroundServiceLifetimeExtender();
Evan Laird3ceaa9b2019-08-05 17:11:54 -040041 private NotificationEntry mEntry;
42 private Notification mNotif;
43
44 @Before
45 public void setup() {
46 mNotif = new Notification.Builder(mContext, "")
47 .setSmallIcon(R.drawable.ic_person)
48 .setContentTitle("Title")
49 .setContentText("Text")
50 .build();
51
Ned Burns636c3792019-09-11 16:59:07 -040052 mEntry = new NotificationEntryBuilder()
53 .setNotification(mNotif)
54 .build();
Evan Laird3ceaa9b2019-08-05 17:11:54 -040055 }
56
57 /**
58 * ForegroundServiceLifetimeExtenderTest
59 */
60 @Test
61 public void testShouldExtendLifetime_should_foreground() {
62 // Extend the lifetime of a FGS notification iff it has not been visible
63 // for the minimum time
64 mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
Ned Burns636c3792019-09-11 16:59:07 -040065 modifySbn(mEntry)
66 .setPostTime(System.currentTimeMillis())
67 .build();
Evan Laird3ceaa9b2019-08-05 17:11:54 -040068 assertTrue(mExtender.shouldExtendLifetime(mEntry));
69 }
70
71 @Test
72 public void testShouldExtendLifetime_shouldNot_foreground() {
73 mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
Ned Burns636c3792019-09-11 16:59:07 -040074 modifySbn(mEntry)
75 .setPostTime(System.currentTimeMillis() - MIN_FGS_TIME_MS - 1)
76 .build();
Evan Laird3ceaa9b2019-08-05 17:11:54 -040077 assertFalse(mExtender.shouldExtendLifetime(mEntry));
78 }
79
80 @Test
81 public void testShouldExtendLifetime_shouldNot_notForeground() {
82 mNotif.flags = 0;
Ned Burns636c3792019-09-11 16:59:07 -040083 modifySbn(mEntry)
84 .setPostTime(System.currentTimeMillis() - MIN_FGS_TIME_MS - 1)
85 .build();
Evan Laird3ceaa9b2019-08-05 17:11:54 -040086 assertFalse(mExtender.shouldExtendLifetime(mEntry));
87 }
88}