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:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user