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.deviceinfo; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.anyString; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.net.wifi.WifiConfiguration; 29 import android.net.wifi.WifiManager; 30 import android.os.Build; 31 import android.provider.Settings; 32 import android.support.v7.preference.PreferenceScreen; 33 34 import com.android.settings.testutils.SettingsRobolectricTestRunner; 35 import com.android.settings.widget.ValidatedEditTextPreference; 36 import com.android.settingslib.bluetooth.LocalBluetoothAdapter; 37 import com.android.settingslib.bluetooth.LocalBluetoothManager; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Answers; 43 import org.mockito.ArgumentCaptor; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.shadows.ShadowApplication; 47 48 @RunWith(SettingsRobolectricTestRunner.class) 49 public class DeviceNamePreferenceControllerTest { 50 private static final String TESTING_STRING = "Testing"; 51 52 @Mock 53 private LocalBluetoothAdapter mBluetoothAdapter; 54 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 55 private LocalBluetoothManager mBluetoothManager; 56 @Mock 57 private WifiManager mWifiManager; 58 @Mock 59 private PreferenceScreen mScreen; 60 private ValidatedEditTextPreference mPreference; 61 private DeviceNamePreferenceController mController; 62 private Context mContext; 63 64 65 @Before 66 public void setUp() { 67 MockitoAnnotations.initMocks(this); 68 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 69 shadowApplication.setSystemService(Context.WIFI_SERVICE, mWifiManager); 70 mContext = shadowApplication.getApplicationContext(); 71 mPreference = new ValidatedEditTextPreference(mContext); 72 when(mBluetoothManager.getBluetoothAdapter()).thenReturn(mBluetoothAdapter); 73 when(mScreen.findPreference(anyString())).thenReturn(mPreference); 74 final WifiConfiguration configuration = new WifiConfiguration(); 75 configuration.SSID = "test-ap"; 76 when(mWifiManager.getWifiApConfiguration()).thenReturn(configuration); 77 78 mController = new DeviceNamePreferenceController(mContext); 79 mController.setLocalBluetoothManager(mBluetoothManager); 80 } 81 82 @Test 83 public void constructor_defaultDeviceNameIsModelName() { 84 assertThat(mController.getSummary()).isEqualTo(Build.MODEL); 85 } 86 87 @Test 88 public void constructor_deviceNameLoadedIfSet() { 89 Settings.Global.putString( 90 mContext.getContentResolver(), Settings.Global.DEVICE_NAME, "Test"); 91 mController = new DeviceNamePreferenceController(mContext); 92 mController.setLocalBluetoothManager(mBluetoothManager); 93 assertThat(mController.getSummary()).isEqualTo("Test"); 94 } 95 96 @Test 97 public void isTextValid_nameUnder33CharactersIsValid() { 98 assertThat(mController.isTextValid("12345678901234567890123456789012")).isTrue(); 99 } 100 101 @Test 102 public void isTextValid_nameTooLongIsInvalid() { 103 assertThat(mController.isTextValid("123456789012345678901234567890123")).isFalse(); 104 } 105 106 @Test 107 public void setDeviceName_preferenceUpdatedWhenDeviceNameUpdated() { 108 forceAcceptDeviceName(); 109 mController.displayPreference(mScreen); 110 mController.onPreferenceChange(mPreference, TESTING_STRING); 111 112 assertThat(mPreference.getSummary()).isEqualTo(TESTING_STRING); 113 } 114 115 @Test 116 public void setDeviceName_bluetoothNameUpdatedWhenDeviceNameUpdated() { 117 forceAcceptDeviceName(); 118 mController.displayPreference(mScreen); 119 mController.onPreferenceChange(mPreference, TESTING_STRING); 120 121 verify(mBluetoothAdapter).setName(eq(TESTING_STRING)); 122 } 123 124 @Test 125 public void setDeviceName_wifiTetherNameUpdatedWhenDeviceNameUpdated() { 126 forceAcceptDeviceName(); 127 mController.displayPreference(mScreen); 128 mController.onPreferenceChange(mPreference, TESTING_STRING); 129 130 ArgumentCaptor<WifiConfiguration> captor = ArgumentCaptor.forClass(WifiConfiguration.class); 131 verify(mWifiManager).setWifiApConfiguration(captor.capture()); 132 assertThat(captor.getValue().SSID).isEqualTo(TESTING_STRING); 133 } 134 135 @Test 136 public void displayPreference_defaultDeviceNameIsModelNameOnPreference() { 137 mController.displayPreference(mScreen); 138 139 assertThat(mPreference.getText()).isEqualTo(Build.MODEL); 140 } 141 142 @Test 143 public void setDeviceName_ignoresIfCancelPressed() { 144 mController.displayPreference(mScreen); 145 mController.onPreferenceChange(mPreference, TESTING_STRING); 146 147 verify(mBluetoothAdapter, never()).setName(eq(TESTING_STRING)); 148 } 149 150 private void forceAcceptDeviceName() { 151 mController.setHost( 152 new DeviceNamePreferenceController.DeviceNamePreferenceHost() { 153 @Override 154 public void showDeviceNameWarningDialog(String deviceName) { 155 mController.confirmDeviceName(); 156 } 157 }); 158 } 159 160 } 161