Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.volume; |
| 18 | |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 19 | import static com.android.systemui.volume.VolumeDialogControllerImpl.STREAMS; |
| 20 | |
| 21 | import static junit.framework.Assert.assertTrue; |
| 22 | |
Rhed Jao | 4684223 | 2018-11-12 14:37:20 +0800 | [diff] [blame] | 23 | import static org.mockito.ArgumentMatchers.any; |
Julia Reynolds | 45d4ab0 | 2018-01-31 14:59:14 -0500 | [diff] [blame] | 24 | import static org.mockito.Mockito.verify; |
Julia Reynolds | 45d4ab0 | 2018-01-31 14:59:14 -0500 | [diff] [blame] | 25 | |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 26 | import android.app.KeyguardManager; |
| 27 | import android.media.AudioManager; |
Rhed Jao | 4684223 | 2018-11-12 14:37:20 +0800 | [diff] [blame] | 28 | import android.os.SystemClock; |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 29 | import android.testing.AndroidTestingRunner; |
| 30 | import android.testing.TestableLooper; |
Rhed Jao | 4684223 | 2018-11-12 14:37:20 +0800 | [diff] [blame] | 31 | import android.view.InputDevice; |
| 32 | import android.view.MotionEvent; |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 33 | import android.view.View; |
| 34 | import android.view.ViewGroup; |
Rhed Jao | 4684223 | 2018-11-12 14:37:20 +0800 | [diff] [blame] | 35 | import android.view.accessibility.AccessibilityManager; |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 36 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 37 | import androidx.test.filters.SmallTest; |
| 38 | |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 39 | import com.android.systemui.SysuiTestCase; |
| 40 | import com.android.systemui.plugins.VolumeDialogController; |
Julia Reynolds | 45d4ab0 | 2018-01-31 14:59:14 -0500 | [diff] [blame] | 41 | import com.android.systemui.plugins.VolumeDialogController.State; |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 42 | import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper; |
| 43 | |
| 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
Rhed Jao | 4684223 | 2018-11-12 14:37:20 +0800 | [diff] [blame] | 47 | import org.mockito.ArgumentCaptor; |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 48 | import org.mockito.Mock; |
Rhed Jao | 4684223 | 2018-11-12 14:37:20 +0800 | [diff] [blame] | 49 | import org.mockito.Mockito; |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 50 | import org.mockito.MockitoAnnotations; |
| 51 | |
| 52 | import java.util.function.Predicate; |
| 53 | |
| 54 | @SmallTest |
| 55 | @RunWith(AndroidTestingRunner.class) |
| 56 | @TestableLooper.RunWithLooper |
| 57 | public class VolumeDialogImplTest extends SysuiTestCase { |
| 58 | |
| 59 | VolumeDialogImpl mDialog; |
| 60 | |
| 61 | @Mock |
| 62 | VolumeDialogController mController; |
| 63 | |
| 64 | @Mock |
| 65 | KeyguardManager mKeyguard; |
| 66 | |
| 67 | @Mock |
| 68 | AccessibilityManagerWrapper mAccessibilityMgr; |
| 69 | |
| 70 | @Before |
| 71 | public void setup() throws Exception { |
| 72 | MockitoAnnotations.initMocks(this); |
| 73 | |
| 74 | mController = mDependency.injectMockDependency(VolumeDialogController.class); |
| 75 | mAccessibilityMgr = mDependency.injectMockDependency(AccessibilityManagerWrapper.class); |
| 76 | getContext().addMockSystemService(KeyguardManager.class, mKeyguard); |
| 77 | |
| 78 | mDialog = new VolumeDialogImpl(getContext()); |
| 79 | mDialog.init(0, null); |
Julia Reynolds | 45d4ab0 | 2018-01-31 14:59:14 -0500 | [diff] [blame] | 80 | State state = createShellState(); |
| 81 | mDialog.onStateChangedH(state); |
| 82 | } |
| 83 | |
| 84 | private State createShellState() { |
| 85 | State state = new VolumeDialogController.State(); |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 86 | for (int i = AudioManager.STREAM_VOICE_CALL; i <= AudioManager.STREAM_ACCESSIBILITY; i++) { |
| 87 | VolumeDialogController.StreamState ss = new VolumeDialogController.StreamState(); |
| 88 | ss.name = STREAMS.get(i); |
Julia Reynolds | 45d4ab0 | 2018-01-31 14:59:14 -0500 | [diff] [blame] | 89 | ss.level = 1; |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 90 | state.states.append(i, ss); |
| 91 | } |
Julia Reynolds | 45d4ab0 | 2018-01-31 14:59:14 -0500 | [diff] [blame] | 92 | return state; |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private void navigateViews(View view, Predicate<View> condition) { |
| 96 | if (view instanceof ViewGroup) { |
| 97 | ViewGroup viewGroup = (ViewGroup) view; |
| 98 | for (int i = 0; i < viewGroup.getChildCount(); i++) { |
| 99 | navigateViews(viewGroup.getChildAt(i), condition); |
| 100 | } |
| 101 | } else { |
| 102 | String resourceName = null; |
| 103 | try { |
| 104 | resourceName = getContext().getResources().getResourceName(view.getId()); |
| 105 | } catch (Exception e) {} |
| 106 | assertTrue("View " + resourceName != null ? resourceName : view.getId() |
| 107 | + " failed test", condition.test(view)); |
| 108 | } |
| 109 | } |
Rhed Jao | 4684223 | 2018-11-12 14:37:20 +0800 | [diff] [blame] | 110 | |
| 111 | @Test |
| 112 | public void testComputeTimeout() { |
| 113 | Mockito.reset(mAccessibilityMgr); |
| 114 | mDialog.rescheduleTimeoutH(); |
| 115 | verify(mAccessibilityMgr).getRecommendedTimeoutMillis( |
| 116 | VolumeDialogImpl.DIALOG_TIMEOUT_MILLIS, |
| 117 | AccessibilityManager.FLAG_CONTENT_CONTROLS); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testComputeTimeout_withHovering() { |
| 122 | Mockito.reset(mAccessibilityMgr); |
| 123 | View dialog = mDialog.getDialogView(); |
| 124 | long uptimeMillis = SystemClock.uptimeMillis(); |
| 125 | MotionEvent event = MotionEvent.obtain(uptimeMillis, uptimeMillis, |
| 126 | MotionEvent.ACTION_HOVER_ENTER, 0, 0, 0); |
| 127 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 128 | dialog.dispatchGenericMotionEvent(event); |
| 129 | event.recycle(); |
| 130 | verify(mAccessibilityMgr).getRecommendedTimeoutMillis( |
| 131 | VolumeDialogImpl.DIALOG_HOVERING_TIMEOUT_MILLIS, |
| 132 | AccessibilityManager.FLAG_CONTENT_CONTROLS); |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testComputeTimeout_withSafetyWarningOn() { |
| 137 | Mockito.reset(mAccessibilityMgr); |
| 138 | ArgumentCaptor<VolumeDialogController.Callbacks> controllerCallbackCapture = |
| 139 | ArgumentCaptor.forClass(VolumeDialogController.Callbacks.class); |
| 140 | verify(mController).addCallback(controllerCallbackCapture.capture(), any()); |
| 141 | VolumeDialogController.Callbacks callbacks = controllerCallbackCapture.getValue(); |
| 142 | callbacks.onShowSafetyWarning(AudioManager.FLAG_SHOW_UI); |
| 143 | verify(mAccessibilityMgr).getRecommendedTimeoutMillis( |
| 144 | VolumeDialogImpl.DIALOG_SAFETYWARNING_TIMEOUT_MILLIS, |
| 145 | AccessibilityManager.FLAG_CONTENT_TEXT |
| 146 | | AccessibilityManager.FLAG_CONTENT_CONTROLS); |
| 147 | } |
| 148 | |
Julia Reynolds | e904fb3 | 2018-02-01 09:27:13 -0500 | [diff] [blame] | 149 | /* |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 150 | @Test |
| 151 | public void testContentDescriptions() { |
| 152 | mDialog.show(SHOW_REASON_UNKNOWN); |
| 153 | ViewGroup dialog = mDialog.getDialogView(); |
| 154 | |
| 155 | navigateViews(dialog, view -> { |
| 156 | if (view instanceof ImageView) { |
| 157 | return !TextUtils.isEmpty(view.getContentDescription()); |
| 158 | } else { |
| 159 | return true; |
| 160 | } |
| 161 | }); |
| 162 | |
| 163 | mDialog.dismiss(DISMISS_REASON_UNKNOWN); |
| 164 | } |
| 165 | |
Julia Reynolds | 45d4ab0 | 2018-01-31 14:59:14 -0500 | [diff] [blame] | 166 | @Test |
| 167 | public void testNoDuplicationOfParentState() { |
| 168 | mDialog.show(SHOW_REASON_UNKNOWN); |
| 169 | ViewGroup dialog = mDialog.getDialogView(); |
| 170 | |
| 171 | navigateViews(dialog, view -> !view.isDuplicateParentStateEnabled()); |
| 172 | |
| 173 | mDialog.dismiss(DISMISS_REASON_UNKNOWN); |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testNoClickableViewGroups() { |
| 178 | mDialog.show(SHOW_REASON_UNKNOWN); |
| 179 | ViewGroup dialog = mDialog.getDialogView(); |
| 180 | |
| 181 | navigateViews(dialog, view -> { |
| 182 | if (view instanceof ViewGroup) { |
| 183 | return !view.isClickable(); |
| 184 | } else { |
| 185 | return true; |
| 186 | } |
| 187 | }); |
| 188 | |
| 189 | mDialog.dismiss(DISMISS_REASON_UNKNOWN); |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void testTristateToggle_withVibrator() { |
| 194 | when(mController.hasVibrator()).thenReturn(true); |
| 195 | |
| 196 | State state = createShellState(); |
| 197 | state.ringerModeInternal = RINGER_MODE_NORMAL; |
| 198 | mDialog.onStateChangedH(state); |
| 199 | |
| 200 | mDialog.show(SHOW_REASON_UNKNOWN); |
| 201 | ViewGroup dialog = mDialog.getDialogView(); |
| 202 | |
| 203 | // click once, verify updates to vibrate |
| 204 | dialog.findViewById(R.id.ringer_icon).performClick(); |
| 205 | verify(mController, times(1)).setRingerMode(RINGER_MODE_VIBRATE, false); |
| 206 | |
| 207 | // fake the update back to the dialog with the new ringer mode |
| 208 | state = createShellState(); |
| 209 | state.ringerModeInternal = RINGER_MODE_VIBRATE; |
| 210 | mDialog.onStateChangedH(state); |
| 211 | |
| 212 | // click once, verify updates to silent |
| 213 | dialog.findViewById(R.id.ringer_icon).performClick(); |
| 214 | verify(mController, times(1)).setRingerMode(RINGER_MODE_SILENT, false); |
| 215 | verify(mController, times(1)).setStreamVolume(STREAM_RING, 0); |
| 216 | |
| 217 | // fake the update back to the dialog with the new ringer mode |
| 218 | state = createShellState(); |
| 219 | state.states.get(STREAM_RING).level = 0; |
| 220 | state.ringerModeInternal = RINGER_MODE_SILENT; |
| 221 | mDialog.onStateChangedH(state); |
| 222 | |
| 223 | // click once, verify updates to normal |
| 224 | dialog.findViewById(R.id.ringer_icon).performClick(); |
| 225 | verify(mController, times(1)).setRingerMode(RINGER_MODE_NORMAL, false); |
| 226 | verify(mController, times(1)).setStreamVolume(STREAM_RING, 0); |
| 227 | } |
| 228 | |
| 229 | @Test |
| 230 | public void testTristateToggle_withoutVibrator() { |
| 231 | when(mController.hasVibrator()).thenReturn(false); |
| 232 | |
| 233 | State state = createShellState(); |
| 234 | state.ringerModeInternal = RINGER_MODE_NORMAL; |
| 235 | mDialog.onStateChangedH(state); |
| 236 | |
| 237 | mDialog.show(SHOW_REASON_UNKNOWN); |
| 238 | ViewGroup dialog = mDialog.getDialogView(); |
| 239 | |
| 240 | // click once, verify updates to silent |
| 241 | dialog.findViewById(R.id.ringer_icon).performClick(); |
| 242 | verify(mController, times(1)).setRingerMode(RINGER_MODE_SILENT, false); |
| 243 | verify(mController, times(1)).setStreamVolume(STREAM_RING, 0); |
| 244 | |
| 245 | // fake the update back to the dialog with the new ringer mode |
| 246 | state = createShellState(); |
| 247 | state.states.get(STREAM_RING).level = 0; |
| 248 | state.ringerModeInternal = RINGER_MODE_SILENT; |
| 249 | mDialog.onStateChangedH(state); |
| 250 | |
| 251 | // click once, verify updates to normal |
| 252 | dialog.findViewById(R.id.ringer_icon).performClick(); |
| 253 | verify(mController, times(1)).setRingerMode(RINGER_MODE_NORMAL, false); |
| 254 | verify(mController, times(1)).setStreamVolume(STREAM_RING, 0); |
| 255 | } |
Julia Reynolds | e904fb3 | 2018-02-01 09:27:13 -0500 | [diff] [blame] | 256 | */ |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 257 | } |