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