1 package com.android.systemui.animation.back
2 
3 import android.util.DisplayMetrics
4 import android.window.BackEvent
5 import androidx.test.ext.junit.runners.AndroidJUnit4
6 import androidx.test.filters.SmallTest
7 import com.android.systemui.SysuiTestCase
8 import com.android.systemui.util.mockito.argumentCaptor
9 import com.android.systemui.util.mockito.capture
10 import com.android.systemui.util.mockito.mock
11 import com.google.common.truth.Truth.assertThat
12 import org.junit.Test
13 import org.junit.runner.RunWith
14 import org.mockito.Mockito.verify
15 
16 @SmallTest
17 @RunWith(AndroidJUnit4::class)
18 class OnBackAnimationCallbackExtensionTest : SysuiTestCase() {
19     private val onBackProgress: (BackTransformation) -> Unit = mock()
20     private val onBackStart: (BackEvent) -> Unit = mock()
21     private val onBackInvoke: () -> Unit = mock()
22     private val onBackCancel: () -> Unit = mock()
23 
24     private val displayMetrics =
<lambda>null25         DisplayMetrics().apply {
26             widthPixels = 100
27             heightPixels = 100
28             density = 1f
29         }
30 
31     private val onBackAnimationCallback =
32         onBackAnimationCallbackFrom(
<lambda>null33             backAnimationSpec = BackAnimationSpec.floatingSystemSurfacesForSysUi { displayMetrics },
34             displayMetrics = displayMetrics,
35             onBackProgressed = onBackProgress,
36             onBackStarted = onBackStart,
37             onBackInvoked = onBackInvoke,
38             onBackCancelled = onBackCancel,
39         )
40 
41     @Test
onBackProgressed_shouldInvoke_onBackProgressnull42     fun onBackProgressed_shouldInvoke_onBackProgress() {
43         val backEvent =
44             BackEvent(
45                 /* touchX = */ 0f,
46                 /* touchY = */ 0f,
47                 /* progress = */ 0f,
48                 /* swipeEdge = */ BackEvent.EDGE_LEFT
49             )
50         onBackAnimationCallback.onBackStarted(backEvent)
51 
52         onBackAnimationCallback.onBackProgressed(backEvent)
53 
54         val argumentCaptor = argumentCaptor<BackTransformation>()
55         verify(onBackProgress).invoke(capture(argumentCaptor))
56 
57         val actual = argumentCaptor.value
58         val tolerance = 0.0001f
59         assertThat(actual.translateX).isWithin(tolerance).of(0f)
60         assertThat(actual.translateY).isWithin(tolerance).of(0f)
61         assertThat(actual.scale).isWithin(tolerance).of(1f)
62     }
63 
64     @Test
onBackStarted_shouldInvoke_onBackStartnull65     fun onBackStarted_shouldInvoke_onBackStart() {
66         val backEvent =
67             BackEvent(
68                 /* touchX = */ 0f,
69                 /* touchY = */ 0f,
70                 /* progress = */ 0f,
71                 /* swipeEdge = */ BackEvent.EDGE_LEFT
72             )
73 
74         onBackAnimationCallback.onBackStarted(backEvent)
75 
76         verify(onBackStart).invoke(backEvent)
77     }
78 
79     @Test
onBackInvoked_shouldInvoke_onBackInvokenull80     fun onBackInvoked_shouldInvoke_onBackInvoke() {
81         onBackAnimationCallback.onBackInvoked()
82 
83         verify(onBackInvoke).invoke()
84     }
85 }
86