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.MediaProjectionEvent 20 import android.media.projection.MediaProjectionInfo 21 import android.media.projection.MediaProjectionManager 22 import android.os.Binder 23 import android.os.IBinder 24 import android.os.UserHandle 25 import android.view.ContentRecordingSession 26 import android.window.WindowContainerToken 27 import com.android.systemui.mediaprojection.MediaProjectionServiceHelper 28 import com.android.systemui.util.mockito.any 29 import com.android.systemui.util.mockito.mock 30 import com.android.systemui.util.mockito.whenever 31 32 class FakeMediaProjectionManager { 33 34 val mediaProjectionManager = mock<MediaProjectionManager>() 35 val helper = mock<MediaProjectionServiceHelper>() 36 37 private val callbacks = mutableListOf<MediaProjectionManager.Callback>() 38 39 init { <lambda>null40 whenever(mediaProjectionManager.addCallback(any(), any())).thenAnswer { 41 callbacks += it.arguments[0] as MediaProjectionManager.Callback 42 return@thenAnswer Unit 43 } <lambda>null44 whenever(mediaProjectionManager.removeCallback(any())).thenAnswer { 45 callbacks -= it.arguments[0] as MediaProjectionManager.Callback 46 return@thenAnswer Unit 47 } <lambda>null48 whenever(helper.updateTaskRecordingSession(any())).thenAnswer { 49 val token = it.arguments[0] as WindowContainerToken 50 dispatchOnSessionSet(session = createSingleTaskSession(token.asBinder())) 51 return@thenAnswer true 52 } 53 } 54 dispatchOnStartnull55 fun dispatchOnStart(info: MediaProjectionInfo = DEFAULT_INFO) { 56 callbacks.forEach { it.onStart(info) } 57 } 58 dispatchOnStopnull59 fun dispatchOnStop(info: MediaProjectionInfo = DEFAULT_INFO) { 60 callbacks.forEach { it.onStop(info) } 61 } 62 dispatchOnSessionSetnull63 fun dispatchOnSessionSet( 64 info: MediaProjectionInfo = DEFAULT_INFO, 65 session: ContentRecordingSession?, 66 ) { 67 callbacks.forEach { it.onRecordingSessionSet(info, session) } 68 } 69 dispatchEventnull70 fun dispatchEvent( 71 event: MediaProjectionEvent, 72 info: MediaProjectionInfo? = DEFAULT_INFO, 73 session: ContentRecordingSession? = null, 74 ) { 75 callbacks.forEach { it.onMediaProjectionEvent(event, info, session) } 76 } 77 78 companion object { createDisplaySessionnull79 fun createDisplaySession(): ContentRecordingSession = 80 ContentRecordingSession.createDisplaySession(/* displayToMirror= */ 123) 81 82 fun createSingleTaskSession(token: IBinder = Binder()): ContentRecordingSession = 83 ContentRecordingSession.createTaskSession(token) 84 85 private const val DEFAULT_PACKAGE_NAME = "com.media.projection.test" 86 private val DEFAULT_USER_HANDLE = UserHandle.getUserHandleForUid(UserHandle.myUserId()) 87 private val DEFAULT_INFO = 88 MediaProjectionInfo(DEFAULT_PACKAGE_NAME, DEFAULT_USER_HANDLE, /* launchCookie= */ null) 89 } 90 } 91