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 17 package com.android.settingslib; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.net.ConnectivityManager; 27 import android.os.UserHandle; 28 import android.os.UserManager; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RobolectricTestRunner; 36 import org.robolectric.RuntimeEnvironment; 37 38 import java.util.ArrayList; 39 import java.util.List; 40 41 @RunWith(RobolectricTestRunner.class) 42 public class TetherUtilTest { 43 44 private Context mContext; 45 46 @Mock 47 private ConnectivityManager mConnectivityManager; 48 @Mock 49 private UserManager mUserManager; 50 51 @Before setUp()52 public void setUp() { 53 mContext = spy(RuntimeEnvironment.application); 54 55 MockitoAnnotations.initMocks(this); 56 doReturn(mConnectivityManager) 57 .when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE); 58 doReturn(mUserManager) 59 .when(mContext).getSystemService(Context.USER_SERVICE); 60 } 61 62 @Test isEntitlementCheckRequired_noConfigManager_returnTrue()63 public void isEntitlementCheckRequired_noConfigManager_returnTrue() { 64 doReturn(null).when(mContext).getSystemService(Context.CARRIER_CONFIG_SERVICE); 65 66 assertThat(TetherUtil.isEntitlementCheckRequired(mContext)).isTrue(); 67 } 68 69 @Test isTetherAvailable_supported_configDisallowed_hasUserRestriction_returnTrue()70 public void isTetherAvailable_supported_configDisallowed_hasUserRestriction_returnTrue() { 71 setupIsTetherAvailable(true, true, true); 72 73 assertThat(TetherUtil.isTetherAvailable(mContext)).isFalse(); 74 } 75 76 @Test isTetherAvailable_notSupported_configDisallowed_hasUserRestriction_returnTrue()77 public void isTetherAvailable_notSupported_configDisallowed_hasUserRestriction_returnTrue() { 78 setupIsTetherAvailable(false, true, true); 79 80 assertThat(TetherUtil.isTetherAvailable(mContext)).isFalse(); 81 } 82 83 @Test isTetherAvailable_supported_configAllowed_hasUserRestriction_returnTrue()84 public void isTetherAvailable_supported_configAllowed_hasUserRestriction_returnTrue() { 85 setupIsTetherAvailable(true, false, true); 86 87 assertThat(TetherUtil.isTetherAvailable(mContext)).isFalse(); 88 } 89 90 @Test isTetherAvailable_notSupported_configAllowed_hasUserRestriction_returnFalse()91 public void isTetherAvailable_notSupported_configAllowed_hasUserRestriction_returnFalse() { 92 setupIsTetherAvailable(false, false, true); 93 94 assertThat(TetherUtil.isTetherAvailable(mContext)).isFalse(); 95 } 96 97 @Test isTetherAvailable_supported_configDisallowed_noUserRestriction_returnTrue()98 public void isTetherAvailable_supported_configDisallowed_noUserRestriction_returnTrue() { 99 setupIsTetherAvailable(true, true, false); 100 101 assertThat(TetherUtil.isTetherAvailable(mContext)).isTrue(); 102 } 103 104 @Test isTetherAvailable_notSupported_configDisallowed_noUserRestriction_returnTrue()105 public void isTetherAvailable_notSupported_configDisallowed_noUserRestriction_returnTrue() { 106 setupIsTetherAvailable(false, true, false); 107 108 assertThat(TetherUtil.isTetherAvailable(mContext)).isTrue(); 109 } 110 111 @Test isTetherAvailable_supported_configAllowed_noUserRestriction_returnTrue()112 public void isTetherAvailable_supported_configAllowed_noUserRestriction_returnTrue() { 113 setupIsTetherAvailable(true, false, false); 114 115 assertThat(TetherUtil.isTetherAvailable(mContext)).isTrue(); 116 } 117 118 @Test isTetherAvailable_notSupported_configAllowed_noUserRestriction_returnFalse()119 public void isTetherAvailable_notSupported_configAllowed_noUserRestriction_returnFalse() { 120 setupIsTetherAvailable(false, false, false); 121 122 assertThat(TetherUtil.isTetherAvailable(mContext)).isFalse(); 123 } 124 setupIsTetherAvailable(boolean tetherSupported, boolean configAllowed, boolean hasBseUserRestriction)125 private void setupIsTetherAvailable(boolean tetherSupported, boolean configAllowed, 126 boolean hasBseUserRestriction) { 127 when(mConnectivityManager.isTetheringSupported()).thenReturn(tetherSupported); 128 129 // For RestrictedLockUtils.checkIfRestrictionEnforced 130 final int userId = UserHandle.myUserId(); 131 List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>(); 132 if (configAllowed) { 133 // Add two enforcing users so that RestrictedLockUtils.checkIfRestrictionEnforced 134 // returns non-null 135 enforcingUsers.add(new UserManager.EnforcingUser(userId, 136 UserManager.RESTRICTION_SOURCE_DEVICE_OWNER)); 137 enforcingUsers.add(new UserManager.EnforcingUser(userId, 138 UserManager.RESTRICTION_SOURCE_PROFILE_OWNER)); 139 } 140 when(mUserManager.getUserRestrictionSources( 141 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId))) 142 .thenReturn(enforcingUsers); 143 144 // For RestrictedLockUtils.hasBaseUserRestriction 145 when(mUserManager.hasBaseUserRestriction( 146 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId))) 147 .thenReturn(hasBseUserRestriction); 148 } 149 } 150