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.settings.brightness.ui.viewmodel 18 19 import android.content.res.mainResources 20 import android.view.View 21 import android.widget.FrameLayout 22 import androidx.test.ext.junit.runners.AndroidJUnit4 23 import androidx.test.filters.SmallTest 24 import com.android.systemui.SysuiTestCase 25 import com.android.systemui.coroutines.collectLastValue 26 import com.android.systemui.kosmos.testScope 27 import com.android.systemui.res.R 28 import com.android.systemui.settings.brightness.domain.interactor.brightnessMirrorShowingInteractor 29 import com.android.systemui.settings.brightness.ui.viewModel.BrightnessMirrorViewModel 30 import com.android.systemui.settings.brightness.ui.viewModel.LocationAndSize 31 import com.android.systemui.settings.brightnessSliderControllerFactory 32 import com.android.systemui.testKosmos 33 import com.android.systemui.util.mockito.any 34 import com.android.systemui.util.mockito.mock 35 import com.android.systemui.util.mockito.whenever 36 import com.google.common.truth.Truth.assertThat 37 import kotlinx.coroutines.test.runTest 38 import org.junit.Test 39 import org.junit.runner.RunWith 40 41 @SmallTest 42 @RunWith(AndroidJUnit4::class) 43 class BrightnessMirrorViewModelTest : SysuiTestCase() { 44 45 private val kosmos = testKosmos() 46 47 private val underTest = <lambda>null48 with(kosmos) { 49 BrightnessMirrorViewModel( 50 brightnessMirrorShowingInteractor, 51 mainResources, 52 brightnessSliderControllerFactory, 53 ) 54 } 55 56 @Test showHideMirror_isShowingnull57 fun showHideMirror_isShowing() = 58 with(kosmos) { 59 testScope.runTest { 60 val showing by collectLastValue(underTest.isShowing) 61 62 assertThat(showing).isFalse() 63 64 underTest.showMirror() 65 assertThat(showing).isTrue() 66 67 underTest.hideMirror() 68 assertThat(showing).isFalse() 69 } 70 } 71 72 @Test locationInWindowAndContainer_correctLocationAndSizenull73 fun locationInWindowAndContainer_correctLocationAndSize() = 74 with(kosmos) { 75 testScope.runTest { 76 val locationAndSize by collectLastValue(underTest.locationAndSize) 77 78 val x = 20 79 val y = 100 80 val height = 50 81 val width = 200 82 val padding = 83 mainResources.getDimensionPixelSize(R.dimen.rounded_slider_background_padding) 84 85 val mockView = 86 mock<View> { 87 whenever(getLocationInWindow(any())).then { 88 it.getArgument<IntArray>(0).apply { 89 this[0] = x 90 this[1] = y 91 } 92 Unit 93 } 94 95 whenever(measuredHeight).thenReturn(height) 96 whenever(measuredWidth).thenReturn(width) 97 } 98 val yOffsetFromContainer = setContainerViewHierarchy(mockView) 99 100 underTest.setLocationAndSize(mockView) 101 102 assertThat(locationAndSize) 103 .isEqualTo( 104 // Adjust for padding around the view 105 LocationAndSize( 106 yOffsetFromWindow = y - padding, 107 yOffsetFromContainer = yOffsetFromContainer - padding, 108 width = width + 2 * padding, 109 height = height + 2 * padding, 110 ) 111 ) 112 } 113 } 114 setContainerViewHierarchynull115 private fun setContainerViewHierarchy(mockView: View): Int { 116 val rootView = FrameLayout(context) 117 val containerView = FrameLayout(context).apply { id = R.id.quick_settings_container } 118 val otherView = FrameLayout(context) 119 120 rootView.addView(containerView) 121 containerView.addView(otherView) 122 otherView.addView(mockView) 123 124 containerView.setLeftTopRightBottom(1, /* top= */ 1, 1, 1) 125 otherView.setLeftTopRightBottom(0, /* top= */ 2, 0, 0) 126 whenever(mockView.parent).thenReturn(otherView) 127 whenever(mockView.top).thenReturn(3) 128 129 return 2 + 3 130 } 131 } 132