Refactor NotifyOpenNetworksPreferenceControllerTest into TogglePreferenceController

NotifyOpenNetworksPreferenceControllerTest is essentially reimplementing TogglePreferenceController.

Add below 2 changes for test failure of CodeInspectionTest

1. Remove some arguments from constructor of the controller
2. Declare the controller in xml instead of in code

Bug: 132391311
Test: manual
      NotifyOpenNetworksPreferenceControllerTestTest

Change-Id: Icda870ef0b90aacbacfe588b23d1b28d2b60941c
This commit is contained in:
Arc Wang
2019-05-16 15:22:39 +08:00
parent 912f3558df
commit 535e72a45b
4 changed files with 36 additions and 61 deletions

View File

@@ -39,7 +39,8 @@
android:title="@string/wifi_notify_open_networks"
android:icon="@drawable/ic_open_wifi_notifications"
android:summary="@string/wifi_notify_open_networks_summary"
settings:keywords="@string/keywords_wifi_notify_open_networks"/>
settings:keywords="@string/keywords_wifi_notify_open_networks"
settings:controller="com.android.settings.wifi.NotifyOpenNetworksPreferenceController"/>
<SwitchPreference
android:key="wifi_cellular_data_fallback"

View File

@@ -76,8 +76,6 @@ public class ConfigureWifiSettings extends DashboardFragment {
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new NotifyOpenNetworksPreferenceController(context,
getSettingsLifecycle()));
controllers.add(new WifiInfoPreferenceController(context, getSettingsLifecycle(),
wifiManager));
controllers.add(new WifiP2pPreferenceController(context, getSettingsLifecycle(),

View File

@@ -22,38 +22,34 @@ import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;
/**
* {@link AbstractPreferenceController} that controls whether we should notify user when open
* {@link TogglePreferenceController} that controls whether we should notify user when open
* network is available.
*/
public class NotifyOpenNetworksPreferenceController extends AbstractPreferenceController
public class NotifyOpenNetworksPreferenceController extends TogglePreferenceController
implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause {
private static final String KEY_NOTIFY_OPEN_NETWORKS = "notify_open_networks";
private SettingObserver mSettingObserver;
public NotifyOpenNetworksPreferenceController(Context context, Lifecycle lifecycle) {
super(context);
lifecycle.addObserver(this);
public NotifyOpenNetworksPreferenceController(Context context) {
super(context, KEY_NOTIFY_OPEN_NETWORKS);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mSettingObserver = new SettingObserver(screen.findPreference(KEY_NOTIFY_OPEN_NETWORKS));
mSettingObserver = new SettingObserver(screen.findPreference(getPreferenceKey()));
}
@Override
@@ -71,39 +67,24 @@ public class NotifyOpenNetworksPreferenceController extends AbstractPreferenceCo
}
@Override
public boolean isAvailable() {
return true;
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (!TextUtils.equals(preference.getKey(), KEY_NOTIFY_OPEN_NETWORKS)) {
return false;
}
if (!(preference instanceof SwitchPreference)) {
return false;
public boolean isChecked() {
return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1;
}
@Override
public boolean setChecked(boolean isChecked) {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
((SwitchPreference) preference).isChecked() ? 1 : 0);
isChecked ? 1 : 0);
return true;
}
@Override
public String getPreferenceKey() {
return KEY_NOTIFY_OPEN_NETWORKS;
}
@Override
public void updateState(Preference preference) {
if (!(preference instanceof SwitchPreference)) {
return;
}
final SwitchPreference notifyOpenNetworks = (SwitchPreference) preference;
notifyOpenNetworks.setChecked(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);
}
class SettingObserver extends ContentObserver {
private final Uri NETWORKS_AVAILABLE_URI = Settings.Global.getUriFor(
Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON);

View File

@@ -29,8 +29,6 @@ import android.provider.Settings;
import androidx.preference.Preference;
import androidx.preference.SwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -39,7 +37,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class NotifyOpenNetworkPreferenceControllerTest {
public class NotifyOpenNetworksPreferenceControllerTest {
private Context mContext;
private NotifyOpenNetworksPreferenceController mController;
@@ -48,7 +46,7 @@ public class NotifyOpenNetworkPreferenceControllerTest {
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new NotifyOpenNetworksPreferenceController(mContext, mock(Lifecycle.class));
mController = new NotifyOpenNetworksPreferenceController(mContext);
}
@Test
@@ -57,32 +55,29 @@ public class NotifyOpenNetworkPreferenceControllerTest {
}
@Test
public void handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing() {
final SwitchPreference pref = new SwitchPreference(mContext);
public void setChecked_withTrue_shouldUpdateSetting() {
Settings.Global.putInt(mContext.getContentResolver(),
WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0);
assertThat(mController.handlePreferenceTreeClick(pref)).isFalse();
}
mController.setChecked(true);
@Test
public void handlePreferenceTreeClick_nonMatchingType_shouldDoNothing() {
final Preference pref = new Preference(mContext);
pref.setKey(mController.getPreferenceKey());
assertThat(mController.handlePreferenceTreeClick(pref)).isFalse();
}
@Test
public void handlePreferenceTreeClick_matchingKeyAndType_shouldUpdateSetting() {
final SwitchPreference pref = new SwitchPreference(mContext);
pref.setChecked(true);
pref.setKey(mController.getPreferenceKey());
assertThat(mController.handlePreferenceTreeClick(pref)).isTrue();
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0))
.isEqualTo(1);
}
@Test
public void setChecked_withFalse_shouldUpdateSetting() {
Settings.Global.putInt(mContext.getContentResolver(),
WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1);
mController.setChecked(false);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0))
.isEqualTo(0);
}
@Test
public void updateState_preferenceSetCheckedWhenSettingsAreEnabled() {
final SwitchPreference preference = mock(SwitchPreference.class);