blob: e98b6b69ee7685d444bdc0c46d8edaa09e1d9824 [file] [log] [blame]
Beth Thibodeauabb338f2020-03-03 16:17:24 -05001/*
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
17package com.android.systemui.screenrecord;
18
19import static org.mockito.ArgumentMatchers.any;
20import static org.mockito.ArgumentMatchers.anyInt;
21import static org.mockito.Mockito.doNothing;
22import static org.mockito.Mockito.doReturn;
23import static org.mockito.Mockito.times;
24import static org.mockito.Mockito.verify;
25
26import android.app.Notification;
27import android.app.NotificationManager;
28import android.content.Intent;
29import android.testing.AndroidTestingRunner;
30
31import androidx.test.filters.SmallTest;
32
33import com.android.internal.logging.UiEventLogger;
34import com.android.systemui.SysuiTestCase;
Beth Thibodeau231ac9b2020-06-17 22:34:42 -040035import com.android.systemui.settings.CurrentUserContextTracker;
Beth Thibodeauabb338f2020-03-03 16:17:24 -050036
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.Mock;
41import org.mockito.Mockito;
42import org.mockito.MockitoAnnotations;
43
44import java.util.concurrent.Executor;
45
46@RunWith(AndroidTestingRunner.class)
47@SmallTest
48public 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 Thibodeau231ac9b2020-06-17 22:34:42 -040062 @Mock
63 private CurrentUserContextTracker mUserContextTracker;
Beth Thibodeauabb338f2020-03-03 16:17:24 -050064
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 Thibodeau231ac9b2020-06-17 22:34:42 -040071 mNotificationManager, mUserContextTracker));
Beth Thibodeauabb338f2020-03-03 16:17:24 -050072
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 Thibodeau231ac9b2020-06-17 22:34:42 -040086
87 doReturn(mContext).when(mUserContextTracker).getCurrentUserContext();
Beth Thibodeauabb338f2020-03-03 16:17:24 -050088 }
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}