blob: 5459fb7a47cb848c9198b795f63b4872bad448e9 [file] [log] [blame]
jackqdyulei5dc1f362017-02-17 14:41:05 -08001/*
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.settingslib;
17
Maggiefab2e2c2017-11-21 11:57:30 -080018import static android.Manifest.permission.WRITE_SECURE_SETTINGS;
jackqdyulei5dc1f362017-02-17 14:41:05 -080019import static com.google.common.truth.Truth.assertThat;
Maggiefab2e2c2017-11-21 11:57:30 -080020import static org.mockito.ArgumentMatchers.argThat;
Daniel Nishi53982802017-05-23 15:54:34 -070021import static org.mockito.Matchers.eq;
22import static org.mockito.Mockito.mock;
Maggiefab2e2c2017-11-21 11:57:30 -080023import static org.mockito.Mockito.spy;
24import static org.mockito.Mockito.verify;
Daniel Nishi53982802017-05-23 15:54:34 -070025import static org.mockito.Mockito.when;
26
Maggieaa080f92018-01-04 15:35:11 -080027import android.app.ActivityManager;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.Intent;
Daniel Nishi53982802017-05-23 15:54:34 -070031import android.content.res.Resources;
Maggieaa080f92018-01-04 15:35:11 -080032import android.location.LocationManager;
33import android.os.UserHandle;
34import android.provider.Settings;
35import android.provider.Settings.Secure;
36import android.text.TextUtils;
37import com.android.settingslib.wrapper.LocationManagerWrapper;
38import java.util.HashMap;
39import java.util.Map;
40import org.junit.Before;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43import org.mockito.ArgumentMatcher;
44import org.mockito.ArgumentMatchers;
45import org.mockito.Mock;
46import org.mockito.MockitoAnnotations;
47import org.robolectric.RuntimeEnvironment;
48import org.robolectric.annotation.Config;
Maggiefab2e2c2017-11-21 11:57:30 -080049import org.robolectric.annotation.Implementation;
50import org.robolectric.annotation.Implements;
51import org.robolectric.shadows.ShadowSettings;
Daniel Nishi53982802017-05-23 15:54:34 -070052
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070053@RunWith(SettingsLibRobolectricTestRunner.class)
Maggiefab2e2c2017-11-21 11:57:30 -080054@Config(
55 manifest = TestConfig.MANIFEST_PATH,
56 sdk = TestConfig.SDK_VERSION,
Maggieaa080f92018-01-04 15:35:11 -080057 shadows = {
58 UtilsTest.ShadowSecure.class,
59 UtilsTest.ShadowLocationManagerWrapper.class})
jackqdyulei5dc1f362017-02-17 14:41:05 -080060public 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
Maggiefab2e2c2017-11-21 11:57:30 -080068 private Context mContext;
Maggieaa080f92018-01-04 15:35:11 -080069 @Mock
70 private LocationManager mLocationManager;
Maggiefab2e2c2017-11-21 11:57:30 -080071
72 @Before
73 public void setUp() {
Maggieaa080f92018-01-04 15:35:11 -080074 MockitoAnnotations.initMocks(this);
Maggiefab2e2c2017-11-21 11:57:30 -080075 mContext = spy(RuntimeEnvironment.application);
Maggieaa080f92018-01-04 15:35:11 -080076 when(mContext.getSystemService(Context.LOCATION_SERVICE)).thenReturn(mLocationManager);
Maggiefab2e2c2017-11-21 11:57:30 -080077 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 Tang0cba58f2018-01-23 21:14:15 -080087 currentUserId,
88 Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS);
Maggiefab2e2c2017-11-21 11:57:30 -080089
90 verify(mContext).sendBroadcastAsUser(
91 argThat(actionMatches(LocationManager.MODE_CHANGING_ACTION)),
92 ArgumentMatchers.eq(UserHandle.of(currentUserId)),
93 ArgumentMatchers.eq(WRITE_SECURE_SETTINGS));
Lifu Tang0cba58f2018-01-23 21:14:15 -080094 assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
95 Settings.Secure.LOCATION_CHANGER, Settings.Secure.LOCATION_CHANGER_UNKNOWN))
96 .isEqualTo(Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS);
Maggiefab2e2c2017-11-21 11:57:30 -080097 }
98
jackqdyulei5dc1f362017-02-17 14:41:05 -080099 @Test
Maggieaa080f92018-01-04 15:35:11 -0800100 public void testUpdateLocationEnabled_sendBroadcast() {
101 int currentUserId = ActivityManager.getCurrentUser();
Lifu Tang0cba58f2018-01-23 21:14:15 -0800102 Utils.updateLocationEnabled(mContext, true, currentUserId,
103 Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS);
Maggieaa080f92018-01-04 15:35:11 -0800104
105 verify(mContext).sendBroadcastAsUser(
106 argThat(actionMatches(LocationManager.MODE_CHANGING_ACTION)),
107 ArgumentMatchers.eq(UserHandle.of(currentUserId)),
108 ArgumentMatchers.eq(WRITE_SECURE_SETTINGS));
Lifu Tang0cba58f2018-01-23 21:14:15 -0800109 assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
110 Settings.Secure.LOCATION_CHANGER, Settings.Secure.LOCATION_CHANGER_UNKNOWN))
111 .isEqualTo(Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS);
Maggieaa080f92018-01-04 15:35:11 -0800112 }
113
114 @Test
jackqdyulei5dc1f362017-02-17 14:41:05 -0800115 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 Nishi53982802017-05-23 15:54:34 -0700137
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 }
Maggiefab2e2c2017-11-21 11:57:30 -0800151
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
Maggieaa080f92018-01-04 15:35:11 -0800166 @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
Maggiefab2e2c2017-11-21 11:57:30 -0800175 public static void reset() {
176 map.clear();
177 }
178 }
Maggieaa080f92018-01-04 15:35:11 -0800179
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 }
jackqdyulei5dc1f362017-02-17 14:41:05 -0800188}