Update Work profile settings
Change work apps toggle to a primary toggle make Xprofile contact search toggle disable/enable when work profile is turned off Add footer and change strings Add tests Test: atest ContactSearchPreferenceControllerTest, atest WorkModePreferenceControllerTest Bug: 253009702 275538029 Change-Id: I3b2044a5fe3f2aff0748d66e701a3f0d7667ab7a
This commit is contained in:
@@ -20,13 +20,19 @@ import static android.provider.Settings.Secure.MANAGED_PROFILE_CONTACT_REMOTE_SE
|
||||
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.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 androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -35,39 +41,51 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ContactSearchPreferenceControllerTest {
|
||||
|
||||
private static final String PREF_KEY = "contacts_search";
|
||||
|
||||
@Mock
|
||||
private UserHandle mManagedUser;
|
||||
private static final int MANAGED_USER_ID = 10;
|
||||
|
||||
private Context mContext;
|
||||
private ContactSearchPreferenceController mController;
|
||||
private RestrictedSwitchPreference mPreference;
|
||||
|
||||
@Mock
|
||||
private UserHandle mManagedUser;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private UserInfo mUserInfo;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mController = new ContactSearchPreferenceController(mContext, PREF_KEY);
|
||||
mController.setManagedUser(mManagedUser);
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||
mPreference = spy(new RestrictedSwitchPreference(mContext));
|
||||
when(mUserInfo.isManagedProfile()).thenReturn(true);
|
||||
when(mUserManager.getUserInfo(anyInt())).thenReturn(mUserInfo);
|
||||
when(mUserManager.getProcessUserId()).thenReturn(0);
|
||||
when(mUserManager.getUserProfiles()).thenReturn(Collections.singletonList(mManagedUser));
|
||||
when(mManagedUser.getIdentifier()).thenReturn(MANAGED_USER_ID);
|
||||
mController = new ContactSearchPreferenceController(mContext, PREF_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_noManagedUser_DISABLED() {
|
||||
mController.setManagedUser(null);
|
||||
when(mUserManager.getProcessUserId()).thenReturn(MANAGED_USER_ID);
|
||||
mController = new ContactSearchPreferenceController(mContext, PREF_KEY);
|
||||
|
||||
assertThat(mController.getAvailabilityStatus())
|
||||
.isNotEqualTo(ContactSearchPreferenceController.AVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_hasManagedUser_AVAILABLE() {
|
||||
mController.setManagedUser(mManagedUser);
|
||||
assertThat(mController.getAvailabilityStatus())
|
||||
.isEqualTo(ContactSearchPreferenceController.AVAILABLE);
|
||||
}
|
||||
@@ -75,32 +93,96 @@ public class ContactSearchPreferenceControllerTest {
|
||||
@Test
|
||||
public void updateState_shouldRefreshContent() {
|
||||
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, mManagedUser.getIdentifier());
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, MANAGED_USER_ID);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isChecked()).isFalse();
|
||||
|
||||
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 1, mManagedUser.getIdentifier());
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 1, MANAGED_USER_ID);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isChecked()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_preferenceShouldBeDisabled() {
|
||||
mController.updateState(mPreference);
|
||||
|
||||
verify(mPreference).setDisabledByAdmin(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPreferenceChange_shouldUpdateProviderValue() {
|
||||
mController.onPreferenceChange(mPreference, false);
|
||||
|
||||
assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 1, mManagedUser.getIdentifier()))
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 1, MANAGED_USER_ID))
|
||||
.isEqualTo(0);
|
||||
|
||||
mController.onPreferenceChange(mPreference, true);
|
||||
|
||||
assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, mManagedUser.getIdentifier()))
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, MANAGED_USER_ID))
|
||||
.isEqualTo(1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onQuietModeDisabled_preferenceEnabled() {
|
||||
when(mUserManager.isQuietModeEnabled(any(UserHandle.class))).thenReturn(false);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isEnabled()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onQuietModeEnabled_preferenceDisabledAndUnchecked() {
|
||||
when(mUserManager.isQuietModeEnabled(any(UserHandle.class))).thenReturn(true);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isEnabled()).isFalse();
|
||||
assertThat(mPreference.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void afterQuietModeTurnedOnAndOffWhenPreferenceChecked_toggleCheckedAndEnabled() {
|
||||
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 1, MANAGED_USER_ID);
|
||||
when(mUserManager.isQuietModeEnabled(any(UserHandle.class))).thenReturn(true);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isEnabled()).isFalse();
|
||||
assertThat(mPreference.isChecked()).isFalse();
|
||||
|
||||
when(mUserManager.isQuietModeEnabled(any(UserHandle.class))).thenReturn(false);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isEnabled()).isTrue();
|
||||
assertThat(mPreference.isChecked()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void afterQuietModeTurnedOnAndOffWhenPreferenceUnchecked_toggleUncheckedAndEnabled() {
|
||||
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, MANAGED_USER_ID);
|
||||
when(mUserManager.isQuietModeEnabled(any(UserHandle.class))).thenReturn(true);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isEnabled()).isFalse();
|
||||
assertThat(mPreference.isChecked()).isFalse();
|
||||
|
||||
when(mUserManager.isQuietModeEnabled(any(UserHandle.class))).thenReturn(false);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isEnabled()).isTrue();
|
||||
assertThat(mPreference.isChecked()).isFalse();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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.accounts;
|
||||
|
||||
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.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.content.Intent;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LifecycleRegistry;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ManagedProfileQuietModeEnablerTest {
|
||||
private static final int MANAGED_USER_ID = 10;
|
||||
private Context mContext;
|
||||
private ManagedProfileQuietModeEnabler mQuietModeEnabler;
|
||||
private LifecycleOwner mLifecycleOwner = new LifecycleOwner() {
|
||||
public LifecycleRegistry registry = new LifecycleRegistry(this);
|
||||
|
||||
@Override
|
||||
public Lifecycle getLifecycle() {
|
||||
return registry;
|
||||
}
|
||||
};
|
||||
|
||||
@Mock
|
||||
private ManagedProfileQuietModeEnabler.QuietModeChangeListener mOnQuietModeChangeListener;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private UserHandle mManagedUser;
|
||||
@Mock
|
||||
private UserInfo mUserInfo;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
when(mUserInfo.isManagedProfile()).thenReturn(true);
|
||||
when(mUserManager.getUserInfo(anyInt())).thenReturn(mUserInfo);
|
||||
when(mUserManager.getProcessUserId()).thenReturn(0);
|
||||
when(mManagedUser.getIdentifier()).thenReturn(MANAGED_USER_ID);
|
||||
when(mUserManager.getUserProfiles()).thenReturn(Collections.singletonList(mManagedUser));
|
||||
mQuietModeEnabler = new ManagedProfileQuietModeEnabler(mContext,
|
||||
mOnQuietModeChangeListener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onSetQuietMode_shouldRequestQuietModeEnabled() {
|
||||
mQuietModeEnabler.setQuietModeEnabled(false);
|
||||
verify(mUserManager).requestQuietModeEnabled(false, mManagedUser);
|
||||
mQuietModeEnabler.setQuietModeEnabled(true);
|
||||
verify(mUserManager).requestQuietModeEnabled(true, mManagedUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onIsQuietModeEnabled_shouldCallIsQuietModeEnabled() {
|
||||
assertThat(mQuietModeEnabler.isQuietModeEnabled()).isEqualTo(
|
||||
verify(mUserManager).isQuietModeEnabled(any()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onQuietModeChanged_listenerNotified() {
|
||||
mQuietModeEnabler.onStart(mLifecycleOwner);
|
||||
mContext.sendBroadcast(new Intent(Intent.ACTION_MANAGED_PROFILE_AVAILABLE).putExtra(
|
||||
Intent.EXTRA_USER_HANDLE, MANAGED_USER_ID));
|
||||
mContext.sendBroadcast(new Intent(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE).putExtra(
|
||||
Intent.EXTRA_USER_HANDLE, MANAGED_USER_ID));
|
||||
verify(mOnQuietModeChangeListener, times(2)).onQuietModeChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStart_shouldRegisterReceiver() {
|
||||
mQuietModeEnabler.onStart(mLifecycleOwner);
|
||||
verify(mContext).registerReceiver(eq(mQuietModeEnabler.mReceiver), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStop_shouldUnregisterReceiver() {
|
||||
// register it first
|
||||
mContext.registerReceiver(mQuietModeEnabler.mReceiver, null,
|
||||
Context.RECEIVER_EXPORTED/*UNAUDITED*/);
|
||||
|
||||
mQuietModeEnabler.onStop(mLifecycleOwner);
|
||||
verify(mContext).unregisterReceiver(mQuietModeEnabler.mReceiver);
|
||||
}
|
||||
}
|
@@ -19,18 +19,18 @@ 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.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 androidx.preference.SwitchPreference;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.widget.MainSwitchPreference;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -38,43 +38,51 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class WorkModePreferenceControllerTest {
|
||||
|
||||
private static final String PREF_KEY = "work_mode";
|
||||
private static final int MANAGED_USER_ID = 10;
|
||||
|
||||
private Context mContext;
|
||||
private WorkModePreferenceController mController;
|
||||
private MainSwitchPreference mPreference;
|
||||
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private UserHandle mManagedUser;
|
||||
|
||||
private Context mContext;
|
||||
private WorkModePreferenceController mController;
|
||||
private SwitchPreference mPreference;
|
||||
@Mock
|
||||
private UserInfo mUserInfo;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||
mPreference = new MainSwitchPreference(mContext);
|
||||
when(mUserInfo.isManagedProfile()).thenReturn(true);
|
||||
when(mUserManager.getUserInfo(anyInt())).thenReturn(mUserInfo);
|
||||
when(mUserManager.getProcessUserId()).thenReturn(0);
|
||||
when(mUserManager.getUserProfiles()).thenReturn(Collections.singletonList(mManagedUser));
|
||||
when(mManagedUser.getIdentifier()).thenReturn(MANAGED_USER_ID);
|
||||
mController = new WorkModePreferenceController(mContext, PREF_KEY);
|
||||
mController.setManagedUser(mManagedUser);
|
||||
mPreference = new SwitchPreference(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_noManagedUser_DISABLED() {
|
||||
mController.setManagedUser(null);
|
||||
when(mUserManager.getProcessUserId()).thenReturn(MANAGED_USER_ID);
|
||||
mController = new WorkModePreferenceController(mContext, PREF_KEY);
|
||||
|
||||
assertThat(mController.getAvailabilityStatus())
|
||||
.isNotEqualTo(WorkModePreferenceController.AVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_hasManagedUser_AVAILABLE() {
|
||||
mController.setManagedUser(mManagedUser);
|
||||
assertThat(mController.getAvailabilityStatus())
|
||||
.isEqualTo(WorkModePreferenceController.AVAILABLE);
|
||||
}
|
||||
@@ -83,41 +91,29 @@ public class WorkModePreferenceControllerTest {
|
||||
public void updateState_shouldRefreshContent() {
|
||||
when(mUserManager.isQuietModeEnabled(any(UserHandle.class)))
|
||||
.thenReturn(false);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isChecked()).isTrue();
|
||||
assertThat(mPreference.getSummary())
|
||||
.isEqualTo(mContext.getText(R.string.work_mode_on_summary));
|
||||
|
||||
when(mUserManager.isQuietModeEnabled(any(UserHandle.class)))
|
||||
.thenReturn(true);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isChecked()).isFalse();
|
||||
assertThat(mPreference.getSummary())
|
||||
.isEqualTo(mContext.getText(R.string.work_mode_off_summary));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPreferenceChange_shouldRequestQuietModeEnabled() {
|
||||
mController.setPreference(mPreference);
|
||||
|
||||
mController.onPreferenceChange(mPreference, true);
|
||||
|
||||
verify(mUserManager).requestQuietModeEnabled(false, mManagedUser);
|
||||
|
||||
mController.onPreferenceChange(mPreference, false);
|
||||
|
||||
verify(mUserManager).requestQuietModeEnabled(true, mManagedUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStart_shouldRegisterReceiver() {
|
||||
mController.onStart();
|
||||
verify(mContext).registerReceiver(eq(mController.mReceiver), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStop_shouldUnregisterReceiver() {
|
||||
// register it first
|
||||
mContext.registerReceiver(mController.mReceiver, null,
|
||||
Context.RECEIVER_EXPORTED/*UNAUDITED*/);
|
||||
|
||||
mController.onStop();
|
||||
verify(mContext).unregisterReceiver(mController.mReceiver);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user