/* * Copyright (C) 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.eyedropper.util import android.graphics.Bitmap import android.graphics.ColorSpace import android.hardware.HardwareBuffer import android.hardware.display.DisplayManager import android.view.Display import android.view.IWindowManager import android.window.IScreenCaptureCallback import android.window.ScreenCapture import androidx.test.ext.junit.runners.AndroidJUnit4 import com.android.eyedropper.di.MainThreadHandlerModule import com.android.eyedropper.di.SystemServiceModule import com.google.common.truth.Truth.assertThat import dagger.hilt.android.testing.BindValue import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest import dagger.hilt.android.testing.UninstallModules import javax.inject.Inject import kotlin.test.assertFailsWith import kotlinx.coroutines.runBlocking import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.mockito.kotlin.any import org.mockito.kotlin.doAnswer import org.mockito.kotlin.mock import org.mockito.kotlin.stub import org.mockito.kotlin.verify /** * Test class for [ScreenCaptureHelperTest] * * Usage: atest EyeDropperUnitTests:ScreenCaptureHelperTest */ @UninstallModules(SystemServiceModule::class, MainThreadHandlerModule::class) @HiltAndroidTest @RunWith(AndroidJUnit4::class) class ScreenCaptureHelperTest { @get:Rule val hiltRule = HiltAndroidRule(this) @BindValue val mockIWindowManager = mock() @BindValue val mockInputHelper = mock() @BindValue val mockWindowHelper = mock() @BindValue val mockDisplayManager = mock() @Inject lateinit var screenCaptureHelper: ScreenCaptureHelper private val displayId = Display.DEFAULT_DISPLAY private val mockColorSpace = ColorSpace.get(ColorSpace.Named.SRGB) private val mockHardwareBuffer = HardwareBuffer.create( 1, 1, HardwareBuffer.RGBA_8888, 1, HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE ) private val mockResult = ScreenCapture.ScreenCaptureResult(mockColorSpace, mockHardwareBuffer) @Before fun setUp() { hiltRule.inject() } @Test fun captureScreenshotAsync_onSuccess_returnsConvertedBitmap() { mockIWindowManager.stub { on { screenCapture(any(), any()) } doAnswer { invocation -> val callback = invocation.arguments[1] as IScreenCaptureCallback callback.onSuccess(mockResult) null } } runBlocking { val bitmap = screenCaptureHelper.captureScreenshotAsync(displayId) assertThat(bitmap).isNotNull() assertThat(bitmap.width).isEqualTo(1) assertThat(bitmap.height).isEqualTo(1) assertThat(bitmap.config).isEqualTo(Bitmap.Config.ARGB_8888) } } @Test fun captureScreenshotAsync_onSuccess_invokesWindowManagerScreenCapture() { mockIWindowManager.stub { on { screenCapture(any(), any()) } doAnswer { invocation -> val callback = invocation.arguments[1] as IScreenCaptureCallback callback.onSuccess(mockResult) null } } runBlocking { screenCaptureHelper.captureScreenshotAsync(displayId) } verify(mockIWindowManager).screenCapture(any(), any()) // Implicitly asserts non-null } @Test fun captureScreenshotAsync_onFailure_throwsRuntimeException() = runBlocking { val errorCode = -1 mockIWindowManager.stub { on { screenCapture(any(), any()) } doAnswer { invocation -> // IScreenCaptureCallback is second param val callback = invocation.arguments[1] as IScreenCaptureCallback // Simulate error case callback.onFailure(errorCode) null } } val exception = assertFailsWith { screenCaptureHelper.captureScreenshotAsync(displayId) } verify(mockIWindowManager).screenCapture(any(), any()) // Implicitly asserts non-null assertThat(exception).hasMessageThat().contains(ScreenCaptureHelper.RUNTIME_ERROR) } @Test fun captureScreenshotAsync_onTimeout_throwsRuntimeException() = runBlocking { mockIWindowManager.stub { on { screenCapture(any(), any()) } doAnswer { } } val startTime = System.currentTimeMillis() // onSuccess/onFailure undefined, so never called. val exception = assertFailsWith { screenCaptureHelper.captureScreenshotAsync(displayId) } val endTime = System.currentTimeMillis() assertThat(exception).hasMessageThat().contains(ScreenCaptureHelper.TIMED_OUT) assertThat(endTime - startTime).isAtLeast(2000L) verify(mockIWindowManager).screenCapture(any(), any()) // Implicitly asserts non-null } }