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 */
16package com.android.settings.location;
17
18import static android.arch.lifecycle.Lifecycle.Event.ON_PAUSE;
19import static android.arch.lifecycle.Lifecycle.Event.ON_RESUME;
20import static com.google.common.truth.Truth.assertThat;
21import static org.mockito.ArgumentMatchers.nullable;
22import static org.mockito.Matchers.any;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import android.arch.lifecycle.LifecycleOwner;
27import android.content.BroadcastReceiver;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
32import android.location.LocationManager;
33import android.provider.Settings;
34import android.provider.Settings.Secure;
35import android.support.v7.preference.Preference;
36import android.support.v7.preference.PreferenceScreen;
37
38import com.android.settings.R;
39import com.android.settings.search.InlineListPayload;
40import com.android.settings.search.InlinePayload;
41import com.android.settings.search.ResultPayload;
42import com.android.settings.testutils.SettingsRobolectricTestRunner;
43import com.android.settings.testutils.shadow.ShadowSecureSettings;
44import com.android.settingslib.core.lifecycle.Lifecycle;
45
46import org.junit.Before;
47import org.junit.Test;
48import org.junit.runner.RunWith;
49import org.mockito.Answers;
50import org.mockito.Mock;
51import org.mockito.MockitoAnnotations;
52import org.robolectric.RuntimeEnvironment;
53import org.robolectric.annotation.Config;
54
55@RunWith(SettingsRobolectricTestRunner.class)
56public 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
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
79    public void isAvailable_shouldReturnTrue() {
80        assertThat(mController.isAvailable()).isTrue();
81    }
82
83    @Test
84    public void updateState_shouldSetSummary() {
85        mController.updateState(mPreference);
86
87        verify(mPreference).setSummary(nullable(String.class));
88    }
89
90    @Test
91    public void updateSummary_shouldSetSummary() {
92        mController.displayPreference(mScreen);
93        mController.updateSummary();
94
95        verify(mPreference).setSummary(nullable(String.class));
96    }
97
98    @Test
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
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
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
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
135    public void onResume_shouldRegisterObserver() {
136        mLifecycle.handleLifecycleEvent(ON_RESUME);
137        verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
138    }
139
140    @Test
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
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
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)
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)
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