Convert location settings to DashboardFragment.

- change LocationMode, LocationSettings, and ScanningSettings to be
DashboardFragment.
- remove LocationSettingsBase and moved base logics into base
controller.
- add controllers for the preferences under the 3 location settings
pages.

Fixes: 68719207
Test: make RunSettingsRoboTests
Change-Id: Icedf691c2a9f7989faebee39ea9da672209b7957
This commit is contained in:
Doris Ling
2017-11-02 16:42:45 -07:00
parent bbdc72c95a
commit 9ed29a2e57
35 changed files with 2657 additions and 626 deletions

View File

@@ -1,13 +1,9 @@
package com.android.settings.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -24,43 +20,33 @@ import org.robolectric.annotation.Config;
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AppLocationPermissionPreferenceControllerTest {
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
private AppLocationPermissionPreferenceController mController;
@Mock
private Context mContext;
private Preference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new AppLocationPermissionPreferenceController(mContext);
mPreference = new Preference(mContext);
mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
}
@Test
public void displayPreference_shouldRemovePreference() {
public void isAvailable_noLocationLinkPermission_shouldReturnFalse() {
Settings.System.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.LOCATION_SETTINGS_LINK_TO_PERMISSIONS_ENABLED,
0);
mController.displayPreference(mScreen);
assertThat(mPreference.isVisible()).isFalse();
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void displayPreference_shouldNotRemovePreference() {
public void displayPreference_hasLocationLinkPermission_shouldReturnTrue() {
Settings.System.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.LOCATION_SETTINGS_LINK_TO_PERMISSIONS_ENABLED,
1);
mController.displayPreference(mScreen);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mController.isAvailable()).isTrue();
}
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2017 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.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
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;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class BluetoothScanningPreferenceControllerTest {
@Mock
private SwitchPreference mPreference;
private Context mContext;
private BluetoothScanningPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new BluetoothScanningPreferenceController(mContext);
when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
}
@Test
public void updateState_bluetoothScanningEnabled_shouldCheckedPreference() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 1);
mController.updateState(mPreference);
verify(mPreference).setChecked(true);
}
@Test
public void updateState_bluetoothScanningDisabled_shouldUncheckedPreference() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0);
mController.updateState(mPreference);
verify(mPreference).setChecked(false);
}
@Test
public void handlePreferenceTreeClick_checked_shouldEnableBluetoothScanning() {
when(mPreference.isChecked()).thenReturn(true);
mController.handlePreferenceTreeClick(mPreference);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0)).isEqualTo(1);
}
@Test
public void handlePreferenceTreeClick_unchecked_shouldDisableBluetoothScanning() {
when(mPreference.isChecked()).thenReturn(false);
mController.handlePreferenceTreeClick(mPreference);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 1)).isEqualTo(0);
}
}

View File

