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.settings.notification; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.when; 25 26 import android.content.ContentResolver; 27 import android.content.Context; 28 import android.provider.Settings.Global; 29 30 import androidx.fragment.app.FragmentActivity; 31 import androidx.preference.DropDownPreference; 32 import androidx.preference.PreferenceScreen; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 42 @RunWith(RobolectricTestRunner.class) 43 public class DockAudioMediaPreferenceControllerTest { 44 45 @Mock 46 private PreferenceScreen mScreen; 47 @Mock(answer = RETURNS_DEEP_STUBS) 48 private FragmentActivity mActivity; 49 @Mock 50 private ContentResolver mContentResolver; 51 @Mock 52 private SoundSettings mSetting; 53 @Mock(answer = RETURNS_DEEP_STUBS) 54 private Context mContext; 55 56 private DockAudioMediaPreferenceController mController; 57 private DropDownPreference mPreference; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 when(mSetting.getActivity()).thenReturn(mActivity); 63 when(mActivity.getContentResolver()).thenReturn(mContentResolver); 64 when(mActivity.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings)) 65 .thenReturn(true); 66 when(mActivity.getResources().getString(anyInt())).thenReturn("test string"); 67 mPreference = new DropDownPreference(RuntimeEnvironment.application); 68 mController = new DockAudioMediaPreferenceController(mContext, mSetting, null); 69 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 70 doReturn(mScreen).when(mSetting).getPreferenceScreen(); 71 } 72 73 @Test isAvailable_hasDockSettings_shouldReturnTrue()74 public void isAvailable_hasDockSettings_shouldReturnTrue() { 75 when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings)) 76 .thenReturn(true); 77 78 assertThat(mController.isAvailable()).isTrue(); 79 } 80 81 @Test isAvailable_noDockSettings_shouldReturnFalse()82 public void isAvailable_noDockSettings_shouldReturnFalse() { 83 when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings)) 84 .thenReturn(false); 85 86 assertThat(mController.isAvailable()).isFalse(); 87 } 88 89 @Test displayPreference_dockAudioDisabled_shouldSelectFirstItem()90 public void displayPreference_dockAudioDisabled_shouldSelectFirstItem() { 91 Global.putInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0); 92 93 mController.displayPreference(mScreen); 94 95 assertThat(mPreference.getValue()).isEqualTo("0"); 96 } 97 98 @Test displayPreference_dockAudioEnabled_shouldSelectSecondItem()99 public void displayPreference_dockAudioEnabled_shouldSelectSecondItem() { 100 Global.putInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 1); 101 102 mController.displayPreference(mScreen); 103 104 assertThat(mPreference.getValue()).isEqualTo("1"); 105 } 106 107 @Test onPreferenceChanged_firstItemSelected_shouldDisableDockAudio()108 public void onPreferenceChanged_firstItemSelected_shouldDisableDockAudio() { 109 mController.displayPreference(mScreen); 110 111 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "0"); 112 113 assertThat(Global.getInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0)) 114 .isEqualTo(0); 115 } 116 117 @Test onPreferenceChanged_secondItemSelected_shouldEnableDockAudio()118 public void onPreferenceChanged_secondItemSelected_shouldEnableDockAudio() { 119 mController.displayPreference(mScreen); 120 121 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "1"); 122 123 assertThat(Global.getInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0)) 124 .isEqualTo(1); 125 } 126 } 127