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