Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui; |
| 18 | |
| 19 | import static com.android.systemui.ForegroundServiceLifetimeExtender.MIN_FGS_TIME_MS; |
| 20 | |
| 21 | import static org.junit.Assert.assertFalse; |
| 22 | import static org.junit.Assert.assertTrue; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 23 | |
| 24 | import android.app.Notification; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 25 | |
| 26 | import androidx.test.filters.SmallTest; |
| 27 | import androidx.test.runner.AndroidJUnit4; |
| 28 | |
| 29 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Beverly | 79c89ec | 2019-12-13 10:33:01 -0500 | [diff] [blame] | 30 | import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame^] | 31 | import com.android.systemui.util.time.FakeSystemClock; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | |
| 37 | @RunWith(AndroidJUnit4.class) |
| 38 | @SmallTest |
| 39 | public class ForegroundServiceNotificationListenerTest extends SysuiTestCase { |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame^] | 40 | private ForegroundServiceLifetimeExtender mExtender; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 41 | private NotificationEntry mEntry; |
| 42 | private Notification mNotif; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame^] | 43 | private final FakeSystemClock mClock = new FakeSystemClock(); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 44 | |
| 45 | @Before |
| 46 | public void setup() { |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame^] | 47 | mExtender = new ForegroundServiceLifetimeExtender(mClock); |
| 48 | |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 49 | mNotif = new Notification.Builder(mContext, "") |
| 50 | .setSmallIcon(R.drawable.ic_person) |
| 51 | .setContentTitle("Title") |
| 52 | .setContentText("Text") |
| 53 | .build(); |
| 54 | |
Ned Burns | 636c379 | 2019-09-11 16:59:07 -0400 | [diff] [blame] | 55 | mEntry = new NotificationEntryBuilder() |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame^] | 56 | .setCreationTime(mClock.uptimeMillis()) |
Ned Burns | 636c379 | 2019-09-11 16:59:07 -0400 | [diff] [blame] | 57 | .setNotification(mNotif) |
| 58 | .build(); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 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; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame^] | 69 | |
| 70 | // No time has elapsed, keep showing |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 71 | assertTrue(mExtender.shouldExtendLifetime(mEntry)); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testShouldExtendLifetime_shouldNot_foreground() { |
| 76 | mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame^] | 77 | |
| 78 | // Entry was created at mClock.uptimeMillis(), advance it MIN_FGS_TIME_MS + 1 |
| 79 | mClock.advanceTime(MIN_FGS_TIME_MS + 1); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 80 | assertFalse(mExtender.shouldExtendLifetime(mEntry)); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testShouldExtendLifetime_shouldNot_notForeground() { |
| 85 | mNotif.flags = 0; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame^] | 86 | |
| 87 | // Entry was created at mClock.uptimeMillis(), advance it MIN_FGS_TIME_MS + 1 |
| 88 | mClock.advanceTime(MIN_FGS_TIME_MS + 1); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 89 | assertFalse(mExtender.shouldExtendLifetime(mEntry)); |
| 90 | } |
| 91 | } |