Merge "Update Wifi Tether band selection UI" into pi-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
d9abb0e914
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* 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_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);
|
||||
}
|
||||
}
|
||||
@@ -24,12 +24,13 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.support.v7.preference.ListPreference;
|
||||
import android.support.v7.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;
|
||||
@@ -42,6 +43,7 @@ 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 Context mContext;
|
||||
@Mock
|
||||
@@ -54,18 +56,21 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
private PreferenceScreen mScreen;
|
||||
|
||||
private WifiTetherApBandPreferenceController mController;
|
||||
private ListPreference mListPreference;
|
||||
private HotspotApBandSelectionPreference mPreference;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mListPreference = new ListPreference(RuntimeEnvironment.application);
|
||||
mPreference = new HotspotApBandSelectionPreference(RuntimeEnvironment.application);
|
||||
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
|
||||
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
|
||||
.thenReturn(mConnectivityManager);
|
||||
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
|
||||
when(mContext.getResources()).thenReturn(RuntimeEnvironment.application.getResources());
|
||||
when(mScreen.findPreference(anyString())).thenReturn(mListPreference);
|
||||
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
config.apBand = WifiConfiguration.AP_BAND_ANY;
|
||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(new WifiConfiguration());
|
||||
|
||||
mController = new WifiTetherApBandPreferenceController(mContext, mListener);
|
||||
}
|
||||
@@ -76,8 +81,9 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
when(mWifiManager.isDualBandSupported()).thenReturn(true);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onPreferenceChange(mPreference, -1);
|
||||
|
||||
assertThat(mListPreference.getEntries().length).isEqualTo(3);
|
||||
assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -87,9 +93,8 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
assertThat(mListPreference.getEntries()).isNull();
|
||||
assertThat(mListPreference.isEnabled()).isFalse();
|
||||
assertThat(mListPreference.getSummary())
|
||||
assertThat(mPreference.isEnabled()).isFalse();
|
||||
assertThat(mPreference.getSummary())
|
||||
.isEqualTo(RuntimeEnvironment.application.getString(R.string.wifi_ap_choose_2G));
|
||||
}
|
||||
|
||||
@@ -99,9 +104,8 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
assertThat(mListPreference.getEntries()).isNull();
|
||||
assertThat(mListPreference.isEnabled()).isFalse();
|
||||
assertThat(mListPreference.getSummary())
|
||||
assertThat(mPreference.isEnabled()).isFalse();
|
||||
assertThat(mPreference.getSummary())
|
||||
.isEqualTo(RuntimeEnvironment.application.getString(R.string.wifi_ap_choose_2G));
|
||||
}
|
||||
|
||||
@@ -112,14 +116,17 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
// -1 is WifiConfiguration.AP_BAND_ANY, for 'Auto' option.
|
||||
mController.onPreferenceChange(mListPreference, "-1");
|
||||
mController.onPreferenceChange(mPreference, -1);
|
||||
assertThat(mController.getBandIndex()).isEqualTo(-1);
|
||||
assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS);
|
||||
|
||||
mController.onPreferenceChange(mListPreference, "1");
|
||||
mController.onPreferenceChange(mPreference, 1);
|
||||
assertThat(mController.getBandIndex()).isEqualTo(1);
|
||||
assertThat(mPreference.getSummary()).isEqualTo("5.0 GHz");
|
||||
|
||||
mController.onPreferenceChange(mListPreference, "0");
|
||||
mController.onPreferenceChange(mPreference, 0);
|
||||
assertThat(mController.getBandIndex()).isEqualTo(0);
|
||||
assertThat(mPreference.getSummary()).isEqualTo("2.4 GHz");
|
||||
|
||||
verify(mListener, times(3)).onTetherConfigUpdated();
|
||||
}
|
||||
@@ -129,7 +136,7 @@ public class WifiTetherApBandPreferenceControllerTest {
|
||||
// Set controller band index to 1 and verify is set.
|
||||
when(mWifiManager.is5GHzBandSupported()).thenReturn(true);
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onPreferenceChange(mListPreference, "1");
|
||||
mController.onPreferenceChange(mPreference, 1);
|
||||
assertThat(mController.getBandIndex()).isEqualTo(1);
|
||||
|
||||
// Disable 5Ghz band
|
||||
|
||||
Reference in New Issue
Block a user