/* * 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.desktop import android.content.ComponentName import android.graphics.Bitmap import android.os.IBinder import androidx.test.ext.junit.runners.AndroidJUnit4 import com.google.common.truth.Truth.assertThat import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest import kotlinx.coroutines.Deferred 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.eq import org.mockito.kotlin.mock import org.mockito.kotlin.verify /** * Test class for [DesktopServiceConnectionTest] * * Usage: atest EyeDropperUnitTests:DesktopServiceConnectionTest */ @HiltAndroidTest @RunWith(AndroidJUnit4::class) class DesktopServiceConnectionTest { @get:Rule val hiltRule = HiltAndroidRule(this) private lateinit var serviceConnection: DesktopServiceConnection private val mockOnSelectColor = mock<((Int) -> Unit)>() private val mockOnAbort = mock<(() -> Unit)>() private val mockDesktopController = mock() private val mockBinder = mock { on { getService() } doReturn mockDesktopController } private val mockIBinder: IBinder = mockBinder private val mockDeferredScreenshotsMap = mock>>() private val componentName = ComponentName("com.android.eyedropper.desktop", "DesktopController") @Before fun setUp() { hiltRule.inject() serviceConnection = DesktopServiceConnection( mockDeferredScreenshotsMap, mockOnSelectColor, mockOnAbort) } @Test fun constructor_initializesState() { assertThat(serviceConnection.isBound).isFalse() } @Test fun onServiceConnected_getsServiceAndShowsOverlay() { assertThat(serviceConnection.isBound).isFalse() serviceConnection.onServiceConnected(componentName, mockIBinder) verify(mockBinder).getService() verify(mockDesktopController).showUiOverlay(eq(mockOnSelectColor), eq(mockOnAbort)) assertThat(serviceConnection.isBound).isTrue() } @Test fun onServiceDisconnected_updatesBoundState() { serviceConnection.onServiceConnected(componentName, mockIBinder) assertThat(serviceConnection.isBound).isTrue() serviceConnection.onServiceDisconnected(componentName) assertThat(serviceConnection.isBound).isFalse() } }