blob: b1dabdda2241b098797ec41d4081823ef6ca8788 [file] [log] [blame]
Evan Laird05597642019-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.StatusBarNotification;
28
29import androidx.test.filters.SmallTest;
30import androidx.test.runner.AndroidJUnit4;
31
32import com.android.systemui.statusbar.notification.collection.NotificationEntry;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37
38@RunWith(AndroidJUnit4.class)
39@SmallTest
40public class ForegroundServiceLifetimeExtenderTest extends SysuiTestCase {
41 private ForegroundServiceLifetimeExtender mExtender = new ForegroundServiceLifetimeExtender();
42 private StatusBarNotification mSbn;
43 private NotificationEntry mEntry;
44 private Notification mNotif;
45
46 @Before
47 public void setup() {
48 mNotif = new Notification.Builder(mContext, "")
49 .setSmallIcon(R.drawable.ic_person)
50 .setContentTitle("Title")
51 .setContentText("Text")
52 .build();
53
54 mSbn = mock(StatusBarNotification.class);
55 when(mSbn.getNotification()).thenReturn(mNotif);
56
57 mEntry = new NotificationEntry(mSbn);
58 }
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;
65 when(mSbn.getPostTime()).thenReturn(System.currentTimeMillis());
66 assertTrue(mExtender.shouldExtendLifetime(mEntry));
67 }
68
69 @Test
70 public void testShouldExtendLifetime_shouldNot_foreground() {
71 mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
72 when(mSbn.getPostTime()).thenReturn(System.currentTimeMillis() - MIN_FGS_TIME_MS - 1);
73 assertFalse(mExtender.shouldExtendLifetime(mEntry));
74 }
75
76 @Test
77 public void testShouldExtendLifetime_shouldNot_notForeground() {
78 mNotif.flags = 0;
79 when(mSbn.getPostTime()).thenReturn(System.currentTimeMillis() - MIN_FGS_TIME_MS - 1);
80 assertFalse(mExtender.shouldExtendLifetime(mEntry));
81 }
82}