1 /* 2 * Copyright (C) 2020 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.deviceinfo; 18 19 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 21 import static com.android.settings.deviceinfo.DeviceNamePreferenceController.RES_SHOW_DEVICE_NAME_BOOL; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import static org.mockito.Mockito.doReturn; 26 import static org.mockito.Mockito.spy; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.bluetooth.BluetoothAdapter; 31 import android.content.Context; 32 import android.content.res.Resources; 33 import android.net.wifi.SoftApConfiguration; 34 import android.net.wifi.WifiManager; 35 import android.os.Build; 36 import android.os.Looper; 37 import android.provider.Settings; 38 39 import androidx.preference.PreferenceManager; 40 import androidx.preference.PreferenceScreen; 41 import androidx.test.core.app.ApplicationProvider; 42 import androidx.test.ext.junit.runners.AndroidJUnit4; 43 44 import com.android.settings.widget.ValidatedEditTextPreference; 45 46 import org.junit.After; 47 import org.junit.Before; 48 import org.junit.Ignore; 49 import org.junit.Test; 50 import org.junit.runner.RunWith; 51 import org.mockito.ArgumentCaptor; 52 import org.mockito.Mock; 53 import org.mockito.MockitoAnnotations; 54 55 @RunWith(AndroidJUnit4.class) 56 public class DeviceNamePreferenceControllerTest { 57 private static final String TESTING_STRING = "Testing"; 58 private static final String TEST_PREFERENCE_KEY = "test_key"; 59 60 @Mock 61 private WifiManager mWifiManager; 62 private PreferenceScreen mScreen; 63 private ValidatedEditTextPreference mPreference; 64 private DeviceNamePreferenceController mController; 65 private Context mContext; 66 private Resources mResources; 67 private BluetoothAdapter mBluetoothAdapter; 68 69 70 @Before setUp()71 public void setUp() { 72 MockitoAnnotations.initMocks(this); 73 mContext = spy(ApplicationProvider.getApplicationContext()); 74 when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager); 75 mResources = spy(mContext.getResources()); 76 when(mContext.getResources()).thenReturn(mResources); 77 78 if (Looper.myLooper() == null) { 79 Looper.prepare(); 80 } 81 PreferenceManager preferenceManager = new PreferenceManager(mContext); 82 mScreen = preferenceManager.createPreferenceScreen(mContext); 83 mPreference = new ValidatedEditTextPreference(mContext); 84 mPreference.setKey(TEST_PREFERENCE_KEY); 85 mScreen.addPreference(mPreference); 86 87 final SoftApConfiguration configuration = 88 new SoftApConfiguration.Builder().setSsid("test-ap").build(); 89 when(mWifiManager.getSoftApConfiguration()).thenReturn(configuration); 90 91 mController = new DeviceNamePreferenceController(mContext, TEST_PREFERENCE_KEY); 92 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 93 } 94 95 @After tearDown()96 public void tearDown() { 97 Settings.Global.putString( 98 mContext.getContentResolver(), Settings.Global.DEVICE_NAME, null); 99 } 100 101 @Test getAvailibilityStatus_availableByDefault()102 public void getAvailibilityStatus_availableByDefault() { 103 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 104 } 105 106 @Test getAvailabilityStatus_unsupportedWhenSet()107 public void getAvailabilityStatus_unsupportedWhenSet() { 108 doReturn(false).when(mResources).getBoolean(RES_SHOW_DEVICE_NAME_BOOL); 109 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 110 } 111 112 @Test constructor_defaultDeviceNameIsModelName()113 public void constructor_defaultDeviceNameIsModelName() { 114 assertThat(mController.getSummary()).isEqualTo(Build.MODEL); 115 } 116 117 @Test constructor_deviceNameLoadedIfSet()118 public void constructor_deviceNameLoadedIfSet() { 119 Settings.Global.putString( 120 mContext.getContentResolver(), Settings.Global.DEVICE_NAME, "Test"); 121 mController = new DeviceNamePreferenceController(mContext, "test_key"); 122 assertThat(mController.getSummary()).isEqualTo("Test"); 123 } 124 125 @Test isTextValid_nameUnder33Characters_isValid()126 public void isTextValid_nameUnder33Characters_isValid() { 127 assertThat(mController.isTextValid("12345678901234567890123456789012")).isTrue(); 128 } 129 130 @Test isTextValid_nameTooLong_isInvalid()131 public void isTextValid_nameTooLong_isInvalid() { 132 assertThat(mController.isTextValid("123456789012345678901234567890123")).isFalse(); 133 } 134 135 @Test setDeviceName_preferenceUpdatedWhenDeviceNameUpdated()136 public void setDeviceName_preferenceUpdatedWhenDeviceNameUpdated() { 137 acceptDeviceName(true); 138 mController.displayPreference(mScreen); 139 mController.onPreferenceChange(mPreference, TESTING_STRING); 140 141 assertThat(mPreference.getSummary()).isEqualTo(TESTING_STRING); 142 } 143 144 // TODO(b/175389659): Determine why this test case fails for virtual but not local devices. 145 @Ignore 146 @Test setDeviceName_bluetoothNameUpdatedWhenDeviceNameUpdated()147 public void setDeviceName_bluetoothNameUpdatedWhenDeviceNameUpdated() { 148 acceptDeviceName(true); 149 mController.displayPreference(mScreen); 150 mController.onPreferenceChange(mPreference, TESTING_STRING); 151 152 assertThat(mBluetoothAdapter.getName()).isEqualTo(TESTING_STRING); 153 } 154 155 @Test setDeviceName_wifiTetherNameUpdatedWhenDeviceNameUpdated()156 public void setDeviceName_wifiTetherNameUpdatedWhenDeviceNameUpdated() { 157 acceptDeviceName(true); 158 mController.displayPreference(mScreen); 159 mController.onPreferenceChange(mPreference, TESTING_STRING); 160 161 ArgumentCaptor<SoftApConfiguration> captor = 162 ArgumentCaptor.forClass(SoftApConfiguration.class); 163 verify(mWifiManager).setSoftApConfiguration(captor.capture()); 164 assertThat(captor.getValue().getSsid()).isEqualTo(TESTING_STRING); 165 } 166 167 @Test displayPreference_defaultDeviceNameIsModelNameOnPreference()168 public void displayPreference_defaultDeviceNameIsModelNameOnPreference() { 169 mController.displayPreference(mScreen); 170 171 assertThat(mPreference.getText()).isEqualTo(Build.MODEL); 172 } 173 174 @Test setDeviceName_okInDeviceNameWarningDialog_shouldChangePreferenceText()175 public void setDeviceName_okInDeviceNameWarningDialog_shouldChangePreferenceText() { 176 acceptDeviceName(true); 177 mController.displayPreference(mScreen); 178 mController.onPreferenceChange(mPreference, TESTING_STRING); 179 180 assertThat(mPreference.getSummary()).isEqualTo(TESTING_STRING); 181 } 182 183 @Test setDeviceName_cancelInDeviceNameWarningDialog_shouldNotChangePreferenceText()184 public void setDeviceName_cancelInDeviceNameWarningDialog_shouldNotChangePreferenceText() { 185 acceptDeviceName(false); 186 mController.displayPreference(mScreen); 187 mController.onPreferenceChange(mPreference, TESTING_STRING); 188 189 assertThat(mPreference.getSummary()).isNotEqualTo(TESTING_STRING); 190 assertThat(mPreference.getText()).isEqualTo(mPreference.getSummary()); 191 } 192 acceptDeviceName(boolean accept)193 private void acceptDeviceName(boolean accept) { 194 mController.setHost(deviceName -> mController.updateDeviceName(accept)); 195 } 196 } 197