Merge "Block location accuracy when DISALLOW_CONFIG_LOCATION is set." into pi-dev

This commit is contained in:
Yueming Wang
2018-03-07 20:10:10 +00:00
committed by Android (Google) Code Review
6 changed files with 347 additions and 42 deletions

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.location;
import static com.google.common.truth.Truth.assertThat;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(SettingsRobolectricTestRunner.class)
public final class InjectedSettingTest {
private static final String TEST_STRING = "test";
@Test
public void buildWithoutPackageName_ShouldReturnNull() {
assertThat(((new InjectedSetting.Builder())
.setClassName(TEST_STRING)
.setTitle(TEST_STRING)
.setSettingsActivity(TEST_STRING).build())).isNull();
}
private InjectedSetting getTestSetting() {
return new InjectedSetting.Builder()
.setPackageName(TEST_STRING)
.setClassName(TEST_STRING)
.setTitle(TEST_STRING)
.setSettingsActivity(TEST_STRING).build();
}
@Test
public void testEquals() {
InjectedSetting setting1 = getTestSetting();
InjectedSetting setting2 = getTestSetting();
assertThat(setting1).isEqualTo(setting2);
}
@Test
public void testHashCode() {
InjectedSetting setting = getTestSetting();
assertThat(setting.hashCode()).isEqualTo(1225314048);
}
}

View File

@@ -24,16 +24,25 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.admin.DevicePolicyManager;
import android.arch.lifecycle.LifecycleOwner;
import android.content.ComponentName;
import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settings.widget.RestrictedAppPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -41,11 +50,13 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(
shadows = {
ShadowUserManager.class
})
public class LocationServicePreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -56,6 +67,8 @@ public class LocationServicePreferenceControllerTest {
private PreferenceScreen mScreen;
@Mock
private SettingsInjector mSettingsInjector;
@Mock
private DevicePolicyManager mDevicePolicyManager;
private Context mContext;
private LocationServicePreferenceController mController;
@@ -73,6 +86,9 @@ public class LocationServicePreferenceControllerTest {
final String key = mController.getPreferenceKey();
when(mScreen.findPreference(key)).thenReturn(mCategory);
when(mCategory.getKey()).thenReturn(key);
when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
.thenReturn(mDevicePolicyManager);
}
@Test
@@ -132,4 +148,33 @@ public class LocationServicePreferenceControllerTest {
verify(mSettingsInjector).reloadStatusMessages();
}
@Test
public void withUserRestriction_shouldDisableLocationAccuracy() {
final List<Preference> preferences = new ArrayList<>();
final RestrictedAppPreference pref = new RestrictedAppPreference(mContext,
UserManager.DISALLOW_CONFIG_LOCATION);
pref.setTitle("Location Accuracy");
preferences.add(pref);
doReturn(preferences).when(mSettingsInjector)
.getInjectedSettings(any(Context.class), anyInt());
int userId = UserHandle.myUserId();
List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
enforcingUsers.add(new UserManager.EnforcingUser(userId,
UserManager.RESTRICTION_SOURCE_DEVICE_OWNER));
ComponentName componentName = new ComponentName("test", "test");
// Ensure that RestrictedLockUtils.checkIfRestrictionEnforced doesn't return null.
ShadowUserManager.getShadow().setUserRestrictionSources(
UserManager.DISALLOW_CONFIG_LOCATION,
UserHandle.of(userId),
enforcingUsers);
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(componentName);
mController.displayPreference(mScreen);
mController.updateState(mCategory);
assertThat(pref.isEnabled()).isFalse();
assertThat(pref.isDisabledByAdmin()).isTrue();
}
}