1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.settings.location;
17 
18 import static org.mockito.ArgumentMatchers.anyInt;
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.provider.Settings;
27 
28 import androidx.lifecycle.LifecycleOwner;
29 
30 import com.android.settings.widget.SwitchBar;
31 import com.android.settings.widget.ToggleSwitch;
32 import com.android.settingslib.RestrictedLockUtils;
33 import com.android.settingslib.core.lifecycle.Lifecycle;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 import org.robolectric.util.ReflectionHelpers;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class LocationSwitchBarControllerTest {
46 
47     @Mock
48     private SwitchBar mSwitchBar;
49     @Mock
50     private ToggleSwitch mSwitch;
51     @Mock
52     private LocationEnabler mEnabler;
53 
54     private Context mContext;
55     private LocationSwitchBarController mController;
56     private LifecycleOwner mLifecycleOwner;
57     private Lifecycle mLifecycle;
58 
59     @Before
setUp()60     public void setUp() {
61         MockitoAnnotations.initMocks(this);
62         mContext = RuntimeEnvironment.application;
63         ReflectionHelpers.setField(mSwitchBar, "mSwitch", mSwitch);
64         mLifecycleOwner = () -> mLifecycle;
65         mLifecycle = new Lifecycle(mLifecycleOwner);
66         mController = spy(new LocationSwitchBarController(mContext, mSwitchBar, mLifecycle));
67         ReflectionHelpers.setField(mController, "mLocationEnabler", mEnabler);
68     }
69 
70     @Test
onStart_shouldAddOnSwitchChangeListener()71     public void onStart_shouldAddOnSwitchChangeListener() {
72         mController.onStart();
73 
74         verify(mSwitchBar).addOnSwitchChangeListener(mController);
75     }
76 
77     @Test
onStop_shouldRemoveOnSwitchChangeListener()78     public void onStop_shouldRemoveOnSwitchChangeListener() {
79         mController.onStart();
80         mController.onStop();
81 
82         verify(mSwitchBar).removeOnSwitchChangeListener(mController);
83     }
84 
85     @Test
onSwitchChanged_switchChecked_shouldSetLocationEnabled()86     public void onSwitchChanged_switchChecked_shouldSetLocationEnabled() {
87         mController.onSwitchChanged(mSwitch, true);
88 
89         verify(mEnabler).setLocationEnabled(true);
90     }
91 
92     @Test
onSwitchChanged_switchUnchecked_shouldSetLocationDisabled()93     public void onSwitchChanged_switchUnchecked_shouldSetLocationDisabled() {
94         mController.onSwitchChanged(mSwitch, false);
95 
96         verify(mEnabler).setLocationEnabled(false);
97     }
98 
99     @Test
onLocationModeChanged_hasEnforcedAdmin_shouldDisableSwitchByAdmin()100     public void onLocationModeChanged_hasEnforcedAdmin_shouldDisableSwitchByAdmin() {
101         final RestrictedLockUtils.EnforcedAdmin admin =
102                 mock(RestrictedLockUtils.EnforcedAdmin.class);
103         doReturn(admin).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
104         doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt());
105 
106         mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
107 
108         verify(mSwitchBar).setDisabledByAdmin(admin);
109     }
110 
111     @Test
onLocationModeChanged_Restricted_shouldDisableSwitch()112     public void onLocationModeChanged_Restricted_shouldDisableSwitch() {
113         doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
114         doReturn(true).when(mEnabler).hasShareLocationRestriction(anyInt());
115 
116         mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, true);
117 
118         verify(mSwitchBar).setEnabled(false);
119     }
120 
121     @Test
onLocationModeChanged_notRestricted_shouldEnableSwitch()122     public void onLocationModeChanged_notRestricted_shouldEnableSwitch() {
123         doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
124         doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt());
125 
126         mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
127 
128         verify(mSwitchBar).setEnabled(true);
129     }
130 
131     @Test
onLocationModeChanged_locationOn_shouldCheckSwitch()132     public void onLocationModeChanged_locationOn_shouldCheckSwitch() {
133         doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
134         doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt());
135         when(mSwitch.isChecked()).thenReturn(false);
136         doReturn(true).when(mEnabler).isEnabled(anyInt());
137 
138         mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
139 
140         verify(mSwitch).setChecked(true);
141     }
142 
143     @Test
onLocationModeChanged_locationOff_shouldUncheckSwitch()144     public void onLocationModeChanged_locationOff_shouldUncheckSwitch() {
145         doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt());
146         doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt());
147         when(mSwitch.isChecked()).thenReturn(true);
148 
149         mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_OFF, false);
150 
151         verify(mSwitch).setChecked(false);
152     }
153 }
154