Merge "Change UI for wireless AP tether band selection"
This commit is contained in:
committed by
Android (Google) Code Review
commit
bef9b58f17
@@ -1,158 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.widget;
|
||||
|
||||
import static com.android.settingslib.CustomDialogPreference.CustomPreferenceDialogFragment;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
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;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class HotspotApBandSelectionPreferenceTest {
|
||||
private HotspotApBandSelectionPreference mPreference;
|
||||
private Context mContext;
|
||||
private Button mSaveButton;
|
||||
private View mLayout;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mSaveButton = spy(new Button(mContext));
|
||||
|
||||
final CustomPreferenceDialogFragment fragment = mock(CustomPreferenceDialogFragment.class);
|
||||
final AlertDialog dialog = mock(AlertDialog.class);
|
||||
when(fragment.getDialog()).thenReturn(dialog);
|
||||
when(dialog.getButton(anyInt())).thenReturn(mSaveButton);
|
||||
|
||||
mPreference = new HotspotApBandSelectionPreference(mContext);
|
||||
ReflectionHelpers.setField(mPreference, "mFragment", fragment);
|
||||
|
||||
final LayoutInflater inflater = LayoutInflater.from(mContext);
|
||||
mLayout = inflater.inflate(R.layout.hotspot_ap_band_selection_dialog,
|
||||
new LinearLayout(mContext), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWifiBand_updatesBandPresetConfigProvided() {
|
||||
mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_ANY);
|
||||
mPreference.onBindDialogView(mLayout);
|
||||
|
||||
// check that the boxes are set correctly when a pre-existing config is set
|
||||
assertThat(mPreference.getWifiBand()).isEqualTo(WifiConfiguration.AP_BAND_ANY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWifiBand_updatesBandWhenBoxesToggled() {
|
||||
mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_ANY);
|
||||
mPreference.onBindDialogView(mLayout);
|
||||
|
||||
assertThat(mPreference.getWifiBand()).isEqualTo(WifiConfiguration.AP_BAND_ANY);
|
||||
|
||||
// make sure we have the expected box then toggle it
|
||||
mPreference.mBox2G.setChecked(false);
|
||||
|
||||
// check that band is updated
|
||||
assertThat(mPreference.getWifiBand()).isEqualTo(WifiConfiguration.AP_BAND_5GHZ);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onSaveInstanceState_skipWhenDialogGone() {
|
||||
mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_2GHZ);
|
||||
mPreference.onBindDialogView(mLayout);
|
||||
// remove the fragment to make the dialog unavailable
|
||||
ReflectionHelpers.setField(mPreference, "mFragment", null);
|
||||
|
||||
mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_ANY);
|
||||
mPreference.onBindDialogView(mLayout);
|
||||
|
||||
// state should only be saved when the dialog is available
|
||||
Parcelable parcelable = mPreference.onSaveInstanceState();
|
||||
mPreference.onRestoreInstanceState(parcelable);
|
||||
assertThat(mPreference.mShouldRestore).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onSaveInstanceState_doesNotCrashWhenViewGone() {
|
||||
mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_2GHZ);
|
||||
mPreference.onBindDialogView(mLayout);
|
||||
// When the device dozes the view and dialog can become null
|
||||
mPreference.mBox5G = null;
|
||||
mPreference.mBox2G = null;
|
||||
ReflectionHelpers.setField(mPreference, "mFragment", null);
|
||||
|
||||
// make sure it does not crash and state is not restored
|
||||
Parcelable parcelable = mPreference.onSaveInstanceState();
|
||||
mPreference.onRestoreInstanceState(parcelable);
|
||||
assertThat(mPreference.mShouldRestore).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onSaveInstanceState_presentWhenDialogPresent() {
|
||||
mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_2GHZ);
|
||||
mPreference.onBindDialogView(mLayout);
|
||||
|
||||
Parcelable parcelable = mPreference.onSaveInstanceState();
|
||||
mPreference.onRestoreInstanceState(parcelable);
|
||||
assertThat(mPreference.mShouldRestore).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void positiveButton_updatedCorrectly() {
|
||||
mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_ANY);
|
||||
mPreference.onBindDialogView(mLayout);
|
||||
|
||||
// button is enabled whole time so far since we have a pre-existing selection
|
||||
verify(mSaveButton, never()).setEnabled(false);
|
||||
|
||||
// clear all boxes and make sure it stays enabled until empty
|
||||
mPreference.mBox2G.setChecked(false);
|
||||
mPreference.mBox5G.setChecked(false);
|
||||
|
||||
// button should be disabled now
|
||||
verify(mSaveButton, times(1)).setEnabled(false);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package com.android.settings.wifi.tether;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -26,16 +27,16 @@ import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiManager;
|
||||
|
||||
import androidx.preference.ListPreference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.widget.HotspotApBandSelectionPreference;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
@@ -43,8 +44,9 @@ import org.robolectric.RuntimeEnvironment;
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class WifiTetherApBandPreferenceControllerTest {
|
||||
|
||||
private static final String ALL_BANDS = "2.4 GHz and 5.0 GHz";
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private static final String ALL_BANDS = "5.0 GHz Band preferred";
|
||||
private static final String TWO_GHZ_STRING = "2.4 GHz Band";
|
||||
private static final String FIVE_GHZ_STRING = "5.0 GHz Band";
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private ConnectivityManager mConnectivityManager;
|
||||
@@ -56,12 +58,13 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
private PreferenceScreen mScreen;
|
||||
|
||||
private WifiTetherApBandPreferenceController mController;
|
||||
private HotspotApBandSelectionPreference mPreference;
|
||||
private ListPreference mPreference;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mPreference = new HotspotApBandSelectionPreference(RuntimeEnvironment.application);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mPreference = new ListPreference(RuntimeEnvironment.application);
|
||||
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
|
||||
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
|
||||
.thenReturn(mConnectivityManager);
|
||||
@@ -71,6 +74,7 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
config.apBand = WifiConfiguration.AP_BAND_ANY;
|
||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(new WifiConfiguration());
|
||||
when(mWifiManager.isDualModeSupported()).thenReturn(false);
|
||||
|
||||
mController = new WifiTetherApBandPreferenceController(mContext, mListener);
|
||||
}
|
||||
@@ -79,9 +83,10 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
public void display_5GhzSupported_shouldDisplayFullList() {
|
||||
when(mWifiManager.getCountryCode()).thenReturn("US");
|
||||
when(mWifiManager.isDualBandSupported()).thenReturn(true);
|
||||
when(mWifiManager.isDualModeSupported()).thenReturn(true);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onPreferenceChange(mPreference, -1);
|
||||
mController.onPreferenceChange(mPreference, "-1");
|
||||
|
||||
assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS);
|
||||
}
|
||||
@@ -110,24 +115,54 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void changePreference_shouldUpdateValue() {
|
||||
public void changePreference_noDualModeWith5G_shouldUpdateValue() {
|
||||
when(mWifiManager.is5GHzBandSupported()).thenReturn(true);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
// -1 is WifiConfiguration.AP_BAND_ANY, for 'Auto' option. This should be prevented from
|
||||
// being set since it is invalid for this configuration
|
||||
mController.onPreferenceChange(mPreference, "-1");
|
||||
assertThat(mController.getBandIndex()).isEqualTo(1);
|
||||
assertThat(mPreference.getSummary()).isEqualTo(FIVE_GHZ_STRING);
|
||||
verify(mListener, times(1)).onTetherConfigUpdated();
|
||||
|
||||
// set to 5 Ghz
|
||||
mController.onPreferenceChange(mPreference, "1");
|
||||
assertThat(mController.getBandIndex()).isEqualTo(1);
|
||||
assertThat(mPreference.getSummary()).isEqualTo(FIVE_GHZ_STRING);
|
||||
verify(mListener, times(2)).onTetherConfigUpdated();
|
||||
|
||||
// set to 2 Ghz
|
||||
mController.onPreferenceChange(mPreference, "0");
|
||||
assertThat(mController.getBandIndex()).isEqualTo(0);
|
||||
assertThat(mPreference.getSummary()).isEqualTo(TWO_GHZ_STRING);
|
||||
verify(mListener, times(3)).onTetherConfigUpdated();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void changePreference_dualModeWith5G_shouldUpdateValue() {
|
||||
when(mWifiManager.is5GHzBandSupported()).thenReturn(true);
|
||||
when(mWifiManager.isDualModeSupported()).thenReturn(true);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
// -1 is WifiConfiguration.AP_BAND_ANY, for 'Auto' option.
|
||||
mController.onPreferenceChange(mPreference, -1);
|
||||
mController.onPreferenceChange(mPreference, "-1");
|
||||
assertThat(mController.getBandIndex()).isEqualTo(-1);
|
||||
assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS);
|
||||
verify(mListener, times(1)).onTetherConfigUpdated();
|
||||
|
||||
mController.onPreferenceChange(mPreference, 1);
|
||||
assertThat(mController.getBandIndex()).isEqualTo(1);
|
||||
assertThat(mPreference.getSummary()).isEqualTo("5.0 GHz");
|
||||
// should revert to the default for 5 Ghz only since this is not supported with this config
|
||||
mController.onPreferenceChange(mPreference, "1");
|
||||
assertThat(mController.getBandIndex()).isEqualTo(-1);
|
||||
assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS);
|
||||
verify(mListener, times(2)).onTetherConfigUpdated();
|
||||
|
||||
mController.onPreferenceChange(mPreference, 0);
|
||||
// set to 2 Ghz
|
||||
mController.onPreferenceChange(mPreference, "0");
|
||||
assertThat(mController.getBandIndex()).isEqualTo(0);
|
||||
assertThat(mPreference.getSummary()).isEqualTo("2.4 GHz");
|
||||
|
||||
assertThat(mPreference.getSummary()).isEqualTo(TWO_GHZ_STRING);
|
||||
verify(mListener, times(3)).onTetherConfigUpdated();
|
||||
}
|
||||
|
||||
@@ -136,7 +171,7 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
// Set controller band index to 1 and verify is set.
|
||||
when(mWifiManager.is5GHzBandSupported()).thenReturn(true);
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onPreferenceChange(mPreference, 1);
|
||||
mController.onPreferenceChange(mPreference, "1");
|
||||
assertThat(mController.getBandIndex()).isEqualTo(1);
|
||||
|
||||
// Disable 5Ghz band
|
||||
|
||||
Reference in New Issue
Block a user