@@ -0,0 +1,218 @@
/*
* Copyright (C) 2017 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.location;
import static android.Manifest.permission.WRITE_SECURE_SETTINGS;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.text.TextUtils;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
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_O)
public class LocationEnablerTest {
@Mock
private UserManager mUserManager;
@Mock
private LocationEnabler.LocationModeChangeListener mListener;
private Context mContext;
private LocationEnabler mEnabler;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mEnabler = spy(new LocationEnabler(mContext, mListener, new Lifecycle()));
}
@Test
public void onResume_shouldSetActiveAndRegisterListener() {
mEnabler.onResume();
verify(mContext).registerReceiver(eq(mEnabler.mReceiver),
eq(mEnabler.INTENT_FILTER_LOCATION_MODE_CHANGED));
}
@Test
public void onResume_shouldRefreshLocationMode() {
mEnabler.onResume();
verify(mEnabler).refreshLocationMode();
}
@Test
public void onPause_shouldUnregisterListener() {
mEnabler.onPause();
verify(mContext).unregisterReceiver(mEnabler.mReceiver);
}
@Test
public void onReceive_shouldRefreshLocationMode() {
mEnabler.onResume();
reset(mListener);
mEnabler.mReceiver.onReceive(mContext, new Intent());
verify(mListener).onLocationModeChanged(anyInt(), anyBoolean());
}
@Test
public void isEnabled_locationOff_shouldReturnFalse() {
assertThat(mEnabler.isEnabled(Settings.Secure.LOCATION_MODE_OFF)).isFalse();
}
@Test
public void isEnabled_restricted_shouldReturnFalse() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(true);
assertThat(mEnabler.isEnabled(Settings.Secure.LOCATION_MODE_OFF)).isFalse();
}
@Test
public void isEnabled_locationONotRestricted_shouldReturnTrue() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
assertThat(mEnabler.isEnabled(Settings.Secure.LOCATION_MODE_BATTERY_SAVING)).isTrue();
}
@Test
public void refreshLocationMode_shouldCallOnLocationModeChanged() {
mEnabler.refreshLocationMode();
verify(mListener).onLocationModeChanged(anyInt(), anyBoolean());
}
@Test
public void setLocationMode_restricted_shouldSetCurrentMode() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(true);
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
mEnabler.setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
verify(mListener)
.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, true);
}
@Test
public void setLocationMode_notRestricted_shouldUpdateSecureSettings() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
mEnabler.setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_BATTERY_SAVING))
.isEqualTo(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
}
@Test
public void setLocationMode_notRestricted_shouldRefreshLocation() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
mEnabler.setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
verify(mEnabler).refreshLocationMode();
}
@Test
public void setLocationMode_notRestricted_shouldBroadcastUpdate() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
mEnabler.setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
verify(mContext).sendBroadcast(argThat(actionMatches(mEnabler.MODE_CHANGING_ACTION)),
eq(WRITE_SECURE_SETTINGS));
}
@Test
public void isManagedProfileRestrictedByBase_notManagedProfile_shouldReturnFalse() {
assertThat(mEnabler.isManagedProfileRestrictedByBase()).isFalse();
}
@Test
public void isManagedProfileRestrictedByBase_notRestricted_shouldReturnFalse() {
mockManagedProfile();
doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt());
assertThat(mEnabler.isManagedProfileRestrictedByBase()).isFalse();
}
@Test
public void isManagedProfileRestrictedByBase_hasManagedProfile_shouldReturnFalse() {
mockManagedProfile();
doReturn(true).when(mEnabler).hasShareLocationRestriction(anyInt());
assertThat(mEnabler.isManagedProfileRestrictedByBase()).isTrue();
}
private void mockManagedProfile() {
final List<UserHandle> userProfiles = new ArrayList<>();
final UserHandle userHandle = mock(UserHandle.class);
when(userHandle.getIdentifier()).thenReturn(5);
userProfiles.add(userHandle);
when(mUserManager.getUserProfiles()).thenReturn(userProfiles);
when(mUserManager.getUserHandle()).thenReturn(1);
when(mUserManager.getUserInfo(5))
.thenReturn(new UserInfo(5, "user 5", UserInfo.FLAG_MANAGED_PROFILE));
}
private static ArgumentMatcher<Intent> actionMatches(String expected) {
return intent -> TextUtils.equals(expected, intent.getAction());
}
}

View File

@@ -0,0 +1,193 @@
/*
* Copyright (C) 2017 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.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
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;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class LocationForWorkPreferenceControllerTest {
@Mock
private RestrictedSwitchPreference mPreference;
@Mock
private PreferenceScreen mScreen;
@Mock
private UserManager mUserManager;
@Mock
private LocationEnabler mEnabler;
@Mock
private UserHandle mUserHandle;
private Context mContext;
private LocationForWorkPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mController = spy(new LocationForWorkPreferenceController(mContext, new Lifecycle()));
mockManagedProfile();
ReflectionHelpers.setField(mController, "mLocationEnabler", mEnabler);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
final String key = mController.getPreferenceKey();
when(mPreference.getKey()).thenReturn(key);
when(mPreference.isVisible()).thenReturn(true);
}
@Test
public void handlePreferenceTreeClick_preferenceChecked_shouldSetRestrictionAndOnSummary() {
mController.displayPreference(mScreen);
when(mPreference.isChecked()).thenReturn(true);
mController.handlePreferenceTreeClick(mPreference);
verify(mUserManager).setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, false,
mUserHandle);
verify(mPreference).setSummary(R.string.switch_on_text);
}
@Test
public void handlePreferenceTreeClick_preferenceUnchecked_shouldSetRestritionAndOffSummary() {
mController.displayPreference(mScreen);
when(mPreference.isChecked()).thenReturn(false);
mController.handlePreferenceTreeClick(mPreference);
verify(mUserManager).setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, true,
mUserHandle);
verify(mPreference).setSummary(R.string.switch_off_text);
}
@Test
public void isAvailable_noManagedProfile_shouldReturnFalse() {
when(mUserManager.getUserProfiles()).thenReturn(new ArrayList<UserHandle>());
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_hasManagedProfile_shouldReturnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void onLocationModeChanged_disabledByAdmin_shouldDisablePreference() {
mController.displayPreference(mScreen);
final EnforcedAdmin admin = mock(EnforcedAdmin.class);
doReturn(admin).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
doReturn(false).when(mEnabler).isManagedProfileRestrictedByBase();
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mPreference).setDisabledByAdmin(any());
verify(mPreference).setChecked(false);
}
@Test
public void onLocationModeChanged_locationOff_shouldDisablePreference() {
mController.displayPreference(mScreen);
doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
doReturn(false).when(mEnabler).isManagedProfileRestrictedByBase();
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_OFF, false);
verify(mPreference).setEnabled(false);
verify(mPreference).setChecked(false);
verify(mPreference).setSummary(R.string.switch_off_text);
}
@Test
public void onLocationModeChanged_locationOn_shouldEnablePreference() {
mController.displayPreference(mScreen);
doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
doReturn(false).when(mEnabler).isManagedProfileRestrictedByBase();
doReturn(true).when(mEnabler).isEnabled(anyInt());
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mPreference).setEnabled(true);
verify(mPreference).setSummary(R.string.switch_on_text);
}
@Test
public void onLocationModeChanged_noRestriction_shouldCheckedPreference() {
mController.displayPreference(mScreen);
doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
doReturn(false).when(mEnabler).isManagedProfileRestrictedByBase();
doReturn(true).when(mEnabler).isEnabled(anyInt());
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mPreference).setChecked(true);
}
@Test
public void onLocationModeChanged_hasRestriction_shouldCheckedPreference() {
mController.displayPreference(mScreen);
doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
doReturn(true).when(mEnabler).isManagedProfileRestrictedByBase();
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mPreference).setChecked(false);
}
private void mockManagedProfile() {
final List<UserHandle> userProfiles = new ArrayList<>();
when(mUserHandle.getIdentifier()).thenReturn(5);
userProfiles.add(mUserHandle);
when(mUserManager.getUserProfiles()).thenReturn(userProfiles);
when(mUserManager.getUserHandle()).thenReturn(1);
when(mUserManager.getUserInfo(5))
.thenReturn(new UserInfo(5, "user 5", UserInfo.FLAG_MANAGED_PROFILE));
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2017 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.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class LocationModeBatterySavingPreferenceControllerTest {
@Test
public void getLocationMode_shouldReturnModeBatterySaving() {
final LocationModeBatterySavingPreferenceController controller =
new LocationModeBatterySavingPreferenceController(mock(Context.class),
new Lifecycle());
assertThat(controller.getLocationMode())
.isEqualTo(Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2017 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.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class LocationModeHighAccuracyPreferenceControllerTest {
@Test
public void getLocationMode_shouldReturnModeHighAccuracy() {
final LocationModeHighAccuracyPreferenceController controller =
new LocationModeHighAccuracyPreferenceController(mock(Context.class),
new Lifecycle());
assertThat(controller.getLocationMode())
.isEqualTo(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
}
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright (C) 2017 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.location;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.UserManager;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
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;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class LocationModePreferenceControllerTest {
@Mock
private LocationSettings mFragment;
@Mock
private SettingsActivity mActivity;
@Mock
private Preference mPreference;
@Mock
private PreferenceScreen mScreen;
@Mock
private UserManager mUserManager;
private Context mContext;
private LocationModePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mController = new LocationModePreferenceController(mContext, mFragment, new Lifecycle());
when(mFragment.getActivity()).thenReturn(mActivity);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
}
@Test
public void onLocationModeChanged_locationOff_shouldDisablePreference() {
when(mUserManager.hasUserRestriction(any())).thenReturn(false);
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_OFF, false);
verify(mPreference).setEnabled(false);
}
@Test
public void onLocationModeChanged_restricted_shouldDisablePreference() {
when(mUserManager.hasUserRestriction(any())).thenReturn(true);
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mPreference).setEnabled(false);
}
@Test
public void onLocationModeChanged_locationOnNotRestricted_shouldEnablePreference() {
when(mUserManager.hasUserRestriction(any())).thenReturn(false);
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mPreference).setEnabled(true);
}
@Test
public void onLocationModeChanged_shouldUpdateSummary() {
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mPreference).setSummary(anyInt());
}
@Test
public void handlePreferenceTreeClick_shouldStartLocationModeFragment() {
final Preference preference = new Preference(mContext);
preference.setKey(mController.getPreferenceKey());
mController.handlePreferenceTreeClick(preference);
verify(mActivity).startPreferencePanel(any(), eq(LocationMode.class.getName()), any(),
eq(R.string.location_mode_screen_title), any(), any(), anyInt());
}
}

View File

@@ -0,0 +1,133 @@
/*
* Copyright (C) 2017 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.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.widget.RadioButtonPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class LocationModeRadioButtonPreferenceControllerTest {
@Mock
private RadioButtonPreference mPreference;
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private LocationModeRadioButtonPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new LocationModeRadioButtonPreferenceControllerTestable(mContext);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
}
@Test
public void displayPreference_shouldAddClickListener() {
mController.displayPreference(mScreen);
verify(mPreference).setOnClickListener(mController);
}
@Test
public void onRadioButtonClicked_shouldSetLocationModeToOwnMode() {
mController.displayPreference(mScreen);
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
mController.onRadioButtonClicked(mPreference);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF))
.isEqualTo(mController.getLocationMode());
}
@Test
public void onLocationModeChanged_otherModeSelected_shouldUncheckPreference() {
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mPreference).setChecked(false);
}
@Test
public void onLocationModeChanged_ownModeSelected_shouldCheckPreference() {
mController.displayPreference(mScreen);
mController.onLocationModeChanged(mController.getLocationMode(), false);
verify(mPreference).setChecked(true);
}
@Test
public void onLocationModeChanged_locationOff_shouldDisablePreference() {
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_OFF, false);
verify(mPreference).setEnabled(false);
}
@Test
public void onLocationModeChanged_locationOn_shouldDisablePreference() {
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mPreference).setEnabled(true);
}
private class LocationModeRadioButtonPreferenceControllerTestable
extends LocationModeRadioButtonPreferenceController {
public LocationModeRadioButtonPreferenceControllerTestable(Context context) {
super(context, new Lifecycle());
}
@Override
public String getPreferenceKey() {
return "test";
}
@Override
protected int getLocationMode() {
return Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2017 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.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class LocationModeSensorsOnlyPreferenceControllerTest {
@Test
public void getLocationMode_shouldReturnModeSensorsOnly() {
final LocationModeSensorsOnlyPreferenceController controller =
new LocationModeSensorsOnlyPreferenceController(mock(Context.class),
new Lifecycle());
assertThat(controller.getLocationMode())
.isEqualTo(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
}
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright (C) 2017 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.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
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_O)
public class LocationServicePreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private LocationSettings mFragment;
@Mock
private PreferenceCategory mCategory;
@Mock
private PreferenceScreen mScreen;
@Mock
private SettingsInjector mSettingsInjector;
private Context mContext;
private LocationServicePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mController = spy(new LocationServicePreferenceController(
mContext, mFragment, new Lifecycle(), mSettingsInjector));
final String key = mController.getPreferenceKey();
when(mScreen.findPreference(key)).thenReturn(mCategory);
when(mCategory.getKey()).thenReturn(key);
}
@Test
public void isAvailable_noInjectedSettings_shouldReturnFalse() {
doReturn(false).when(mSettingsInjector).hasInjectedSettings(anyInt());
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_hasInjectedSettings_shouldReturnFalse() {
doReturn(true).when(mSettingsInjector).hasInjectedSettings(anyInt());
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void onResume_shouldRegisterListener() {
mController.onResume();
verify(mContext).registerReceiver(eq(mController.mInjectedSettingsReceiver),
eq(mController.INTENT_FILTER_INJECTED_SETTING_CHANGED));
}
@Test
public void onPause_shouldUnregisterListener() {
mController.onResume();
mController.onPause();
verify(mContext).unregisterReceiver(mController.mInjectedSettingsReceiver);
}
@Test
public void updateState_shouldRemoveAllAndAddInjectedSettings() {
final List<Preference> preferences = new ArrayList<>();
final Preference pref1 = new Preference(mContext);
pref1.setTitle("Title1");
final Preference pref2 = new Preference(mContext);
pref2.setTitle("Title2");
preferences.add(pref1);
preferences.add(pref2);
doReturn(preferences).when(mSettingsInjector)
.getInjectedSettings(any(Context.class), anyInt());
when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext);
mController.displayPreference(mScreen);
mController.updateState(mCategory);
verify(mCategory).removeAll();
verify(mCategory).addPreference(pref1);
verify(mCategory).addPreference(pref2);
}
@Test
public void onLocationModeChanged_shouldRequestReloadInjectedSettigns() {
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mSettingsInjector).reloadStatusMessages();
}
}

View File

@@ -0,0 +1,155 @@
/*
* Copyright (C) 2017 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.location;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.ToggleSwitch;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.core.lifecycle.Lifecycle;
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;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class LocationSwitchBarControllerTest {
@Mock
private SwitchBar mSwitchBar;
@Mock
private ToggleSwitch mSwitch;
@Mock
private LocationEnabler mEnabler;
private Context mContext;
private LocationSwitchBarController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
ReflectionHelpers.setField(mSwitchBar, "mSwitch", mSwitch);
mController = spy(new LocationSwitchBarController(
mContext, mSwitchBar, new Lifecycle()));
ReflectionHelpers.setField(mController, "mLocationEnabler", mEnabler);
}
@Test
public void onStart_shouldShowSwitchBarAndAddOnSwitchChangeListener() {
mController.onStart();
verify(mSwitchBar).show();
verify(mSwitchBar).addOnSwitchChangeListener(mController);
}
@Test
public void onStop_shouldHideSwitchBarAndRemoveOnSwitchChangeListener() {
mController.onStart();
mController.onStop();
verify(mSwitchBar).hide();
verify(mSwitchBar).removeOnSwitchChangeListener(mController);
}
@Test
public void onSwitchChanged_switchChecked_shouldSetPreviousLocationMode() {
mController.onSwitchChanged(mSwitch, true);
verify(mEnabler).setLocationMode(
android.provider.Settings.Secure.LOCATION_MODE_PREVIOUS);
}
@Test
public void onSwitchChanged_switchUnchecked_shouldSetLocationModeOff() {
mController.onSwitchChanged(mSwitch, false);
verify(mEnabler).setLocationMode(android.provider.Settings.Secure.LOCATION_MODE_OFF);
}
@Test
public void onLocationModeChanged_hasEnforcedAdmin_shouldDisableSwitchByAdmin() {
final RestrictedLockUtils.EnforcedAdmin admin =
mock(RestrictedLockUtils.EnforcedAdmin.class);
doReturn(admin).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt());
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mSwitchBar).setDisabledByAdmin(admin);
}
@Test
public void onLocationModeChanged_Restricted_shouldDisableSwitch() {
doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
doReturn(true).when(mEnabler).hasShareLocationRestriction(anyInt());
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, true);
verify(mSwitchBar).setEnabled(false);
}
@Test
public void onLocationModeChanged_notRestricted_shouldEnableSwitch() {
doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt());
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mSwitchBar).setEnabled(true);
}
@Test
public void onLocationModeChanged_locationOn_shouldCheckSwitch() {
doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt());
when(mSwitch.isChecked()).thenReturn(false);
doReturn(true).when(mEnabler).isEnabled(anyInt());
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mSwitch).setChecked(true);
}
@Test
public void onLocationModeChanged_locationOff_shouldUncheckSwitch() {
doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt());
when(mSwitch.isChecked()).thenReturn(true);
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_OFF, false);
verify(mSwitch).setChecked(false);
}
}

View File

@@ -0,0 +1,176 @@
/*
* Copyright (C) 2017 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.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.TestConfig;
import com.android.settings.applications.InstalledAppDetails;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.widget.AppPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.location.RecentLocationApps;
import com.android.settingslib.location.RecentLocationApps.Request;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
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_O)
public class RecentLocationRequestPreferenceControllerTest {
@Mock
private LocationSettings mFragment;
@Mock
private PreferenceCategory mCategory;
@Mock
private PreferenceScreen mScreen;
@Mock
private RecentLocationApps mRecentLocationApps;
private Context mContext;
private RecentLocationRequestPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mController = spy(new RecentLocationRequestPreferenceController(
mContext, mFragment, new Lifecycle(), mRecentLocationApps));
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mCategory);
final String key = mController.getPreferenceKey();
when(mCategory.getKey()).thenReturn(key);
when(mCategory.getContext()).thenReturn(mContext);
}
@Test
public void onLocationModeChanged_LocationOn_shouldEnablePreference() {
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
verify(mCategory).setEnabled(true);
}
@Test
public void onLocationModeChanged_LocationOff_shouldDisablePreference() {
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_OFF, false);
verify(mCategory).setEnabled(false);
}
@Test
public void updateState_noRecentRequest_shouldRemoveAllAndAddBanner() {
doReturn(new ArrayList<>()).when(mRecentLocationApps).getAppList();
mController.displayPreference(mScreen);
mController.updateState(mCategory);
verify(mCategory).removeAll();
verify(mCategory).addPreference(
argThat(titleMatches(mContext.getString(R.string.location_no_recent_apps))));
}
@Test
public void updateState_hasRecentRequest_shouldRemoveAllAndAddInjectedSettings() {
final List<RecentLocationApps.Request> requests = new ArrayList<>();
final Request req1 = mock(Request.class);
final Request req2 = mock(Request.class);
requests.add(req1);
requests.add(req2);
doReturn(requests).when(mRecentLocationApps).getAppList();
final String title = "testTitle";
final AppPreference preference = mock(AppPreference.class);
when(preference.getTitle()).thenReturn(title);
doReturn(preference).when(mController)
.createAppPreference(any(Context.class), any(Request.class));
mController.displayPreference(mScreen);
mController.updateState(mCategory);
verify(mCategory).removeAll();
verify(mCategory, times(2)).addPreference(argThat(titleMatches(title)));
}
@Test
public void createAppPreference_shouldAddClickListener() {
final Request request = mock(Request.class);
final AppPreference preference = mock(AppPreference.class);
doReturn(preference).when(mController)
.createAppPreference(any(Context.class));
mController.createAppPreference(mContext, request);
verify(preference).setOnPreferenceClickListener(
any(RecentLocationRequestPreferenceController.PackageEntryClickedListener.class));
}
@Test
public void onPreferenceClick_shouldLaunchAppDetails() {
final SettingsActivity activity = mock(SettingsActivity.class);
when(mFragment.getActivity()).thenReturn(activity);
final List<RecentLocationApps.Request> requests = new ArrayList<>();
final Request request = mock(Request.class);
requests.add(request);
doReturn(requests).when(mRecentLocationApps).getAppList();
final AppPreference preference = new AppPreference(mContext);
doReturn(preference).when(mController).createAppPreference(any(Context.class));
mController.displayPreference(mScreen);
mController.updateState(mCategory);
preference.performClick();
verify(activity).startPreferencePanelAsUser(any(), eq(InstalledAppDetails.class.getName()),
any(Bundle.class), anyInt(), any(), any());
}
private static ArgumentMatcher<Preference> titleMatches(String expected) {
return preference -> TextUtils.equals(expected, preference.getTitle());
}
}

View File

@@ -34,13 +34,12 @@ import org.robolectric.annotation.Config;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class ScanningSettingsTest {
private Context mContext;
private ScanningSettings mSettings;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2017 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.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
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;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class WifiScanningPreferenceControllerTest {
@Mock
private SwitchPreference mPreference;
private Context mContext;
private WifiScanningPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new WifiScanningPreferenceController(mContext);
when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
}
@Test
public void updateState_wifiScanningEnabled_shouldCheckedPreference() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1);
mController.updateState(mPreference);
verify(mPreference).setChecked(true);
}
@Test
public void updateState_wifiScanningDisabled_shouldUncheckedPreference() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0);
mController.updateState(mPreference);
verify(mPreference).setChecked(false);
}
@Test
public void handlePreferenceTreeClick_checked_shouldEnableWifiScanning() {
when(mPreference.isChecked()).thenReturn(true);
mController.handlePreferenceTreeClick(mPreference);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0)).isEqualTo(1);
}
@Test
public void handlePreferenceTreeClick_unchecked_shouldDisableWifiScanning() {
when(mPreference.isChecked()).thenReturn(false);
mController.handlePreferenceTreeClick(mPreference);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1)).isEqualTo(0);
}
}