1 /* 2 * Copyright (C) 2021 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.wifi; 18 19 import static android.os.UserManager.DISALLOW_CONFIG_WIFI; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.ArgumentMatchers.anyString; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.never; 26 import static org.mockito.Mockito.reset; 27 import static org.mockito.Mockito.spy; 28 import static org.mockito.Mockito.verify; 29 import static org.mockito.Mockito.when; 30 31 import android.content.Context; 32 import android.os.Bundle; 33 import android.os.UserManager; 34 35 import androidx.test.core.app.ApplicationProvider; 36 37 import org.junit.After; 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.junit.MockitoJUnit; 44 import org.mockito.junit.MockitoRule; 45 import org.robolectric.RobolectricTestRunner; 46 47 @RunWith(RobolectricTestRunner.class) 48 public class WifiRestrictionsCacheTest { 49 50 private static final int USER_OWNER = 0; 51 private static final int USER_1 = 1; 52 private static final int USER_2 = 2; 53 private static final int USER_3 = 3; 54 private static final int USER_GUEST = 10; 55 56 @Rule 57 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 58 @Mock 59 UserManager mUserManager; 60 @Mock 61 Bundle mUserRestrictionsOwner; 62 @Mock 63 Bundle mUserRestrictionsGuest; 64 65 private Context mContext; 66 private WifiRestrictionsCache mWifiRestrictionsCacheOwner; 67 private WifiRestrictionsCache mWifiRestrictionsCacheGuest; 68 69 @Before setUp()70 public void setUp() { 71 mContext = spy(ApplicationProvider.getApplicationContext()); 72 when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); 73 74 when(mContext.getUserId()).thenReturn(USER_OWNER); 75 when(mUserManager.getUserRestrictions()).thenReturn(mUserRestrictionsOwner); 76 when(mUserRestrictionsOwner.getBoolean(anyString())).thenReturn(false); 77 mWifiRestrictionsCacheOwner = WifiRestrictionsCache.getInstance(mContext); 78 79 when(mContext.getUserId()).thenReturn(USER_GUEST); 80 when(mUserManager.getUserRestrictions()).thenReturn(mUserRestrictionsGuest); 81 when(mUserRestrictionsGuest.getBoolean(anyString())).thenReturn(true); 82 mWifiRestrictionsCacheGuest = WifiRestrictionsCache.getInstance(mContext); 83 } 84 85 @After tearDown()86 public void tearDown() { 87 WifiRestrictionsCache.clearInstance(); 88 } 89 90 @Test getInstance_sameUserId_sameInstance()91 public void getInstance_sameUserId_sameInstance() { 92 when(mContext.getUserId()).thenReturn(USER_OWNER); 93 WifiRestrictionsCache instance1 = WifiRestrictionsCache.getInstance(mContext); 94 95 WifiRestrictionsCache instance2 = WifiRestrictionsCache.getInstance(mContext); 96 97 assertThat(instance1).isEqualTo(instance2); 98 } 99 100 @Test getInstance_diffUserId_diffInstance()101 public void getInstance_diffUserId_diffInstance() { 102 when(mContext.getUserId()).thenReturn(USER_OWNER); 103 WifiRestrictionsCache instance1 = WifiRestrictionsCache.getInstance(mContext); 104 105 when(mContext.getUserId()).thenReturn(USER_GUEST); 106 WifiRestrictionsCache instance2 = WifiRestrictionsCache.getInstance(mContext); 107 108 assertThat(instance1).isNotEqualTo(instance2); 109 } 110 111 @Test clearInstance_instanceShouldBeEmpty()112 public void clearInstance_instanceShouldBeEmpty() { 113 WifiRestrictionsCache.clearInstance(); 114 115 assertThat(WifiRestrictionsCache.sInstances.size()).isEqualTo(0); 116 } 117 118 @Test getRestriction_firstTime_getFromSystem()119 public void getRestriction_firstTime_getFromSystem() { 120 Bundle userRestrictions = mock(Bundle.class); 121 WifiRestrictionsCache wifiRestrictionsCache = mockInstance(USER_1, userRestrictions); 122 123 wifiRestrictionsCache.getRestriction(DISALLOW_CONFIG_WIFI); 124 125 verify(userRestrictions).getBoolean(DISALLOW_CONFIG_WIFI); 126 } 127 128 @Test getRestriction_secondTime_notGetFromSystem()129 public void getRestriction_secondTime_notGetFromSystem() { 130 Bundle userRestrictions = mock(Bundle.class); 131 WifiRestrictionsCache wifiRestrictionsCache = mockInstance(USER_2, userRestrictions); 132 // First time to get the restriction value 133 wifiRestrictionsCache.getRestriction(DISALLOW_CONFIG_WIFI); 134 reset(userRestrictions); 135 136 // Second time to get the restriction value 137 wifiRestrictionsCache.getRestriction(DISALLOW_CONFIG_WIFI); 138 139 verify(userRestrictions, never()).getBoolean(DISALLOW_CONFIG_WIFI); 140 } 141 142 @Test clearRestrictions_shouldGetRestrictionFromSystemAgain()143 public void clearRestrictions_shouldGetRestrictionFromSystemAgain() { 144 Bundle userRestrictions = mock(Bundle.class); 145 WifiRestrictionsCache wifiRestrictionsCache = mockInstance(USER_3, userRestrictions); 146 // First time to get the restriction value 147 wifiRestrictionsCache.getRestriction(DISALLOW_CONFIG_WIFI); 148 reset(userRestrictions); 149 150 // Clear the cache and then second time to get the restriction value 151 wifiRestrictionsCache.clearRestrictions(); 152 wifiRestrictionsCache.getRestriction(DISALLOW_CONFIG_WIFI); 153 154 verify(userRestrictions).getBoolean(DISALLOW_CONFIG_WIFI); 155 } 156 157 @Test isConfigWifiAllowed_ownerUser_returnTrue()158 public void isConfigWifiAllowed_ownerUser_returnTrue() { 159 assertThat(mWifiRestrictionsCacheOwner.isConfigWifiAllowed()).isTrue(); 160 } 161 162 @Test isConfigWifiAllowed_guestUser_returnFalse()163 public void isConfigWifiAllowed_guestUser_returnFalse() { 164 assertThat(mWifiRestrictionsCacheGuest.isConfigWifiAllowed()).isFalse(); 165 } 166 mockInstance(int userId, Bundle userRestrictions)167 private WifiRestrictionsCache mockInstance(int userId, Bundle userRestrictions) { 168 when(mContext.getUserId()).thenReturn(userId); 169 when(mUserManager.getUserRestrictions()).thenReturn(userRestrictions); 170 return WifiRestrictionsCache.getInstance(mContext); 171 } 172 } 173