Beth Thibodeau | abb338f | 2020-03-03 16:17:24 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.screenrecord; |
| 18 | |
| 19 | import static org.mockito.ArgumentMatchers.any; |
| 20 | import static org.mockito.ArgumentMatchers.anyInt; |
| 21 | import static org.mockito.Mockito.doNothing; |
| 22 | import static org.mockito.Mockito.doReturn; |
| 23 | import static org.mockito.Mockito.times; |
| 24 | import static org.mockito.Mockito.verify; |
| 25 | |
| 26 | import android.app.Notification; |
| 27 | import android.app.NotificationManager; |
| 28 | import android.content.Intent; |
| 29 | import android.testing.AndroidTestingRunner; |
| 30 | |
| 31 | import androidx.test.filters.SmallTest; |
| 32 | |
| 33 | import com.android.internal.logging.UiEventLogger; |
| 34 | import com.android.systemui.SysuiTestCase; |
| 35 | |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | import org.mockito.Mock; |
| 40 | import org.mockito.Mockito; |
| 41 | import org.mockito.MockitoAnnotations; |
| 42 | |
| 43 | import java.util.concurrent.Executor; |
| 44 | |
| 45 | @RunWith(AndroidTestingRunner.class) |
| 46 | @SmallTest |
| 47 | public class RecordingServiceTest extends SysuiTestCase { |
| 48 | |
| 49 | @Mock |
| 50 | private UiEventLogger mUiEventLogger; |
| 51 | @Mock |
| 52 | private RecordingController mController; |
| 53 | @Mock |
| 54 | private NotificationManager mNotificationManager; |
| 55 | @Mock |
| 56 | private ScreenMediaRecorder mScreenMediaRecorder; |
| 57 | @Mock |
| 58 | private Notification mNotification; |
| 59 | @Mock |
| 60 | private Executor mExecutor; |
| 61 | |
| 62 | private RecordingService mRecordingService; |
| 63 | |
| 64 | @Before |
| 65 | public void setUp() throws Exception { |
| 66 | MockitoAnnotations.initMocks(this); |
| 67 | mRecordingService = Mockito.spy(new RecordingService(mController, mExecutor, mUiEventLogger, |
| 68 | mNotificationManager)); |
| 69 | |
| 70 | // Return actual context info |
| 71 | doReturn(mContext).when(mRecordingService).getApplicationContext(); |
| 72 | doReturn(mContext.getUserId()).when(mRecordingService).getUserId(); |
| 73 | doReturn(mContext.getPackageName()).when(mRecordingService).getPackageName(); |
| 74 | doReturn(mContext.getContentResolver()).when(mRecordingService).getContentResolver(); |
| 75 | |
| 76 | // Mock notifications |
| 77 | doNothing().when(mRecordingService).createRecordingNotification(); |
| 78 | doReturn(mNotification).when(mRecordingService).createProcessingNotification(); |
| 79 | doReturn(mNotification).when(mRecordingService).createSaveNotification(any()); |
| 80 | |
| 81 | doNothing().when(mRecordingService).startForeground(anyInt(), any()); |
| 82 | doReturn(mScreenMediaRecorder).when(mRecordingService).getRecorder(); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testLogStartRecording() { |
| 87 | Intent startIntent = RecordingService.getStartIntent(mContext, 0, 0, false); |
| 88 | mRecordingService.onStartCommand(startIntent, 0, 0); |
| 89 | |
| 90 | verify(mUiEventLogger, times(1)).log(Events.ScreenRecordEvent.SCREEN_RECORD_START); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testLogStopFromQsTile() { |
| 95 | Intent stopIntent = RecordingService.getStopIntent(mContext); |
| 96 | mRecordingService.onStartCommand(stopIntent, 0, 0); |
| 97 | |
| 98 | // Verify that we log the correct event |
| 99 | verify(mUiEventLogger, times(1)).log(Events.ScreenRecordEvent.SCREEN_RECORD_END_QS_TILE); |
| 100 | verify(mUiEventLogger, times(0)) |
| 101 | .log(Events.ScreenRecordEvent.SCREEN_RECORD_END_NOTIFICATION); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testLogStopFromNotificationIntent() { |
| 106 | Intent stopIntent = RecordingService.getNotificationIntent(mContext); |
| 107 | mRecordingService.onStartCommand(stopIntent, 0, 0); |
| 108 | |
| 109 | // Verify that we log the correct event |
| 110 | verify(mUiEventLogger, times(1)) |
| 111 | .log(Events.ScreenRecordEvent.SCREEN_RECORD_END_NOTIFICATION); |
| 112 | verify(mUiEventLogger, times(0)).log(Events.ScreenRecordEvent.SCREEN_RECORD_END_QS_TILE); |
| 113 | } |
| 114 | } |