Merge changes from topic "refine ConfigureWifiSettings"

* changes:
  Refactor NotifyOpenNetworksPreferenceControllerTest into TogglePreferenceController
  Refactor UseOpenWifiPreferenceController into TogglePreferenceController
  Refactor WifiWakeupPreferenceController into TogglePreferenceController
This commit is contained in:
Arc Wang
2019-05-25 14:04:35 +00:00
committed by Android (Google) Code Review
8 changed files with 150 additions and 246 deletions

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);

View File

@@ -43,7 +43,6 @@ import androidx.preference.Preference;
import androidx.preference.SwitchPreference;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.google.common.collect.Lists;
@@ -76,8 +75,6 @@ public class UseOpenWifiPreferenceControllerTest {
sAppDataNoActivity = new NetworkScorerAppData(0, null, null, null, null);
}
@Mock
private Lifecycle mLifecycle;
@Mock
private Fragment mFragment;
@Mock
@@ -97,7 +94,8 @@ public class UseOpenWifiPreferenceControllerTest {
}
private void createController() {
mController = new UseOpenWifiPreferenceController(mContext, mFragment, mLifecycle);
mController = new UseOpenWifiPreferenceController(mContext);
mController.setFragment(mFragment);
}
/**
@@ -146,52 +144,28 @@ public class UseOpenWifiPreferenceControllerTest {
}
@Test
public void onPreferenceChange_nonMatchingKey_shouldDoNothing() {
createController();
final SwitchPreference pref = new SwitchPreference(mContext);
assertThat(mController.onPreferenceChange(pref, null)).isFalse();
}
@Test
public void onPreferenceChange_notAvailable_shouldDoNothing() {
createController();
final Preference pref = new Preference(mContext);
pref.setKey(mController.getPreferenceKey());
assertThat(mController.onPreferenceChange(pref, null)).isFalse();
}
@Test
public void onPreferenceChange_matchingKeyAndAvailable_enableShouldStartEnableActivity() {
public void setChecked_withTrue_enableShouldStartEnableActivity() {
setupScorers(Lists.newArrayList(sAppData, sAppDataNoActivity));
createController();
final SwitchPreference pref = new SwitchPreference(mContext);
pref.setKey(mController.getPreferenceKey());
mController.setChecked(true);
assertThat(mController.onPreferenceChange(pref, null)).isFalse();
verify(mFragment).startActivityForResult(mIntentCaptor.capture(),
eq(REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY));
Intent activityIntent = mIntentCaptor.getValue();
final Intent activityIntent = mIntentCaptor.getValue();
assertThat(activityIntent.getComponent()).isEqualTo(sEnableActivityComponent);
assertThat(activityIntent.getAction()).isEqualTo(NetworkScoreManager.ACTION_CUSTOM_ENABLE);
}
@Test
public void onPreferenceChange_matchingKeyAndAvailable_disableShouldUpdateSetting() {
public void setChecked_withFalse_disableShouldUpdateSetting() {
setupScorers(Lists.newArrayList(sAppData, sAppDataNoActivity));
Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
sEnableActivityComponent.getPackageName());
createController();
final SwitchPreference pref = new SwitchPreference(mContext);
pref.setKey(mController.getPreferenceKey());
mController.setChecked(false);
assertThat(mController.onPreferenceChange(pref, null)).isTrue();
assertThat(Settings.Global.getString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE))
.isEqualTo("");
}

View File

@@ -35,7 +35,6 @@ import androidx.preference.SwitchPreference;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -55,14 +54,13 @@ public class WifiWakeupPreferenceControllerTest {
private LocationManager mLocationManager;
@Mock
private SwitchPreference mPreference;
@Mock
private Lifecycle mLifecycle;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new WifiWakeupPreferenceController(mContext, mFragment, mLifecycle);
mController = new WifiWakeupPreferenceController(mContext);
mController.setFragment(mFragment);
mController.mLocationManager = mLocationManager;
mController.mPreference = mPreference;
@@ -71,42 +69,29 @@ public class WifiWakeupPreferenceControllerTest {
}
@Test
public void handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing() {
final SwitchPreference pref = new SwitchPreference(mContext);
public void setChecked_scanEnableLocationEnable_wifiWakeupEnable() {
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
doReturn(true).when(mLocationManager).isLocationEnabled();
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_WAKEUP_ENABLED, 0))
.isEqualTo(1);
}
@Test
public void handlePreferenceTreeClick_wifiWakeupEnableScanningDisable_wifiWakeupEnable() {
public void updateState_wifiWakeupEnableScanningDisable_wifiWakeupDisabled() {
final SwitchPreference preference = new SwitchPreference(mContext);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
doReturn(true).when(mLocationManager).isLocationEnabled();
mController.handlePreferenceTreeClick(mPreference);
final boolean isWifiWakeupEnabled = Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1;
mController.updateState(preference);
assertThat(isWifiWakeupEnabled).isTrue();
assertThat(preference.isChecked()).isFalse();
assertThat(preference.getSummary())
.isEqualTo(mContext.getString(R.string.wifi_wakeup_summary));
}
@Test
@@ -114,6 +99,7 @@ public class WifiWakeupPreferenceControllerTest {
final SwitchPreference preference = new SwitchPreference(mContext);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
doReturn(true).when(mLocationManager).isLocationEnabled();
mController.updateState(preference);