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 import com.android.settingslib.utils.StringUtil; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.robolectric.RobolectricTestRunner; 31 import org.robolectric.RuntimeEnvironment; 32 33 @RunWith(RobolectricTestRunner.class) 34 public class TopLevelLocationPreferenceControllerTest { 35 private static final String PREFERENCE_KEY = "top_level_location"; 36 private Context mContext; 37 private TopLevelLocationPreferenceController mController; 38 private LocationManager mLocationManager; 39 40 @Before setUp()41 public void setUp() { 42 mContext = RuntimeEnvironment.application; 43 mController = new TopLevelLocationPreferenceController(mContext, PREFERENCE_KEY); 44 mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); 45 } 46 47 @Test isAvailable_byDefault_shouldReturnTrue()48 public void isAvailable_byDefault_shouldReturnTrue() { 49 assertThat(mController.isAvailable()).isTrue(); 50 } 51 52 @Test getSummary_whenLocationIsOff_shouldReturnStringForOff()53 public void getSummary_whenLocationIsOff_shouldReturnStringForOff() { 54 mLocationManager.setLocationEnabledForUser(false, android.os.Process.myUserHandle()); 55 assertThat(mController.getSummary()).isEqualTo( 56 mContext.getString(R.string.location_settings_summary_location_off)); 57 } 58 59 @Test getSummary_whenLocationIsOn_shouldPreservePreviousText()60 public void getSummary_whenLocationIsOn_shouldPreservePreviousText() { 61 final int locationAppCount = 5; 62 // Retrieve summary text once. 63 mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle()); 64 mController.setLocationAppCount(locationAppCount); 65 mController.getSummary(); 66 // Turn off location. 67 mLocationManager.setLocationEnabledForUser(false, android.os.Process.myUserHandle()); 68 // Turn on location again and check if the previous summary text is still cached. 69 mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle()); 70 assertThat(mController.getSummary()).isEqualTo( 71 StringUtil.getIcuPluralsString(mContext, locationAppCount, 72 R.string.location_settings_summary_location_on)); 73 } 74 75 @Test getSummary_whenLocationAppCountIsOne_shouldShowSingularString()76 public void getSummary_whenLocationAppCountIsOne_shouldShowSingularString() { 77 final int locationAppCount = 1; 78 mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle()); 79 mController.setLocationAppCount(locationAppCount); 80 assertThat(mController.getSummary()).isEqualTo( 81 StringUtil.getIcuPluralsString(mContext, locationAppCount, 82 R.string.location_settings_summary_location_on)); 83 } 84 85 @Test getSummary_whenLocationAppCountIsGreaterThanOne_shouldShowPluralString()86 public void getSummary_whenLocationAppCountIsGreaterThanOne_shouldShowPluralString() { 87 final int locationAppCount = 5; 88 mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle()); 89 mController.setLocationAppCount(locationAppCount); 90 assertThat(mController.getSummary()).isEqualTo( 91 StringUtil.getIcuPluralsString(mContext, locationAppCount, 92 R.string.location_settings_summary_location_on)); 93 } 94 }