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.android.settings.notification.SettingPref.TYPE_GLOBAL; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.ContentResolver; 29 import android.content.Context; 30 import android.provider.Settings.Global; 31 32 import androidx.fragment.app.FragmentActivity; 33 import androidx.preference.PreferenceScreen; 34 35 import com.android.settings.SettingsPreferenceFragment; 36 import com.android.settingslib.core.lifecycle.Lifecycle; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 46 import java.util.ArrayList; 47 import java.util.List; 48 49 @RunWith(RobolectricTestRunner.class) 50 public class SettingPrefControllerTest { 51 52 @Mock 53 private PreferenceScreen mScreen; 54 @Mock 55 private SoundSettings mSetting; 56 @Mock 57 private FragmentActivity mActivity; 58 @Mock 59 private ContentResolver mContentResolver; 60 61 private Context mContext; 62 private PreferenceControllerTestable mController; 63 private SettingPref mPreference; 64 65 @Before setUp()66 public void setUp() { 67 MockitoAnnotations.initMocks(this); 68 mContext = spy(RuntimeEnvironment.application); 69 when(mContext.getContentResolver()).thenReturn(mContentResolver); 70 when(mSetting.getActivity()).thenReturn(mActivity); 71 doReturn(mScreen).when(mSetting).getPreferenceScreen(); 72 mController = new PreferenceControllerTestable(mContext, mSetting, null); 73 mPreference = mController.getPref(); 74 } 75 76 @Test displayPreference_shouldInitPreference()77 public void displayPreference_shouldInitPreference() { 78 mController.displayPreference(mScreen); 79 80 verify(mPreference).init(mSetting); 81 } 82 83 @Test isAvailable_shouldCallisApplicable()84 public void isAvailable_shouldCallisApplicable() { 85 mController.isAvailable(); 86 87 verify(mPreference).isApplicable(mContext); 88 } 89 90 @Test getPreferenceKey_shouldReturnPrefKey()91 public void getPreferenceKey_shouldReturnPrefKey() { 92 assertThat(mController.getPreferenceKey()).isEqualTo(PreferenceControllerTestable.KEY_TEST); 93 } 94 95 @Test updateState_shouldUpdatePreference()96 public void updateState_shouldUpdatePreference() { 97 mController.updateState(null); 98 99 verify(mPreference).update(mContext); 100 } 101 102 @Test onResume_shouldRegisterContentObserver()103 public void onResume_shouldRegisterContentObserver() { 104 mController.displayPreference(mScreen); 105 mController.onResume(); 106 107 verify(mContentResolver).registerContentObserver( 108 Global.getUriFor("Setting1"), false, mController.getObserver()); 109 } 110 111 @Test onPause_shouldUnregisterContentObserver()112 public void onPause_shouldUnregisterContentObserver() { 113 mController.displayPreference(mScreen); 114 mController.onPause(); 115 116 verify(mContentResolver).unregisterContentObserver(mController.getObserver()); 117 } 118 119 @Test onContentChange_shouldUpdatePreference()120 public void onContentChange_shouldUpdatePreference() { 121 mController.displayPreference(mScreen); 122 mController.onResume(); 123 mController.getObserver().onChange(false, Global.getUriFor("Setting1")); 124 125 verify(mPreference).update(mContext); 126 } 127 128 @Test updateNonIndexableKeys_applicable_shouldNotUpdate()129 public void updateNonIndexableKeys_applicable_shouldNotUpdate() { 130 final List<String> keys = new ArrayList<>(); 131 132 mController.updateNonIndexableKeys(keys); 133 134 assertThat(keys).isEmpty(); 135 } 136 137 @Test updateNonIndexableKeys_notApplicable_shouldUpdate()138 public void updateNonIndexableKeys_notApplicable_shouldUpdate() { 139 mController.setApplicable(false); 140 final List<String> keys = new ArrayList<>(); 141 142 mController.updateNonIndexableKeys(keys); 143 144 assertThat(keys).isNotEmpty(); 145 } 146 147 private class PreferenceControllerTestable extends SettingPrefController { 148 149 private static final String KEY_TEST = "key1"; 150 private boolean mApplicable = true; 151 PreferenceControllerTestable(Context context, SettingsPreferenceFragment parent, Lifecycle lifecycle)152 private PreferenceControllerTestable(Context context, SettingsPreferenceFragment parent, 153 Lifecycle lifecycle) { 154 super(context, parent, lifecycle); 155 mPreference = spy(new SettingPref( 156 TYPE_GLOBAL, KEY_TEST, "Setting1", 1) { 157 @Override 158 public boolean isApplicable(Context context) { 159 return mApplicable; 160 } 161 }); 162 } 163 getPref()164 SettingPref getPref() { 165 return mPreference; 166 } 167 getObserver()168 PreferenceControllerTestable.SettingsObserver getObserver() { 169 return mSettingsObserver; 170 } 171 setApplicable(boolean applicable)172 void setApplicable(boolean applicable) { 173 mApplicable = applicable; 174 } 175 } 176 } 177