Merge "Create preference controller for each tethering option"

This commit is contained in:
TreeHugger Robot
2020-01-08 02:08:39 +00:00
committed by Android (Google) Code Review
8 changed files with 852 additions and 48 deletions

View File

@@ -0,0 +1,87 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class BluetoothTetherPreferenceControllerTest {
@Mock
private ConnectivityManager mConnectivityManager;
private BluetoothTetherPreferenceController mController;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[] {""});
mController = new BluetoothTetherPreferenceController(mContext, mock(Lifecycle.class));
}
@Test
public void lifecycle_shouldRegisterReceiverOnStart() {
mController.onStart();
verify(mContext).registerReceiver(
eq(mController.mBluetoothChangeReceiver),
any());
}
@Test
public void lifecycle_shouldUnregisterReceiverOnStop() {
mController.onStart();
mController.onStop();
verify(mContext).unregisterReceiver(
eq(mController.mBluetoothChangeReceiver));
}
@Test
public void display_availableChangedCorrectly() {
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[] {""});
assertThat(mController.isAvailable()).isTrue();
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
}
}

View File

@@ -26,7 +26,6 @@ import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -83,9 +82,22 @@ public class TetherEnablerTest {
mNetworkPolicyManager);
when(mConnectivityManager.getTetherableIfaces()).thenReturn(new String[0]);
panReference.set(mBluetoothPan);
when(context.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE))
.thenReturn(mSharedPreferences);
mEnabler = new TetherEnabler(context, new SwitchBarController(mSwitchBar), panReference);
}
@Test
public void lifecycle_onPause_unRegisterSharedPreferenceListener() {
mEnabler.onResume();
verify(mSharedPreferences).registerOnSharedPreferenceChangeListener(
eq(mEnabler));
mEnabler.onPause();
verify(mSharedPreferences).unregisterOnSharedPreferenceChangeListener(
eq(mEnabler));
}
@Test
public void lifecycle_onStart_setCheckedCorrectly() {
when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[]{""});
@@ -122,29 +134,27 @@ public class TetherEnablerTest {
@Test
public void onSwitchToggled_onlyStartsWifiTetherWhenNeeded() {
when(mSharedPreferences.getBoolean(TetherEnabler.WIFI_TETHER_KEY, true)).thenReturn(true);
when(mWifiManager.isWifiApEnabled()).thenReturn(true);
mEnabler.onSwitchToggled(true);
verify(mConnectivityManager, never()).startTethering(anyInt(), anyBoolean(), any(), any());
doReturn(false).when(mWifiManager).isWifiApEnabled();
mEnabler.onSwitchToggled(true);
verify(mConnectivityManager, times(1))
.startTethering(anyInt(), anyBoolean(), any(), any());
verify(mConnectivityManager).startTethering(anyInt(), anyBoolean(), any(), any());
}
@Test
public void onSwitchToggled_shouldStartUSBTetherWhenSelected() {
SharedPreferences preference = mock(SharedPreferences.class);
ReflectionHelpers.setField(mEnabler, "mSharedPreferences", preference);
when(preference.getBoolean(mEnabler.WIFI_TETHER_KEY, true)).thenReturn(false);
when(preference.getBoolean(mEnabler.USB_TETHER_KEY, false)).thenReturn(true);
when(preference.getBoolean(mEnabler.BLUETOOTH_TETHER_KEY, true)).thenReturn(false);
when(preference.getBoolean(TetherEnabler.WIFI_TETHER_KEY, true)).thenReturn(false);
when(preference.getBoolean(TetherEnabler.USB_TETHER_KEY, false)).thenReturn(true);
when(preference.getBoolean(TetherEnabler.BLUETOOTH_TETHER_KEY, true)).thenReturn(false);
mEnabler.startTether();
verify(mConnectivityManager, times(1))
.startTethering(eq(ConnectivityManager.TETHERING_USB), anyBoolean(), any(), any());
verify(mConnectivityManager).startTethering(
eq(ConnectivityManager.TETHERING_USB), anyBoolean(), any(), any());
verify(mConnectivityManager, never())
.startTethering(eq(ConnectivityManager.TETHERING_WIFI), anyBoolean(), any(), any());
verify(mConnectivityManager, never()).startTethering(
@@ -158,11 +168,11 @@ public class TetherEnablerTest {
when(adapter.getState()).thenReturn(BluetoothAdapter.STATE_OFF);
mEnabler.startTethering(ConnectivityManager.TETHERING_BLUETOOTH);
verify(adapter, times(1)).enable();
verify(adapter).enable();
when(adapter.getState()).thenReturn(BluetoothAdapter.STATE_ON);
mEnabler.startTethering(ConnectivityManager.TETHERING_BLUETOOTH);
verify(mConnectivityManager, times(1)).startTethering(
verify(mConnectivityManager).startTethering(
eq(ConnectivityManager.TETHERING_BLUETOOTH), anyBoolean(), any(), any());
}
}

View File

@@ -0,0 +1,84 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class UsbTetherPreferenceControllerTest {
@Mock
private ConnectivityManager mConnectivityManager;
private Context mContext;
private UsbTetherPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[] {""});
mController = new UsbTetherPreferenceController(mContext, mock(Lifecycle.class));
}
@Test
public void lifecycle_shouldRegisterReceiverOnStart() {
mController.onStart();
verify(mContext).registerReceiver(eq(mController.mUsbChangeReceiver), any());
}
@Test
public void lifecycle_shouldUnregisterReceiverOnStop() {
mController.onStart();
mController.onStop();
verify(mContext).unregisterReceiver(eq(mController.mUsbChangeReceiver));
}
@Test
public void display_availableChangedCorrectly() {
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[] {""});
assertThat(mController.isAvailable()).isTrue();
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
}
}

