Update Network & internet->Wi-Fi to use MasterSwitchPreference.

- Add a preference controller for Network & internet->Wi-Fi to control
  the preference toggling and summary update.
- Refactor WifiSettings and WifiEnabler to share code between the new
  wifi preference controller and the wifi setting.
- Refactor BluetoothSummaryHelper to have a common base class with the
  WifiSummaryHelper.
- Rename the summary helper to summary updater.

Bug: 34280769
Test: make RunSettingsRoboTests
Change-Id: I00ebfc161bcef89331bb41ba405ed8cb8232d248
This commit is contained in:
Doris Ling
2017-01-23 16:16:06 -08:00
parent 42c61c10cc
commit c4c9f4d50e
24 changed files with 926 additions and 171 deletions

View File

@@ -63,7 +63,7 @@ public class BluetoothSettingsSummaryProviderTest {
mSummaryProvider.setListening(true);
verify(mBluetoothManager.getEventManager()).registerCallback(
mSummaryProvider.mSummaryHelper);
mSummaryProvider.mSummaryUpdater);
}
@Test
@@ -71,7 +71,7 @@ public class BluetoothSettingsSummaryProviderTest {
mSummaryProvider.setListening(false);
verify(mBluetoothManager.getEventManager()).unregisterCallback(
mSummaryProvider.mSummaryHelper);
mSummaryProvider.mSummaryUpdater);
}
@Test

View File

