1 /*
2  * Copyright (C) 2021 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.util
18 
19 import android.app.WallpaperInfo
20 import android.app.WallpaperManager
21 import android.os.IBinder
22 import android.testing.AndroidTestingRunner
23 import android.testing.TestableLooper.RunWithLooper
24 import android.view.View
25 import android.view.ViewRootImpl
26 import androidx.test.filters.SmallTest
27 import com.android.systemui.SysuiTestCase
28 import com.android.systemui.util.mockito.eq
29 import com.android.systemui.wallpapers.data.repository.FakeWallpaperRepository
30 import org.junit.Before
31 import org.junit.Rule
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 import org.mockito.Mock
35 import org.mockito.Mockito.`when`
36 import org.mockito.Mockito.any
37 import org.mockito.Mockito.anyFloat
38 import org.mockito.Mockito.clearInvocations
39 import org.mockito.Mockito.doThrow
40 import org.mockito.Mockito.times
41 import org.mockito.Mockito.verify
42 import org.mockito.Mockito.mock
43 import org.mockito.Mockito.never
44 import org.mockito.Mockito.`when` as whenever
45 import org.mockito.junit.MockitoJUnit
46 
47 @RunWith(AndroidTestingRunner::class)
48 @RunWithLooper
49 @SmallTest
50 class WallpaperControllerTest : SysuiTestCase() {
51 
52     @Mock
53     private lateinit var wallpaperManager: WallpaperManager
54     @Mock
55     private lateinit var root: View
56     @Mock
57     private lateinit var viewRootImpl: ViewRootImpl
58     @Mock
59     private lateinit var windowToken: IBinder
60     private val wallpaperRepository = FakeWallpaperRepository()
61 
62     @JvmField
63     @Rule
64     val mockitoRule = MockitoJUnit.rule()
65 
66     private lateinit var wallaperController: WallpaperController
67 
68     @Before
69     fun setup() {
70         `when`(root.viewRootImpl).thenReturn(viewRootImpl)
71         `when`(root.windowToken).thenReturn(windowToken)
72         `when`(root.isAttachedToWindow).thenReturn(true)
73 
74         wallaperController = WallpaperController(wallpaperManager, wallpaperRepository)
75 
76         wallaperController.rootView = root
77     }
78 
79     @Test
80     fun setNotificationShadeZoom_updatesWallpaperManagerZoom() {
81         wallaperController.setNotificationShadeZoom(0.5f)
82 
83         verify(wallpaperManager).setWallpaperZoomOut(any(), eq(0.5f))
84     }
85 
86     @Test
87     fun setUnfoldTransitionZoom_updatesWallpaperManagerZoom() {
88         wallaperController.setUnfoldTransitionZoom(0.5f)
89 
90         verify(wallpaperManager).setWallpaperZoomOut(any(), eq(0.5f))
91     }
92 
93     @Test
94     fun setUnfoldTransitionZoom_defaultUnfoldTransitionIsDisabled_doesNotUpdateWallpaperZoom() {
95         wallpaperRepository.wallpaperInfo.value = createWallpaperInfo(
96             useDefaultTransition = false
97         )
98 
99         wallaperController.setUnfoldTransitionZoom(0.5f)
100 
101         verify(wallpaperManager, never()).setWallpaperZoomOut(any(), anyFloat())
102     }
103 
104     @Test
105     fun setUnfoldTransitionZoomAndNotificationShadeZoom_updatesWithMaximumZoom() {
106         wallaperController.setUnfoldTransitionZoom(0.7f)
107         clearInvocations(wallpaperManager)
108 
109         wallaperController.setNotificationShadeZoom(0.5f)
110 
111         verify(wallpaperManager).setWallpaperZoomOut(any(), eq(0.7f))
112     }
113 
114     @Test
115     fun setNotificationShadeZoomAndThenUnfoldTransition_updatesWithMaximumZoom() {
116         wallaperController.setNotificationShadeZoom(0.7f)
117         clearInvocations(wallpaperManager)
118 
119         wallaperController.setUnfoldTransitionZoom(0.5f)
120 
121         verify(wallpaperManager).setWallpaperZoomOut(any(), eq(0.7f))
122     }
123 
124     @Test
125     fun setNotificationZoom_invalidWindow_doesNotSetZoom() {
126         `when`(root.isAttachedToWindow).thenReturn(false)
127 
128         verify(wallpaperManager, times(0)).setWallpaperZoomOut(any(), anyFloat())
129     }
130 
131     @Test
132     fun setNotificationZoom_exceptionWhenUpdatingZoom_doesNotFail() {
133         doThrow(IllegalArgumentException("test exception")).`when`(wallpaperManager)
134             .setWallpaperZoomOut(any(), anyFloat())
135 
136         wallaperController.setNotificationShadeZoom(0.5f)
137 
138         verify(wallpaperManager).setWallpaperZoomOut(any(), anyFloat())
139     }
140 
141     private fun createWallpaperInfo(useDefaultTransition: Boolean = true): WallpaperInfo {
142         val info = mock(WallpaperInfo::class.java)
143         whenever(info.shouldUseDefaultUnfoldTransition())
144             .thenReturn(useDefaultTransition)
145         return info
146     }
147 }
148