Merge "Update auto wi-fi to prompt user for permissions" into pi-dev

This commit is contained in:
TreeHugger Robot
2018-04-02 01:36:37 +00:00
committed by Android (Google) Code Review
9 changed files with 416 additions and 32 deletions

View File

@@ -0,0 +1,84 @@
/*
* Copyright (C) 2018 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 static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.app.Fragment;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.provider.Settings;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
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;
@RunWith(SettingsRobolectricTestRunner.class)
public class WifiScanningRequiredFragmentTest {
private WifiScanningRequiredFragment mFragment;
private Context mContext;
private ContentResolver mResolver;
@Mock
Fragment mCallbackFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFragment = spy(WifiScanningRequiredFragment.newInstance());
mContext = RuntimeEnvironment.application;
mResolver = mContext.getContentResolver();
doReturn(mContext).when(mFragment).getContext();
mFragment.setTargetFragment(mCallbackFragment, 1000);
Settings.Global.putInt(mResolver, Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0);
}
@After
public void tearDown() {
SettingsShadowResources.reset();
}
@Test
public void onClick_positiveButtonSetsWifiScanningOn()
throws Settings.SettingNotFoundException {
mFragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
assertThat(Settings.Global.getInt(mResolver, Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE))
.isEqualTo(1);
}
@Test
public void onClick_positiveButtonCallsBackToActivity() {
mFragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
verify(mCallbackFragment).onActivityResult(anyInt(), anyInt(), isNull());
}
}

View File

@@ -19,15 +19,20 @@ package com.android.settings.wifi;
import static android.provider.Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE;
import static android.provider.Settings.Global.WIFI_WAKEUP_ENABLED;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import android.app.Fragment;
import android.content.Context;
import android.location.LocationManager;
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.dashboard.DashboardFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
@@ -35,21 +40,34 @@ 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;
@RunWith(SettingsRobolectricTestRunner.class)
public class WifiWakeupPreferenceControllerTest {
private static final String NO_LOCATION_STRING =
"Unavailable because location is turned off. Turn on location.";
private Context mContext;
private WifiWakeupPreferenceController mController;
@Mock
DashboardFragment mFragment;
@Mock
LocationManager mLocationManager;
@Mock
SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new WifiWakeupPreferenceController(mContext);
mController = new WifiWakeupPreferenceController(mContext, mFragment);
mController.mLocationManager = mLocationManager;
mController.mPreference = mPreference;
Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
doReturn(true).when(mLocationManager).isLocationEnabled();
}
@After
@@ -84,39 +102,72 @@ public class WifiWakeupPreferenceControllerTest {
}
@Test
public void updateState_preferenceSetCheckedAndSetEnabledWhenWakeupSettingEnabled() {
public void updateState_preferenceSetCheckedWhenWakeupSettingEnabled() {
final SwitchPreference preference = mock(SwitchPreference.class);
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
mController.updateState(preference);
verify(preference).setChecked(true);
verify(preference).setEnabled(true);
verify(preference).setSummary(R.string.wifi_wakeup_summary);
}
@Test
public void updateState_preferenceSetUncheckedAndSetEnabledWhenWakeupSettingDisabled() {
public void updateState_preferenceSetUncheckedWhenWakeupSettingDisabled() {
final SwitchPreference preference = mock(SwitchPreference.class);
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
mController.updateState(preference);
verify(preference).setChecked(false);
verify(preference).setEnabled(true);
verify(preference).setSummary(R.string.wifi_wakeup_summary);
}
@Test
public void updateState_preferenceSetUncheckedAndSetDisabledWhenWifiScanningDisabled() {
public void updateState_preferenceSetUncheckedWhenWifiScanningDisabled() {
final SwitchPreference preference = mock(SwitchPreference.class);
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
mController.updateState(preference);
verify(preference).setChecked(true);
verify(preference).setEnabled(false);
verify(preference).setSummary(R.string.wifi_wakeup_summary_scanning_disabled);
verify(preference).setChecked(false);
}
@Test
public void updateState_preferenceSetUncheckedWhenWakeupSettingEnabledNoLocation() {
final SwitchPreference preference = mock(SwitchPreference.class);
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
doReturn(false).when(mLocationManager).isLocationEnabled();
mController.updateState(preference);
verify(preference).setChecked(false);
verify(preference).setSummary(mController.getNoLocationSummary());
}
@Test
public void updateState_preferenceSetUncheckedWhenWakeupSettingDisabledLocationEnabled() {
final SwitchPreference preference = mock(SwitchPreference.class);
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
doReturn(false).when(mLocationManager).isLocationEnabled();
mController.updateState(preference);
verify(preference).setChecked(false);
verify(preference).setSummary(mController.getNoLocationSummary());
}
@Test
public void updateState_preferenceSetUncheckedWhenWifiScanningDisabledLocationEnabled() {
final SwitchPreference preference = mock(SwitchPreference.class);
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
doReturn(false).when(mLocationManager).isLocationEnabled();
mController.updateState(preference);
verify(preference).setChecked(false);
verify(preference).setSummary(mController.getNoLocationSummary());
}
}