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.surfaceeffects.glowboxeffect
18 
19 import android.graphics.Color
20 import android.graphics.Paint
21 import android.testing.TestableLooper
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.animation.AnimatorTestRule
26 import com.android.systemui.surfaceeffects.PaintDrawCallback
27 import com.google.common.truth.Truth.assertThat
28 import org.junit.Before
29 import org.junit.Rule
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 
33 @SmallTest
34 @RunWith(AndroidJUnit4::class)
35 @TestableLooper.RunWithLooper(setAsMainLooper = true)
36 class GlowBoxEffectTest : SysuiTestCase() {
37 
38     @get:Rule val animatorTestRule = AnimatorTestRule(this)
39     private lateinit var config: GlowBoxConfig
40     private lateinit var glowBoxEffect: GlowBoxEffect
41     private lateinit var drawCallback: PaintDrawCallback
42 
43     @Before
setupnull44     fun setup() {
45         drawCallback =
46             object : PaintDrawCallback {
47                 override fun onDraw(paint: Paint) {}
48             }
49         config =
50             GlowBoxConfig(
51                 startCenterX = 0f,
52                 startCenterY = 0f,
53                 endCenterX = 0f,
54                 endCenterY = 0f,
55                 width = 1f,
56                 height = 1f,
57                 color = Color.WHITE,
58                 blurAmount = 0.1f,
59                 duration = 100L,
60                 easeInDuration = 100L,
61                 easeOutDuration = 100L
62             )
63         glowBoxEffect = GlowBoxEffect(config, drawCallback)
64     }
65 
66     @Test
play_paintCallback_triggersDrawCallbacknull67     fun play_paintCallback_triggersDrawCallback() {
68         var paintFromCallback: Paint? = null
69         drawCallback =
70             object : PaintDrawCallback {
71                 override fun onDraw(paint: Paint) {
72                     paintFromCallback = paint
73                 }
74             }
75         glowBoxEffect = GlowBoxEffect(config, drawCallback)
76 
77         assertThat(paintFromCallback).isNull()
78 
79         glowBoxEffect.play()
80         animatorTestRule.advanceTimeBy(50L)
81 
82         assertThat(paintFromCallback).isNotNull()
83     }
84 
85     @Test
play_followsAnimationStateInOrdernull86     fun play_followsAnimationStateInOrder() {
87         assertThat(glowBoxEffect.state).isEqualTo(GlowBoxEffect.AnimationState.NOT_PLAYING)
88 
89         glowBoxEffect.play()
90 
91         assertThat(glowBoxEffect.state).isEqualTo(GlowBoxEffect.AnimationState.EASE_IN)
92 
93         animatorTestRule.advanceAnimationDuration(config.easeInDuration + 50L)
94 
95         assertThat(glowBoxEffect.state).isEqualTo(GlowBoxEffect.AnimationState.MAIN)
96 
97         animatorTestRule.advanceAnimationDuration(config.duration + 50L)
98 
99         assertThat(glowBoxEffect.state).isEqualTo(GlowBoxEffect.AnimationState.EASE_OUT)
100 
101         animatorTestRule.advanceAnimationDuration(config.easeOutDuration + 50L)
102 
103         assertThat(glowBoxEffect.state).isEqualTo(GlowBoxEffect.AnimationState.NOT_PLAYING)
104     }
105 
106     @Test
finish_statePlaying_finishesAnimationnull107     fun finish_statePlaying_finishesAnimation() {
108         assertThat(glowBoxEffect.state).isEqualTo(GlowBoxEffect.AnimationState.NOT_PLAYING)
109 
110         glowBoxEffect.play()
111         glowBoxEffect.finish()
112 
113         assertThat(glowBoxEffect.state).isEqualTo(GlowBoxEffect.AnimationState.EASE_OUT)
114     }
115 
116     @Test
finish_stateNotPlaying_doesNotFinishAnimationnull117     fun finish_stateNotPlaying_doesNotFinishAnimation() {
118         assertThat(glowBoxEffect.state).isEqualTo(GlowBoxEffect.AnimationState.NOT_PLAYING)
119 
120         glowBoxEffect.finish()
121 
122         assertThat(glowBoxEffect.state).isEqualTo(GlowBoxEffect.AnimationState.NOT_PLAYING)
123     }
124 }
125