blob: 283a47ca3622f6d069170b6760acb1bcc4898a04 [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;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.mockito.Mock;
40import org.mockito.Mockito;
41import org.mockito.MockitoAnnotations;
42
43import java.util.concurrent.Executor;
44
45@RunWith(AndroidTestingRunner.class)
46@SmallTest
47public 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}