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