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 | |
| 19 | import static com.android.systemui.volume.Events.DISMISS_REASON_UNKNOWN; |
| 20 | import static com.android.systemui.volume.Events.SHOW_REASON_UNKNOWN; |
| 21 | import static com.android.systemui.volume.VolumeDialogControllerImpl.STREAMS; |
| 22 | |
| 23 | import static junit.framework.Assert.assertTrue; |
| 24 | |
| 25 | import android.app.KeyguardManager; |
| 26 | import android.media.AudioManager; |
| 27 | import android.support.test.filters.SmallTest; |
| 28 | import android.testing.AndroidTestingRunner; |
| 29 | import android.testing.TestableLooper; |
| 30 | import android.text.TextUtils; |
| 31 | import android.view.View; |
| 32 | import android.view.ViewGroup; |
| 33 | import android.widget.ImageView; |
| 34 | |
| 35 | import com.android.systemui.SysuiTestCase; |
| 36 | import com.android.systemui.plugins.VolumeDialogController; |
| 37 | import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper; |
| 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.mockito.Mock; |
| 43 | import org.mockito.MockitoAnnotations; |
| 44 | |
| 45 | import java.util.function.Predicate; |
| 46 | |
| 47 | @SmallTest |
| 48 | @RunWith(AndroidTestingRunner.class) |
| 49 | @TestableLooper.RunWithLooper |
| 50 | public class VolumeDialogImplTest extends SysuiTestCase { |
| 51 | |
| 52 | VolumeDialogImpl mDialog; |
| 53 | |
| 54 | @Mock |
| 55 | VolumeDialogController mController; |
| 56 | |
| 57 | @Mock |
| 58 | KeyguardManager mKeyguard; |
| 59 | |
| 60 | @Mock |
| 61 | AccessibilityManagerWrapper mAccessibilityMgr; |
| 62 | |
| 63 | @Before |
| 64 | public void setup() throws Exception { |
| 65 | MockitoAnnotations.initMocks(this); |
| 66 | |
| 67 | mController = mDependency.injectMockDependency(VolumeDialogController.class); |
| 68 | mAccessibilityMgr = mDependency.injectMockDependency(AccessibilityManagerWrapper.class); |
| 69 | getContext().addMockSystemService(KeyguardManager.class, mKeyguard); |
| 70 | |
| 71 | mDialog = new VolumeDialogImpl(getContext()); |
| 72 | mDialog.init(0, null); |
| 73 | VolumeDialogController.State state = new VolumeDialogController.State(); |
| 74 | for (int i = AudioManager.STREAM_VOICE_CALL; i <= AudioManager.STREAM_ACCESSIBILITY; i++) { |
| 75 | VolumeDialogController.StreamState ss = new VolumeDialogController.StreamState(); |
| 76 | ss.name = STREAMS.get(i); |
| 77 | state.states.append(i, ss); |
| 78 | } |
| 79 | mDialog.onStateChangedH(state); |
| 80 | } |
| 81 | |
| 82 | private void navigateViews(View view, Predicate<View> condition) { |
| 83 | if (view instanceof ViewGroup) { |
| 84 | ViewGroup viewGroup = (ViewGroup) view; |
| 85 | for (int i = 0; i < viewGroup.getChildCount(); i++) { |
| 86 | navigateViews(viewGroup.getChildAt(i), condition); |
| 87 | } |
| 88 | } else { |
| 89 | String resourceName = null; |
| 90 | try { |
| 91 | resourceName = getContext().getResources().getResourceName(view.getId()); |
| 92 | } catch (Exception e) {} |
| 93 | assertTrue("View " + resourceName != null ? resourceName : view.getId() |
| 94 | + " failed test", condition.test(view)); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testContentDescriptions() { |
| 100 | mDialog.show(SHOW_REASON_UNKNOWN); |
| 101 | ViewGroup dialog = mDialog.getDialogView(); |
| 102 | |
| 103 | navigateViews(dialog, view -> { |
| 104 | if (view instanceof ImageView) { |
| 105 | return !TextUtils.isEmpty(view.getContentDescription()); |
| 106 | } else { |
| 107 | return true; |
| 108 | } |
| 109 | }); |
| 110 | |
| 111 | mDialog.dismiss(DISMISS_REASON_UNKNOWN); |
| 112 | } |
| 113 | |
| 114 | } |