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 package com.android.settings.location; 17 18 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.nullable; 22 import static org.mockito.Matchers.any; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.BroadcastReceiver; 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.content.IntentFilter; 31 import android.location.LocationManager; 32 import android.provider.Settings; 33 import android.provider.Settings.Secure; 34 import android.support.v7.preference.Preference; 35 import android.support.v7.preference.PreferenceScreen; 36 37 import com.android.settings.R; 38 import com.android.settings.display.AutoBrightnessPreferenceController; 39 import com.android.settings.search.InlineListPayload; 40 import com.android.settings.search.InlinePayload; 41 import com.android.settings.search.InlineSwitchPayload; 42 import com.android.settings.search.ResultPayload; 43 import com.android.settings.testutils.SettingsRobolectricTestRunner; 44 import com.android.settings.TestConfig; 45 import com.android.settings.testutils.shadow.ShadowSecureSettings; 46 import com.android.settingslib.core.lifecycle.Lifecycle; 47 48 import org.junit.Before; 49 import org.junit.Test; 50 import org.junit.runner.RunWith; 51 import org.mockito.Answers; 52 import org.mockito.Mock; 53 import org.mockito.MockitoAnnotations; 54 import org.robolectric.RuntimeEnvironment; 55 import org.robolectric.annotation.Config; 56 import org.robolectric.shadows.ShadowApplication; 57 58 @RunWith(SettingsRobolectricTestRunner.class) 59 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 60 public class LocationPreferenceControllerTest { 61 @Mock 62 private Preference mPreference; 63 @Mock 64 private PreferenceScreen mScreen; 65 66 private Lifecycle mLifecycle; 67 private LocationPreferenceController mController; 68 69 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 70 private Context mContext; 71 72 @Before 73 public void setUp() { 74 MockitoAnnotations.initMocks(this); 75 mLifecycle = new Lifecycle(); 76 mController = new LocationPreferenceController(mContext, mLifecycle); 77 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 78 } 79 80 @Test 81 public void isAvailable_shouldReturnTrue() { 82 assertThat(mController.isAvailable()).isTrue(); 83 } 84 85 @Test 86 public void updateState_shouldSetSummary() { 87 mController.updateState(mPreference); 88 89 verify(mPreference).setSummary(nullable(String.class)); 90 } 91 92 @Test 93 public void updateSummary_shouldSetSummary() { 94 mController.displayPreference(mScreen); 95 mController.updateSummary(); 96 97 verify(mPreference).setSummary(nullable(String.class)); 98 } 99 100 @Test 101 public void getLocationSummary_locationOff_shouldSetSummaryOff() { 102 Secure.putInt(mContext.getContentResolver(), 103 Secure.LOCATION_MODE, Secure.LOCATION_MODE_OFF); 104 105 assertThat(mController.getLocationSummary(mContext)).isEqualTo( 106 mContext.getString(R.string.location_off_summary)); 107 } 108 109 @Test 110 public void getLocationSummary_sensorsOnly_shouldSetSummarySensorsOnly() { 111 Secure.putInt(mContext.getContentResolver(), 112 Secure.LOCATION_MODE, Secure.LOCATION_MODE_SENSORS_ONLY); 113 114 assertThat(mController.getLocationSummary(mContext)).isEqualTo( 115 mContext.getString(R.string.location_on_summary, 116 mContext.getString(R.string.location_mode_sensors_only_title))); 117 } 118 119 @Test 120 public void getLocationSummary_highAccuracy_shouldSetSummarHighAccuracy() { 121 Secure.putInt(mContext.getContentResolver(), 122 Secure.LOCATION_MODE, Secure.LOCATION_MODE_HIGH_ACCURACY); 123 124 assertThat(mController.getLocationSummary(mContext)).isEqualTo( 125 mContext.getString(R.string.location_on_summary, 126 mContext.getString(R.string.location_mode_high_accuracy_title))); 127 } 128 129 @Test 130 public void getLocationSummary_batterySaving_shouldSetSummaryBatterySaving() { 131 Secure.putInt(mContext.getContentResolver(), 132 Secure.LOCATION_MODE, Secure.LOCATION_MODE_BATTERY_SAVING); 133 134 assertThat(mController.getLocationSummary(mContext)).isEqualTo( 135 mContext.getString(R.string.location_on_summary, 136 mContext.getString(R.string.location_mode_battery_saving_title))); 137 } 138 139 @Test 140 public void getLocationString_shouldCorrectString() { 141 assertThat(mController.getLocationString(Secure.LOCATION_MODE_OFF)).isEqualTo( 142 R.string.location_mode_location_off_title); 143 assertThat(mController.getLocationString(Secure.LOCATION_MODE_SENSORS_ONLY)).isEqualTo( 144 R.string.location_mode_sensors_only_title); 145 assertThat(mController.getLocationString(Secure.LOCATION_MODE_BATTERY_SAVING)).isEqualTo( 146 R.string.location_mode_battery_saving_title); 147 assertThat(mController.getLocationString(Secure.LOCATION_MODE_HIGH_ACCURACY)).isEqualTo( 148 R.string.location_mode_high_accuracy_title); 149 } 150 151 @Test 152 public void onResume_shouldRegisterObserver() { 153 mLifecycle.onResume(); 154 verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class)); 155 } 156 157 @Test 158 public void onPause_shouldUnregisterObserver() { 159 mLifecycle.onPause(); 160 verify(mContext).unregisterReceiver(any(BroadcastReceiver.class)); 161 } 162 163 @Test 164 public void locationProvidersChangedReceiver_updatesPreferenceSummary() { 165 mController.displayPreference(mScreen); 166 mController.onResume(); 167 168 mController.mLocationProvidersChangedReceiver.onReceive( 169 mContext, 170 new Intent().setAction(LocationManager.PROVIDERS_CHANGED_ACTION)); 171 172 verify(mPreference).setSummary(any()); 173 } 174 175 @Test 176 public void testPreferenceController_ProperResultPayloadType() { 177 final Context context = RuntimeEnvironment.application; 178 mController = new LocationPreferenceController(context, null /* lifecycle */); 179 ResultPayload payload = mController.getResultPayload(); 180 assertThat(payload).isInstanceOf(InlineListPayload.class); 181 } 182 183 @Test 184 @Config(shadows = ShadowSecureSettings.class) 185 public void testSetValue_updatesCorrectly() { 186 int newValue = Secure.LOCATION_MODE_BATTERY_SAVING; 187 ContentResolver resolver = mContext.getContentResolver(); 188 Settings.Secure.putInt(resolver, Secure.LOCATION_MODE, Secure.LOCATION_MODE_OFF); 189 190 ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue); 191 int updatedValue = Settings.Secure.getInt(resolver, Secure.LOCATION_MODE, 192 Secure.LOCATION_MODE_OFF); 193 194 assertThat(updatedValue).isEqualTo(newValue); 195 } 196 197 @Test 198 @Config(shadows = ShadowSecureSettings.class) 199 public void testGetValue_correctValueReturned() { 200 int expectedValue = Secure.LOCATION_MODE_BATTERY_SAVING; 201 ContentResolver resolver = mContext.getContentResolver(); 202 Settings.Secure.putInt(resolver, Secure.LOCATION_MODE, expectedValue); 203 204 int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext); 205 206 assertThat(newValue).isEqualTo(expectedValue); 207 } 208 } 209