Merge "Allow "None" to be selected as a Network rating provider." into oc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
b8eb78b321
@@ -74,14 +74,19 @@ public class NetworkScorerPicker extends InstrumentedPreferenceFragment implemen
|
||||
screen.removeAll();
|
||||
|
||||
final List<NetworkScorerAppData> scorers = mNetworkScoreManager.getAllValidScorers();
|
||||
if (scorers.isEmpty()) {
|
||||
final RadioButtonPreference nonePref = new RadioButtonPreference(getPrefContext());
|
||||
nonePref.setTitle(R.string.network_scorer_picker_none_preference);
|
||||
nonePref.setChecked(true);
|
||||
screen.addPreference(nonePref);
|
||||
return;
|
||||
}
|
||||
final String defaultAppKey = getActiveScorerPackage();
|
||||
|
||||
final RadioButtonPreference nonePref = new RadioButtonPreference(getPrefContext());
|
||||
nonePref.setTitle(R.string.network_scorer_picker_none_preference);
|
||||
if (scorers.isEmpty()) {
|
||||
nonePref.setChecked(true);
|
||||
} else {
|
||||
nonePref.setKey(null);
|
||||
nonePref.setChecked(TextUtils.isEmpty(defaultAppKey));
|
||||
nonePref.setOnClickListener(this);
|
||||
}
|
||||
screen.addPreference(nonePref);
|
||||
|
||||
final int numScorers = scorers.size();
|
||||
for (int i = 0; i < numScorers; i++) {
|
||||
final RadioButtonPreference pref = new RadioButtonPreference(getPrefContext());
|
||||
|
@@ -17,14 +17,16 @@ package com.android.settings.network;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.NetworkScorerAppData;
|
||||
import android.provider.Settings;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link PreferenceController} that shows the active network scorer and toggles the preference
|
||||
* based on {@link Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED}.
|
||||
* based on whether or not there are valid scorers installed.
|
||||
*/
|
||||
public class NetworkScorerPickerPreferenceController extends PreferenceController {
|
||||
|
||||
@@ -45,8 +47,9 @@ public class NetworkScorerPickerPreferenceController extends PreferenceControlle
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
boolean enabled = Settings.Global.getInt(mContext.getContentResolver(),
|
||||
Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1;
|
||||
final List<NetworkScorerAppData> allValidScorers =
|
||||
mNetworkScoreManager.getAllValidScorers();
|
||||
boolean enabled = !allValidScorers.isEmpty();
|
||||
preference.setEnabled(enabled);
|
||||
if (!enabled) {
|
||||
preference.setSummary(null);
|
||||
|
@@ -16,8 +16,8 @@
|
||||
|
||||
package com.android.settings.network;
|
||||
|
||||
import static android.provider.Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -25,11 +25,12 @@ import static org.mockito.Mockito.when;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.net.NetworkScorerAppData;
|
||||
import android.provider.Settings;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
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;
|
||||
@@ -38,6 +39,8 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class NetworkScorerPickerPreferenceControllerTest {
|
||||
@@ -65,11 +68,12 @@ public class NetworkScorerPickerPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void updateState_preferenceSetSummaryAsActiveScorerLabel() {
|
||||
Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1);
|
||||
ComponentName scorer = new ComponentName(TEST_SCORER_PACKAGE, TEST_SCORER_CLASS);
|
||||
NetworkScorerAppData scorerAppData = new NetworkScorerAppData(
|
||||
0, scorer, TEST_SCORER_LABEL, null /* enableUseOpenWifiActivity */,
|
||||
null /* networkAvailableNotificationChannelId */);
|
||||
when(mNetworkScorer.getAllValidScorers())
|
||||
.thenReturn(Collections.singletonList(scorerAppData));
|
||||
when(mNetworkScorer.getActiveScorer()).thenReturn(scorerAppData);
|
||||
Preference preference = mock(Preference.class);
|
||||
|
||||
@@ -79,8 +83,13 @@ public class NetworkScorerPickerPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_noActiveScorer_preferenceSetSummaryToNone() {
|
||||
Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1);
|
||||
public void updateState_scorersAvailable_noActiveScorer_preferenceSetSummaryToNone() {
|
||||
ComponentName scorer = new ComponentName(TEST_SCORER_PACKAGE, TEST_SCORER_CLASS);
|
||||
NetworkScorerAppData scorerAppData = new NetworkScorerAppData(
|
||||
0, scorer, TEST_SCORER_LABEL, null /* enableUseOpenWifiActivity */,
|
||||
null /* networkAvailableNotificationChannelId */);
|
||||
when(mNetworkScorer.getAllValidScorers())
|
||||
.thenReturn(Collections.singletonList(scorerAppData));
|
||||
when(mNetworkScorer.getActiveScorer()).thenReturn(null);
|
||||
Preference preference = mock(Preference.class);
|
||||
|
||||
@@ -91,9 +100,25 @@ public class NetworkScorerPickerPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_networkRecommendationsDisabled_preferenceDisabled() {
|
||||
Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 0);
|
||||
when(mNetworkScorer.getActiveScorer()).thenReturn(null);
|
||||
public void updateState_scorersAvailable_preferenceEnabled() {
|
||||
ComponentName scorer = new ComponentName(TEST_SCORER_PACKAGE, TEST_SCORER_CLASS);
|
||||
NetworkScorerAppData scorerAppData = new NetworkScorerAppData(
|
||||
0, scorer, TEST_SCORER_LABEL, null /* enableUseOpenWifiActivity */,
|
||||
null /* networkAvailableNotificationChannelId */);
|
||||
when(mNetworkScorer.getAllValidScorers())
|
||||
.thenReturn(Collections.singletonList(scorerAppData));
|
||||
|
||||
Preference preference = mock(Preference.class);
|
||||
|
||||
mController.updateState(preference);
|
||||
|
||||
verify(preference).setEnabled(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_noScorersAvailable_preferenceDisabled() {
|
||||
when(mNetworkScorer.getAllValidScorers())
|
||||
.thenReturn(Collections.emptyList());
|
||||
Preference preference = mock(Preference.class);
|
||||
|
||||
mController.updateState(preference);
|
||||
|
@@ -20,6 +20,7 @@ import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -112,6 +113,33 @@ public class NetworkScorerPickerTest {
|
||||
verify(mPreferenceScreen).addPreference(arg.capture());
|
||||
assertThat(arg.getValue().getTitle()).isEqualTo(
|
||||
mContext.getString(R.string.network_scorer_picker_none_preference));
|
||||
assertThat(arg.getValue().isChecked()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateCandidates_validScorers_noActiveScorer() {
|
||||
ComponentName scorer = new ComponentName(TEST_SCORER_PACKAGE_1, TEST_SCORER_CLASS_1);
|
||||
NetworkScorerAppData scorerAppData = new NetworkScorerAppData(
|
||||
0, scorer, TEST_SCORER_LABEL_1, null /* enableUseOpenWifiActivity */,
|
||||
null /* networkAvailableNotificationChannelId */);
|
||||
when(mNetworkScoreManager.getAllValidScorers()).thenReturn(
|
||||
Lists.newArrayList(scorerAppData));
|
||||
when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(null);
|
||||
|
||||
ArgumentCaptor<RadioButtonPreference> arg =
|
||||
ArgumentCaptor.forClass(RadioButtonPreference.class);
|
||||
|
||||
mFragment.updateCandidates();
|
||||
|
||||
verify(mPreferenceScreen, times(2)).addPreference(arg.capture());
|
||||
|
||||
final RadioButtonPreference nonePref = arg.getAllValues().get(0);
|
||||
assertThat(nonePref.getKey()).isNull();
|
||||
assertThat(nonePref.isChecked()).isTrue();
|
||||
|
||||
final RadioButtonPreference testScorerPref = arg.getAllValues().get(1);
|
||||
assertThat(testScorerPref.getTitle()).isEqualTo(TEST_SCORER_LABEL_1);
|
||||
assertThat(testScorerPref.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -128,7 +156,10 @@ public class NetworkScorerPickerTest {
|
||||
|
||||
mFragment.updateCandidates();
|
||||
|
||||
verify(mPreferenceScreen).addPreference(arg.capture());
|
||||
// The first preference added is the "none" preference and the second is the
|
||||
// pref for the test scorer.
|
||||
verify(mPreferenceScreen, times(2)).addPreference(arg.capture());
|
||||
// Returns the last captured value which is expected to be the test scorer pref.
|
||||
RadioButtonPreference pref = arg.getValue();
|
||||
assertThat(pref.getTitle()).isEqualTo(TEST_SCORER_LABEL_1);
|
||||
assertThat(pref.isChecked()).isTrue();
|
||||
|
Reference in New Issue
Block a user