1 /* 2 * Copyright (C) 2016 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.settings.notification; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.nullable; 24 import static org.mockito.Mockito.never; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceScreen; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RobolectricTestRunner; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class VolumeSeekBarPreferenceControllerTest { 42 43 @Mock 44 private Context mContext; 45 @Mock 46 private PreferenceScreen mScreen; 47 @Mock 48 private VolumeSeekBarPreference mPreference; 49 @Mock 50 private VolumeSeekBarPreference.Callback mCallback; 51 @Mock 52 private AudioHelper mHelper; 53 54 private VolumeSeekBarPreferenceControllerTestable mController; 55 56 @Before setUp()57 public void setUp() { 58 MockitoAnnotations.initMocks(this); 59 when(mScreen.findPreference(nullable(String.class))).thenReturn(mPreference); 60 when(mPreference.getKey()).thenReturn("key"); 61 mController = new VolumeSeekBarPreferenceControllerTestable(mContext, mCallback, true, 62 mPreference.getKey()); 63 mController.setAudioHelper(mHelper); 64 } 65 66 @Test displayPreference_available_shouldUpdatePreference()67 public void displayPreference_available_shouldUpdatePreference() { 68 mController.displayPreference(mScreen); 69 70 verify(mPreference).setCallback(mCallback); 71 verify(mPreference).setStream(VolumeSeekBarPreferenceControllerTestable.AUDIO_STREAM); 72 verify(mPreference).setMuteIcon(VolumeSeekBarPreferenceControllerTestable.MUTE_ICON); 73 } 74 75 @Test displayPreference_notAvailable_shouldNotUpdatePreference()76 public void displayPreference_notAvailable_shouldNotUpdatePreference() { 77 mController = new VolumeSeekBarPreferenceControllerTestable(mContext, mCallback, false, 78 mPreference.getKey()); 79 80 mController.displayPreference(mScreen); 81 82 verify(mPreference, never()).setCallback(any(VolumeSeekBarPreference.Callback.class)); 83 verify(mPreference, never()).setStream(anyInt()); 84 verify(mPreference, never()).setMuteIcon(anyInt()); 85 } 86 87 @Test onResume_shouldResumePreference()88 public void onResume_shouldResumePreference() { 89 mController.displayPreference(mScreen); 90 91 mController.onResume(); 92 93 verify(mPreference).onActivityResume(); 94 } 95 96 @Test onPause_shouldPausePreference()97 public void onPause_shouldPausePreference() { 98 mController.displayPreference(mScreen); 99 100 mController.onPause(); 101 102 verify(mPreference).onActivityPause(); 103 } 104 105 @Test sliderMethods_handleNullPreference()106 public void sliderMethods_handleNullPreference() { 107 when(mHelper.getStreamVolume(mController.getAudioStream())).thenReturn(4); 108 when(mHelper.getMaxVolume(mController.getAudioStream())).thenReturn(10); 109 when(mHelper.getMinVolume(mController.getAudioStream())).thenReturn(1); 110 111 assertThat(mController.getMax()).isEqualTo(10); 112 assertThat(mController.getMin()).isEqualTo(1); 113 assertThat(mController.getSliderPosition()).isEqualTo(4); 114 115 mController.setSliderPosition(9); 116 verify(mHelper).setStreamVolume(mController.getAudioStream(), 9); 117 } 118 119 @Test setSliderPosition_passesAlongValue()120 public void setSliderPosition_passesAlongValue() { 121 mController.displayPreference(mScreen); 122 123 mController.setSliderPosition(2); 124 verify(mPreference).setProgress(2); 125 } 126 127 @Test getMaxValue_passesAlongValue()128 public void getMaxValue_passesAlongValue() { 129 when(mPreference.getMax()).thenReturn(6); 130 mController.displayPreference(mScreen); 131 132 assertThat(mController.getMax()).isEqualTo(6); 133 } 134 135 @Test getMinValue_passesAlongValue()136 public void getMinValue_passesAlongValue() { 137 when(mPreference.getMin()).thenReturn(1); 138 mController.displayPreference(mScreen); 139 140 assertThat(mController.getMin()).isEqualTo(1); 141 } 142 143 @Test getSliderPosition_passesAlongValue()144 public void getSliderPosition_passesAlongValue() { 145 when(mPreference.getProgress()).thenReturn(7); 146 mController.displayPreference(mScreen); 147 148 assertThat(mController.getSliderPosition()).isEqualTo(7); 149 } 150 151 private class VolumeSeekBarPreferenceControllerTestable 152 extends VolumeSeekBarPreferenceController { 153 154 private final static int AUDIO_STREAM = 1; 155 private final static int MUTE_ICON = 2; 156 157 private boolean mAvailable; 158 VolumeSeekBarPreferenceControllerTestable(Context context, VolumeSeekBarPreference.Callback callback, boolean available, String key)159 VolumeSeekBarPreferenceControllerTestable(Context context, 160 VolumeSeekBarPreference.Callback callback, boolean available, String key) { 161 super(context, key); 162 setCallback(callback); 163 mAvailable = available; 164 } 165 166 @Override getPreferenceKey()167 public String getPreferenceKey() { 168 return "key"; 169 } 170 171 @Override handlePreferenceTreeClick(Preference preference)172 public boolean handlePreferenceTreeClick(Preference preference) { 173 return false; 174 } 175 176 @Override getAvailabilityStatus()177 public int getAvailabilityStatus() { 178 return mAvailable ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 179 } 180 181 @Override getAudioStream()182 public int getAudioStream() { 183 return AUDIO_STREAM; 184 } 185 186 @Override getMuteIcon()187 public int getMuteIcon() { 188 return MUTE_ICON; 189 } 190 } 191 } 192