1 /*
2  * Copyright (C) 2018 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 
17 package com.android.settings.location;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.location.LocationManager;
23 
24 import com.android.settings.R;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.robolectric.RobolectricTestRunner;
30 import org.robolectric.RuntimeEnvironment;
31 
32 @RunWith(RobolectricTestRunner.class)
33 public class TopLevelLocationPreferenceControllerTest {
34     private static final String PREFERENCE_KEY = "top_level_location";
35     private Context mContext;
36     private TopLevelLocationPreferenceController mController;
37     private LocationManager mLocationManager;
38 
39     @Before
setUp()40     public void setUp() {
41         mContext = RuntimeEnvironment.application;
42         mController = new TopLevelLocationPreferenceController(mContext, PREFERENCE_KEY);
43         mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
44     }
45 
46     @Test
isAvailable_byDefault_shouldReturnTrue()47     public void isAvailable_byDefault_shouldReturnTrue() {
48         assertThat(mController.isAvailable()).isTrue();
49     }
50 
51     @Test
getSummary_whenLocationIsOff_shouldReturnStringForOff()52     public void getSummary_whenLocationIsOff_shouldReturnStringForOff() {
53         mLocationManager.setLocationEnabledForUser(false, android.os.Process.myUserHandle());
54         assertThat(mController.getSummary()).isEqualTo(
55                 mContext.getString(R.string.location_settings_summary_location_off));
56     }
57 
58     @Test
getSummary_whenLocationIsOn_shouldShowLoadingString()59     public void getSummary_whenLocationIsOn_shouldShowLoadingString() {
60         mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle());
61         assertThat(mController.getSummary()).isEqualTo(
62                 mContext.getString(R.string.location_settings_loading_app_permission_stats));
63     }
64 
65     @Test
getSummary_whenLocationAppCountIsOne_shouldShowSingularString()66     public void getSummary_whenLocationAppCountIsOne_shouldShowSingularString() {
67         final int LOCATION_APP_COUNT = 1;
68         mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle());
69         mController.setLocationAppCount(LOCATION_APP_COUNT);
70         assertThat(mController.getSummary()).isEqualTo(
71                 mContext.getResources().getQuantityString(
72                         R.plurals.location_settings_summary_location_on,
73                         LOCATION_APP_COUNT, LOCATION_APP_COUNT));
74     }
75 
76     @Test
getSummary_whenLocationAppCountIsGreaterThanOne_shouldShowPluralString()77     public void getSummary_whenLocationAppCountIsGreaterThanOne_shouldShowPluralString() {
78         final int LOCATION_APP_COUNT = 5;
79         mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle());
80         mController.setLocationAppCount(LOCATION_APP_COUNT);
81         assertThat(mController.getSummary()).isEqualTo(
82                 mContext.getResources().getQuantityString(
83                         R.plurals.location_settings_summary_location_on,
84                         LOCATION_APP_COUNT, LOCATION_APP_COUNT));
85     }
86 }