View File

@@ -0,0 +1,133 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network;
import static com.android.settings.network.WifiTetherDisablePreferenceController.PREF_KEY;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import androidx.lifecycle.Lifecycle;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class WifiTetherDisablePreferenceControllerTest {
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
private SharedPreferences mSharedPreferences;
@Mock
private PreferenceScreen mPreferenceScreen;
private SwitchPreference mPreference;
private Context mContext;
private WifiTetherDisablePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
mPreference = spy(SwitchPreference.class);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{""});
when(mContext.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE))
.thenReturn(mSharedPreferences);
mController = new WifiTetherDisablePreferenceController(mContext, mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mScreen", mPreferenceScreen);
ReflectionHelpers.setField(mController, "mPreference", mPreference);
when(mPreferenceScreen.findPreference(PREF_KEY)).thenReturn(mPreference);
}
@Test
public void lifecycle_shouldRegisterReceiverOnResume() {
mController.onResume();
verify(mSharedPreferences).registerOnSharedPreferenceChangeListener(eq(mController));
}
@Test
public void lifecycle_shouldUnregisterReceiverOnStop() {
mController.onResume();
mController.onPause();
verify(mSharedPreferences).unregisterOnSharedPreferenceChangeListener(eq(mController));
}
@Test
public void display_availableChangedCorrectly() {
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"test"});
ReflectionHelpers.setField(mController, "mBluetoothTetherEnabled", false);
ReflectionHelpers.setField(mController, "mUSBTetherEnabled", false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void switch_shouldListenToUsbAndBluetooth() {
when(mSharedPreferences.getBoolean(
BluetoothTetherPreferenceController.PREF_KEY, false)).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences,
BluetoothTetherPreferenceController.PREF_KEY);
verify(mPreference).setVisible(eq(true));
when(mSharedPreferences.getBoolean(
UsbTetherPreferenceController.PREF_KEY, false)).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences,
UsbTetherPreferenceController.PREF_KEY);
assertThat(mController.shouldShow()).isTrue();
when(mSharedPreferences.getBoolean(
UsbTetherPreferenceController.PREF_KEY, false)).thenReturn(false);
mController.onSharedPreferenceChanged(mSharedPreferences,
UsbTetherPreferenceController.PREF_KEY);
assertThat(mController.shouldShow()).isTrue();
when(mSharedPreferences.getBoolean(
BluetoothTetherPreferenceController.PREF_KEY, false)).thenReturn(false);
when(mSharedPreferences.edit()).thenReturn(mock(SharedPreferences.Editor.class));
when(mPreference.isChecked()).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences,
BluetoothTetherPreferenceController.PREF_KEY);
verify(mPreference).setChecked(eq(false));
verify(mPreference).setVisible(eq(false));
}
}