1 /* 2 * Copyright (C) 2023 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.statusbar.phone.fragment 18 19 import android.testing.AndroidTestingRunner 20 import android.testing.TestableLooper 21 import android.view.View 22 import androidx.test.filters.SmallTest 23 import com.android.systemui.SysuiTestCase 24 import com.android.systemui.animation.AnimatorTestRule 25 import junit.framework.Assert.assertEquals 26 import org.junit.Before 27 import org.junit.Rule 28 import org.junit.Test 29 import org.junit.runner.RunWith 30 31 private const val TEST_SOURCE_1 = 1 32 private const val TEST_SOURCE_2 = 2 33 private const val TEST_ANIMATION_DURATION = 100L 34 private const val INITIAL_ALPHA = 1f 35 36 @RunWith(AndroidTestingRunner::class) 37 @TestableLooper.RunWithLooper(setAsMainLooper = true) 38 @SmallTest 39 class MultiSourceMinAlphaControllerTest : SysuiTestCase() { 40 41 private val view = View(context) 42 private val multiSourceMinAlphaController = 43 MultiSourceMinAlphaController(view, initialAlpha = INITIAL_ALPHA) 44 45 @get:Rule val animatorTestRule = AnimatorTestRule(this) 46 47 @Before setupnull48 fun setup() { 49 multiSourceMinAlphaController.reset() 50 } 51 52 @Test testSetAlphanull53 fun testSetAlpha() { 54 multiSourceMinAlphaController.setAlpha(alpha = 0.5f, sourceId = TEST_SOURCE_1) 55 assertEquals(0.5f, view.alpha) 56 } 57 58 @Test testAnimateToAlphanull59 fun testAnimateToAlpha() { 60 multiSourceMinAlphaController.animateToAlpha( 61 alpha = 0.5f, 62 sourceId = TEST_SOURCE_1, 63 duration = TEST_ANIMATION_DURATION 64 ) 65 animatorTestRule.advanceTimeBy(TEST_ANIMATION_DURATION) 66 assertEquals(0.5f, view.alpha) 67 } 68 69 @Test testResetnull70 fun testReset() { 71 multiSourceMinAlphaController.animateToAlpha( 72 alpha = 0.5f, 73 sourceId = TEST_SOURCE_1, 74 duration = TEST_ANIMATION_DURATION 75 ) 76 multiSourceMinAlphaController.setAlpha(alpha = 0.7f, sourceId = TEST_SOURCE_2) 77 multiSourceMinAlphaController.reset() 78 // advance time to ensure that animators are cancelled when the controller is reset 79 animatorTestRule.advanceTimeBy(TEST_ANIMATION_DURATION) 80 assertEquals(INITIAL_ALPHA, view.alpha) 81 } 82 83 @Test testMinOfTwoSourcesIsAppliednull84 fun testMinOfTwoSourcesIsApplied() { 85 multiSourceMinAlphaController.setAlpha(alpha = 0f, sourceId = TEST_SOURCE_1) 86 multiSourceMinAlphaController.setAlpha(alpha = 0.5f, sourceId = TEST_SOURCE_2) 87 assertEquals(0f, view.alpha) 88 multiSourceMinAlphaController.setAlpha(alpha = 1f, sourceId = TEST_SOURCE_1) 89 assertEquals(0.5f, view.alpha) 90 } 91 92 @Test testSetAlphaForSameSourceCancelsAnimatornull93 fun testSetAlphaForSameSourceCancelsAnimator() { 94 multiSourceMinAlphaController.animateToAlpha( 95 alpha = 0f, 96 sourceId = TEST_SOURCE_1, 97 duration = TEST_ANIMATION_DURATION 98 ) 99 animatorTestRule.advanceTimeBy(TEST_ANIMATION_DURATION / 2) 100 multiSourceMinAlphaController.setAlpha(alpha = 1f, sourceId = TEST_SOURCE_1) 101 animatorTestRule.advanceTimeBy(TEST_ANIMATION_DURATION / 2) 102 // verify that animation was cancelled and the setAlpha call overrides the alpha value of 103 // the animation 104 assertEquals(1f, view.alpha) 105 } 106 } 107