@@ -22,6 +22,7 @@ import android.content.Context;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
@@ -45,7 +46,7 @@ import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BluetoothSummaryHelperTest {
public class BluetoothSummaryUpdaterTest {
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -53,7 +54,7 @@ public class BluetoothSummaryHelperTest {
@Mock
private LocalBluetoothAdapter mBtAdapter;
private BluetoothSummaryHelper mHelper;
private BluetoothSummaryUpdater mSummaryUpdater;
@Mock
private SummaryListener mListener;
@@ -64,43 +65,42 @@ public class BluetoothSummaryHelperTest {
when(mBtAdapter.isEnabled()).thenReturn(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTED);
mContext = RuntimeEnvironment.application.getApplicationContext();
mHelper = new BluetoothSummaryHelper(mContext, mBluetoothManager);
mHelper.setOnSummaryChangeListener(mListener);
mSummaryUpdater = new BluetoothSummaryUpdater(mContext, mListener, mBluetoothManager);
}
@Test
public void setListening_shouldRegisterListener() {
mHelper.setListening(true);
public void register_true_shouldRegisterListener() {
mSummaryUpdater.register(true);
verify(mBluetoothManager.getEventManager()).registerCallback(mHelper);
verify(mBluetoothManager.getEventManager()).registerCallback(mSummaryUpdater);
}
@Test
public void setNotListening_shouldUnregisterListener() {
mHelper.setListening(false);
public void register_false_shouldUnregisterListener() {
mSummaryUpdater.register(false);
verify(mBluetoothManager.getEventManager()).unregisterCallback(mHelper);
verify(mBluetoothManager.getEventManager()).unregisterCallback(mSummaryUpdater);
}
@Test
public void setListening_shouldSendSummaryChange() {
mHelper.setListening(true);
public void register_true_shouldSendSummaryChange() {
mSummaryUpdater.register(true);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_connected));
}
@Test
public void onBluetoothStateChanged_btDisabled_shouldSendDisabledSummary() {
mHelper.setListening(true);
mHelper.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
mSummaryUpdater.register(true);
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disabled));
}
@Test
public void onBluetoothStateChanged_btEnabled_connected_shouldSendConnectedSummary() {
mHelper.setListening(true);
mHelper.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
mSummaryUpdater.register(true);
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_connected));
}
@@ -108,8 +108,8 @@ public class BluetoothSummaryHelperTest {
@Test
public void onBluetoothStateChanged_btEnabled_notConnected_shouldSendDisconnectedMessage() {
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED);
mHelper.setListening(true);
mHelper.onBluetoothStateChanged(BluetoothAdapter.STATE_TURNING_ON);
mSummaryUpdater.register(true);
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_TURNING_ON);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disconnected));
}
@@ -122,41 +122,45 @@ public class BluetoothSummaryHelperTest {
when(mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy())
.thenReturn(devices);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED);
mHelper.setListening(true);
mSummaryUpdater.register(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTED);
mHelper.onConnectionStateChanged(null /* device */, BluetoothAdapter.STATE_CONNECTED);
mSummaryUpdater.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_CONNECTED);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_connected));
}
@Test
public void onConnectionStateChanged_inconsistentState_shouldSendDisconnectedMessage() {
mHelper.setListening(true);
mHelper.onConnectionStateChanged(null /* device */, BluetoothAdapter.STATE_CONNECTED);
mSummaryUpdater.register(true);
mSummaryUpdater.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_CONNECTED);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disconnected));
}
@Test
public void onConnectionStateChanged_connecting_shouldSendConnectingMessage() {
mHelper.setListening(true);
mSummaryUpdater.register(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTING);
mHelper.onConnectionStateChanged(null /* device */, BluetoothAdapter.STATE_CONNECTING);
mSummaryUpdater.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_CONNECTING);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_connecting));
}
@Test
public void onConnectionStateChanged_disconnecting_shouldSendDisconnectingMessage() {
mHelper.setListening(true);
mSummaryUpdater.register(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTING);
mHelper.onConnectionStateChanged(null /* device */, BluetoothAdapter.STATE_DISCONNECTING);
mSummaryUpdater.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_DISCONNECTING);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disconnecting));
}
private class SummaryListener implements BluetoothSummaryHelper.OnSummaryChangeListener {
private class SummaryListener implements OnSummaryChangeListener {
String summary;
@Override

View File

@@ -27,6 +27,7 @@ import android.widget.Switch;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import org.junit.Before;
import org.junit.Test;
@@ -118,4 +119,30 @@ public class MasterSwitchPreferenceTest {
toggle.setChecked(false);
verify(listener).onPreferenceChange(preference, false);
}
@Test
public void setDisabledByAdmin_hasEnforcedAdmin_shouldDisableButton() {
final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
final PreferenceViewHolder holder = new PreferenceViewHolder(
LayoutInflater.from(mContext).inflate(R.layout.preference_widget_master_switch, null));
final Switch toggle = (Switch) holder.itemView.findViewById(R.id.switchWidget);
toggle.setEnabled(true);
preference.onBindViewHolder(holder);
preference.setDisabledByAdmin(mock(EnforcedAdmin.class));
assertThat(toggle.isEnabled()).isFalse();
}
@Test
public void setDisabledByAdmin_noEnforcedAdmin_shouldEnaableButton() {
final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
final PreferenceViewHolder holder = new PreferenceViewHolder(
LayoutInflater.from(mContext).inflate(R.layout.preference_widget_master_switch, null));
final Switch toggle = (Switch) holder.itemView.findViewById(R.id.switchWidget);
toggle.setEnabled(false);
preference.onBindViewHolder(holder);
preference.setDisabledByAdmin(null);
assertThat(toggle.isEnabled()).isTrue();
}
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright (C) 2017 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 android.content.Context;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
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;
import org.robolectric.annotation.Config;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SummaryUpdaterTest {
private Context mContext;
private SummaryUpdaterTestable mSummaryUpdater;
@Mock
private SummaryListener mListener;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application.getApplicationContext();
mSummaryUpdater = new SummaryUpdaterTestable(mContext, mListener);
}
@Test
public void notifyChangeIfNeeded_fistTimeInit_shouldNotifyChange() {
final String summary = "initialized";
mSummaryUpdater.setTestSummary(summary);
mSummaryUpdater.notifyChangeIfNeeded();
verify(mListener).onSummaryChanged(summary);
}
@Test
public void notifyChangeIfNeeded_summaryUpdated_shouldNotifyChange() {
final String summaryInit = "initialized";
mSummaryUpdater.setTestSummary(summaryInit);
mSummaryUpdater.notifyChangeIfNeeded();
final String summaryUpdate = "updated";
mSummaryUpdater.setTestSummary(summaryUpdate);
mSummaryUpdater.notifyChangeIfNeeded();
verify(mListener).onSummaryChanged(summaryUpdate);
}
@Test
public void notifyChangeIfNeeded_summaryNotUpdated_shouldOnlyNotifyChangeOnce() {
final String summaryInit = "initialized";
mSummaryUpdater.setTestSummary(summaryInit);
mSummaryUpdater.notifyChangeIfNeeded();
final String summaryUpdate = "updated";
mSummaryUpdater.setTestSummary(summaryUpdate);
mSummaryUpdater.notifyChangeIfNeeded();
mSummaryUpdater.notifyChangeIfNeeded();
verify(mListener, times(1)).onSummaryChanged(summaryUpdate);
}
private class SummaryListener implements SummaryUpdater.OnSummaryChangeListener {
String summary;
@Override
public void onSummaryChanged(String summary) {
this.summary = summary;
}
}
public final class SummaryUpdaterTestable extends SummaryUpdater {
private String mTestSummary;
SummaryUpdaterTestable(Context context, SummaryUpdater.OnSummaryChangeListener listener) {
super(context, listener);
}
@Override
public void register(boolean register) {
}
@Override
public void notifyChangeIfNeeded() {
super.notifyChangeIfNeeded();
}
@Override
public String getSummary() {
return mTestSummary;
}
public void setTestSummary(String summary) {
mTestSummary = summary;
}
}
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright (C) 2017 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.wifi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.Preference.OnPreferenceChangeListener;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.widget.MasterSwitchPreference;
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;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class WifiMasterSwitchPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
Context mMockContext;
@Mock
private WifiManager mWifiManager;
@Mock
private PreferenceScreen mScreen;
@Mock
private MasterSwitchPreference mPreference;
private Context mContext;
private WifiMasterSwitchPreferenceController mController;
private FakeFeatureFactory mFakeFeatureFactory;
private MetricsFeatureProvider mMetricsFeatureProvider;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mMockContext);
mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mMockContext);
mMetricsFeatureProvider = mFakeFeatureFactory.getMetricsFeatureProvider();
mContext = spy(RuntimeEnvironment.application.getApplicationContext());
mController = new WifiMasterSwitchPreferenceController(mContext, mMetricsFeatureProvider);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mWifiManager.getWifiState()).thenReturn(WifiManager.WIFI_STATE_DISABLED);
}
@Test
public void isAvailable_shouldAlwaysReturnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void onResume_shouldRegisterCallback() {
mController.onResume();
verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
}
@Test
public void onPause_shouldUnregisterCallback() {
mController.onResume();
mController.onPause();
verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
}
@Test
public void onStart_shouldRegisterPreferenceChangeListener() {
mController.displayPreference(mScreen);
mController.onStart();
verify(mPreference).setOnPreferenceChangeListener(any(OnPreferenceChangeListener.class));
}
@Test
public void onStop_shouldRegisterPreferenceChangeListener() {
mController.displayPreference(mScreen);
mController.onStart();
mController.onStop();
verify(mPreference).setOnPreferenceChangeListener(null);
}
@Test
public void onSummaryChanged_shouldUpdatePreferenceSummary() {
mController.displayPreference(mScreen);
mController.onSummaryChanged("test summary");
verify(mPreference).setSummary("test summary");
}
}

