blob: bca8deeafd3401cd61d0f3aaffdd7199231cb30e [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;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040023
24import android.app.Notification;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040025
26import androidx.test.filters.SmallTest;
27import androidx.test.runner.AndroidJUnit4;
28
29import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverly79c89ec2019-12-13 10:33:01 -050030import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050031import com.android.systemui.util.time.FakeSystemClock;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040032
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 {
Evan Lairdccf5c5e2020-02-06 14:01:28 -050040 private ForegroundServiceLifetimeExtender mExtender;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040041 private NotificationEntry mEntry;
42 private Notification mNotif;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050043 private final FakeSystemClock mClock = new FakeSystemClock();
Evan Laird3ceaa9b2019-08-05 17:11:54 -040044
45 @Before
46 public void setup() {
Evan Lairdccf5c5e2020-02-06 14:01:28 -050047 mExtender = new ForegroundServiceLifetimeExtender(mClock);
48
Evan Laird3ceaa9b2019-08-05 17:11:54 -040049 mNotif = new Notification.Builder(mContext, "")
50 .setSmallIcon(R.drawable.ic_person)
51 .setContentTitle("Title")
52 .setContentText("Text")
53 .build();
54
Ned Burns636c3792019-09-11 16:59:07 -040055 mEntry = new NotificationEntryBuilder()
Evan Lairdccf5c5e2020-02-06 14:01:28 -050056 .setCreationTime(mClock.uptimeMillis())
Ned Burns636c3792019-09-11 16:59:07 -040057 .setNotification(mNotif)
58 .build();
Evan Laird3ceaa9b2019-08-05 17:11:54 -040059 }
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;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050069
70 // No time has elapsed, keep showing
Evan Laird3ceaa9b2019-08-05 17:11:54 -040071 assertTrue(mExtender.shouldExtendLifetime(mEntry));
72 }
73
74 @Test
75 public void testShouldExtendLifetime_shouldNot_foreground() {
76 mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050077
78 // Entry was created at mClock.uptimeMillis(), advance it MIN_FGS_TIME_MS + 1
79 mClock.advanceTime(MIN_FGS_TIME_MS + 1);
Evan Laird3ceaa9b2019-08-05 17:11:54 -040080 assertFalse(mExtender.shouldExtendLifetime(mEntry));
81 }
82
83 @Test
84 public void testShouldExtendLifetime_shouldNot_notForeground() {
85 mNotif.flags = 0;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050086
87 // Entry was created at mClock.uptimeMillis(), advance it MIN_FGS_TIME_MS + 1
88 mClock.advanceTime(MIN_FGS_TIME_MS + 1);
Evan Laird3ceaa9b2019-08-05 17:11:54 -040089 assertFalse(mExtender.shouldExtendLifetime(mEntry));
90 }
91}