1 /*
2  * Copyright (C) 2024 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.mediaprojection.taskswitcher
18 
19 import android.media.projection.MediaProjectionInfo
20 import android.media.projection.MediaProjectionManager
21 import android.os.Binder
22 import android.os.IBinder
23 import android.os.UserHandle
24 import android.view.ContentRecordingSession
25 import android.window.WindowContainerToken
26 import com.android.systemui.mediaprojection.MediaProjectionServiceHelper
27 import com.android.systemui.util.mockito.any
28 import com.android.systemui.util.mockito.mock
29 import com.android.systemui.util.mockito.whenever
30 
31 class FakeMediaProjectionManager {
32 
33     val mediaProjectionManager = mock<MediaProjectionManager>()
34     val helper = mock<MediaProjectionServiceHelper>()
35 
36     private val callbacks = mutableListOf<MediaProjectionManager.Callback>()
37 
38     init {
<lambda>null39         whenever(mediaProjectionManager.addCallback(any(), any())).thenAnswer {
40             callbacks += it.arguments[0] as MediaProjectionManager.Callback
41             return@thenAnswer Unit
42         }
<lambda>null43         whenever(mediaProjectionManager.removeCallback(any())).thenAnswer {
44             callbacks -= it.arguments[0] as MediaProjectionManager.Callback
45             return@thenAnswer Unit
46         }
<lambda>null47         whenever(helper.updateTaskRecordingSession(any())).thenAnswer {
48             val token = it.arguments[0] as WindowContainerToken
49             dispatchOnSessionSet(session = createSingleTaskSession(token.asBinder()))
50             return@thenAnswer true
51         }
52     }
53 
dispatchOnStartnull54     fun dispatchOnStart(info: MediaProjectionInfo = DEFAULT_INFO) {
55         callbacks.forEach { it.onStart(info) }
56     }
57 
dispatchOnStopnull58     fun dispatchOnStop(info: MediaProjectionInfo = DEFAULT_INFO) {
59         callbacks.forEach { it.onStop(info) }
60     }
61 
dispatchOnSessionSetnull62     fun dispatchOnSessionSet(
63         info: MediaProjectionInfo = DEFAULT_INFO,
64         session: ContentRecordingSession?
65     ) {
66         callbacks.forEach { it.onRecordingSessionSet(info, session) }
67     }
68 
69     companion object {
createDisplaySessionnull70         fun createDisplaySession(): ContentRecordingSession =
71             ContentRecordingSession.createDisplaySession(/* displayToMirror = */ 123)
72 
73         fun createSingleTaskSession(token: IBinder = Binder()): ContentRecordingSession =
74             ContentRecordingSession.createTaskSession(token)
75 
76         private const val DEFAULT_PACKAGE_NAME = "com.media.projection.test"
77         private val DEFAULT_USER_HANDLE = UserHandle.getUserHandleForUid(UserHandle.myUserId())
78         private val DEFAULT_INFO =
79             MediaProjectionInfo(
80                 DEFAULT_PACKAGE_NAME,
81                 DEFAULT_USER_HANDLE,
82                 /* launchCookie = */ null
83             )
84     }
85 }
86