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