/* * 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.ui import android.graphics.Bitmap import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertIsFocused import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.android.eyedropper.ui.touchscreen.TouchscreenMainScreen import com.android.eyedropper.ui.touchscreen.TouchscreenMainScreenTestTags import com.android.eyedropper.ui.touchscreen.TouchscreenReticleState import com.android.eyedropper.ui.touchscreen.TouchscreenViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import org.mockito.kotlin.verify @RunWith(AndroidJUnit4::class) class TouchscreenMainScreenTest { @get:Rule val composeTestRule = createComposeRule() private val mockSendColorResult = mock<() -> Unit>() private val mockSendAbortResult = mock<() -> Unit>() private val testBitmap = Bitmap.createBitmap(20, 20, Bitmap.Config.ARGB_8888) private val testImageBitmap = testBitmap.asImageBitmap() private val imageBitmapStateFlow = MutableStateFlow(testImageBitmap) private val pointerPositionStateFlow = MutableStateFlow(Offset(10f, 10f)) private val uiStateStateFlow = MutableStateFlow(TouchscreenReticleState()) private val mockViewModel = mock { on { imageBitmap } doReturn imageBitmapStateFlow.asStateFlow() on { uiState } doReturn uiStateStateFlow.asStateFlow() on { bitmapPointerPosition } doReturn pointerPositionStateFlow.asStateFlow() on { sendColorResult } doReturn mockSendColorResult on { sendAbortResult } doReturn mockSendAbortResult } @Before fun setup() { val mockOnColorSelected = mock<(Int) -> Unit>() val mockOnAbort = mock<() -> Unit>() mockViewModel.initialize( bitmap = testBitmap, onColorSelected = mockOnColorSelected, onSelectionCanceled = mockOnAbort, ) } @Test fun touchscreenMainScreen_onLaunch_takesFocus() { composeTestRule.setContent { TouchscreenMainScreen(viewModel = mockViewModel) } composeTestRule.onNodeWithTag(TouchscreenMainScreenTestTags.MAIN_BOX) .assertExists() composeTestRule.onNodeWithTag(TouchscreenMainScreenTestTags.MAIN_BOX) .assertIsFocused() composeTestRule.onNodeWithTag(TouchscreenMainScreenTestTags.MAIN_BOX) .assertIsDisplayed() composeTestRule.onNodeWithTag(TouchscreenMainScreenTestTags.SCREENSHOT_RETICLE_BOX) .assertIsDisplayed() composeTestRule.onNodeWithTag(TouchscreenMainScreenTestTags.CONTROL_BAR_BOX) .assertIsDisplayed() } @Test fun touchscreenMainScreen_controlBar_selectClicked() { composeTestRule.setContent { TouchscreenMainScreen(viewModel = mockViewModel) } composeTestRule.onNodeWithText("Apply").performClick() verify(mockViewModel.sendColorResult)?.invoke() } @Test fun touchscreenMainScreen_controlBar_cancelClicked() { composeTestRule.setContent { TouchscreenMainScreen(viewModel = mockViewModel) } composeTestRule.onNodeWithText("Cancel").performClick() verify(mockViewModel.sendAbortResult)?.invoke() } }