blob: 9a40421a353f690a02b80bddc09fafd37e6dfd27 [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
Evan Laird9b2a4802020-05-08 11:56:18 -040029import com.android.systemui.statusbar.NotificationInteractionTracker;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040030import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Beverly79c89ec2019-12-13 10:33:01 -050031import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050032import com.android.systemui.util.time.FakeSystemClock;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040033
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
Evan Laird9b2a4802020-05-08 11:56:18 -040037import org.mockito.Mock;
38import org.mockito.MockitoAnnotations;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040039
40@RunWith(AndroidJUnit4.class)
41@SmallTest
42public class ForegroundServiceNotificationListenerTest extends SysuiTestCase {
Evan Lairdccf5c5e2020-02-06 14:01:28 -050043 private ForegroundServiceLifetimeExtender mExtender;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040044 private NotificationEntry mEntry;
45 private Notification mNotif;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050046 private final FakeSystemClock mClock = new FakeSystemClock();
Evan Laird3ceaa9b2019-08-05 17:11:54 -040047
Evan Laird9b2a4802020-05-08 11:56:18 -040048 @Mock
49 private NotificationInteractionTracker mInteractionTracker;
50
Evan Laird3ceaa9b2019-08-05 17:11:54 -040051 @Before
52 public void setup() {
Evan Laird9b2a4802020-05-08 11:56:18 -040053 MockitoAnnotations.initMocks(this);
54 mExtender = new ForegroundServiceLifetimeExtender(mInteractionTracker, mClock);
Evan Lairdccf5c5e2020-02-06 14:01:28 -050055
Evan Laird3ceaa9b2019-08-05 17:11:54 -040056 mNotif = new Notification.Builder(mContext, "")
57 .setSmallIcon(R.drawable.ic_person)
58 .setContentTitle("Title")
59 .setContentText("Text")
60 .build();
61
Ned Burns636c3792019-09-11 16:59:07 -040062 mEntry = new NotificationEntryBuilder()
Evan Lairdccf5c5e2020-02-06 14:01:28 -050063 .setCreationTime(mClock.uptimeMillis())
Ned Burns636c3792019-09-11 16:59:07 -040064 .setNotification(mNotif)
65 .build();
Evan Laird3ceaa9b2019-08-05 17:11:54 -040066 }
67
68 /**
69 * ForegroundServiceLifetimeExtenderTest
70 */
71 @Test
72 public void testShouldExtendLifetime_should_foreground() {
73 // Extend the lifetime of a FGS notification iff it has not been visible
74 // for the minimum time
75 mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050076
77 // No time has elapsed, keep showing
Evan Laird3ceaa9b2019-08-05 17:11:54 -040078 assertTrue(mExtender.shouldExtendLifetime(mEntry));
79 }
80
81 @Test
82 public void testShouldExtendLifetime_shouldNot_foreground() {
83 mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050084
85 // Entry was created at mClock.uptimeMillis(), advance it MIN_FGS_TIME_MS + 1
86 mClock.advanceTime(MIN_FGS_TIME_MS + 1);
Evan Laird3ceaa9b2019-08-05 17:11:54 -040087 assertFalse(mExtender.shouldExtendLifetime(mEntry));
88 }
89
90 @Test
91 public void testShouldExtendLifetime_shouldNot_notForeground() {
92 mNotif.flags = 0;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050093
94 // Entry was created at mClock.uptimeMillis(), advance it MIN_FGS_TIME_MS + 1
95 mClock.advanceTime(MIN_FGS_TIME_MS + 1);
Evan Laird3ceaa9b2019-08-05 17:11:54 -040096 assertFalse(mExtender.shouldExtendLifetime(mEntry));
97 }
Evan Laird1d7db5b2020-05-27 23:46:32 -040098
99 @Test
100 public void testShouldExtendLifetime_shouldNot_interruped() {
101 // GIVEN a notification that would trigger lifetime extension
102 mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
103
104 // GIVEN the notification has alerted
105 mEntry.setInterruption();
106
107 // THEN the notification does not need to have its lifetime extended by this extender
108 assertFalse(mExtender.shouldExtendLifetime(mEntry));
109 }
Evan Laird3ceaa9b2019-08-05 17:11:54 -0400110}