1 /* 2 * Copyright (C) 2018 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.settings; 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 TetherSettingsTest { 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 setupIsTetherAvailable(true); 62 63 when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]); 64 when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]); 65 } 66 67 @Test testTetherNonIndexableKeys_tetherAvailable_keysNotReturned()68 public void testTetherNonIndexableKeys_tetherAvailable_keysNotReturned() { 69 // To let TetherUtil.isTetherAvailable return true, select one of the combinations 70 setupIsTetherAvailable(true); 71 72 final List<String> niks = 73 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 74 75 assertThat(niks).doesNotContain(TetherSettings.KEY_TETHER_PREFS_SCREEN); 76 assertThat(niks).doesNotContain(TetherSettings.KEY_WIFI_TETHER); 77 } 78 79 @Test testTetherNonIndexableKeys_tetherNotAvailable_keysReturned()80 public void testTetherNonIndexableKeys_tetherNotAvailable_keysReturned() { 81 // To let TetherUtil.isTetherAvailable return false, select one of the combinations 82 setupIsTetherAvailable(false); 83 84 final List<String> niks = 85 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 86 87 assertThat(niks).contains(TetherSettings.KEY_TETHER_PREFS_SCREEN); 88 assertThat(niks).contains(TetherSettings.KEY_WIFI_TETHER); 89 } 90 91 @Test testTetherNonIndexableKeys_usbNotAvailable_usbKeyReturned()92 public void testTetherNonIndexableKeys_usbNotAvailable_usbKeyReturned() { 93 when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]); 94 95 final List<String> niks = 96 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 97 98 assertThat(niks).contains(TetherSettings.KEY_USB_TETHER_SETTINGS); 99 } 100 101 @Test testTetherNonIndexableKeys_usbAvailable_usbKeyNotReturned()102 public void testTetherNonIndexableKeys_usbAvailable_usbKeyNotReturned() { 103 // We can ignore the condition of Utils.isMonkeyRunning() 104 // In normal case, monkey and robotest should not execute at the same time 105 when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[]{"dummyRegex"}); 106 107 final List<String> niks = 108 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 109 110 assertThat(niks).doesNotContain(TetherSettings.KEY_USB_TETHER_SETTINGS); 111 } 112 113 @Test testTetherNonIndexableKeys_bluetoothNotAvailable_bluetoothKeyReturned()114 public void testTetherNonIndexableKeys_bluetoothNotAvailable_bluetoothKeyReturned() { 115 when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]); 116 117 final List<String> niks = 118 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 119 120 assertThat(niks).contains(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING); 121 } 122 123 @Test testTetherNonIndexableKeys_bluetoothAvailable_bluetoothKeyNotReturned()124 public void testTetherNonIndexableKeys_bluetoothAvailable_bluetoothKeyNotReturned() { 125 when(mConnectivityManager.getTetherableBluetoothRegexs()) 126 .thenReturn(new String[]{"dummyRegex"}); 127 128 final List<String> niks = 129 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 130 131 assertThat(niks).doesNotContain(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING); 132 } 133 setupIsTetherAvailable(boolean returnValue)134 private void setupIsTetherAvailable(boolean returnValue) { 135 when(mConnectivityManager.isTetheringSupported()).thenReturn(true); 136 137 // For RestrictedLockUtils.checkIfRestrictionEnforced 138 final int userId = UserHandle.myUserId(); 139 List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>(); 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(!returnValue); 148 } 149 } 150