Merge "Adjust UX for toggles when active scorer is not set."
This commit is contained in:
committed by
Android (Google) Code Review
commit
ff45106b49
@@ -19,12 +19,12 @@ package com.android.settings.wifi;
|
||||
import static android.provider.Settings.Global.USE_OPEN_WIFI_PACKAGE;
|
||||
import static com.android.settings.wifi.UseOpenWifiPreferenceController.REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.app.Activity;
|
||||
import android.app.Fragment;
|
||||
import android.content.ComponentName;
|
||||
@@ -36,11 +36,13 @@ import android.provider.Settings;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.network.NetworkScoreManagerWrapper;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -51,10 +53,14 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class UseOpenWifiPreferenceControllerTest {
|
||||
private static ComponentName ENABLE_ACTIVITY_COMPONENT = new ComponentName("package", "activityClass");
|
||||
private static ComponentName ENABLE_ACTIVITY_COMPONENT =
|
||||
new ComponentName("package", "activityClass");
|
||||
private static NetworkScorerAppData APP_DATA =
|
||||
new NetworkScorerAppData(0, null, null, ENABLE_ACTIVITY_COMPONENT, null);
|
||||
private static NetworkScorerAppData APP_DATA_NO_ACTIVITY =
|
||||
@@ -79,28 +85,45 @@ public class UseOpenWifiPreferenceControllerTest {
|
||||
mContext, mFragment, mNetworkScoreManagerWrapper, mLifecycle);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_noScorer() {
|
||||
when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(null);
|
||||
/**
|
||||
* Sets the scorers.
|
||||
* @param scorers list of scorers returned by {@link NetworkScoreManager#getAllValidScorers()}.
|
||||
* First scorer in the list is the active scorer.
|
||||
*/
|
||||
private void setupScorers(@NonNull List<NetworkScorerAppData> scorers) {
|
||||
when(mNetworkScoreManagerWrapper.getActiveScorerPackage()).thenReturn(
|
||||
ENABLE_ACTIVITY_COMPONENT.getPackageName());
|
||||
when(mNetworkScoreManagerWrapper.getAllValidScorers()).thenReturn(scorers);
|
||||
when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(scorers.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_returnsFalseWhenNoScorerSet() {
|
||||
createController();
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_noEnableActivity() {
|
||||
public void testIsAvailable_returnsFalseWhenScorersNotSupported() {
|
||||
setupScorers(Lists.newArrayList(APP_DATA_NO_ACTIVITY));
|
||||
createController();
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_returnsTrueIfActiveScorerSupported() {
|
||||
setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
|
||||
createController();
|
||||
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_returnsTrueIfNonActiveScorerSupported() {
|
||||
setupScorers(Lists.newArrayList(APP_DATA_NO_ACTIVITY, APP_DATA));
|
||||
when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA_NO_ACTIVITY);
|
||||
|
||||
createController();
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_enableActivityExists() {
|
||||
when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
|
||||
|
||||
createController();
|
||||
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
@@ -117,8 +140,6 @@ public class UseOpenWifiPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onPreferenceChange_notAvailable_shouldDoNothing() {
|
||||
when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA_NO_ACTIVITY);
|
||||
|
||||
createController();
|
||||
|
||||
final Preference pref = new Preference(mContext);
|
||||
@@ -129,7 +150,7 @@ public class UseOpenWifiPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onPreferenceChange_matchingKeyAndAvailable_enableShouldStartEnableActivity() {
|
||||
when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
|
||||
setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
|
||||
createController();
|
||||
|
||||
final SwitchPreference pref = new SwitchPreference(mContext);
|
||||
@@ -145,7 +166,7 @@ public class UseOpenWifiPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onPreferenceChange_matchingKeyAndAvailable_disableShouldUpdateSetting() {
|
||||
when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
|
||||
setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
|
||||
Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
|
||||
ENABLE_ACTIVITY_COMPONENT.getPackageName());
|
||||
|
||||
@@ -161,6 +182,7 @@ public class UseOpenWifiPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onActivityResult_nonmatchingRequestCode_shouldDoNothing() {
|
||||
setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
|
||||
createController();
|
||||
|
||||
assertThat(mController.onActivityResult(234 /* requestCode */ , Activity.RESULT_OK))
|
||||
@@ -171,6 +193,7 @@ public class UseOpenWifiPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onActivityResult_matchingRequestCode_nonOkResult_shouldDoNothing() {
|
||||
setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
|
||||
createController();
|
||||
|
||||
assertThat(mController
|
||||
@@ -182,7 +205,7 @@ public class UseOpenWifiPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onActivityResult_matchingRequestCode_okResult_updatesSetting() {
|
||||
when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
|
||||
setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
|
||||
createController();
|
||||
|
||||
assertThat(mController
|
||||
@@ -193,8 +216,8 @@ public class UseOpenWifiPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_preferenceSetCheckedAndSetVisibleWhenSettingsAreEnabled() {
|
||||
when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
|
||||
public void updateState_noEnableActivity_preferenceDisabled_summaryChanged() {
|
||||
setupScorers(Lists.newArrayList(APP_DATA_NO_ACTIVITY));
|
||||
createController();
|
||||
|
||||
final SwitchPreference preference = mock(SwitchPreference.class);
|
||||
@@ -203,19 +226,39 @@ public class UseOpenWifiPreferenceControllerTest {
|
||||
|
||||
mController.updateState(preference);
|
||||
|
||||
verify(preference).setVisible(true);
|
||||
verify(preference).setChecked(true);
|
||||
verify(preference).setChecked(false);
|
||||
verify(preference).setSummary(
|
||||
R.string.use_open_wifi_automatically_summary_scorer_unsupported_disabled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_preferenceSetCheckedAndSetVisibleWhenSettingsAreDisabled() {
|
||||
final SwitchPreference preference = mock(SwitchPreference.class);
|
||||
Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE, "");
|
||||
public void updateState_noScorer_preferenceDisabled_summaryChanged() {
|
||||
when(mNetworkScoreManagerWrapper.getAllValidScorers()).thenReturn(new ArrayList<>());
|
||||
createController();
|
||||
|
||||
final SwitchPreference preference = mock(SwitchPreference.class);
|
||||
Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
|
||||
ENABLE_ACTIVITY_COMPONENT.getPackageName());
|
||||
|
||||
mController.updateState(preference);
|
||||
|
||||
verify(preference).setVisible(false);
|
||||
verify(preference).setChecked(false);
|
||||
verify(preference).setSummary(
|
||||
R.string.use_open_wifi_automatically_summary_scoring_disabled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_enableActivityExists_preferenceEnabled() {
|
||||
setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
|
||||
createController();
|
||||
|
||||
final SwitchPreference preference = mock(SwitchPreference.class);
|
||||
Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
|
||||
ENABLE_ACTIVITY_COMPONENT.getPackageName());
|
||||
|
||||
mController.updateState(preference);
|
||||
|
||||
verify(preference).setChecked(true);
|
||||
verify(preference).setSummary(R.string.use_open_wifi_automatically_summary);
|
||||
}
|
||||
}
|
||||
|
@@ -23,6 +23,7 @@ import static android.provider.Settings.Global.WIFI_WAKEUP_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;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
@@ -30,6 +31,7 @@ import android.support.v14.preference.SwitchPreference;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.network.NetworkScoreManagerWrapper;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||
@@ -39,6 +41,7 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
@@ -50,17 +53,23 @@ import org.robolectric.annotation.Config;
|
||||
shadows = { SettingsShadowResources.class })
|
||||
public class WifiWakeupPreferenceControllerTest {
|
||||
|
||||
private static final String TEST_SCORER_PACKAGE_NAME = "Test Scorer";
|
||||
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private NetworkScoreManagerWrapper mNetworkScorer;
|
||||
private WifiWakeupPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mController = new WifiWakeupPreferenceController(mContext, mock(Lifecycle.class));
|
||||
mController = new WifiWakeupPreferenceController(
|
||||
mContext, mock(Lifecycle.class), mNetworkScorer);
|
||||
Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
|
||||
SettingsShadowResources.overrideResource(
|
||||
com.android.internal.R.integer.config_wifi_wakeup_available, 0);
|
||||
when(mNetworkScorer.getActiveScorerPackage()).thenReturn(TEST_SCORER_PACKAGE_NAME);
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -145,4 +154,18 @@ public class WifiWakeupPreferenceControllerTest {
|
||||
verify(preference).setEnabled(false);
|
||||
verify(preference).setSummary(R.string.wifi_wakeup_summary_scanning_disabled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_preferenceSetUncheckedAndSetDisabledWhenScoringDisabled() {
|
||||
final SwitchPreference preference = mock(SwitchPreference.class);
|
||||
Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1);
|
||||
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
|
||||
when(mNetworkScorer.getActiveScorerPackage()).thenReturn(null);
|
||||
|
||||
mController.updateState(preference);
|
||||
|
||||
verify(preference).setChecked(true);
|
||||
verify(preference).setEnabled(false);
|
||||
verify(preference).setSummary(R.string.wifi_wakeup_summary_scoring_disabled);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user