jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 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.settingslib; |
| 17 | |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 18 | import static android.Manifest.permission.WRITE_SECURE_SETTINGS; |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.argThat; |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 21 | import static org.mockito.Matchers.eq; |
| 22 | import static org.mockito.Mockito.mock; |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 23 | import static org.mockito.Mockito.spy; |
| 24 | import static org.mockito.Mockito.verify; |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.when; |
| 26 | |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 27 | import android.app.ActivityManager; |
| 28 | import android.content.ContentResolver; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 31 | import android.content.res.Resources; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 32 | import android.location.LocationManager; |
| 33 | import android.os.UserHandle; |
| 34 | import android.provider.Settings; |
| 35 | import android.provider.Settings.Secure; |
| 36 | import android.text.TextUtils; |
| 37 | import com.android.settingslib.wrapper.LocationManagerWrapper; |
| 38 | import java.util.HashMap; |
| 39 | import java.util.Map; |
| 40 | import org.junit.Before; |
| 41 | import org.junit.Test; |
| 42 | import org.junit.runner.RunWith; |
| 43 | import org.mockito.ArgumentMatcher; |
| 44 | import org.mockito.ArgumentMatchers; |
| 45 | import org.mockito.Mock; |
| 46 | import org.mockito.MockitoAnnotations; |
| 47 | import org.robolectric.RuntimeEnvironment; |
| 48 | import org.robolectric.annotation.Config; |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 49 | import org.robolectric.annotation.Implementation; |
| 50 | import org.robolectric.annotation.Implements; |
| 51 | import org.robolectric.shadows.ShadowSettings; |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 52 | |
Tony Mantler | 4ca9ebd | 2017-07-31 10:54:20 -0700 | [diff] [blame] | 53 | @RunWith(SettingsLibRobolectricTestRunner.class) |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 54 | @Config( |
| 55 | manifest = TestConfig.MANIFEST_PATH, |
| 56 | sdk = TestConfig.SDK_VERSION, |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 57 | shadows = { |
| 58 | UtilsTest.ShadowSecure.class, |
| 59 | UtilsTest.ShadowLocationManagerWrapper.class}) |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 60 | public class UtilsTest { |
| 61 | private static final double[] TEST_PERCENTAGES = {0, 0.4, 0.5, 0.6, 49, 49.3, 49.8, 50, 100}; |
| 62 | private static final String PERCENTAGE_0 = "0%"; |
| 63 | private static final String PERCENTAGE_1 = "1%"; |
| 64 | private static final String PERCENTAGE_49 = "49%"; |
| 65 | private static final String PERCENTAGE_50 = "50%"; |
| 66 | private static final String PERCENTAGE_100 = "100%"; |
| 67 | |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 68 | private Context mContext; |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 69 | @Mock |
| 70 | private LocationManager mLocationManager; |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 71 | |
| 72 | @Before |
| 73 | public void setUp() { |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 74 | MockitoAnnotations.initMocks(this); |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 75 | mContext = spy(RuntimeEnvironment.application); |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 76 | when(mContext.getSystemService(Context.LOCATION_SERVICE)).thenReturn(mLocationManager); |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 77 | ShadowSecure.reset(); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testUpdateLocationMode_sendBroadcast() { |
| 82 | int currentUserId = ActivityManager.getCurrentUser(); |
| 83 | Utils.updateLocationMode( |
| 84 | mContext, |
| 85 | Secure.LOCATION_MODE_OFF, |
| 86 | Secure.LOCATION_MODE_HIGH_ACCURACY, |
Lifu Tang | 0cba58f | 2018-01-23 21:14:15 -0800 | [diff] [blame] | 87 | currentUserId, |
| 88 | Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS); |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 89 | |
| 90 | verify(mContext).sendBroadcastAsUser( |
| 91 | argThat(actionMatches(LocationManager.MODE_CHANGING_ACTION)), |
| 92 | ArgumentMatchers.eq(UserHandle.of(currentUserId)), |
| 93 | ArgumentMatchers.eq(WRITE_SECURE_SETTINGS)); |
Lifu Tang | 0cba58f | 2018-01-23 21:14:15 -0800 | [diff] [blame] | 94 | assertThat(Settings.Secure.getInt(mContext.getContentResolver(), |
| 95 | Settings.Secure.LOCATION_CHANGER, Settings.Secure.LOCATION_CHANGER_UNKNOWN)) |
| 96 | .isEqualTo(Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS); |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 97 | } |
| 98 | |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 99 | @Test |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 100 | public void testUpdateLocationEnabled_sendBroadcast() { |
| 101 | int currentUserId = ActivityManager.getCurrentUser(); |
Lifu Tang | 0cba58f | 2018-01-23 21:14:15 -0800 | [diff] [blame] | 102 | Utils.updateLocationEnabled(mContext, true, currentUserId, |
| 103 | Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS); |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 104 | |
| 105 | verify(mContext).sendBroadcastAsUser( |
| 106 | argThat(actionMatches(LocationManager.MODE_CHANGING_ACTION)), |
| 107 | ArgumentMatchers.eq(UserHandle.of(currentUserId)), |
| 108 | ArgumentMatchers.eq(WRITE_SECURE_SETTINGS)); |
Lifu Tang | 0cba58f | 2018-01-23 21:14:15 -0800 | [diff] [blame] | 109 | assertThat(Settings.Secure.getInt(mContext.getContentResolver(), |
| 110 | Settings.Secure.LOCATION_CHANGER, Settings.Secure.LOCATION_CHANGER_UNKNOWN)) |
| 111 | .isEqualTo(Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS); |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 115 | public void testFormatPercentage_RoundTrue_RoundUpIfPossible() { |
| 116 | final String[] expectedPercentages = {PERCENTAGE_0, PERCENTAGE_0, PERCENTAGE_1, |
| 117 | PERCENTAGE_1, PERCENTAGE_49, PERCENTAGE_49, PERCENTAGE_50, PERCENTAGE_50, |
| 118 | PERCENTAGE_100}; |
| 119 | |
| 120 | for (int i = 0, size = TEST_PERCENTAGES.length; i < size; i++) { |
| 121 | final String percentage = Utils.formatPercentage(TEST_PERCENTAGES[i], true); |
| 122 | assertThat(percentage).isEqualTo(expectedPercentages[i]); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testFormatPercentage_RoundFalse_NoRound() { |
| 128 | final String[] expectedPercentages = {PERCENTAGE_0, PERCENTAGE_0, PERCENTAGE_0, |
| 129 | PERCENTAGE_0, PERCENTAGE_49, PERCENTAGE_49, PERCENTAGE_49, PERCENTAGE_50, |
| 130 | PERCENTAGE_100}; |
| 131 | |
| 132 | for (int i = 0, size = TEST_PERCENTAGES.length; i < size; i++) { |
| 133 | final String percentage = Utils.formatPercentage(TEST_PERCENTAGES[i], false); |
| 134 | assertThat(percentage).isEqualTo(expectedPercentages[i]); |
| 135 | } |
| 136 | } |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 137 | |
| 138 | @Test |
| 139 | public void testStorageManagerDaysToRetainUsesResources() { |
| 140 | Resources resources = mock(Resources.class); |
| 141 | when(resources.getInteger( |
| 142 | eq( |
| 143 | com.android |
| 144 | .internal |
| 145 | .R |
| 146 | .integer |
| 147 | .config_storageManagerDaystoRetainDefault))) |
| 148 | .thenReturn(60); |
| 149 | assertThat(Utils.getDefaultStorageManagerDaysToRetain(resources)).isEqualTo(60); |
| 150 | } |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 151 | |
| 152 | private static ArgumentMatcher<Intent> actionMatches(String expected) { |
| 153 | return intent -> TextUtils.equals(expected, intent.getAction()); |
| 154 | } |
| 155 | |
| 156 | @Implements(value = Settings.Secure.class) |
| 157 | public static class ShadowSecure extends ShadowSettings.ShadowSecure { |
| 158 | private static Map<String, Integer> map = new HashMap<>(); |
| 159 | |
| 160 | @Implementation |
| 161 | public static boolean putIntForUser(ContentResolver cr, String name, int value, int userHandle) { |
| 162 | map.put(name, value); |
| 163 | return true; |
| 164 | } |
| 165 | |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 166 | @Implementation |
| 167 | public static int getIntForUser(ContentResolver cr, String name, int def, int userHandle) { |
| 168 | if (map.containsKey(name)) { |
| 169 | return map.get(name); |
| 170 | } else { |
| 171 | return def; |
| 172 | } |
| 173 | } |
| 174 | |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 175 | public static void reset() { |
| 176 | map.clear(); |
| 177 | } |
| 178 | } |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 179 | |
| 180 | @Implements(value = LocationManagerWrapper.class) |
| 181 | public static class ShadowLocationManagerWrapper { |
| 182 | |
| 183 | @Implementation |
| 184 | public void setLocationEnabledForUser(boolean enabled, UserHandle userHandle) { |
| 185 | // Do nothing |
| 186 | } |
| 187 | } |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 188 | } |