• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.settings.display;
18 
19 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.anyInt;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.content.Context;
30 import android.os.UserHandle;
31 import android.os.UserManager;
32 import android.provider.Settings;
33 
34 import com.android.settings.R;
35 import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
36 import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
37 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
38 
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.ArgumentCaptor;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 
50 import java.util.ArrayList;
51 import java.util.Collections;
52 import java.util.List;
53 
54 @RunWith(RobolectricTestRunner.class)
55 @Config(shadows = ShadowDevicePolicyManager.class)
56 public class TimeoutPreferenceControllerTest {
57 
58     private static final int TIMEOUT = 30;
59     private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
60     private static final String DEFAULT_TIMEOUT = "300000"; // 5 minutes
61 
62     private Context mContext;
63     @Mock
64     private TimeoutListPreference mPreference;
65     @Mock
66     private UserManager mUserManager;
67 
68     private TimeoutPreferenceController mController;
69 
70     @Before
setUp()71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73         mContext = spy(RuntimeEnvironment.application);
74         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
75         when(mPreference.getValue()).thenReturn(DEFAULT_TIMEOUT);
76         mController = new TimeoutPreferenceController(mContext, KEY_SCREEN_TIMEOUT);
77     }
78 
79     @After
tearDown()80     public void tearDown() {
81           ShadowRestrictedLockUtilsInternal.reset();
82     }
83 
84     @Test
testOnPreferenceChange_SetTimeout_ReturnCorrectTimeout()85     public void testOnPreferenceChange_SetTimeout_ReturnCorrectTimeout() {
86         mController.onPreferenceChange(mPreference, Integer.toString(TIMEOUT));
87 
88         final int mode = Settings.System.getInt(mContext.getContentResolver(),
89                 SCREEN_OFF_TIMEOUT, 0);
90         assertThat(mode).isEqualTo(TIMEOUT);
91     }
92 
93     @Test
testUpdateStateNoAdminTimeouts()94     public void testUpdateStateNoAdminTimeouts() {
95         when(mUserManager.getProfiles(anyInt())).thenReturn(Collections.emptyList());
96         mController.updateState(mPreference);
97         verify(mPreference).removeUnusableTimeouts(0, null);
98     }
99 
100     @Test
testUpdateStateWithAdminTimeouts()101     public void testUpdateStateWithAdminTimeouts() {
102         final int profileUserId = UserHandle.myUserId();
103         final long timeout = 10000;
104         when(mUserManager.getProfiles(profileUserId)).thenReturn(Collections.emptyList());
105         ShadowDevicePolicyManager.getShadow().setMaximumTimeToLock(profileUserId, timeout);
106 
107         mController.updateState(mPreference);
108         verify(mPreference).removeUnusableTimeouts(timeout, null);
109     }
110 
111     @Test
testUpdateStateWithAdminTimeoutsAndRestriction()112     public void testUpdateStateWithAdminTimeoutsAndRestriction() {
113         final int profileUserId = UserHandle.myUserId();
114         final long timeout = 100;
115         when(mUserManager.getProfiles(profileUserId)).thenReturn(Collections.emptyList());
116         ShadowDevicePolicyManager.getShadow().setMaximumTimeToLock(profileUserId, timeout);
117 
118         int userId = UserHandle.myUserId();
119         List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
120         // Add two enforcing users so that RestrictedLockUtils.checkIfRestrictionEnforced returns
121         // non-null.
122         enforcingUsers.add(new UserManager.EnforcingUser(userId,
123                 UserManager.RESTRICTION_SOURCE_DEVICE_OWNER));
124         enforcingUsers.add(new UserManager.EnforcingUser(userId,
125                 UserManager.RESTRICTION_SOURCE_PROFILE_OWNER));
126         when(mUserManager.getUserRestrictionSources(
127                 UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT, UserHandle.of(userId)))
128                 .thenReturn(enforcingUsers);
129 
130         mController.updateState(mPreference);
131 
132         ArgumentCaptor<Long> longCaptor = ArgumentCaptor.forClass(Long.class);
133         ArgumentCaptor<EnforcedAdmin> adminCaptor = ArgumentCaptor.forClass(EnforcedAdmin.class);
134 
135         verify(mPreference, times(2))
136                 .removeUnusableTimeouts(longCaptor.capture(), adminCaptor.capture());
137         assertThat(longCaptor.getValue()).isEqualTo(0);
138         assertThat(adminCaptor.getValue()).isNotNull();
139     }
140 
141     @Test
142     @Config(shadows = ShadowRestrictedLockUtilsInternal.class)
updateState_selectedTimeoutLargerThanAdminMax_shouldSetSummaryToUpdatedPrefValue()143     public void updateState_selectedTimeoutLargerThanAdminMax_shouldSetSummaryToUpdatedPrefValue() {
144         final int profileUserId = UserHandle.myUserId();
145         final long allowedTimeout = 480000L; // 8 minutes
146         when(mUserManager.getProfiles(profileUserId)).thenReturn(Collections.emptyList());
147         ShadowDevicePolicyManager.getShadow().setMaximumTimeToLock(profileUserId, allowedTimeout);
148         ShadowRestrictedLockUtilsInternal.setMaximumTimeToLockIsSet(true);
149         final CharSequence[] timeouts = {"15000", "30000", "60000", "120000", "300000", "600000"};
150         final CharSequence[] summaries = {"15s", "30s", "1m", "2m", "5m", "10m"};
151         // set current timeout to be 10 minutes, which is longer than the allowed 8 minutes
152         Settings.System.putLong(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, 600000L);
153         when(mPreference.getEntries()).thenReturn(summaries);
154         when(mPreference.getEntryValues()).thenReturn(timeouts);
155         when(mPreference.getValue()).thenReturn("300000");
156 
157         mController.updateState(mPreference);
158 
159         verify(mPreference).setSummary(mContext.getString(R.string.screen_timeout_summary, "5m"));
160     }
161 }
162