Home | History | Annotate | Download | only in volume
      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;
     27 import android.media.session.MediaSession;
     28 import android.support.test.filters.SmallTest;
     29 import com.android.systemui.SysuiTestCase;
     30 import com.android.systemui.keyguard.WakefulnessLifecycle;
     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);
     48         mVolumeController.setEnableDialogs(true, true);
     49     }
     50 
     51     @Test
     52     public void testVolumeChangeW_deviceNotInteractiveAOD() {
     53         when(mStatusBar.isDeviceInteractive()).thenReturn(false);
     54         when(mStatusBar.getWakefulnessState()).thenReturn(WakefulnessLifecycle.WAKEFULNESS_AWAKE);
     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);
     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);
     74         mVolumeController.onVolumeChangedW(0, AudioManager.FLAG_SHOW_UI);
     75         verify(mCallback, times(1)).onShowRequested(Events.SHOW_REASON_VOLUME_CHANGED);
     76     }
     77 
     78     @Test
     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
     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 
    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 }
    109