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 android.content.Context; 19 import android.provider.Settings.Secure; 20 import android.support.v7.preference.Preference; 21 import android.support.v7.preference.PreferenceScreen; 22 23 import com.android.settings.R; 24 import com.android.settings.SettingsRobolectricTestRunner; 25 import com.android.settings.TestConfig; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.mockito.Answers; 31 import org.mockito.Mock; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.annotation.Config; 34 import org.robolectric.shadows.ShadowApplication; 35 36 import static com.google.common.truth.Truth.assertThat; 37 import static org.mockito.Matchers.anyString; 38 import static org.mockito.Mockito.verify; 39 import static org.mockito.Mockito.when; 40 41 @RunWith(SettingsRobolectricTestRunner.class) 42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 43 public class LocationPreferenceControllerTest { 44 @Mock 45 private Preference mPreference; 46 @Mock 47 private PreferenceScreen mScreen; 48 49 private LocationPreferenceController mController; 50 51 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 52 private Context mContext; 53 54 @Before setUp()55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 mController = new LocationPreferenceController(mContext); 58 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 59 } 60 61 @Test isAvailable_shouldReturnTrue()62 public void isAvailable_shouldReturnTrue() { 63 assertThat(mController.isAvailable()).isTrue(); 64 } 65 66 @Test updateState_shouldSetSummary()67 public void updateState_shouldSetSummary() { 68 mController.updateState(mPreference); 69 70 verify(mPreference).setSummary(anyString()); 71 } 72 73 @Test updateSummary_shouldSetSummary()74 public void updateSummary_shouldSetSummary() { 75 mController.displayPreference(mScreen); 76 mController.updateSummary(); 77 78 verify(mPreference).setSummary(anyString()); 79 } 80 81 @Test getLocationSummary_locationOff_shouldSetSummaryOff()82 public void getLocationSummary_locationOff_shouldSetSummaryOff() { 83 Secure.putInt(mContext.getContentResolver(), 84 Secure.LOCATION_MODE, Secure.LOCATION_MODE_OFF); 85 86 assertThat(mController.getLocationSummary(mContext)).isEqualTo( 87 mContext.getString(R.string.location_off_summary)); 88 } 89 90 @Test getLocationSummary_sensorsOnly_shouldSetSummarySensorsOnly()91 public void getLocationSummary_sensorsOnly_shouldSetSummarySensorsOnly() { 92 Secure.putInt(mContext.getContentResolver(), 93 Secure.LOCATION_MODE, Secure.LOCATION_MODE_SENSORS_ONLY); 94 95 assertThat(mController.getLocationSummary(mContext)).isEqualTo( 96 mContext.getString(R.string.location_on_summary, 97 mContext.getString(R.string.location_mode_sensors_only_title))); 98 } 99 100 @Test getLocationSummary_highAccuracy_shouldSetSummarHighAccuracy()101 public void getLocationSummary_highAccuracy_shouldSetSummarHighAccuracy() { 102 Secure.putInt(mContext.getContentResolver(), 103 Secure.LOCATION_MODE, Secure.LOCATION_MODE_HIGH_ACCURACY); 104 105 assertThat(mController.getLocationSummary(mContext)).isEqualTo( 106 mContext.getString(R.string.location_on_summary, 107 mContext.getString(R.string.location_mode_high_accuracy_title))); 108 } 109 110 @Test getLocationSummary_batterySaving_shouldSetSummaryBatterySaving()111 public void getLocationSummary_batterySaving_shouldSetSummaryBatterySaving() { 112 Secure.putInt(mContext.getContentResolver(), 113 Secure.LOCATION_MODE, Secure.LOCATION_MODE_BATTERY_SAVING); 114 115 assertThat(mController.getLocationSummary(mContext)).isEqualTo( 116 mContext.getString(R.string.location_on_summary, 117 mContext.getString(R.string.location_mode_battery_saving_title))); 118 } 119 120 @Test getLocationString_shouldCorrectString()121 public void getLocationString_shouldCorrectString() { 122 assertThat(mController.getLocationString(Secure.LOCATION_MODE_OFF)).isEqualTo( 123 R.string.location_mode_location_off_title); 124 assertThat(mController.getLocationString(Secure.LOCATION_MODE_SENSORS_ONLY)).isEqualTo( 125 R.string.location_mode_sensors_only_title); 126 assertThat(mController.getLocationString(Secure.LOCATION_MODE_BATTERY_SAVING)).isEqualTo( 127 R.string.location_mode_battery_saving_title); 128 assertThat(mController.getLocationString(Secure.LOCATION_MODE_HIGH_ACCURACY)).isEqualTo( 129 R.string.location_mode_high_accuracy_title); 130 } 131 132 } 133