View File

@@ -0,0 +1,136 @@
/*
* Copyright (C) 2017 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.wifi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
import com.android.settingslib.wifi.WifiStatusTracker;
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;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class WifiSummaryUpdaterTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private WifiManager mWifiManager;
@Mock
private SummaryListener mListener;
private Context mContext;
private WifiSummaryUpdater mSummaryUpdater;
private WifiStatusTracker mWifiTracker;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mWifiTracker = new WifiStatusTracker(mWifiManager);
mContext = spy(RuntimeEnvironment.application.getApplicationContext());
mSummaryUpdater = new WifiSummaryUpdater(mContext, mListener, mWifiTracker);
}
@Test
public void register_true_shouldRegisterListener() {
mSummaryUpdater.register(true);
verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
}
@Test
public void register_false_shouldUnregisterListener() {
mSummaryUpdater.register(true);
mSummaryUpdater.register(false);
verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
}
@Test
public void onReceive_networkStateChanged_shouldSendSummaryChange() {
mSummaryUpdater.register(true);
mContext.sendBroadcast(new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION));
verify(mListener).onSummaryChanged(anyString());
}
@Test
public void onReceive_rssiChanged_shouldSendSummaryChange() {
mSummaryUpdater.register(true);
mContext.sendBroadcast(new Intent(WifiManager.RSSI_CHANGED_ACTION));
verify(mListener).onSummaryChanged(anyString());
}
@Test
public void getSummary_wifiDisabled_shouldReturnDisabled() {
mWifiTracker.enabled = false;
assertThat(mSummaryUpdater.getSummary()).isEqualTo(
mContext.getString(R.string.wifi_disabled_generic));
}
@Test
public void getSummary_wifiDisconnected_shouldReturnDisconnected() {
mWifiTracker.enabled = true;
mWifiTracker.connected = false;
assertThat(mSummaryUpdater.getSummary()).isEqualTo(
mContext.getString(R.string.disconnected));
}
@Test
public void getSummary_wifiConnected_shouldReturnSsid() {
mWifiTracker.enabled = true;
mWifiTracker.connected = true;
mWifiTracker.ssid = "Test Ssid";
assertThat(mSummaryUpdater.getSummary()).isEqualTo("Test Ssid");
}
private class SummaryListener implements OnSummaryChangeListener {
String summary;
@Override
public void onSummaryChanged(String summary) {
this.summary = summary;
}
}
}