Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 org.mockito.Mockito.mock; |
| 20 | import static org.mockito.Mockito.never; |
| 21 | import static org.mockito.Mockito.times; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
| 25 | import android.content.Context; |
| 26 | import android.media.AudioManager; |
Beverly | 4e93661 | 2017-07-28 14:05:30 -0400 | [diff] [blame] | 27 | import android.media.session.MediaSession; |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 28 | import android.support.test.filters.SmallTest; |
| 29 | import com.android.systemui.SysuiTestCase; |
Beverly | 85e05eb | 2017-07-21 14:49:27 -0400 | [diff] [blame] | 30 | import com.android.systemui.keyguard.WakefulnessLifecycle; |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 31 | import com.android.systemui.statusbar.phone.StatusBar; |
| 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | |
| 36 | @SmallTest |
| 37 | public class VolumeDialogControllerImplTest extends SysuiTestCase { |
| 38 | |
| 39 | TestableVolumeDialogControllerImpl mVolumeController; |
| 40 | VolumeDialogControllerImpl.C mCallback; |
| 41 | StatusBar mStatusBar; |
| 42 | |
| 43 | @Before |
| 44 | public void setup() throws Exception { |
| 45 | mCallback = mock(VolumeDialogControllerImpl.C.class); |
| 46 | mStatusBar = mock(StatusBar.class); |
| 47 | mVolumeController = new TestableVolumeDialogControllerImpl(mContext, mCallback, mStatusBar); |
Luke Song | e003666 | 2017-12-12 12:27:08 -0800 | [diff] [blame] | 48 | mVolumeController.setEnableDialogs(true, true); |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testVolumeChangeW_deviceNotInteractiveAOD() { |
| 53 | when(mStatusBar.isDeviceInteractive()).thenReturn(false); |
Beverly | 85e05eb | 2017-07-21 14:49:27 -0400 | [diff] [blame] | 54 | when(mStatusBar.getWakefulnessState()).thenReturn(WakefulnessLifecycle.WAKEFULNESS_AWAKE); |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 55 | mVolumeController.onVolumeChangedW(0, AudioManager.FLAG_SHOW_UI); |
| 56 | verify(mCallback, never()).onShowRequested(Events.SHOW_REASON_VOLUME_CHANGED); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testVolumeChangeW_deviceInteractive() { |
| 61 | when(mStatusBar.isDeviceInteractive()).thenReturn(true); |
Beverly | 85e05eb | 2017-07-21 14:49:27 -0400 | [diff] [blame] | 62 | when(mStatusBar.getWakefulnessState()).thenReturn(WakefulnessLifecycle.WAKEFULNESS_AWAKE); |
| 63 | mVolumeController.onVolumeChangedW(0, AudioManager.FLAG_SHOW_UI); |
| 64 | verify(mCallback, times(1)).onShowRequested(Events.SHOW_REASON_VOLUME_CHANGED); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testVolumeChangeW_deviceInteractive_StartedSleeping() { |
| 69 | when(mStatusBar.isDeviceInteractive()).thenReturn(true); |
| 70 | when(mStatusBar.getWakefulnessState()).thenReturn(WakefulnessLifecycle.WAKEFULNESS_AWAKE); |
| 71 | mVolumeController.onVolumeChangedW(0, AudioManager.FLAG_SHOW_UI); |
| 72 | when(mStatusBar.isDeviceInteractive()).thenReturn(false); |
| 73 | when(mStatusBar.getWakefulnessState()).thenReturn(WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP); |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 74 | mVolumeController.onVolumeChangedW(0, AudioManager.FLAG_SHOW_UI); |
| 75 | verify(mCallback, times(1)).onShowRequested(Events.SHOW_REASON_VOLUME_CHANGED); |
| 76 | } |
| 77 | |
Beverly | 4e93661 | 2017-07-28 14:05:30 -0400 | [diff] [blame] | 78 | @Test |
Beverly | a2f682f | 2018-02-01 11:29:25 -0500 | [diff] [blame] | 79 | public void testVolumeChangeW_nullStatusBar() { |
| 80 | VolumeDialogControllerImpl.C callback = mock(VolumeDialogControllerImpl.C.class); |
| 81 | TestableVolumeDialogControllerImpl nullStatusBarTestableDialog = new |
| 82 | TestableVolumeDialogControllerImpl(mContext, callback, null); |
| 83 | nullStatusBarTestableDialog.setEnableDialogs(true, true); |
| 84 | nullStatusBarTestableDialog.onVolumeChangedW(0, AudioManager.FLAG_SHOW_UI); |
| 85 | verify(callback, times(1)).onShowRequested(Events.SHOW_REASON_VOLUME_CHANGED); |
| 86 | } |
| 87 | |
| 88 | @Test |
Beverly | 4e93661 | 2017-07-28 14:05:30 -0400 | [diff] [blame] | 89 | public void testOnRemoteVolumeChanged_newStream_noNullPointer() { |
| 90 | MediaSession.Token token = new MediaSession.Token(null); |
| 91 | mVolumeController.mMediaSessionsCallbacksW.onRemoteVolumeChanged(token, 0); |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testOnRemoteRemove_newStream_noNullPointer() { |
| 96 | MediaSession.Token token = new MediaSession.Token(null); |
| 97 | mVolumeController.mMediaSessionsCallbacksW.onRemoteRemoved(token); |
| 98 | } |
| 99 | |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 100 | static class TestableVolumeDialogControllerImpl extends VolumeDialogControllerImpl { |
| 101 | public TestableVolumeDialogControllerImpl(Context context, C callback, StatusBar s) { |
| 102 | super(context); |
| 103 | mCallbacks = callback; |
| 104 | mStatusBar = s; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | } |