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 | |
Evan Laird | 9b2a480 | 2020-05-08 11:56:18 -0400 | [diff] [blame] | 29 | import com.android.systemui.statusbar.NotificationInteractionTracker; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 30 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Beverly | 79c89ec | 2019-12-13 10:33:01 -0500 | [diff] [blame] | 31 | import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 32 | import com.android.systemui.util.time.FakeSystemClock; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 33 | |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
Evan Laird | 9b2a480 | 2020-05-08 11:56:18 -0400 | [diff] [blame] | 37 | import org.mockito.Mock; |
| 38 | import org.mockito.MockitoAnnotations; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 39 | |
| 40 | @RunWith(AndroidJUnit4.class) |
| 41 | @SmallTest |
| 42 | public class ForegroundServiceNotificationListenerTest extends SysuiTestCase { |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 43 | private ForegroundServiceLifetimeExtender mExtender; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 44 | private NotificationEntry mEntry; |
| 45 | private Notification mNotif; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 46 | private final FakeSystemClock mClock = new FakeSystemClock(); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 47 | |
Evan Laird | 9b2a480 | 2020-05-08 11:56:18 -0400 | [diff] [blame] | 48 | @Mock |
| 49 | private NotificationInteractionTracker mInteractionTracker; |
| 50 | |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 51 | @Before |
| 52 | public void setup() { |
Evan Laird | 9b2a480 | 2020-05-08 11:56:18 -0400 | [diff] [blame] | 53 | MockitoAnnotations.initMocks(this); |
| 54 | mExtender = new ForegroundServiceLifetimeExtender(mInteractionTracker, mClock); |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 55 | |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 56 | mNotif = new Notification.Builder(mContext, "") |
| 57 | .setSmallIcon(R.drawable.ic_person) |
| 58 | .setContentTitle("Title") |
| 59 | .setContentText("Text") |
| 60 | .build(); |
| 61 | |
Ned Burns | 636c379 | 2019-09-11 16:59:07 -0400 | [diff] [blame] | 62 | mEntry = new NotificationEntryBuilder() |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 63 | .setCreationTime(mClock.uptimeMillis()) |
Ned Burns | 636c379 | 2019-09-11 16:59:07 -0400 | [diff] [blame] | 64 | .setNotification(mNotif) |
| 65 | .build(); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 66 | } |
| 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 Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 76 | |
| 77 | // No time has elapsed, keep showing |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 78 | assertTrue(mExtender.shouldExtendLifetime(mEntry)); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testShouldExtendLifetime_shouldNot_foreground() { |
| 83 | mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 84 | |
| 85 | // Entry was created at mClock.uptimeMillis(), advance it MIN_FGS_TIME_MS + 1 |
| 86 | mClock.advanceTime(MIN_FGS_TIME_MS + 1); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 87 | assertFalse(mExtender.shouldExtendLifetime(mEntry)); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testShouldExtendLifetime_shouldNot_notForeground() { |
| 92 | mNotif.flags = 0; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 93 | |
| 94 | // Entry was created at mClock.uptimeMillis(), advance it MIN_FGS_TIME_MS + 1 |
| 95 | mClock.advanceTime(MIN_FGS_TIME_MS + 1); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 96 | assertFalse(mExtender.shouldExtendLifetime(mEntry)); |
| 97 | } |
Evan Laird | 1d7db5b | 2020-05-27 23:46:32 -0400 | [diff] [blame] | 98 | |
| 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 Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 110 | } |