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