Charles Chen | 54fce2c | 2019-03-13 18:17:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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; |
| 18 | |
| 19 | import static android.view.Display.DEFAULT_DISPLAY; |
| 20 | import static android.view.Display.INVALID_DISPLAY; |
| 21 | |
| 22 | import static org.mockito.ArgumentMatchers.any; |
| 23 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 24 | import static org.mockito.ArgumentMatchers.anyInt; |
| 25 | import static org.mockito.ArgumentMatchers.anyLong; |
| 26 | import static org.mockito.ArgumentMatchers.eq; |
| 27 | import static org.mockito.Mockito.doNothing; |
| 28 | import static org.mockito.Mockito.doReturn; |
| 29 | import static org.mockito.Mockito.mock; |
| 30 | import static org.mockito.Mockito.never; |
| 31 | import static org.mockito.Mockito.spy; |
| 32 | import static org.mockito.Mockito.verify; |
| 33 | import static org.mockito.Mockito.when; |
| 34 | |
| 35 | import android.content.Context; |
| 36 | import android.hardware.display.DisplayManager; |
| 37 | import android.testing.AndroidTestingRunner; |
| 38 | import android.testing.TestableLooper.RunWithLooper; |
| 39 | import android.util.SparseArray; |
| 40 | import android.view.Display; |
| 41 | import android.view.WindowManager; |
| 42 | |
| 43 | import androidx.test.filters.SmallTest; |
| 44 | |
| 45 | import com.android.systemui.Dependency; |
| 46 | import com.android.systemui.SysuiTestCase; |
| 47 | import com.android.systemui.statusbar.phone.NavigationBarFragment; |
| 48 | |
| 49 | import org.junit.After; |
| 50 | import org.junit.Before; |
| 51 | import org.junit.Test; |
| 52 | import org.junit.runner.RunWith; |
| 53 | |
| 54 | /** atest NavigationBarControllerTest */ |
| 55 | @RunWith(AndroidTestingRunner.class) |
| 56 | @RunWithLooper |
| 57 | @SmallTest |
| 58 | public class NavigationBarControllerTest extends SysuiTestCase { |
| 59 | |
| 60 | private NavigationBarController mNavigationBarController; |
| 61 | private Display mDisplay; |
| 62 | private NavigationBarFragment mDefaultNavBar; |
| 63 | private NavigationBarFragment mSecondaryNavBar; |
| 64 | |
| 65 | private static final int SECONDARY_DISPLAY = 1; |
| 66 | |
| 67 | @Before |
| 68 | public void setUp() { |
| 69 | mContext.putComponent(CommandQueue.class, mock(CommandQueue.class)); |
| 70 | mNavigationBarController = spy( |
| 71 | new NavigationBarController(mContext, Dependency.get(Dependency.MAIN_HANDLER))); |
| 72 | initializeNavigationBars(); |
| 73 | } |
| 74 | |
| 75 | private void initializeNavigationBars() { |
| 76 | mNavigationBarController.mNavigationBars = mock(SparseArray.class); |
| 77 | mDefaultNavBar = mock(NavigationBarFragment.class); |
| 78 | mDefaultNavBar.mDisplayId = DEFAULT_DISPLAY; |
| 79 | doReturn(mDefaultNavBar) |
| 80 | .when(mNavigationBarController.mNavigationBars).get(DEFAULT_DISPLAY); |
| 81 | |
| 82 | mSecondaryNavBar = mock(NavigationBarFragment.class); |
| 83 | mSecondaryNavBar.mDisplayId = SECONDARY_DISPLAY; |
| 84 | doReturn(mSecondaryNavBar) |
| 85 | .when(mNavigationBarController.mNavigationBars).get(SECONDARY_DISPLAY); |
| 86 | } |
| 87 | |
| 88 | @After |
| 89 | public void tearDown() { |
| 90 | mNavigationBarController = null; |
| 91 | mDisplay = null; |
| 92 | mDefaultNavBar = null; |
| 93 | mSecondaryNavBar = null; |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testCreateNavigationBarsIncludeDefaultTrue() { |
| 98 | initializeDisplayManager(); |
Matthew Ng | 9438065 | 2019-04-08 13:43:07 -0700 | [diff] [blame] | 99 | doNothing().when(mNavigationBarController).createNavigationBar(any(), any()); |
Charles Chen | 54fce2c | 2019-03-13 18:17:29 +0800 | [diff] [blame] | 100 | |
Matthew Ng | 9438065 | 2019-04-08 13:43:07 -0700 | [diff] [blame] | 101 | mNavigationBarController.createNavigationBars(true, null); |
Charles Chen | 54fce2c | 2019-03-13 18:17:29 +0800 | [diff] [blame] | 102 | |
Matthew Ng | 9438065 | 2019-04-08 13:43:07 -0700 | [diff] [blame] | 103 | verify(mNavigationBarController).createNavigationBar(any(Display.class), any()); |
Charles Chen | 54fce2c | 2019-03-13 18:17:29 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testCreateNavigationBarsIncludeDefaultFalse() { |
| 108 | initializeDisplayManager(); |
Matthew Ng | 9438065 | 2019-04-08 13:43:07 -0700 | [diff] [blame] | 109 | doNothing().when(mNavigationBarController).createNavigationBar(any(), any()); |
Charles Chen | 54fce2c | 2019-03-13 18:17:29 +0800 | [diff] [blame] | 110 | |
Matthew Ng | 9438065 | 2019-04-08 13:43:07 -0700 | [diff] [blame] | 111 | mNavigationBarController.createNavigationBars(false, null); |
Charles Chen | 54fce2c | 2019-03-13 18:17:29 +0800 | [diff] [blame] | 112 | |
Matthew Ng | 9438065 | 2019-04-08 13:43:07 -0700 | [diff] [blame] | 113 | verify(mNavigationBarController, never()).createNavigationBar(any(), any()); |
Charles Chen | 54fce2c | 2019-03-13 18:17:29 +0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | private void initializeDisplayManager() { |
| 117 | DisplayManager displayManager = mock(DisplayManager.class); |
| 118 | mDisplay = mContext.getSystemService(WindowManager.class).getDefaultDisplay(); |
| 119 | Display[] displays = {mDisplay}; |
| 120 | when(displayManager.getDisplays()).thenReturn(displays); |
| 121 | mContext.addMockSystemService(Context.DISPLAY_SERVICE, displayManager); |
| 122 | } |
| 123 | |
| 124 | // Tests if NPE occurs when call checkNavBarModes() with invalid display. |
| 125 | @Test |
| 126 | public void testCheckNavBarModesWithInvalidDisplay() { |
| 127 | mNavigationBarController.checkNavBarModes(INVALID_DISPLAY); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testCheckNavBarModesWithDefaultDisplay() { |
| 132 | doNothing().when(mDefaultNavBar).checkNavBarModes(); |
| 133 | |
| 134 | mNavigationBarController.checkNavBarModes(DEFAULT_DISPLAY); |
| 135 | |
| 136 | verify(mDefaultNavBar).checkNavBarModes(); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void testCheckNavBarModesWithSecondaryDisplay() { |
| 141 | doNothing().when(mSecondaryNavBar).checkNavBarModes(); |
| 142 | |
| 143 | mNavigationBarController.checkNavBarModes(SECONDARY_DISPLAY); |
| 144 | |
| 145 | verify(mSecondaryNavBar).checkNavBarModes(); |
| 146 | } |
| 147 | |
| 148 | // Tests if NPE occurs when call finishBarAnimations() with invalid display. |
| 149 | @Test |
| 150 | public void testFinishBarAnimationsWithInvalidDisplay() { |
| 151 | mNavigationBarController.finishBarAnimations(INVALID_DISPLAY); |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void testFinishBarAnimationsWithDefaultDisplay() { |
| 156 | doNothing().when(mDefaultNavBar).finishBarAnimations(); |
| 157 | |
| 158 | mNavigationBarController.finishBarAnimations(DEFAULT_DISPLAY); |
| 159 | |
| 160 | verify(mDefaultNavBar).finishBarAnimations(); |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void testFinishBarAnimationsWithSecondaryDisplay() { |
| 165 | doNothing().when(mSecondaryNavBar).finishBarAnimations(); |
| 166 | |
| 167 | mNavigationBarController.finishBarAnimations(SECONDARY_DISPLAY); |
| 168 | |
| 169 | verify(mSecondaryNavBar).finishBarAnimations(); |
| 170 | } |
| 171 | |
| 172 | // Tests if NPE occurs when call touchAutoDim() with invalid display. |
| 173 | @Test |
| 174 | public void testTouchAutoDimWithInvalidDisplay() { |
| 175 | mNavigationBarController.touchAutoDim(INVALID_DISPLAY); |
| 176 | } |
| 177 | |
| 178 | @Test |
| 179 | public void testTouchAutoDimWithDefaultDisplay() { |
| 180 | doNothing().when(mDefaultNavBar).touchAutoDim(); |
| 181 | |
| 182 | mNavigationBarController.touchAutoDim(DEFAULT_DISPLAY); |
| 183 | |
| 184 | verify(mDefaultNavBar).touchAutoDim(); |
| 185 | } |
| 186 | |
| 187 | @Test |
| 188 | public void testTouchAutoDimWithSecondaryDisplay() { |
| 189 | doNothing().when(mSecondaryNavBar).touchAutoDim(); |
| 190 | |
| 191 | mNavigationBarController.touchAutoDim(SECONDARY_DISPLAY); |
| 192 | |
| 193 | verify(mSecondaryNavBar).touchAutoDim(); |
| 194 | } |
| 195 | |
| 196 | // Tests if NPE occurs when call transitionTo() with invalid display. |
| 197 | @Test |
| 198 | public void testTransitionToWithInvalidDisplay() { |
| 199 | mNavigationBarController.transitionTo(INVALID_DISPLAY, 3, true); |
| 200 | } |
| 201 | |
| 202 | @Test |
| 203 | public void testTransitionToWithDefaultDisplay() { |
| 204 | doNothing().when(mDefaultNavBar).transitionTo(anyInt(), anyBoolean()); |
| 205 | |
| 206 | mNavigationBarController.transitionTo(DEFAULT_DISPLAY, 3, true); |
| 207 | |
| 208 | verify(mDefaultNavBar).transitionTo(eq(3), eq(true)); |
| 209 | } |
| 210 | |
| 211 | @Test |
| 212 | public void testTransitionToWithSecondaryDisplay() { |
| 213 | doNothing().when(mSecondaryNavBar).transitionTo(anyInt(), anyBoolean()); |
| 214 | |
| 215 | mNavigationBarController.transitionTo(SECONDARY_DISPLAY, 3, true); |
| 216 | |
| 217 | verify(mSecondaryNavBar).transitionTo(eq(3), eq(true)); |
| 218 | } |
| 219 | |
| 220 | // Tests if NPE occurs when call disableAnimationsDuringHide() with invalid display. |
| 221 | @Test |
| 222 | public void testDisableAnimationsDuringHideWithInvalidDisplay() { |
| 223 | mNavigationBarController.disableAnimationsDuringHide(INVALID_DISPLAY, 500L); |
| 224 | } |
| 225 | |
| 226 | @Test |
| 227 | public void testDisableAnimationsDuringHideWithDefaultDisplay() { |
| 228 | doNothing().when(mDefaultNavBar).disableAnimationsDuringHide(anyLong()); |
| 229 | |
| 230 | mNavigationBarController.disableAnimationsDuringHide(DEFAULT_DISPLAY, 500L); |
| 231 | |
| 232 | verify(mDefaultNavBar).disableAnimationsDuringHide(eq(500L)); |
| 233 | } |
| 234 | |
| 235 | @Test |
| 236 | public void testDisableAnimationsDuringHideWithSecondaryDisplay() { |
| 237 | doNothing().when(mSecondaryNavBar).disableAnimationsDuringHide(anyLong()); |
| 238 | |
| 239 | mNavigationBarController.disableAnimationsDuringHide(SECONDARY_DISPLAY, 500L); |
| 240 | |
| 241 | verify(mSecondaryNavBar).disableAnimationsDuringHide(eq(500L)); |
| 242 | } |
| 243 | } |