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.settings.network; 18 19 import android.bluetooth.BluetoothAdapter; 20 import android.bluetooth.BluetoothPan; 21 import android.bluetooth.BluetoothProfile; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.database.ContentObserver; 27 import android.net.ConnectivityManager; 28 import android.provider.Settings; 29 import android.support.v7.preference.Preference; 30 31 import com.android.settings.R; 32 import com.android.settings.testutils.SettingsRobolectricTestRunner; 33 import com.android.settings.TestConfig; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.annotation.Config; 42 import org.robolectric.util.ReflectionHelpers; 43 44 import java.util.concurrent.atomic.AtomicReference; 45 46 import static org.mockito.Matchers.any; 47 import static org.mockito.Mockito.mock; 48 import static org.mockito.Mockito.spy; 49 import static org.mockito.Mockito.verify; 50 import static org.mockito.Mockito.verifyNoMoreInteractions; 51 import static org.mockito.Mockito.verifyZeroInteractions; 52 import static org.mockito.Mockito.when; 53 54 @RunWith(SettingsRobolectricTestRunner.class) 55 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 56 public class TetherPreferenceControllerTest { 57 58 @Mock 59 private Context mContext; 60 @Mock 61 private ConnectivityManager mConnectivityManager; 62 @Mock 63 private BluetoothAdapter mBluetoothAdapter; 64 @Mock 65 private Preference mPreference; 66 67 private TetherPreferenceController mController; 68 69 @Before 70 public void setUp() { 71 MockitoAnnotations.initMocks(this); 72 mController = spy(TetherPreferenceController.class); 73 ReflectionHelpers.setField(mController, "mContext", mContext); 74 ReflectionHelpers.setField(mController, "mConnectivityManager", mConnectivityManager); 75 ReflectionHelpers.setField(mController, "mBluetoothAdapter", mBluetoothAdapter); 76 ReflectionHelpers.setField(mController, "mPreference", mPreference); 77 } 78 79 @Test 80 public void lifeCycle_onCreate_shouldInitBluetoothPan() { 81 mController.onCreate(null); 82 83 verify(mBluetoothAdapter).getProfileProxy(mContext, mController.mBtProfileServiceListener, 84 BluetoothProfile.PAN); 85 } 86 87 @Test 88 public void goThroughLifecycle_shouldDestoryBluetoothProfile() { 89 final BluetoothPan pan = mock(BluetoothPan.class); 90 final AtomicReference<BluetoothPan> panRef = 91 ReflectionHelpers.getField(mController, "mBluetoothPan"); 92 panRef.set(pan); 93 94 mController.onDestroy(); 95 96 verify(mBluetoothAdapter).closeProfileProxy(BluetoothProfile.PAN, pan); 97 } 98 99 @Test 100 public void updateSummary_noPreference_noInteractionWithConnectivityManager() { 101 ReflectionHelpers.setField(mController, "mPreference", null); 102 mController.updateSummary(); 103 verifyNoMoreInteractions(mConnectivityManager); 104 } 105 106 @Test 107 public void updateSummary_wifiTethered_shouldShowHotspotMessage() { 108 when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[]{"123"}); 109 when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"123"}); 110 111 mController.updateSummary(); 112 verify(mPreference).setSummary(R.string.tether_settings_summary_hotspot_on_tether_off); 113 } 114 115 @Test 116 public void updateSummary_btThetherOn_shouldShowTetherMessage() { 117 when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[]{"123"}); 118 when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[]{"123"}); 119 120 mController.updateSummary(); 121 verify(mPreference).setSummary(R.string.tether_settings_summary_hotspot_off_tether_on); 122 } 123 124 @Test 125 public void updateSummary_tetherOff_shouldShowTetherOffMessage() { 126 when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[]{"123"}); 127 when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"456"}); 128 129 mController.updateSummary(); 130 verify(mPreference).setSummary(R.string.switch_off_text); 131 } 132 133 @Test 134 public void updateSummary_wifiBtTetherOn_shouldShowHotspotAndTetherMessage() { 135 when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[]{"123", "456"}); 136 when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"456"}); 137 when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[]{"23"}); 138 139 mController.updateSummary(); 140 verify(mPreference).setSummary(R.string.tether_settings_summary_hotspot_on_tether_on); 141 } 142 143 @Test 144 public void airplaneModeOn_shouldUpdateSummaryToOff() { 145 ReflectionHelpers.setField(mController, "mContext", RuntimeEnvironment.application); 146 147 Settings.Global.putInt(RuntimeEnvironment.application.getContentResolver(), 148 Settings.Global.AIRPLANE_MODE_ON, 0); 149 150 mController.onResume(); 151 152 verifyZeroInteractions(mPreference); 153 154 Settings.Global.putInt(RuntimeEnvironment.application.getContentResolver(), 155 Settings.Global.AIRPLANE_MODE_ON, 1); 156 157 final ContentObserver observer = ReflectionHelpers.getField(mController, 158 "mAirplaneModeObserver"); 159 observer.onChange(true, Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON)); 160 161 verify(mPreference).setSummary(R.string.switch_off_text); 162 } 163 164 @Test 165 public void onResume_shouldRegisterTetherReceiver() { 166 when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class)); 167 168 mController.onResume(); 169 170 verify(mContext).registerReceiver( 171 any(TetherPreferenceController.TetherBroadcastReceiver.class), 172 any(IntentFilter.class)); 173 } 174 175 @Test 176 public void onPause_shouldUnregisterTetherReceiver() { 177 when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class)); 178 mController.onResume(); 179 180 mController.onPause(); 181 182 verify(mContext).unregisterReceiver( 183 any(TetherPreferenceController.TetherBroadcastReceiver.class)); 184 } 185 186 @Test 187 public void tetherStatesChanged_shouldUpdateSummary() { 188 final Context context = RuntimeEnvironment.application; 189 ReflectionHelpers.setField(mController, "mContext", context); 190 mController.onResume(); 191 192 context.sendBroadcast(new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)); 193 194 verify(mController).updateSummary(); 195 } 196 197 } 198