1 package com.android.launcher3.taskbar.navbutton 2 3 import android.content.res.Resources 4 import android.view.Surface 5 import android.view.Surface.ROTATION_270 6 import android.view.Surface.Rotation 7 import android.view.View 8 import android.view.ViewGroup 9 import android.widget.FrameLayout 10 import android.widget.ImageView 11 import android.widget.LinearLayout 12 import androidx.test.runner.AndroidJUnit4 13 import com.android.launcher3.DeviceProfile 14 import com.android.launcher3.R 15 import com.android.launcher3.taskbar.TaskbarManager 16 import java.lang.IllegalStateException 17 import org.junit.Assume.assumeTrue 18 import org.junit.Before 19 import org.junit.Test 20 import org.junit.runner.RunWith 21 import org.mockito.Mock 22 import org.mockito.Mockito.`when` as whenever 23 import org.mockito.MockitoAnnotations 24 25 @RunWith(AndroidJUnit4::class) 26 class NavButtonLayoutFactoryTest { 27 28 @Mock lateinit var mockDeviceProfile: DeviceProfile 29 @Mock lateinit var mockParentButtonContainer: FrameLayout 30 @Mock lateinit var mockNavLayout: LinearLayout 31 @Mock lateinit var mockStartContextualLayout: ViewGroup 32 @Mock lateinit var mockEndContextualLayout: ViewGroup 33 @Mock lateinit var mockResources: Resources 34 @Mock lateinit var mockBackButton: ImageView 35 @Mock lateinit var mockRecentsButton: ImageView 36 @Mock lateinit var mockHomeButton: ImageView 37 38 private var surfaceRotation = Surface.ROTATION_0 39 40 @Before setupnull41 fun setup() { 42 MockitoAnnotations.initMocks(this) 43 44 // Init end nav buttons 45 whenever(mockNavLayout.childCount).thenReturn(3) 46 whenever(mockNavLayout.findViewById<View>(R.id.back)).thenReturn(mockBackButton) 47 whenever(mockNavLayout.findViewById<View>(R.id.home)).thenReturn(mockHomeButton) 48 whenever(mockNavLayout.findViewById<View>(R.id.recent_apps)).thenReturn(mockRecentsButton) 49 50 // Init top level layout 51 whenever(mockParentButtonContainer.findViewById<LinearLayout>(R.id.end_nav_buttons)) 52 .thenReturn(mockNavLayout) 53 whenever(mockParentButtonContainer.findViewById<ViewGroup>(R.id.end_contextual_buttons)) 54 .thenReturn(mockEndContextualLayout) 55 whenever(mockParentButtonContainer.findViewById<ViewGroup>(R.id.start_contextual_buttons)) 56 .thenReturn(mockStartContextualLayout) 57 } 58 59 @Test getKidsLayoutternull60 fun getKidsLayoutter() { 61 assumeTrue(TaskbarManager.FLAG_HIDE_NAVBAR_WINDOW) 62 mockDeviceProfile.isTaskbarPresent = true 63 val layoutter: NavButtonLayoutFactory.NavButtonLayoutter = 64 getLayoutter( 65 isKidsMode = true, 66 isInSetup = false, 67 isThreeButtonNav = false, 68 phoneMode = false, 69 surfaceRotation = surfaceRotation 70 ) 71 assert(layoutter is KidsNavLayoutter) 72 } 73 74 @Test getSetupLayoutternull75 fun getSetupLayoutter() { 76 assumeTrue(TaskbarManager.FLAG_HIDE_NAVBAR_WINDOW) 77 mockDeviceProfile.isTaskbarPresent = true 78 val layoutter: NavButtonLayoutFactory.NavButtonLayoutter = 79 getLayoutter( 80 isKidsMode = false, 81 isInSetup = true, 82 isThreeButtonNav = false, 83 phoneMode = false, 84 surfaceRotation = surfaceRotation 85 ) 86 assert(layoutter is SetupNavLayoutter) 87 } 88 89 @Test getTaskbarNavLayoutternull90 fun getTaskbarNavLayoutter() { 91 assumeTrue(TaskbarManager.FLAG_HIDE_NAVBAR_WINDOW) 92 mockDeviceProfile.isTaskbarPresent = true 93 val layoutter: NavButtonLayoutFactory.NavButtonLayoutter = 94 getLayoutter( 95 isKidsMode = false, 96 isInSetup = false, 97 isThreeButtonNav = false, 98 phoneMode = false, 99 surfaceRotation = surfaceRotation 100 ) 101 assert(layoutter is TaskbarNavLayoutter) 102 } 103 104 @Test(expected = IllegalStateException::class) noValidLayoutForLargeScreenTaskbarNotPresentnull105 fun noValidLayoutForLargeScreenTaskbarNotPresent() { 106 assumeTrue(TaskbarManager.FLAG_HIDE_NAVBAR_WINDOW) 107 mockDeviceProfile.isTaskbarPresent = false 108 getLayoutter( 109 isKidsMode = false, 110 isInSetup = false, 111 isThreeButtonNav = false, 112 phoneMode = false, 113 surfaceRotation = surfaceRotation 114 ) 115 } 116 117 @Test getTaskbarPortraitLayoutternull118 fun getTaskbarPortraitLayoutter() { 119 assumeTrue(TaskbarManager.FLAG_HIDE_NAVBAR_WINDOW) 120 mockDeviceProfile.isTaskbarPresent = false 121 val layoutter: NavButtonLayoutFactory.NavButtonLayoutter = 122 getLayoutter( 123 isKidsMode = false, 124 isInSetup = false, 125 isThreeButtonNav = true, 126 phoneMode = true, 127 surfaceRotation = surfaceRotation 128 ) 129 assert(layoutter is PhonePortraitNavLayoutter) 130 } 131 132 @Test getTaskbarLandscapeLayoutternull133 fun getTaskbarLandscapeLayoutter() { 134 assumeTrue(TaskbarManager.FLAG_HIDE_NAVBAR_WINDOW) 135 mockDeviceProfile.isTaskbarPresent = false 136 setDeviceProfileLandscape() 137 val layoutter: NavButtonLayoutFactory.NavButtonLayoutter = 138 getLayoutter( 139 isKidsMode = false, 140 isInSetup = false, 141 isThreeButtonNav = true, 142 phoneMode = true, 143 surfaceRotation = surfaceRotation 144 ) 145 assert(layoutter is PhoneLandscapeNavLayoutter) 146 } 147 148 @Test getTaskbarSeascapeLayoutternull149 fun getTaskbarSeascapeLayoutter() { 150 assumeTrue(TaskbarManager.FLAG_HIDE_NAVBAR_WINDOW) 151 mockDeviceProfile.isTaskbarPresent = false 152 setDeviceProfileLandscape() 153 val layoutter: NavButtonLayoutFactory.NavButtonLayoutter = 154 getLayoutter( 155 isKidsMode = false, 156 isInSetup = false, 157 isThreeButtonNav = true, 158 phoneMode = true, 159 surfaceRotation = ROTATION_270 160 ) 161 assert(layoutter is PhoneSeascapeNavLayoutter) 162 } 163 164 @Test(expected = IllegalStateException::class) noValidLayoutForPhoneGestureNavnull165 fun noValidLayoutForPhoneGestureNav() { 166 assumeTrue(TaskbarManager.FLAG_HIDE_NAVBAR_WINDOW) 167 mockDeviceProfile.isTaskbarPresent = false 168 getLayoutter( 169 isKidsMode = false, 170 isInSetup = false, 171 isThreeButtonNav = false, 172 phoneMode = true, 173 surfaceRotation = surfaceRotation 174 ) 175 } 176 setDeviceProfileLandscapenull177 private fun setDeviceProfileLandscape() { 178 // Use reflection to modify landscape field 179 val landscapeField = mockDeviceProfile.javaClass.getDeclaredField("isLandscape") 180 landscapeField.isAccessible = true 181 landscapeField.set(mockDeviceProfile, true) 182 } 183 getLayoutternull184 private fun getLayoutter( 185 isKidsMode: Boolean, 186 isInSetup: Boolean, 187 isThreeButtonNav: Boolean, 188 phoneMode: Boolean, 189 @Rotation surfaceRotation: Int 190 ): NavButtonLayoutFactory.NavButtonLayoutter { 191 return NavButtonLayoutFactory.getUiLayoutter( 192 deviceProfile = mockDeviceProfile, 193 navButtonsView = mockParentButtonContainer, 194 resources = mockResources, 195 isKidsMode = isKidsMode, 196 isInSetup = isInSetup, 197 isThreeButtonNav = isThreeButtonNav, 198 phoneMode = phoneMode, 199 surfaceRotation = surfaceRotation 200 ) 201 } 202 } 203