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; |
| 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
| 26 | import android.app.Notification; |
| 27 | import android.service.notification.NotificationListenerService.Ranking; |
| 28 | import android.service.notification.StatusBarNotification; |
| 29 | |
| 30 | import androidx.test.filters.SmallTest; |
| 31 | import androidx.test.runner.AndroidJUnit4; |
| 32 | |
| 33 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | |
| 39 | @RunWith(AndroidJUnit4.class) |
| 40 | @SmallTest |
| 41 | public class ForegroundServiceNotificationListenerTest extends SysuiTestCase { |
| 42 | private ForegroundServiceLifetimeExtender mExtender = new ForegroundServiceLifetimeExtender(); |
| 43 | private StatusBarNotification mSbn; |
| 44 | private NotificationEntry mEntry; |
| 45 | private Notification mNotif; |
| 46 | |
| 47 | @Before |
| 48 | public void setup() { |
| 49 | mNotif = new Notification.Builder(mContext, "") |
| 50 | .setSmallIcon(R.drawable.ic_person) |
| 51 | .setContentTitle("Title") |
| 52 | .setContentText("Text") |
| 53 | .build(); |
| 54 | |
| 55 | mSbn = mock(StatusBarNotification.class); |
| 56 | when(mSbn.getNotification()).thenReturn(mNotif); |
| 57 | |
| 58 | mEntry = new NotificationEntry(mSbn, mock(Ranking.class)); |
| 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; |
| 69 | when(mSbn.getPostTime()).thenReturn(System.currentTimeMillis()); |
| 70 | assertTrue(mExtender.shouldExtendLifetime(mEntry)); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testShouldExtendLifetime_shouldNot_foreground() { |
| 75 | mNotif.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 76 | when(mSbn.getPostTime()).thenReturn(System.currentTimeMillis() - MIN_FGS_TIME_MS - 1); |
| 77 | assertFalse(mExtender.shouldExtendLifetime(mEntry)); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testShouldExtendLifetime_shouldNot_notForeground() { |
| 82 | mNotif.flags = 0; |
| 83 | when(mSbn.getPostTime()).thenReturn(System.currentTimeMillis() - MIN_FGS_TIME_MS - 1); |
| 84 | assertFalse(mExtender.shouldExtendLifetime(mEntry)); |
| 85 | } |
| 86 | } |