1 package com.android.settings.location; 2 3 import android.content.Context; 4 import android.os.UserManager; 5 import android.provider.Settings; 6 import android.support.v7.preference.Preference; 7 import android.support.v7.preference.PreferenceScreen; 8 9 import com.android.settings.SettingsRobolectricTestRunner; 10 import com.android.settings.TestConfig; 11 12 import org.junit.Before; 13 import org.junit.Test; 14 import org.junit.runner.RunWith; 15 import org.mockito.Answers; 16 import org.mockito.Mock; 17 import org.mockito.MockitoAnnotations; 18 import org.robolectric.annotation.Config; 19 import org.robolectric.shadows.ShadowApplication; 20 21 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 22 import static org.mockito.Matchers.any; 23 import static org.mockito.Matchers.anyString; 24 import static org.mockito.Mockito.never; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 @RunWith(SettingsRobolectricTestRunner.class) 29 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 30 public class AppLocationPermissionPreferenceControllerTest { 31 @Mock 32 private Preference mPreference; 33 @Mock(answer = RETURNS_DEEP_STUBS) 34 private PreferenceScreen mScreen; 35 36 private AppLocationPermissionPreferenceController mController; 37 38 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 39 private Context mContext; 40 41 @Before 42 public void setUp() { 43 MockitoAnnotations.initMocks(this); 44 when(mScreen.findPreference(anyString())).thenReturn(mPreference); 45 mController = new AppLocationPermissionPreferenceController(mContext); 46 } 47 48 @Test 49 public void displayPreference_shouldRemovePreference() { 50 Settings.System.putInt(mContext.getContentResolver(), 51 android.provider.Settings.Global.LOCATION_SETTINGS_LINK_TO_PERMISSIONS_ENABLED, 52 0); 53 when(mScreen.getPreferenceCount()).thenReturn(1); 54 when(mScreen.getPreference(0)).thenReturn(mPreference); 55 when(mPreference.getKey()).thenReturn(mController.getPreferenceKey()); 56 57 mController.displayPreference(mScreen); 58 59 verify(mScreen).removePreference(any(Preference.class)); 60 } 61 62 @Test 63 public void displayPreference_shouldNotRemovePreference() { 64 Settings.System.putInt(mContext.getContentResolver(), 65 android.provider.Settings.Global.LOCATION_SETTINGS_LINK_TO_PERMISSIONS_ENABLED, 66 1); 67 mController.displayPreference(mScreen); 68 69 verify(mScreen, never()).removePreference(any(Preference.class)); 70 } 71 72 } 73